You are here

function acl_add_nodes in ACL 8

Same name and namespace in other branches
  1. 7 acl.module \acl_add_nodes()

Provide access control to all nodes selected by a subquery, based upon an ACL id.

1 call to acl_add_nodes()
AclTest::testNodeAclAddRemoveFromNode in src/Tests/AclTest.php
Includes acl_node_add_acl, acl_node_remove_acl, acl_node_clear_acls

File

./acl.module, line 126
An API module providing by-user access control lists.

Code

function acl_add_nodes(SelectInterface $subselect, $acl_id, $view, $update, $delete, $priority = 0) {
  $database = \Drupal::database();
  $database
    ->delete('acl_node')
    ->condition('acl_id', $acl_id)
    ->condition('nid', $subselect, 'IN')
    ->execute();
  $subselect
    ->addExpression($acl_id, 'acl_id');
  $subselect
    ->addExpression((int) $view, 'grant_view');
  $subselect
    ->addExpression((int) $update, 'grant_update');
  $subselect
    ->addExpression((int) $delete, 'grant_delete');
  $subselect
    ->addExpression($priority, 'priority');
  if (\Drupal::database()
    ->driver() == 'mysql') {
    $database
      ->insert('acl_node')
      ->from($subselect)
      ->execute();
  }
  else {

    // The PostgreSQL and SQLite drivers currently fail to
    // generate the required parentheses around the subselect and
    // cause an error in their respective database systems.
    $results = $subselect
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    if (!empty($results)) {
      $query = $database
        ->insert('acl_node');
      $query
        ->fields([
        'acl_id',
        'nid',
        'grant_view',
        'grant_update',
        'grant_delete',
        'priority',
      ]);
      foreach ($results as $result) {
        $query
          ->values($result);
      }
      $query
        ->execute();
    }
  }
}