/** * @Given /^groups memberships:$/ */ public function groupsMemberships(TableNode $table) { $memberships = $table->getHash(); foreach ($memberships as $membership) { // Find group node. $group_node = $membership['group']; foreach ($this->nodes as $node) { if ($node->type == 'group' && $node->title == $group_node) { $group_node = $node; } } // Subscribe nodes and users to group. if (isset($membership['members'])) { $members = explode(",", $membership['members']); foreach ($this->users as $user) { if (in_array($user->name, $members)) { og_group('node', $group_node->nid, array('entity' => $user, 'entity_type' => 'user', "membership type" => OG_MEMBERSHIP_TYPE_DEFAULT)); // Patch till i figure out why rules are not firing. if ($user->name == 'editor') { og_role_grant('node', $group_node->nid, $user->uid, 4); } } } } if (isset($membership['nodes'])) { $content = explode(",", $membership['nodes']); foreach ($this->nodes as $node) { if ($node->type != 'group' && in_array($node->title, $content)) { og_group('node', $group_node->nid, array('entity' => $node, 'entity_type' => 'node', 'state' => OG_STATE_ACTIVE)); } } } } }
/** * @Given /^I am logged in as a user from "([^"]*)"$/ */ public function iAmLoggedInAsAUserFrom($company) { // Log-in and then group the created user to the given company. $this->assertAuthenticatedByRole('authenticated user'); $uid = $this->user->uid; $nid = $this->getEntityId($company); og_group('node', $nid, array('entity' => $uid)); }
/** * Create, update or delete OG membership based on field values. */ public function OgMembershipCrud($entity_type, $entity, $field, $instance, $langcode, &$items) { $diff = $this->groupAudiencegetDiff($entity_type, $entity, $field, $instance, $langcode, $items); if (!$diff) { return; } $field_name = $field['field_name']; $group_type = $field['settings']['target_type']; $diff += array('insert' => array(), 'delete' => array()); // Delete first, so we don't trigger cardinality errors. if ($diff['delete']) { og_membership_delete_multiple($diff['delete']); } foreach ($diff['insert'] as $gid) { $values = array('entity_type' => $entity_type, 'entity' => $entity, 'field_name' => $field_name); og_group($group_type, $gid, $values); } }
/** * Wrapper for og_membership creation * * @param integer $ogID Organic Group ID * @param integer $drupalID drupal User ID */ function og_membership_create($ogID, $drupalID) { if (function_exists('og_entity_query_alter')) { // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch // // @TODO Find more solid way to check - try system_get_info('module', 'og'). // // Also, since we don't know how to get the entity type of the // group, we'll assume it's 'node' og_group('node', $ogID, array('entity' => user_load($drupalID))); } else { // Works for the OG 7.x-1.x branch og_group($ogID, array('entity' => user_load($drupalID))); } }
/** * Create, update or delete OG membership based on field values. */ public function OgMembershipCrud($entity_type, $entity, $field, $instance, $langcode, &$items) { if (!user_access('administer group') && !field_access('edit', $field, $entity_type, $entity)) { // User has no access to field. return; } if (!($diff = $this->groupAudiencegetDiff($entity_type, $entity, $field, $instance, $langcode, $items))) { return; } $field_name = $field['field_name']; $group_type = $field['settings']['target_type']; $diff += array('insert' => array(), 'delete' => array()); // Delete first, so we don't trigger cardinality errors. if ($diff['delete']) { og_membership_delete_multiple($diff['delete']); } if (!$diff['insert']) { return; } // Prepare an array with the membership state, if it was provided in the widget. $states = array(); foreach ($items as $item) { $gid = $item['target_id']; if (empty($item['state']) || !in_array($gid, $diff['insert'])) { // State isn't provided, or not an "insert" operation. continue; } $states[$gid] = $item['state']; } foreach ($diff['insert'] as $gid) { $values = array('entity_type' => $entity_type, 'entity' => $entity, 'field_name' => $field_name); if (!empty($states[$gid])) { $values['state'] = $states[$gid]; } og_group($group_type, $gid, $values); } }
/** * Override. We need to handle files being replaced through this method. */ public function putEntity($entity_id) { // this request is only a file // no other data is addeed if ($this->request['file']) { $oldFile = file_load($entity_id); $this->request['file']->filename = $oldFile->filename; if ($file = file_move($this->request['file'], $oldFile->uri, FILE_EXISTS_REPLACE)) { if ($oldFile->{OG_AUDIENCE_FIELD}) { og_group('node', $oldFile->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'], array('entity_type' => 'file', 'entity' => $file)); } return array($this->viewEntity($entity_id)); } else { throw new RestfulBadRequestException('Error moving file.'); } } return parent::putEntity($entity_id); }
/** * add user to group and grant a role. * * extends grantSingleAuthorization() * * @param drupal user objet $user * @param string $authorization_id in form organic group gid-rid such as 7-2 * @param array $user_auth_data is array specific to this consumer_type. Stored in $user->data['ldap_authorizations']['og_group'] * * @return TRUE if granted or grant exists, FALSE if not grantable or failed. */ public function grantSingleAuthorization(&$user, $authorization_id, &$user_auth_data) { $result = FALSE; $watchdog_tokens = array('%authorization_id' => $authorization_id, '%username' => $user->name, '%ogversion' => $this->ogVersion); if ($this->detailedWatchdogLog) { watchdog('ldap_auth_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() beginning to grant authorization for $group_name=%group_name to user %username', $watchdog_tokens, WATCHDOG_DEBUG); } if ($this->ogVersion == 1) { list($gid, $rid) = @explode('-', $authorization_id); } else { list($group_type, $gid, $rid) = @explode(':', $authorization_id); $watchdog_tokens['%group_type'] = $group_type; } $watchdog_tokens['%gid'] = $gid; $watchdog_tokens['%rid'] = $rid; $watchdog_tokens['%uid'] = $user->uid; $available_consumer_ids = $this->availableConsumerIDs(TRUE); // CASE 1: Bad Parameters if (!$authorization_id || !$gid || !$rid || !is_object($user) || $this->ogVersion == 2 && !$group_type) { watchdog('ldap_auth_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() improper parameters.', $watchdog_tokens, WATCHDOG_ERROR); return FALSE; } // CASE 2: gid-rid does not exist if (!in_array($authorization_id, $available_consumer_ids)) { $result = FALSE; watchdog('ldap_authorization_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() failed to grant %username the group-role %authorization_id because group-role does not exist', $watchdog_tokens, WATCHDOG_ERROR); return FALSE; } $ldap_granted = $this->hasLdapGrantedAuthorization($user, $authorization_id); $granted = $this->hasAuthorization($user, $authorization_id); // CASE 3: user already granted permissions via ldap grant if ($ldap_granted && $granted) { watchdog('ldap_auth_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() <hr />not granted: gid=%gid, for username=%username, <br />because user already belongs to group', $watchdog_tokens, WATCHDOG_DEBUG); return TRUE; } // CASE 4: user already granted permissions, but NOT via ldap grant if ($granted && !$ldap_granted) { // need to make ldap granted watchdog('ldap_authorization_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() <hr />membership already exists for: gid=%gid, rid=%rid, for username=%username, <br />but made ldap granted.', $watchdog_tokens, WATCHDOG_DEBUG); return TRUE; // return true so is made ldap granted, even though membership is not created. } // CASE 5: grant role if ($this->detailedWatchdogLog) { watchdog('ldap_auth_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() calling og_role_grant(%group_type, %gid, %uid, %rid). og version=%ogversion', $watchdog_tokens, WATCHDOG_DEBUG); } if ($this->ogVersion == 2) { $values = array('entity_type' => 'user', 'entity' => $user->uid, 'field_name' => FALSE, 'state' => OG_STATE_ACTIVE); $og_membership = og_group($group_type, $gid, $values); og_role_grant($group_type, $gid, $user->uid, $rid); } else { $values = array('entity type' => 'user', 'entity' => $user, 'state' => OG_STATE_ACTIVE, 'membership type' => OG_MEMBERSHIP_TYPE_DEFAULT); watchdog('ldap_auth_og', 'og_group1', $watchdog_tokens, WATCHDOG_DEBUG); $user_entity = og_group($gid, $values); watchdog('ldap_auth_og', 'og_role_grant1', $watchdog_tokens, WATCHDOG_DEBUG); og_role_grant($gid, $user->uid, $rid); } if ($this->detailedWatchdogLog) { watchdog('ldap_auth_og', 'LdapAuthorizationConsumerOG.grantSingleAuthorization() <hr />granted: group_type=%group_type gid=%gid, rid=%rid for username=%username', $watchdog_tokens, WATCHDOG_DEBUG); } return TRUE; }
/** * Creates multiple group memberships. * * Provide group membership data in the following format: * * | user | group | role on group | membership status | * | Foo | The Group | administrator member | Active | * * @Given group memberships: */ public function addGroupMemberships(TableNode $groupMembershipsTable) { foreach ($groupMembershipsTable->getHash() as $groupMembershipHash) { if (isset($groupMembershipHash['group']) && isset($groupMembershipHash['user'])) { $group = $this->getGroupByName($groupMembershipHash['group']); $user = user_load_by_name($groupMembershipHash['user']); // Add user to group with the proper group permissions and status if ($group && $user) { // Add the user to the group og_group("node", $group->nid, array("entity type" => "user", "entity" => $user, "membership type" => OG_MEMBERSHIP_TYPE_DEFAULT, "state" => $this->getMembershipStatusByName($groupMembershipHash['membership status']))); // Grant user roles $group_role = $this->getGroupRoleByName($groupMembershipHash['role on group']); og_role_grant("node", $group->nid, $user->uid, $group_role); } else { if (!$group) { throw new Exception(sprintf("No group was found with name %s.", $groupMembershipHash['group'])); } if (!$user) { throw new Exception(sprintf("No user was found with name %s.", $groupMembershipHash['user'])); } } } else { throw new Exception(sprintf("The group and user information is required.")); } } }
public function og2Grants($og_actions, &$user, &$user_auth_data) { foreach ($og_actions['grants'] as $group_entity_type => $gids) { foreach ($gids as $gid => $granting_rids) { // all rids ldap believes user should be granted and attributed to ldap $all_group_roles = og_roles($group_entity_type, FALSE, $gid, FALSE, TRUE); // all roles rid => role_name array w/ authen or anon roles $authenticated_rid = array_search(OG_AUTHENTICATED_ROLE, $all_group_roles); $anonymous_rid = array_search(OG_ANONYMOUS_ROLE, $all_group_roles); $all_group_rids = array_keys($all_group_roles); // all rids array w/ authen or anon rids $users_group_rids = array_keys(og_get_user_roles($group_entity_type, $gid, $user->uid, TRUE)); // users current rids w/authen or anon roles returned $users_group_rids = array_diff($users_group_rids, array($anonymous_rid)); $new_rids = array_diff($granting_rids, $users_group_rids, array($anonymous_rid)); // rids to be added without anonymous rid // debug("new rids"); debug($new_rids);debug("granting_rids"); debug($granting_rids);debug("users_group_rids"); debug($users_group_rids); // if adding OG_AUTHENTICATED_ROLE or any other role and does not currently have OG_AUTHENTICATED_ROLE, group if (!in_array($authenticated_rid, $users_group_rids) && count($new_rids) > 0) { $values = array('entity_type' => 'user', 'entity' => $user->uid, 'field_name' => FALSE, 'state' => OG_STATE_ACTIVE); $og_membership = og_group($group_entity_type, $gid, $values); // debug("consumer_id=$consumer_id, og group called( $group_entity_type, $gid, values:"); debug($values); debug("response og_membership"); debug($og_membership); $consumer_id = join(':', array($group_entity_type, $gid, $authenticated_rid)); $user_auth_data[$consumer_id] = array('date_granted' => time(), 'consumer_id_mixed_case' => $consumer_id); $new_rids = array_diff($new_rids, array($authenticated_rid)); // granted on membership creation } foreach ($new_rids as $i => $rid) { // debug("role grant $group_entity_type $gid $user->uid $rid"); og_role_grant($group_entity_type, $gid, $user->uid, $rid); } foreach ($granting_rids as $i => $rid) { // attribute to ldap regardless of if is being granted. $consumer_id = join(':', array($group_entity_type, $gid, $rid)); $user_auth_data[$consumer_id] = array('date_granted' => time(), 'consumer_id_mixed_case' => $consumer_id); } } } }
/** * Associates a given node with a course * Necessary because we add that context on form submit * which doesn't happen when the API creates nodes * @Given :type :title is content for course :course_title */ public function nodeIsContentOfCourse($node_type, $node_title, $course_title) { $node = $this->findNodeByTitle($node_type, $node_title); $course = $this->findNodeByTitle('course', $course_title); $membership = og_group('node', $course->nid, array('entity_type' => 'node', 'entity' => $node, 'field_name' => 'og_group_ref', 'state' => OG_STATE_ACTIVE)); $node->og_group_ref[LANGUAGE_NONE][0]['target_id'] = $course->nid; node_save($node); if (!$membership) { throw new \Exception(sprintf("Could not add %s '%s' to course '%s'"), $node_type, $node_title, $course_title); } }
$_SERVER['HTTP_USER_AGENT'] = 'console'; $modules_to_enable = array('og', 'entity'); // Bootstrap Drupal. include_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); module_enable(array('og', 'entity')); // Run cron after installing drupal_cron_run(); // Create users with ID 2 to 5. $uids = array(); foreach (range(2, 5) as $i) { $values = array('name' => 'og_test_user' . $i, 'mail' => 'og_test_user' . $i . '@example.com', 'pass' => user_password(5), 'status' => 1); $account = entity_create('user', $values); entity_save('user', $account); $uids[] = $account; } // Create a group node. og_create_field(OG_GROUP_FIELD, 'node', 'article'); $values = array('uid' => $uids[0]->uid, 'type' => 'article', 'title' => 'Group node'); $node = entity_create('node', $values); $node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1; entity_save('node', $node); // Explicetly set the created timestamp. $node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['created'] = 1000000000; entity_save('node', $node); // Assign users to group. $group = og_get_group('node', $node->nid); $items = array(1 => OG_STATE_ACTIVE, 2 => OG_STATE_PENDING, 3 => OG_STATE_BLOCKED); foreach ($items as $key => $state) { og_group($group->gid, 'user', $uids[$key], $state); }
/** * Grants the given role to the current user, for the given group. * * @Given /^I am a "([^"]*)" of the group "([^"]*)"$/ */ public function iAmAMemberOfTheGroup($role, $group_name) { // Get group $group = $this->getGroupByName($group_name); $role = $this->getGroupRoleByName($role); $account = $this->dkanContext->getCurrentUser(); if (isset($account)) { og_group('node', $group->getIdentifier(), array("entity type" => "user", "entity" => $account, "membership type" => OG_MEMBERSHIP_TYPE_DEFAULT)); og_role_grant('node', $group->getIdentifier(), $account->uid, $role); } else { throw new \InvalidArgumentException(sprintf('Could not find current user')); } }
/** * @Given /^I am a "([^"]*)" of the group "([^"]*)"$/ */ public function iAmAMemberOfTheGroup($role, $group_name) { $nid = db_query('SELECT nid FROM node WHERE title = :group_name', array(':group_name' => $group_name))->fetchField(); if ($account = $this->getCurrentUser()) { og_group('node', $nid, array("entity type" => "user", "entity" => $account, "membership type" => OG_MEMBERSHIP_TYPE_DEFAULT)); } else { throw new \InvalidArgumentException(sprintf('Could not find current user')); } }
/** * Remove/add the courses for s given values. * * @param $group * Determine if we need to group or un group the courses. */ public static function __RemoveCourses($group = TRUE) { $fields = array('field_faculty', 'field_department_id'); $search_values = array('field_faculty' => "Harvard Graduate School of Design", 'field_department_id' => 'Architecture'); // Un-grouping removed courses from the group. $courses = harvard_courses_related_nodes($fields, $search_values); if (!empty($courses)) { foreach ($courses as $course_id) { if ($group) { og_group('node', 2, array('entity_type' => 'node', 'entity' => $course_id)); } else { og_ungroup('node', 2, 'node', $course_id); } } } }
/** * grant single authorization * * @see ldapAuthorizationConsumerAbstract::grantSingleAuthorization() * */ public function grantSingleAuthorization(&$user, $consumer_id, $consumer, &$user_auth_data, $reset = FALSE) { $watchdog_tokens = array('%consumer_id' => $consumer_id, '%username' => $user->name, '%ogversion' => $this->ogVersion, '%function' => 'LdapAuthorizationConsumerOG.grantSingleAuthorization()'); if ($this->hasAuthorization($user, $consumer_id)) { og_invalidate_cache(); // if trying to grant, but things already granted, flush cache if ($this->hasAuthorization($user, $consumer_id)) { return TRUE; } } if (empty($consumer['exists'])) { if ($this->detailedWatchdogLog) { watchdog('ldap_auth_og', '%function consumer_id %consumer_id does not exist', $watchdog_tokens, WATCHDOG_DEBUG); } return FALSE; } if ($this->ogVersion == 1) { list($gid, $rid) = @explode('-', $consumer_id); } else { list($group_entity_type, $gid, $rid) = @explode(':', $consumer_id); $watchdog_tokens['%entity_type'] = $group_entity_type; } $watchdog_tokens['%gid'] = $gid; $watchdog_tokens['%rid'] = $rid; $watchdog_tokens['%uid'] = $user->uid; $watchdog_tokens['%entity_type'] = $group_entity_type; // CASE: grant role if ($this->detailedWatchdogLog) { watchdog('ldap_auth_og', '%function calling og_role_grant(%entity_type, %gid, %uid, %rid). og version=%ogversion', $watchdog_tokens, WATCHDOG_DEBUG); } if ($this->ogVersion == 1) { $values = array('entity type' => 'user', 'entity' => $user, 'state' => OG_STATE_ACTIVE, 'membership type' => OG_MEMBERSHIP_TYPE_DEFAULT); $user_entity = og_group($gid, $values); og_role_grant($gid, $user->uid, $rid); if ($reset) { og_invalidate_cache(); } } else { $values = array('entity_type' => 'user', 'entity' => $user->uid, 'field_name' => FALSE, 'state' => OG_STATE_ACTIVE); $og_membership = og_group($group_entity_type, $gid, $values); og_role_grant($group_entity_type, $gid, $user->uid, $rid); if ($reset) { og_invalidate_cache(array($gid)); } } if ($this->detailedWatchdogLog) { watchdog('ldap_auth_og', '%function <hr />granted: entity_type=%entity_type gid=%gid, rid=%rid for username=%username', $watchdog_tokens, WATCHDOG_DEBUG); } return TRUE; }
/** * Adds a member to an organic group with the specified role. * * @param object $account * The user to be added in group. * @param string $group_role * The machine name of the group role. * @param object $group * The group node. * @param string $group_type * (optional) The group's entity type. * * @throws \Exception * Print out descriptive error message by throwing an exception. */ protected function addMembertoGroup($account, $group_role, $group, $group_type = 'node') { list($gid, , ) = entity_extract_ids($group_type, $group); $membership = og_group($group_type, $gid, array('entity type' => 'user', 'entity' => $account)); if (!$membership) { throw new \Exception("The Organic Group membership could not be created."); } // Add role for membership. $roles = og_roles($group_type, $group->type, $gid); $rid = array_search($group_role, $roles); if (!$rid) { throw new \Exception("'{$group_role}' is not a valid group role."); } og_role_grant($group_type, $gid, $account->uid, $rid); }