<dtml-var manage_page_header>

<dtml-with "_(management_view='Security')">
  <dtml-if manage_tabs>
    <dtml-var manage_tabs>
  </dtml-if>
</dtml-with>

<main class="container-fluid">

<dtml-let roles="valid_roles()"
          short_form="len(roles) > 30">

  <p class="form-help mb-4">
    The listing below shows the current security settings for this item.
    You can also assign
    <strong><a href="manage_listLocalRoles">local roles</a></strong> to
    users, which give users extra roles in the context of this object and
    its subobjects.<br />
  </p>

  <form action="manage_reportUserPermissions" method="get" class="mb-4">
    <div class="form-group row form-inline">
      <label for="username" class="col-sm-3 col-md-2">Username:</label>
      <div class="col-sm-9 col-md-10">
        <input class="form-control mr-3" type="text" id="username" name="user" 
          required="required" placeholder="Enter name of user ..." />
        <input type="submit" 
          class="btn btn-primary mr-sm-2"
          title="Show user permissions and roles in the context of the current object"
          value="Show permissions" />
        <input type="button"
          class="btn btn-primary mr-3"
          title="Add a New Local Role for the entered or any other User Name"
          onclick="window.location.href='manage_listLocalRoles?username=' + $('#username').val()"
          value="Assign Local Roles" />
      </div>
    </div>
  </form>

  <hr/>

  <dtml-if short_form>

    <h3>Permissions</h3>

    <p class="form-help mb-4">
      Click on one of the permission names to see the roles they are assigned
      to.
    </p>

    <table>
      <dtml-in "ac_inherited_permissions(1)">
        <tr>
          <td class="zmi-acquire-title nowrap">
            <dtml-let pname="_['sequence-key']">
              <a href="manage_permissionForm?permission_to_manage=&dtml.url_quote-pname;">&dtml-pname;</a>
            </dtml-let>
          </td>
        </tr>
      </dtml-in>
    </table>

  <dtml-else>

    <h3>Role to permission assignments</h3>

    <p class="form-help mb-4">
      This table shows which permissions are assigned to each role. Permissions
      are rows and roles are columns.<br/>
      When a role is assigned to a permission, users with the given role
      will be able to perform tasks associated with the permission on this
      item.
      <dtml-unless isTopLevelPrincipiaApplicationObject>
        <br/>
        When the <em>Acquire?</em> checkbox is selected
        then the containing objects's permission settings are used. Note: the
        acquired permission settings may be augmented by selecting Roles for
        a permission in addition to selecting to acquire permissions.
        </dtml-unless>
    </p>

    <dtml-with "_.namespace(valid_roles=roles)">
      <form action="manage_changePermissions" method="post">
        <table id="table-permissions" class="table table-hover table-bordered table-responsive-sm">
          <thead>
            <tr>
              <th scope="col" colspan="2" class="zmi-table-head-permisions">
                Permissions
              </th>
              <th scope="col" colspan="<dtml-var expr="_.len(valid_roles)">" 
                  class="zmi-table-head-roles">Roles</th>
              <th class="blank">&nbsp;</th>
            </tr>
          </thead>
          <tbody>

          <dtml-in permission_settings mapping>
            <dtml-let permission_title="name">
              <dtml-if "not (_['sequence-index']%10)">
                <tr class="zmi-table-head">
                  <th class="zmi-acquire-title" title="Acquire?">
                    <dtml-unless isTopLevelPrincipiaApplicationObject>
                      <a href="manage_acquiredForm">Acquire?</a>
                    </dtml-unless>
                  </th>
                  <th class="zmi-permission" title="Permission">Permission</th>
                  <dtml-in valid_roles>
                    <th class="zmi-rolename" title="&dtml-sequence-item;">
                      <div><span>&dtml-sequence-item;</span></div>
                    </th>
                  </dtml-in>
                  <th class="blank"><div><span>&nbsp;</span></div></th>
                </tr>
              </dtml-if>
              <tr>
                <td class="zmi-acquire-check">
                  <dtml-if isTopLevelPrincipiaApplicationObject>
                    &nbsp;
                  <dtml-else>
                    <input type="checkbox" name="acquire_&dtml-hashed_name;"
                           &dtml-acquire; />
                  </dtml-if>
                </td>
                <td class="zmi-acquire-title nowrap">
                  <a href="manage_permissionForm?permission_to_manage=&dtml.url_quote-name;">&dtml-name;</a>
                </td>
                <dtml-in roles mapping>
                  <td class="zmi-rolename-check"
                      title="Role: <dtml-var "valid_roles[_['sequence-index']]">
Permission: &dtml-permission_title;" 
                      onclick="$(this).children('input').trigger('click')">
                    <label class="sr-only sr-only-focusable" 
                           for="&dtml-name;">
                      Role: <dtml-var "valid_roles[_['sequence-index']]">,
                      Permission: <dtml-var "_['permission_title']">
                    </label>
                    <input type="checkbox" &dtml-checked; id="&dtml-name;"
                           name="&dtml-name;" onclick="event.stopPropagation()" />
                  </td>
                </dtml-in>
                <td class="blank">&nbsp;</td>
              </tr>
            </dtml-let>
          </dtml-in permission_settings>

          </tbody>
        </table>

        <div class="zmi-controls">
          <input type="submit" class="btn btn-primary" name="submit"
                 value="Save Changes" />
        </div>
      </form>
    </dtml-with>

  </dtml-if short_form>

</dtml-let>

  <dtml-if "_['id'] != 'acl_users'">

    <hr/>

    <h3>Add or delete user-defined roles</h3>

    <p class="form-help mt-4">
      You can define new roles by entering a role name and clicking the
      &quot;Add Role&quot; button.
    </p>

    <form action="manage_defined_roles" method="post" class="form">
      <div class="form-group">
        <label for="username" class="nowrap">User defined roles</label>
        <div class="form-inline">
          <input type="text" class="form-control col-sm-6 col-md-3 mr-2 mb-2"
                 id="role" name="role" placeholder="Define a new user role ..." />
          <input type="submit" class="btn btn-primary mb-2" name="submit"
                 value="Add Role" /> 
        </div>
      </div>
      <dtml-if userdefined_roles>
        <div class="form-group">
          <div class="form-inline">
            <select name="roles:list"
                    class="form-control col-sm-6 col-md-3 mr-2  mb-2">
              <dtml-in userdefined_roles>
                <option value="&dtml-sequence-item;">&dtml-sequence-item;</option>
              </dtml-in>
            </select>
            <input type="submit" class="btn btn-primary  mb-2" name="submit"
                   value="Delete Role" /> 
          </div>
        </div>
      </dtml-if>
    </form>
  </dtml-if>
</main>

<dtml-var manage_page_footer>
