<section>
  <div class="well">
    <h2>LDAP is {{#if access.enabled}}<b>enabled</b>{{else}}<b class="text-warning">not configured</b>{{/if}}</h2>
    <div>
      {{#if access.enabled}}
        Rancher is configured to allow access to any LDAP user.
      {{else}}
        Rancher can be configured to restrict access to a set of users and group members.  This is not currently set up, so anybody that reach this page (or the API) has full control over the system.
      {{/if}}
    </div>
  </div>

  {{#if access.enabled}}
    <div class="well">
      <h4>Authentication</h4>
      <hr/>
      <p class="help-block">To change the server configuration, disable access control below and then set it up again.</p>

      <div class="row">
        <div class="col-md-4">
          <h4>General</h4>
          <div><b>Server: </b> <span class="text-muted">{{model.server}}:{{model.port}}</span></div>
          <div><b>TLS: </b> <span class="text-muted">{{if model.tls "Yes" "No"}}</span></div>
          <div><b>Service Account: </b> <span class="text-muted">{{model.serviceAccountUsername}}</span></div>
          <div><b>Search Base: </b> <span class="text-muted">{{model.domain}}</span></div>
          <div><b>Default Domain: </b> <span class="text-muted">{{model.loginDomain}}</span></div>
        </div>
        <div class="col-md-4">
          <h4>Users</h4>
          <div><b>Object Class: </b> <span class="text-muted">{{model.userObjectClass}}</span></div>
          <div><b>Login Field: </b> <span class="text-muted">{{model.userLoginField}}</span></div>
          <div><b>Name Field: </b> <span class="text-muted">{{model.userNameField}}</span></div>
          <div><b>Search Field: </b> <span class="text-muted">{{model.userSearchField}}</span></div>
          <div><b>Enabled Field: </b> <span class="text-muted">{{model.userEnabledAttribute}}</span></div>
          <div><b>Disabled BitMask: </b> <span class="text-muted">{{model.userDisabledBitMask}}</span></div>
        </div>
        <div class="col-md-4">
          <h4>Group</h4>
          <div><b>Object Class: </b> <span class="text-muted">{{model.groupObjectClass}}</span></div>
          <div><b>Name Field: </b> <span class="text-muted">{{model.groupNameField}}</span></div>
          <div><b>Search Field: </b> <span class="text-muted">{{model.groupSearchField}}</span></div>
        </div>
      </div>
    </div>

    <div class="well">
      <h4>Site Access</h4>
      {{#if showingAccessControl}}
        <p class="help-block">
          Configure who should be allowed to log in and user Rancher.  <b>Note:</b> Members added to environments are automatically allowed.
        </p>
        <hr/>

        <div class="row">
          <div class="col-md-6">
            <div class="radio">
              <label>{{radio-button selection=model.accessMode value="unrestricted"}} Allow any user</label>
            </div>
            <div class="radio">
              <label>{{radio-button selection=model.accessMode value="restricted" disabled=true}} Restrict to specific users and group members (coming soon)</label>
            </div>
          </div>
          <div class="col-md-6">
          </div>
        </div>

        <hr/>

        <button class="btn btn-primary" disabled={{saveDisabled}} {{action "saveAuthorization"}}>
          {{#if saved}}
            Saved
          {{else}}
            {{#if saving}}
              <i class="icon icon-spinner icon-spin"></i> Saving...
            {{else}}
              Save authorization configuration
            {{/if}}
          {{/if}}
        </button>
      {{else}}
        <p class="help-block">
          By default, only users that are a member of a environment are allowed to log in and access Rancher.
        </p>
        <button class="btn btn-primary" {{action "showAccessControl"}}>Customize</button>
      {{/if}}
    </div>

    <div class="well">
      <h4>Danger Zone&trade;</h4>
      <hr/>

      <p>
        <b class="text-danger">Caution:</b> Disabling access control will give complete control over Rancher to anyone that can reach this page or the API.
      </p>

      {{#if confirmDisable}}
        <button class="btn btn-danger" {{action "disable"}}>
          <i class="icon icon-alert"></i> Are you sure?  Click again to really disable access control
        </button>
      {{else}}
        <button class="btn btn-danger" {{action "promptDisable"}}>
          <i class="fa fa-umbrella"></i> Disable access control
        </button>
      {{/if}}

    </div>
  {{/if}}

  {{#unless access.enabled}}


    <div class="well">
      <h4>1. Configure an LDAP server</h4>
      <hr/>
      <p class="help-block">Enter the address, port, and protocol to connect to your LDAP/ActiveDirectory server.  <code>389</code> is the standard port for insecure, <code>636</code> for TLS.</p>
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label>Hostname or IP Address*</label>
            {{input type="text" value=model.server classNames="form-control"}}
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group">
            <label>Port</label>
            <div class="input-group">
              {{input type="text" value=model.port type="number" min=1 max=65535 classNames="form-control"}}
              <span class="input-group-addon">
                <label>TLS {{input type="checkbox" checked=model.tls}}</label>
              </span>
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="checkbox">
          </div>
        </div>
      </div>

      <p class="help-block">Rancher needs a service account that has (read-only) access to all of the domains that will be able to login, so that we can determine what groups a user is a member of when they make a request with an API key.</p>
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label>Service Account Username*</label>
            {{input type="text" value=model.serviceAccountUsername classNames="form-control"}}
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group">
            <label>Service Account Password*</label>
            {{input type="password" value=model.serviceAccountPassword classNames="form-control"}}
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <p class="help-block">Only users and groups below the base will be used.</p>
            <label>Search Base*</label>
            {{input type="text" value=model.domain classNames="form-control" placeholder="e.g. dc=mycompany,dc=com"}}
          </div>
        </div>
        <div class="col-md-6">
          <p class="help-block">This domain will be used if a user logs in without specifing one.</p>
          <div class="form-group">
            <label>Default Login Domain*</label>
            {{input type="text" value=model.loginDomain classNames="form-control" placeholder="e.g. mycompany"}}
          </div>
        </div>
      </div>
    </div>

    <div class="well">
      <h4>2. Customize Schema</h4>
      <hr/>
      <p class="help-block">If your schema does not match the standard ActiveDirectory format, you can customize it here.</p>
      <div class="row">
        <div class="col-md-6">
          <h4>Users</h4>
          <div class="form-group">
            <label>Object Class</label>
            {{input type="text" value=model.userObjectClass type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Login Field</label>
            {{input type="text" value=model.userLoginField type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Name Field</label>
            {{input type="text" value=model.userNameField type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Search Field</label>
            {{input type="text" value=model.userSearchField type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Status Field</label>
            {{input type="text" value=model.userEnabledAttribute type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Disabled Status Bitmask</label>
            {{input type="text" value=model.userDisabledBitMask type="number" min=1 classNames="form-control"}}
          </div>
        </div>
        <div class="col-md-6">
          <h4>Groups</h4>
          <div class="form-group">
            <label>Object Class</label>
            {{input type="text" value=model.groupObjectClass type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Name Field</label>
            {{input type="text" value=model.groupNameField type="text" classNames="form-control"}}
          </div>
          <div class="form-group">
            <label>Search Field</label>
            {{input type="text" value=model.groupSearchField type="text" classNames="form-control"}}
          </div>
        </div>
      </div>
    </div>

    <div class="well">
      <h4>3. Test and enable authentication</h4>
      <hr/>
      <p class="help-block">Check that everything is configured correctly by testing authentication with your LDAP account:</p>
      {{top-errors errors=errors}}

      <div class="row">
        <div class="col-md-4">
          <div class="form-group">
            <label>Your Username*</label>
            {{input type="text" value=username classNames="form-control"}}
          </div>
        </div>
        <div class="col-md-4">
          <div class="form-group">
            <label>Your Password*</label>
            {{input type="password" value=password classNames="form-control"}}
          </div>
        </div>
        <div class="col-md-4">
          <div class="form-group">
            <label>&nbsp;</label>
            <button disabled={{createDisabled}} class="btn btn-primary" style="display: block;" {{action "test"}}>
              {{#if testing}}
                <i class="icon icon-spinner icon-spin"></i> Testing...
              {{else}}
                Authenticate
              {{/if}}
            </button>
          </div>
        </div>
      </div>
    </div>
  {{/unless}}
</section>
