You are here

function og_set_global_access_module in Organic groups 7

Add default roles and permissions of a module to the global permissions.

This function is called whenever a module is enabled. Calling this function directly will re-assign permissions to thier default roles.

Parameters

$module: The module name.

Return value

Array with the global roles, as new records might have been added.

1 call to og_set_global_access_module()
og_modules_enabled in ./og.module
Implements hook_modules_enabled().

File

./og.module, line 2435
Enable users to create and manage groups with roles and permissions.

Code

function og_set_global_access_module($module) {
  $default_roles = og_get_default_roles();
  $global_roles = og_get_global_roles();
  $permissions = og_get_permissions();

  // The roles that should be added.
  $roles_to_add = array();
  if (empty($global_roles)) {

    // Add all the roles, there are no roles defined yet. This is probably
    // becuase OG is only being installed.
    $roles_to_add = reset($default_roles);
  }
  elseif (!empty($default_roles[$module])) {

    // Diff the roles that should be added with the ones already defined as
    // global roles.
    $roles_to_add = array_diff($default_roles[$module], $global_roles);
  }

  // Add a new global role.
  if (!empty($roles_to_add)) {
    foreach ($roles_to_add as $name) {
      $role = og_create_global_role($name);
      $global_roles[$role->rid] = $name;
    }
  }

  // If there are permissions defined, make sure they were not applied already,
  // as it might happen if a module was disabled and re-enabled.
  $perms_to_add = array();
  $perms_to_add_by_rid = array();
  foreach ($permissions as $key => $value) {
    if ($value['module'] == $module) {
      $perms_to_add[$key] = $value;
    }
  }
  if ($perms_to_add) {

    // Get the assigned permissions of the global roles.
    $global_roles_perms = og_role_permissions($global_roles);

    // Get the roles keyed by thier name.
    $global_roles_flip = array_flip($global_roles);
    foreach ($perms_to_add as $key => $value) {
      if (!empty($value['default role'])) {

        // Don't try to assign permissions that are already assigned.
        foreach ($value['default role'] as $role) {
          $rid = $global_roles_flip[$role];
          if (empty($global_roles_perms[$rid][$key])) {

            // Get the  permissions to be added in the form:
            // array(
            //   '1' => array( // '1' is the role ID.
            //     'perm_foo' => 'perm_foo',
            //     'perm_bar' => 'perm_bar',
            //    ),
            // );
            $perms_to_add_by_rid[$rid][$key] = $key;
          }
        }
      }
    }
  }
  if ($perms_to_add_by_rid) {
    foreach ($perms_to_add_by_rid as $rid => $perms) {

      // Assign the permissions to the roles.
      og_role_change_permissions($rid, $perms);
    }
  }
  return $global_roles;
}