Commit 17a9cf8c authored by git's avatar git Committed by amitaibu

Issue #2153555 by Amitaibu, NofarG: Maintain roles when reverting an...

Issue #2153555 by Amitaibu, NofarG: Maintain roles when reverting an overridden group back to default roles and permissions.
parent 79de3136
......@@ -57,6 +57,7 @@ function og_uninstall() {
'og_upgrade_7001',
'og_node_access_strict',
'og_features_ignore_og_fields',
'og_maintain_overridden_roles',
);
foreach ($vars as $var) {
variable_del($var);
......
......@@ -2858,6 +2858,13 @@ function og_role_delete($rid) {
/**
* Delete all roles belonging to a group.
*
* This will also maintain user roles when revertting an ovverriden group.
* For example, if in the overridden group users were assigned to the role
* "administrator", upon reverting back to default roles and
* permissions, OG will search for existing roles with that name, and re-assign
* the correct role ID, and the users that had "administrator" will still have
* it.
*
* @param $group_type
* The group type.
* @param $gid
......@@ -2866,13 +2873,27 @@ function og_role_delete($rid) {
function og_delete_user_roles_by_group($group_type, $group) {
// Check if group has overriden roles defined.
list($gid, $vid,$bundle) = entity_extract_ids($group_type, $group);
$global_roles = array_flip(og_roles($group_type, $bundle));
if ($roles = og_roles($group_type, $bundle, $gid, TRUE)) {
foreach ($roles as $rid => $name) {
if (variable_get('og_maintain_overridden_roles', TRUE) && !empty($global_roles[$name])) {
// Role name exists in the global roles, update the role ID to the
// global one.
db_update('og_users_roles')
->fields(array('rid' => $global_roles[$name]))
->condition('rid', $rid)
->condition('group_type', $group_type)
->condition('gid', $gid)
->execute();
}
og_role_delete($rid);
}
}
}
/**
* Get the role names of role IDs.
*
......
......@@ -780,13 +780,30 @@ class OgDefaultAccessFieldTestCase extends DrupalWebTestCase {
$this->assertNotEqual($og_roles, $new_og_roles, t('Group with default access field enabled has own roles and permissions settings.'));
// Assert the newley created admin role was mapped to the default one.
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid);
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid, FALSE);
$this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has overriden "admin" role.'));
// Disable existing group's default access field.
variable_set('og_maintain_overridden_roles', TRUE);
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
$wrapper->save();
$this->assertEqual($og_roles, og_roles('entity_test', 'main', $entity->pid), t('Group with enabled default access field that was disabled is assigned to the global roles and permissions settings.'));
// Assert admin role was maintained from the overriden group.
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid, FALSE);
$this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('"admin" role maintained from overriden group.'));
// Override group.
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(1);
$wrapper->save();
// Assert admin role was not maintained from the overriden group.
variable_set('og_maintain_overridden_roles', FALSE);
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
$wrapper->save();
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid, FALSE);
$this->assertFalse(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('"admin" role not maintained from overriden group.'));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment