You are here

function secure_permissions_build_roles in Secure Permissions 6

Same name and namespace in other branches
  1. 7.2 secure_permissions.module \secure_permissions_build_roles()
  2. 7 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 156
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 = user_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;
    drupal_write_record('role', $role);
  }

  // Delete old roles.
  foreach ($remove_roles as $rid => $name) {

    // Never delete the default roles.
    if (!in_array($rid, array(
      DRUPAL_ANONYMOUS_RID,
      DRUPAL_AUTHENTICATED_RID,
    ))) {
      db_query("DELETE FROM {role} WHERE name = '%s'", $name);
      db_query("DELETE FROM {permission} WHERE rid = %d", $rid);
    }
  }
  return TRUE;
}