Пример #1
0
 /**
  * Return whereas a user is an editorial team member or not.
  *
  * @param int $uid
  *    User UID.
  *
  * @return bool
  *    TRUE if the user is a team member, FALSE otherwise.
  */
 public function isEditoralTeamMember($uid = 0)
 {
     $is_team_member = FALSE;
     $account = user_load($uid);
     $groups = og_get_groups_by_user($account, 'node');
     if ($groups) {
         $is_team_member = (bool) db_select('node', 'n')->fields('n', array('nid'))->condition('n.type', 'editorial_team')->condition('n.nid', $groups)->execute()->fetchAll(\PDO::FETCH_COLUMN);
     }
     return $is_team_member;
 }
 /**
  * Overrides RestfulEntityBaseMultipleBundles::getQueryForList().
  */
 public function queryForListFilter(\EntityFieldQuery $query)
 {
     // Prepare list of valid accounts for current user.
     $account = $this->getAccount();
     $valid_groups = og_get_groups_by_user($account);
     // If no valid group was found, put a fake group, so the fieldCondition
     // will not fail, though will not match any message.
     if (empty($valid_groups)) {
         $valid_groups = array('node' => array());
     }
     parent::queryForListFilter($query);
     // Accept only messages in the valid groups
     $query->fieldCondition('field_meter_account', 'target_id', $valid_groups['node'], 'IN');
 }
 /**
  * {@inheritdoc}
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     parent::buildEntityFieldQuery();
     global $user;
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     // FIXME: drupal.org/node/1413108
     unset($query->tags['entityreference']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     $group_type = $this->field['settings']['target_type'];
     $entity_info = entity_get_info($group_type);
     if (!field_info_field(OG_GROUP_FIELD)) {
         // There are no groups, so falsify query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         return $query;
     }
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1, '=');
     $account = user_load($user->uid);
     if (user_access('administer site configuration', $account)) {
         // Site administrator can choose also groups he is not member of.
         $query->fieldCondition('c4m_og_status', 'value', array('deleted'), 'NOT IN');
         return $query;
     }
     $user_groups = og_get_groups_by_user(NULL, $group_type);
     $user_groups = $user_groups ? $user_groups : array();
     if ($user_groups) {
         $query->propertyCondition($entity_info['entity keys']['id'], $user_groups, 'IN');
     } else {
         // User doesn't have permission to select any group so falsify this
         // query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
     }
     $unallowed_values = array('requested', 'archived', 'rejected', 'deleted');
     $query->fieldCondition('c4m_og_status', 'value', $unallowed_values, 'NOT IN');
     return $query;
 }
Пример #4
0
 /**
  * Build an EntityFieldQuery to get referencable entities.
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     global $user;
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     // FIXME: drupal.org/node/1413108
     unset($query->tags['entityreference']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     $group_type = $this->field['settings']['target_type'];
     $entity_info = entity_get_info($group_type);
     if (!field_info_field(OG_GROUP_FIELD)) {
         // There are no groups, so falsify query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         return $query;
     }
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1, '=');
     if (empty($this->instance['field_mode'])) {
         return $query;
     }
     $field_mode = $this->instance['field_mode'];
     $user_groups = og_get_groups_by_user(NULL, $group_type);
     $user_groups = $user_groups ? $user_groups : array();
     $user_groups = array_merge($user_groups, $this->getGidsForCreate());
     // Show the user only the groups they belong to.
     if ($field_mode == 'default') {
         if ($user_groups && !empty($this->instance) && $this->instance['entity_type'] == 'node') {
             // Determine which groups should be selectable.
             $node = $this->entity;
             $node_type = $this->instance['bundle'];
             $ids = array();
             foreach ($user_groups as $gid) {
                 // Check if user has "create" permissions on those groups.
                 // If the user doesn't have create permission, check if perhaps the
                 // content already exists and the user has edit permission.
                 if (og_user_access($group_type, $gid, "create {$node_type} content")) {
                     $ids[] = $gid;
                 } elseif (!empty($node->nid) && (og_user_access($group_type, $gid, "update any {$node_type} content") || $user->uid == $node->uid && og_user_access($group_type, $gid, "update own {$node_type} content"))) {
                     $node_groups = isset($node_groups) ? $node_groups : og_get_entity_groups('node', $node->nid);
                     if (in_array($gid, $node_groups['node'])) {
                         $ids[] = $gid;
                     }
                 }
             }
         } else {
             $ids = $user_groups;
         }
         if ($ids) {
             $query->propertyCondition($entity_info['entity keys']['id'], $ids, 'IN');
         } else {
             // User doesn't have permission to select any group so falsify this
             // query.
             $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         }
     } elseif ($field_mode == 'admin' && $user_groups) {
         // Show only groups the user doesn't belong to.
         if (!empty($this->instance) && $this->instance['entity_type'] == 'node') {
             // Don't include the groups, the user doesn't have create
             // permission.
             $node_type = $this->instance['bundle'];
             foreach ($user_groups as $delta => $gid) {
                 if (!og_user_access($group_type, $gid, "create {$node_type} content")) {
                     unset($user_groups[$delta]);
                 }
             }
         }
         if ($user_groups) {
             $query->propertyCondition($entity_info['entity keys']['id'], $user_groups, 'NOT IN');
         }
     }
     return $query;
 }
<?php

$expire_time = REQUEST_TIME - 60 * 60 * 24 * 180;
$sql = "select distinct u.uid, name from {users} u inner join {og_users_roles} o on u.uid = o.uid where access < :expire_time limit 10;";
$query = db_query($sql, array(':expire_time' => $expire_time));
$data = $query->fetchAllAssoc('uid');
$mids = array();
foreach ($data as $row) {
    $uid = $row->uid;
    $name = $row->name;
    drush_log('Removing all memberships of ' . $name . ' (' . $uid . ')');
    $account = user_load($uid);
    $groups = og_get_groups_by_user($account);
    if ($groups) {
        foreach ($groups as $group_type => $gids) {
            foreach ($gids as $gid) {
                // Load membership.
                $membership = og_get_membership($group_type, $gid, 'user', $uid);
                if ($membership) {
                    $mids[$membership->id] = $membership->id;
                }
            }
        }
    }
    // Remove stale records in the {og_users_roles} table.
    db_delete('og_users_roles')->condition('uid', $uid)->execute();
    // Remove all roles from user.
    user_save($account, array('roles' => array('2' => 'authenticated user')));
}
og_membership_delete_multiple($mids);
 /**
  * @see ldapAuthorizationConsumerAbstract::usersAuthorizations
  */
 public function usersAuthorizations(&$user, $reset = FALSE, $return_data = TRUE)
 {
     static $users;
     if (!is_array($users)) {
         $users = array();
         // no cache exists, create static array
     } elseif ($reset && isset($users[$user->uid])) {
         unset($users[$user->uid]);
         // clear users cache
     } elseif (!$return_data) {
         return NULL;
         // simply clearing cache
     } elseif (!empty($users[$user->uid])) {
         return $users[$user->uid];
         // return cached data
     }
     $authorizations = array();
     if ($this->ogVersion == 1) {
         $gids = og_get_groups_by_user($user);
         foreach ($gids as $i => $gid) {
             $roles = og_get_user_roles($gid, $user->uid);
             if (!empty($roles[$this->defaultMembershipRid])) {
                 // if you aren't a member, doesn't matter what roles you have in og 1.5
                 if (isset($roles[$this->anonymousRid])) {
                     unset($roles[$this->anonymousRid]);
                 }
                 // ignore anonymous role
                 $rids = array_values($roles);
                 asort($rids, SORT_NUMERIC);
                 // go low to high to get default memberships first
                 foreach ($rids as $rid) {
                     $authorizations[] = ldap_authorization_og_authorization_id($gid, $rid);
                 }
             }
         }
     } else {
         // og 7.x-2.x
         $user_entities = entity_load('user', array($user->uid));
         $memberships = og_get_entity_groups('user', $user_entities[$user->uid]);
         foreach ($memberships as $entity_type => $entity_memberships) {
             foreach ($entity_memberships as $og_membership_id => $gid) {
                 $roles = og_get_user_roles($entity_type, $gid, $user->uid);
                 foreach ($roles as $rid => $discard) {
                     $authorizations[] = ldap_authorization_og_authorization_id($gid, $rid, $entity_type);
                 }
             }
         }
     }
     $users[$user->uid] = $authorizations;
     return $authorizations;
 }
 /**
  * Build an EntityFieldQuery to get referencable entities.
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // The "node_access" tag causes errors, so we replace it with
     // "entity_field_access" tag instead.
     // @see _node_query_node_access_alter().
     unset($query->tags['node_access']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     $group_type = $this->field['settings']['target_type'];
     $entity_info = entity_get_info($group_type);
     if (!field_info_field(OG_GROUP_FIELD)) {
         // There are no groups, so falsify query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         return $query;
     }
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1, '=');
     $user_groups = og_get_groups_by_user(NULL, $group_type);
     $reference_type = $this->field['settings']['handler_settings']['reference_type'];
     // Show the user only the groups they belong to.
     if ($reference_type == 'my_groups') {
         if ($user_groups && !empty($this->instance) && $this->instance['entity_type'] == 'node') {
             // Check if user has "create" permissions on those groups.
             $node_type = $this->instance['bundle'];
             $ids = array();
             foreach ($user_groups as $gid) {
                 if (og_user_access($group_type, $gid, "create {$node_type} content")) {
                     $ids[] = $gid;
                 }
             }
         } else {
             $ids = $user_groups;
         }
         if ($ids) {
             $query->propertyCondition($entity_info['entity keys']['id'], $ids, 'IN');
         } else {
             // User doesn't have permission to select any group so falsify this
             // query.
             $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         }
     } elseif ($reference_type == 'other_groups' && $user_groups) {
         // Show only group the user doesn't belong to.
         $query->propertyCondition($entity_info['entity keys']['id'], $user_groups, 'NOT IN');
     }
     return $query;
 }