<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
ldap_tests = []
ldap_tests << OpenStruct.new(
  test_type: :connection,
  label: t(:test_ldap_connection_label, "Testing connection..."),
  help_header: t(:test_ldap_connection_help_header,
                 "Canvas can't connect to your LDAP server"),
  help_body: t(:test_ldap_connection_help_body,
               "The connection either timed out or was refused. "\
               "Things to consider:"),
  help: [
    t(:test_ldap_connection_help_fixes_1,
      "Canvas is connecting to %{ips}",
      ips: presenter.ldap_ips),
    t(:test_ldap_connection_help_fixes_2, "This was only a connection test. "\
                                       "SSL certificates were not validated."),
    t(:test_ldap_connection_help_fixes_3, "Check your firewall settings. "\
                                       "Are all Canvas IP address allowed to"\
                                       " access your server?")
  ],
  url: account_test_ldap_connections_url(account)
)
ldap_tests << OpenStruct.new(
  test_type: :bind,
  label: t(:test_ldap_bind_label, "Testing LDAP bind..."),
  help_header: t(:test_ldap_bind_help_header,
                 "Canvas can't bind (login) to your LDAP server"),
  help_body: t(:test_ldap_bind_help_body,
               "Your LDAP server rejected the bind attempt. Things to consider:"),
  help: [
    t(:test_ldap_bind_help_fixes_1,
      "Verify the provided filter string (i.e. '(sAMAccountName={{login}})')."),
    t(:test_ldap_bind_help_fixes_2,
      "Does the username require more scoping information? "\
        "(i.e. 'cn=Canvas,ou=people,dc=example,dc=com').")
  ],
  url: account_test_ldap_binds_url(account)
)
ldap_tests << OpenStruct.new(
  test_type: :search,
  label: t(:test_ldap_search_label, "Testing LDAP search..."),
  help_header: t(:test_ldap_search_help_header,
                 "Canvas can't search your LDAP instance"),
  help_body: t(:test_ldap_search_help_body,
               "The search either failed or returned 0 results. "\
                 "Things to consider:"),
  help: [
    t(:test_ldap_search_help_fixes_1,
      "Verify the provided filter string (i.e. '(sAMAccountName={{login}})')."),
    t(:test_ldap_search_help_fixes_2,
      "Verify the provided search base (i.e. 'ou=people,dc=example,dc=com')."),
    t(:test_ldap_search_help_fixes_3,
      "Verify that the user object in LDAP has search privileges for the"\
      " provided search base.")
  ],
  url: account_test_ldap_searches_url(account)
)
%>

<div style="display: none;width:100%">
  <div id="test_ldap_dialog" style="padding-top:10px;">
    <h3>
      <%= t(:test_ldap_dialog_page_title, "Testing Your LDAP Configuration") %>
    </h3>
    <table style="margin-<%= direction('left') %>:20px">
      <% ldap_tests.each do |test| %>
        <tr>
          <td><h4 style="width: 400px"><%= test.label %></h4></td>
          <td><div id="ldap_<%= test.test_type %>_result"></div></td>
        </tr>
        <tr>
          <td colspan="2">
            <div id="ldap_<%= test.test_type %>_help"
                 style="padding-<%= direction('left') %>:15px;display:none">
              <h4><%= test.help_header %></h4>
              <p>
                <%= test.help_body %>
                <ul>
                  <% test.help.each do |help| %>
                    <li><%= help %></li>
                  <% end %>
                </ul>
              </p>
            </div>
          </td>
        </tr>
      <% end %>
      <tr>
        <td><h4><%= t(:test_ldap_login_label, "Test user login...") %></h4></td>
        <td><div id="ldap_login_result"></div></td>
      </tr>
      <tr>
        <td colspan="2">
          <div id="ldap_login_form"
               style="padding-<%= direction('left') %>:15px;padding-bottom:10px;display:none;">
            <h4>
              <%= t(:test_ldap_login_form_header,
                    "Supply a valid LDAP username/password to test login:") %>
            </h4>
            <form id="ldap_test_login_form" action="">
              <table>
                <tr>
                  <td style="width:86px;">
                    <%= blabel :ldap_test_login, :user, en: "Username" %>
                  </td>
                  <td>
                    <%= text_field :ldap_test_login, :user, style: "width:170px" %>
                  </td>
                </tr>
                <tr>
                  <td>
                    <%= blabel :ldap_test_login, :pass, en: "Password" %>
                  </td>
                  <td>
                    <%= password_field :ldap_test_login, :pass, style: "width:170px" %>
                  </td>
                  <td style="padding-<%= direction('left') %>:26px;">
                    <input type="submit"
                           value="<%= t(:ldap_test_login_button, "Test Login") %>"/>
                  </td>
                </tr>
              </table>
            </form>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <div id="ldap_login_help" style="padding-<%= direction('left') %>:15px;display:none">
            <p id="ldap_login_help_error" style='color:red'>
              <%= t(:test_ldap_login_help_header,
                    "The LDAP server denied your credentials") %>
            </p>
          </div>
        </td>
      </tr>
    </table>
    <button type="button" class="btn submit_button ldap_test_close" style="float:<%= direction('right') %>">
      <%= t(:test_ldap_dialog_close, "Close") %>
    </button>
  </div>
</div>

<% js_env LDAP_TESTS: ldap_tests.map{|t|t.send(:table)},
          LOGIN_TEST_URL: account_test_ldap_logins_url(account) %>
<% js_bundle :ldap_settings_test %>
