/** * Overrides \RestfulEntityBase::getQueryForList(). */ public function getQueryForList() { $query = parent::getQueryForList(); // Get the configured roles. if (!$options = $this->getPluginKey('options')) { return $query; } // Get a list of role ids for the configured roles. $roles_list = user_roles(); $selected_rids = array(); foreach ($roles_list as $rid => $role) { if (in_array($role, $options['roles'])) { $selected_rids[] = $rid; } } if (empty($selected_rids)) { return $query; } // Get the list of user ids belonging to the selected roles. $uids = db_query('SELECT uid FROM {users_roles} WHERE rid IN (:rids)', array( ':rids' => $selected_rids, ))->fetchAllAssoc('uid'); // Restrict the list of entities to the nodes authored by any user on the // list of users with the administrator role. if (!empty($uids)) { $query->propertyCondition('uid', array_keys($uids), 'IN'); } return $query; }
/** * Overrides \RestfulEntityBase::checkPropertyAccess(). * * Allow user to create a label for the unsaved term, even if the user doesn't * have access to update existing terms, as required by the entity metadata * wrapper's access check. */ protected function checkPropertyAccess($op, $public_field_name, EntityMetadataWrapper $property, EntityMetadataWrapper $wrapper) { $info = $property->info(); $term = $wrapper->value(); if (!empty($info['name']) && $info['name'] == 'name' && empty($term->tid) && $op == 'edit') { return TRUE; } return parent::checkPropertyAccess($op, $public_field_name, $property, $wrapper); }
/** * Overrides RestfulEntityBase::publicFieldsInfo(). */ public function publicFieldsInfo() { $public_fields = parent::publicFieldsInfo(); $public_fields['type'] = array( 'property' => 'name', 'wrapper_method' => 'getBundle', 'wrapper_method_on_entity' => TRUE, ); return $public_fields; }
/** * {@inheritdoc} * * Override RestfulEntityBase::createEntity() to test if meter already exists, * to allow update existing nodes in stead of creating a copy. */ public function createEntity() { // Check if an electricity entity with the same parameters exists. $query = new EntityFieldQuery(); $result = $query->entityCondition('entity_type', 'electricity_raw')->propertyCondition('meter_nid', $this->request['meter'])->propertyCondition('timestamp', $this->request['timestamp'])->propertyCondition('meter_type', $this->request['meter_type'])->propertyCondition('rate_type', $this->request['rate_type'])->propertyCondition('frequency', $this->request['frequency'])->range(0, 1)->execute(); if (!empty($result['electricity_raw'])) { // Node exists, update it. $id = key($result['electricity_raw']); return parent::updateEntity($id); } // New node. return parent::createEntity(); }
/** * {@inheritdoc} * * Return the basic entity field query for messages, with additional filter * that matches only messages accessible by the current user. */ public function getEntityFieldQuery() { $query = parent::getEntityFieldQuery(); // Add condition to match only messages accessible by the current user. // Find the list of valid OGs for current user $account = $this->getAccount(); $wrapper = entity_metadata_wrapper('user', $account); $gids = $wrapper->og_user_node->value(array('identifier' => TRUE)); if (!$gids) { // User is not a member in any group. throw new \RestfulUnauthorizedException('Current user is not related to any account. No messages to show.'); } // Filter to match only messages from these OGs $query->fieldCondition('field_meter_account', 'target_id', $gids, 'IN'); return $query; }
/** * Overrides RestfulEntityBase::getQueryForList(). * * Add dates filter. */ public function getEntityFieldQuery() { $request = $this->getRequest(); $query = parent::getEntityFieldQuery(); if (empty($request['from_date'])) { throw new \RestfulBadRequestException('Missing from_date parameter.'); } if (!self::validDate($request['from_date'])) { throw new \RestfulBadRequestException('Wrong from_date format. Expecting yyyy-mm-dd.'); } $query->propertyCondition('date', $request['from_date'], '>='); if (empty($request['until_date'])) { throw new \RestfulBadRequestException('Missing until_date parameter.'); } if (!self::validDate($request['until_date'])) { throw new \RestfulBadRequestException('Wrong until_date format. Expecting yyyy-mm-dd.'); } $query->propertyCondition('date', $request['until_date'], '<='); return $query; }
public function propertyValuesPreprocess($property_name, $value, $public_field_name) { $request = $this->getRequest(); self::cleanRequest($request); if ($public_field_name == 'terms') { $new_value = array(); foreach ($request['terms'] as $term) { $new_value[] = is_array($term) ? $term['tid'] : $term; } return $new_value; } return parent::propertyValuesPreprocess($property_name, $value, $public_field_name); }
/** * Overrides \RestfulEntityBase::getQueryForList(). * * Skip the anonymous user in listing. */ public function getQueryForList() { $query = parent::getQueryForList(); $query->entityCondition('entity_id', 0, '>'); return $query; }
/** * Overrides RestfulEntityBase::getQueryCount(). * * Expose only published nodes. */ public function getQueryCount() { $query = parent::getQueryCount(); $query->propertyCondition('status', NODE_PUBLISHED); return $query; }
/** * Overrides RestfulEntityBase::access(). * * If "File entity" module exists, determine access by its provided permissions * otherwise, check if variable is set to allow anonymous users to upload. * Defaults to authenticated user. */ public function access() { // The getAccount method may return a RestfulUnauthorizedException when an // authenticated user cannot be found. Since this is called from the access // callback, not from the page callback we need to catch the exception. try { $account = $this->getAccount(); } catch (\RestfulUnauthorizedException $e) { // If a user is not found then load the anonymous user to check // permissions. $account = drupal_anonymous_user(); } if (module_exists('file_entity')) { return user_access('bypass file access', $account) || user_access('create files', $account); } return (variable_get('restful_file_upload_allow_anonymous_user', FALSE) || $account->uid) && parent::access(); }
/** * Overrides RestfulEntityBase::getQueryForList(). */ public function getQueryForList() { $query = parent::getQueryForList(); $query->entityCondition('bundle', array_keys($this->getBundles()), 'IN'); return $query; }