You are here

function merci_permissions_grant_permissions in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 modules/merci_permissions/merci_permissions.module \merci_permissions_grant_permissions()

Helper function to grant permissions to a role.

1 call to merci_permissions_grant_permissions()
merci_permissions_settings_form_submit in modules/merci_permissions/merci_permissions.module

File

modules/merci_permissions/merci_permissions.module, line 248
Provides functions for resetting permissions on the permission page.

Code

function merci_permissions_grant_permissions($role_name, $new_permissions) {
  $permissions = array();
  $updated_permissions = '';
  $role = merci_permissions_get_role($role_name);
  if (count($new_permissions) > 0) {

    // Fetch the permissions string for the given role id
    $permissions = merci_permissions_get_permissions_for_role($role->name);

    // Check to see if there are existing permissions
    if (count($permissions) > 0) {

      // Add the new permissions if the role doesn't already have the permission
      foreach ($new_permissions as $permission) {
        if (!in_array($permission, $permissions)) {
          $permissions[] = trim($permission);
        }
      }

      // rebuild the permission string
      $updated_permissions = join(', ', $permissions);
      db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_permissions, $role->rid);
    }
    else {

      // No permissions have been set for this role, so we need to insert some
      foreach ($new_permissions as $permission) {
        $permissions[] = trim($permission);
      }

      // rebuild the permission string
      $updated_permissions = join(', ', $permissions);
      db_query("INSERT INTO {permission} (rid, perm, tid) VALUES(%d,'%s',%d)", $role->rid, $updated_permissions, 0);
    }
  }
  return;
}