You are here

class OAuth2ServerEntityController in OAuth2 Server 7

The entity controller for oauth2_server entities.

Provides enhanced exportables, allowing scopes to be exported with servers.

Hierarchy

Expanded class hierarchy of OAuth2ServerEntityController

1 string reference to 'OAuth2ServerEntityController'
oauth2_server_entity_info in ./oauth2_server.module
Implements hook_entity_info().

File

includes/oauth2_server.server_controller.inc, line 8

View source
class OAuth2ServerEntityController extends EntityAPIControllerExportable {

  /**
   * Overrides EntityAPIControllerExportable::invoke().
   */
  public function invoke($hook, $entity) {
    if ($hook == 'delete') {

      // To ease figuring out whether this is a revert, make sure that the
      // entity status is updated in case the providing module has been
      // disabled.
      if (entity_has_status('oauth2_server_scope', $entity, ENTITY_IN_CODE) && !module_exists($entity->module)) {
        $entity->status = ENTITY_CUSTOM;
      }
      $is_revert = entity_has_status('oauth2_server_scope', $entity, ENTITY_IN_CODE);
    }

    // The server serves as a bundle for scopes and clients.
    if ($hook == 'insert') {
      field_attach_create_bundle('oauth2_server_scope', $entity->name);
      field_attach_create_bundle('oauth2_server_client', $entity->name);
    }
    elseif ($hook == 'delete' && empty($is_revert)) {
      field_attach_delete_bundle('oauth2_server_scope', $entity->name);
      field_attach_delete_bundle('oauth2_server_client', $entity->name);
    }
    elseif ($hook == 'update' && $entity->original->name != $entity->name) {
      field_attach_rename_bundle('oauth2_server_scope', $entity->original->name, $entity->name);
      field_attach_rename_bundle('oauth2_server_client', $entity->original->name, $entity->name);
    }
    parent::invoke($hook, $entity);
  }

  /**
   * Overrides EntityAPIControllerExportable::export().
   */
  public function export($entity, $prefix = '') {
    $vars = get_object_vars($entity);
    unset($vars['server_id'], $vars['status'], $vars['module'], $vars['is_new']);
    $vars['scopes'] = array();

    // Get a list of all scopes belonging to this server.
    // Add the "oauth2_server_scope_export" tag so that other modules can
    // restrict which scopes should be exported.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'oauth2_server_scope');
    $query
      ->propertyCondition('server', $entity->name);
    $query
      ->addTag('oauth2_server_scope_export');
    $query
      ->addMetaData('oauth2_server', $entity);
    $results = $query
      ->execute();
    if ($results) {
      $scope_ids = array_keys($results['oauth2_server_scope']);
      $scopes = entity_load('oauth2_server_scope', $scope_ids);
      foreach ($scopes as $scope) {
        $scope_vars = get_object_vars($scope);
        unset($scope_vars['scope_id'], $scope_vars['server']);
        $vars['scopes'][] = $scope_vars;
      }
    }
    return entity_var_json_export($vars, $prefix);
  }

  /**
   * Overrides EntityAPIControllerExportable::save().
   */
  public function save($entity, DatabaseTransaction $transaction = NULL) {

    // There are scopes to be saved.
    if (!empty($entity->scopes)) {
      $existing_scopes = array();

      // Gather scope names, load all existing scopes, rekey them by name.
      $scope_names = array();
      foreach ($entity->scopes as $scope_values) {
        $scope_names[] = $scope_values['name'];
      }
      $scopes = oauth2_server_scope_load_multiple($entity->name, $scope_names);
      foreach ($scopes as $scope) {
        $existing_scopes[$scope->name] = $scope;
      }

      // Handle the insert / update.
      foreach ($entity->scopes as $scope_values) {
        $scope = entity_create('oauth2_server_scope', $scope_values);
        $scope->server = $entity->name;

        // A scope with the same name exists, steal its id, do an update.
        if (isset($existing_scopes[$scope->name])) {
          $existing_scope = $existing_scopes[$scope->name];
          $scope->scope_id = $existing_scope->scope_id;
          unset($scope->is_new);
        }
        $scope
          ->save();
      }
    }
    return parent::save($entity, $transaction);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildContent public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::buildContent
EntityAPIController::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::create
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIControllerExportable::$entityCacheByName protected property
EntityAPIControllerExportable::$nameKey protected property
EntityAPIControllerExportable::applyConditions protected function
EntityAPIControllerExportable::attachLoad protected function Overridden. Overrides DrupalDefaultEntityController::attachLoad
EntityAPIControllerExportable::buildQuery protected function Support loading by name key. Overrides EntityAPIController::buildQuery
EntityAPIControllerExportable::cacheGet protected function Overridden. Overrides DrupalDefaultEntityController::cacheGet
EntityAPIControllerExportable::cacheGetByName protected function Like cacheGet() but keyed by name.
EntityAPIControllerExportable::cacheSet protected function Overridden. Overrides DrupalDefaultEntityController::cacheSet
EntityAPIControllerExportable::delete public function Overridden to care about reverted entities. Overrides EntityAPIController::delete
EntityAPIControllerExportable::load public function Overridden to support passing numeric ids as well as names as $ids. Overrides EntityAPIController::load
EntityAPIControllerExportable::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides EntityAPIController::resetCache
EntityAPIControllerExportable::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::view
EntityAPIControllerExportable::__construct public function Overridden. Overrides EntityAPIController::__construct
OAuth2ServerEntityController::export public function Overrides EntityAPIControllerExportable::export(). Overrides EntityAPIControllerExportable::export
OAuth2ServerEntityController::invoke public function Overrides EntityAPIControllerExportable::invoke(). Overrides EntityAPIControllerExportable::invoke
OAuth2ServerEntityController::save public function Overrides EntityAPIControllerExportable::save(). Overrides EntityAPIControllerExportable::save