<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      xmlns:i18n="http://xml.zope.org/namespaces/i18n"
    lang="en"
    metal:use-macro="context/prefs_main_template/macros/master"
    i18n:domain="plone">

<metal:javascript fill-slot="javascript_head_slot">
<script src="${portal_url}/++plone++castle/components/usersgroups.js"></script>
</metal:javascript>

<body>

<metal:main fill-slot="prefs_configlet_content"
    tal:define="template_id string:@@usergroup-userprefs;
                showAll python:request.get('showAll', '') and not view.newSearch and 'y';
                Batch python:modules['Products.CMFPlone'].Batch;
                b_start python:0 if showAll or view.newSearch else request.get('b_start',0);
                b_size python:showAll and len(view.searchResults) or 20;
                portal_roles view/portal_roles;
                portal_url context/portal_url;">

  <article id="content">

    <a href=""
       id="setup-link"
       tal:attributes="href string:$portal_url/@@overview-controlpanel"
       i18n:translate="">
        Site Setup
    </a>

    <h1 class="documentFirstHeading"
        i18n:translate="">Users and Groups</h1>

    <div metal:use-macro="context/global_statusmessage/macros/portal_message">
      Portal status message
    </div>

    <div id="content-core">

      <div class="autotabs">
        <div class="autotoc-nav">
          <a class="active"
             href="${portal_url}/@@usergroup-userprefs"
             i18n:translate="label_users">Users</a>
          <a href="${portal_url}/@@usergroup-groupprefs"
             i18n:translate="label_groups">Groups</a>
          <a href="${portal_url}/@@usergroup-controlpanel"
             i18n:translate="label_usergroup_settings">Settings</a>
          <a href="${portal_url}/@@member-fields"
             i18n:translate="label_member_fields">Member fields</a>
        </div>

        <p i18n:translate="user_roles_note" class="discreet">
          Note that roles set here apply directly to a user.
          The symbol <img i18n:name="image_link_icon" tal:replace="structure context/site_icon.png" />
          indicates a role inherited from membership in a group.
        </p>
        <p tal:condition="view/show_users_listing_warning" class="portalMessage warning">
          <strong i18n:translate="">Note</strong>
          <span i18n:translate="description_pas_users_listing">Some or all of your PAS user source
          plugins do not allow listing of users, so you may not see
          the users defined by those plugins unless doing a specific
          search.</span>
        </p>
        <form action=""
              class="pat-formautofocus"
              name="users_search"
              method="post"
              tal:attributes="action string:$portal_url/$template_id"
              tal:define="findAll python:'form.button.FindAll' in request.keys();
                          portal_users view/searchResults;
                          batch python:Batch(portal_users, b_size, int(b_start), orphan=1);
                          batchformkeys python:['searchstring','_authenticator'];
                          many_users view/many_users">
          <input type="hidden" name="form.submitted" value="1" />

          <div class="field">
            <label for="quickSearch" i18n:translate="label_user_search">User Search</label>
                          <input class="quickSearch"
                                 id="quickSearch"
                                 type="text"
                                 name="searchstring"
                                 value=""
                                 tal:attributes="value view/searchString;"
                                 />

                          <input type="submit"
                                 class="searchButton context"
                                 name="form.button.Search"
                                 value="Search"
                                 i18n:attributes="value label_search;"
                                 />

                          <input type="submit"
                                 class="searchButton standalone"
                                 name="form.button.FindAll"
                                 value="Show all"
                                 i18n:attributes="value label_showall;"
                                 tal:condition="not:many_users"
                                 />
                           <a class="plone-btn plone-btn-primary context pull-right" id="add-user"
                              tal:attributes="href string:${portal_url}/@@new-user">
                              Add New User
                           </a>
          </div>
          <table class="usergroups-listing" summary="User Listing">
              <tbody>
                  <tal:block tal:condition="portal_users" >
                  <tr class="odd">
                      <th rowspan="" i18n:translate="listingheader_user_name">User name</th>
                      <th>Roles</th>
                      <th>Actions</th>
                  </tr>
                  </tal:block>
                  <tal:block repeat="user batch">
                    <tr tal:define="oddrow repeat/user/odd;
                                    userid user/userid;
                                    userquery python:view.makeQuery(userid=userid);
                                    assigned_roles python: [r for r in portal_roles if user['roles'][r]['inherited'] or user['roles'][r]['explicit']];
                                    group_roles python: [r for r in portal_roles if user['roles'][r]['inherited']]"
                        class="${python: not assigned_roles and 'disabled'}">

                        <td>
                            <a href="@@user-information"
                               tal:define="fullname user/fullname"
                               tal:attributes="href string:$portal_url/@@user-information?${userquery};
                                               title userid">
                                <tal:fullname tal:condition="fullname">
                                  ${fullname}(${user/login})
                                </tal:fullname>
                                <tal:notfullname tal:condition="not: fullname">
                                  ${user/login}
                                </tal:notfullname>
                            </a>
                            <input type="hidden" name="users.id:records" tal:attributes="value userid" />
                        </td>

                        <td class="roles-value" tal:condition="assigned_roles">
                          <select multiple="true" name="users.roles:list:records" class="pat-select2">
                            <tal:roles tal:repeat="portal_role portal_roles">
                            <tal:block tal:define="inherited python:user['roles'][portal_role]['inherited'];
                                                   explicit python:user['roles'][portal_role]['explicit'];
                                                   enabled python:user['roles'][portal_role]['canAssign']">
                              <option tal:attributes="value portal_role;
                                                      selected python:'checked' if explicit else nothing;"
                                      tal:condition="python: enabled and not inherited">${portal_role}</option>
                            </tal:block>
                            </tal:roles>
                          </select>
                          <div class="inherited-roles" tal:condition="group_roles">
                            Roles from groups:
                            <tal:roles tal:repeat="portal_role portal_roles">
                            <tal:block tal:define="inherited python:user['roles'][portal_role]['inherited'];
                                                   explicit python:user['roles'][portal_role]['explicit'];
                                                   enabled python:user['roles'][portal_role]['canAssign']">
                              <span tal:condition="inherited">${portal_role}</span>
                            </tal:block>
                            </tal:roles>
                          </div>
                        </td>
                        <td class="roles-value" tal:condition="not: assigned_roles">
                          Disabled
                        </td>

                        <td class="actions">
                          <button tal:condition="user/can_set_password|nothing"
                                  class="plone-btn plone-btn-default plone-btn-xs
                                         castle-btn-reset-password"
                                  type="button" tal:condition="assigned_roles"
                                  data-userid="${user/id}"
                                  data-login="${user/login}">Change password</button>
                          <button class="plone-btn plone-btn-default plone-btn-xs
                                         castle-btn-resetattempts"
                                  type="button" tal:condition="assigned_roles"
                                  data-userid="${user/id}"
                                  data-login="${user/login}">Reset login attempts</button>
                          <button class="plone-btn plone-btn-warning plone-btn-xs
                                         castle-btn-disable-user"
                                  type="button" tal:condition="assigned_roles"
                                  data-userid="${user/id}"
                                  data-login="${user/login}">Disable</button>
                          <button class="plone-btn plone-btn-warning plone-btn-xs
                                         castle-btn-enable-user"
                                  type="button" tal:condition="not: assigned_roles"
                                  data-userid="${user/id}"
                                  data-login="${user/login}">Enable</button>
                          <button class="plone-btn plone-btn-danger plone-btn-xs
                                         castle-btn-delete-user"
                                  type="button"
                                  data-userid="${user/id}"
                                  data-login="${user/login}">Delete</button>
                        </td>
                    </tr>
                  </tal:block>
                  <tr tal:condition="not:batch">
                      <td tal:condition="view/searchString"
                          i18n:translate="text_nomatches"
                          style="text-align:center;">No matches</td>
                      <tal:block tal:condition="not:view/searchString">
                        <td tal:condition="many_users"
                            class="discreet"
                            i18n:translate="text_no_user_searchstring"
                            style="text-align:center; font-size: 100%;">
                            Enter a username to search for
                        </td>
                        <td tal:condition="not:many_users"
                            class="discreet"
                            i18n:translate="text_no_user_searchstring_largesite"
                            style="text-align:center; font-size: 100%;">
                            Enter a username to search for, or click 'Show All'
                        </td>
                      </tal:block>
                  </tr>
              </tbody>
          </table>

          <div metal:use-macro="context/batch_macros/macros/navigation" />

          <div class="showAllSearchResults"
               tal:condition="python:batch.next or batch.previous"
               tal:define="mq python:modules['ZTUtils'].make_query;
                           keys batchformkeys|nothing;
                           linkparams python:keys and dict([(key, request.form[key]) for key in keys if key in request]) or request.form;
                           url batch_base_url | string:${context/absolute_url}/${template_id}">
              <a tal:attributes="href python: '%s?%s' % (url, mq( linkparams, {'showAll':'y'} ))"
                 i18n:translate="description_pas_show_all_search_results">
                  Show all search results
              </a>
          </div>

          <input type="hidden" value="b_start" name="b_start"
                 tal:attributes="value b_start"/>

          <input type="hidden" value="" name="showAll"
                 tal:attributes="value showAll"/>

          <div tal:condition="batch">

            <div class="formControls">
              <input class="context"
                 type="submit"
                 name="form.button.Modify"
                 value="Save"
                 i18n:attributes="value label_save;"
                 />
            </div>
          </div>

          <input tal:replace="structure context/@@authenticator/authenticator" />

        </form>
      </div>
    </div>

  </article>

</metal:main>

</body>
</html>
