You are here

function oa_access_initialize_permissions in Open Atrium Core 7.2

Initializes new permissions that were added after module install.

You should call this function when a new permission is added AFTER the initial install of your module. (Open Atrium Access will take care of initializing any permissions on install of your module for you.)

When possible avoid calling this multiple times without the $groups argument. It's better to call it once for multiple permissions, because it will reduce the number of repeated database queries.

At the moment, all this does is set the default permissions for permissions with a type of OA_ACCESS_GROUP_DEFAULT_OPTION_ALL or OA_ACCESS_TEAM_DEFAULT_OPTION_ALL. If you don't have any with those types, you don't have to ever call this function.

Parameters

string|object $perms: The machine name of the new permission or an associative array containing the full permission array's keyed by permission machine name.

array $groups: (Optional) The nids of groups that were just added. If omitted, it will find all the appropriate groups in the system. This is primary for internal use.

3 calls to oa_access_initialize_permissions()
oa_access_modules_installed in modules/oa_access/oa_access.module
Implements hook_modules_installed().
oa_access_node_insert in modules/oa_access/oa_access.module
Implements hook_node_insert().
OpenAtriumAccessAllTestCase::testModuleEnableAndInitialize in modules/oa_access/tests/oa_access.test

File

modules/oa_access/oa_access.module, line 688
Code for the Open Atrium Access module.

Code

function oa_access_initialize_permissions($perms, array $groups = array()) {
  if (is_string($perms)) {
    $all_perms = oa_access_get_permissions(TRUE);
    if (!isset($all_perms[$perms])) {
      throw new Exception("Unknown permission: {$perms}");
    }
    $perms = array(
      $perms => $all_perms[$perms],
    );
  }
  if (empty($groups)) {
    $groups = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->condition('type', 'oa_space')
      ->execute()
      ->fetchCol();
  }
  $updated = FALSE;
  foreach ($perms as $name => $perm) {
    $nids = array();
    if ($perm['type'] & OA_ACCESS_TEAM_PERMISSION && ($perm['type'] & OA_ACCESS_TEAM_DEFAULT_OPTION_ALL) == OA_ACCESS_TEAM_DEFAULT_OPTION_ALL) {
      $nids = $groups;
    }
    if ($perm['type'] & OA_ACCESS_GROUP_PERMISSION && ($perm['type'] & OA_ACCESS_GROUP_DEFAULT_OPTION_ALL) == OA_ACCESS_GROUP_DEFAULT_OPTION_ALL) {

      // Add the '0' nid for the 'All' group.
      $nids[] = 0;
    }
    if (count($nids) > 0) {
      foreach ($nids as $nid) {
        db_merge('oa_access')
          ->key(array(
          'nid' => $nid,
          'permission' => $name,
        ))
          ->fields(array(
          'module' => $perm['module'],
        ))
          ->execute();
      }
      $updated = TRUE;
    }
  }
  if ($updated) {
    drupal_static_reset('oa_access_get_group_permissions');
  }
}