You are here

function secure_permissions_build_roles in Secure Permissions 7

Same name and namespace in other branches
  1. 6 secure_permissions.module \secure_permissions_build_roles()
  2. 7.2 secure_permissions.module \secure_permissions_build_roles()

Build the roles table correctly.

1 call to secure_permissions_build_roles()
secure_permissions_rebuild in ./secure_permissions.module
Rebuild permissions, based on presets from the API.

File

./secure_permissions.module, line 193
Secure Permissions module file.

Code

function secure_permissions_build_roles() {

  // Get the currently defined roles for the site, and sort() them so
  // we can diff the arrays properly.
  $roles = secure_permissions_get_existing_roles();
  sort($roles);

  // Get the roles defined by this module's hook.
  $secure_roles = secure_permissions_get_roles();
  if (empty($secure_roles)) {
    return FALSE;
  }

  // Compute the difference for add/delete.
  $new_roles = array_diff($secure_roles, $roles);
  $remove_roles = array_diff($roles, $secure_roles);

  // Add new roles.
  foreach ($new_roles as $rid => $name) {
    $role = new stdClass();
    $role->name = $name;
    user_role_save($role);
  }

  // Delete old roles.
  $omit = array(
    DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID,
  );
  $admin_rid = variable_get('user_admin_role', 0);
  if (!empty($admin_rid)) {
    $omit[] = $admin_rid;
  }
  foreach ($remove_roles as $name) {
    $role = user_role_load_by_name($name);
    if (!empty($role) && !in_array($role->rid, $omit)) {
      user_role_delete($name);
    }
  }
  return TRUE;
}