You are here

function permissions_lock_form_alter in Permissions Lock 7

Same name and namespace in other branches
  1. 8 permissions_lock.module \permissions_lock_form_alter()

Implements hook_form_alter().

File

./permissions_lock.module, line 82
Lock permissions on the permissions administration pages for certain roles

Code

function permissions_lock_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_admin_permissions' || $form_id == 'user_permissions_profile_permissions_form') {

    // Get all available permissions in the form
    $available_permissions = _permissions_lock_get_form_available_permissions($form);
    if (!user_access('manage permissions unrestricted')) {

      // add our custom validate handler, to restore unset permissions back to their current settings
      $form["#validate"][] = 'permissions_lock_form_validate';

      // get locked roles & permissions, to remove them from the form
      $locked_roles = permissions_lock_get_locked_roles();
      $locked_permissions = permissions_lock_get_locked_permissions();

      // for roles that are locked, we remove their complete column. The permissions for this role will remain untouched.
      if (!empty($locked_roles)) {
        foreach ($locked_roles as $locked_rid) {
          unset($form['checkboxes'][$locked_rid]);
          unset($form['role_names'][$locked_rid]);
        }

        /* if we are on a role-specific page, we redirect user to general permissions page,
           as there is nothing to change on the specific page anyway */
        $editing_rid = arg(3);
        if (is_numeric($editing_rid) && $editing_rid > 0 && in_array($editing_rid, $locked_roles)) {
          drupal_goto('admin/people/permissions');
        }
      }

      // get the roles that still remain (unlocked)
      $available_roles = $form['role_names'];

      // now remove the permission rows for all locked permissions
      if (!empty($available_roles) && !empty($available_permissions)) {

        // make sure there is something left to unset
        foreach ($available_permissions as $perm) {
          foreach ($available_roles as $rid => $role) {

            // check if this permission is marked locked. If so, remove the row
            if (in_array($perm, $locked_permissions)) {
              unset($form['permission'][$perm]);
              unset($form['checkboxes'][$rid]['#options'][$perm]);
            }
          }
        }
        _permissions_lock_cleanup_orphan_headers($form);
      }
    }
  }
}