You are here

public function AccessGrantEntityController::save in Access Control Kit 7

Saves an access grant to the database.

Parameters

object $grant: An access grant object with the following properties:

  • gid: (optional) The unique ID for the grant being saved. If $grant->gid is empty or omitted, a new grant will be inserted.
  • uid: The ID of the user being granted access.
  • rid: The ID of the role being granted to the user.
  • scheme: The machine name of the scheme to which the grant belongs.
  • original: (optional) The original grant object before any changes were applied. When omitted, the unchanged grant object is loaded from the database and stored in this property.

Since an access grant is an entity, any fields contained in the grant object are saved alongside the grant object.

Return value

int Status constant indicating whether the grant was inserted (SAVED_NEW) or updated (SAVED_UPDATED). When inserting a new grant, $grant->gid will contain the ID of the newly created grant.

File

./access_grant_entity_controller.inc, line 77
Contains the access grant entity controller.

Class

AccessGrantEntityController
Provides the entity controller for access grants.

Code

public function save($grant) {
  $transaction = db_transaction();
  try {

    // Load the stored entity, if any.
    if (!empty($grant->gid) && !isset($grant->original)) {
      $grant->original = entity_load_unchanged('access_grant', $grant->gid);
    }
    field_attach_presave('access_grant', $grant);
    module_invoke_all('access_grant_presave', $grant);
    module_invoke_all('entity_presave', $grant, 'access_grant');
    if (empty($grant->gid)) {
      $op = 'insert';
      $status = drupal_write_record('access_grant', $grant);
      field_attach_insert('access_grant', $grant);
      $this
        ->resetCache();
    }
    else {
      $op = 'update';
      $status = drupal_write_record('access_grant', $grant, 'gid');
      field_attach_update('access_grant', $grant);
      $this
        ->resetCache(array(
        $grant->gid,
      ));
    }
    module_invoke_all('access_grant_' . $op, $grant);
    module_invoke_all('entity_' . $op, $grant, 'access_grant');
    unset($grant->original);
    return $status;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('access', $e);
    throw $e;
  }
}