Commit 1895c22d authored by salvis's avatar salvis

#2610538 by id.tarzanych: Replace Database::getConnection() with \Drupal::database().

parent 4c600fbe
......@@ -5,6 +5,8 @@ CHANGELOG for ACL for Drupal 8
acl 8.x-1.x-dev:
Fixes:
- #2580207 by mccrodp: Add missing acl_remove_all_users() function.
Tasks:
- #2610538 by id.tarzanych: Replace Database::getConnection() with \Drupal::database().
acl 8.x-1.0-alpha1 (2015-10-19):
......
......@@ -7,24 +7,23 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
/**
* Implementation of acl_edit_form().
*/
function _acl_edit_form($acl_id, $label = NULL, $new_acl = FALSE) {
$connection = Database::getConnection();
$database = \Drupal::database();
$accounts = array();
if (!$new_acl) {
// Ensure the ACL in question even exists.
if (!($record = $connection->query("SELECT name, figure FROM {acl} WHERE acl_id = :acl_id", [
if (!($record = $database->query("SELECT name, figure FROM {acl} WHERE acl_id = :acl_id", [
'acl_id' => $acl_id,
])->fetchAssoc())) {
return array();
}
$result = $connection->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid LEFT JOIN {acl_user} aclu ON aclu.uid = u.uid WHERE acl_id = :acl_id", [
$result = $database->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid LEFT JOIN {acl_user} aclu ON aclu.uid = u.uid WHERE acl_id = :acl_id", [
'acl_id' => $acl_id,
]);
......@@ -112,7 +111,7 @@ function _acl_edit_form_after_build($form, FormStateInterface $form_state) {
}
}
elseif (!empty($triggering_element['#value']) && $triggering_element['#value'] == $form['add_button']['#value'] && !empty($form['add']['#value'])) {
$user = Database::getConnection()->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE ud.name = :name", [
$user = \Drupal::database()->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE ud.name = :name", [
'name' => $form['add']['#value'],
])->fetchObject();
if (!$user) {
......@@ -145,14 +144,14 @@ function _acl_edit_form_after_build($form, FormStateInterface $form_state) {
* The module that embedded our form must call this function!
*/
function acl_save_form($form, $priority = NULL) {
$connection = Database::getConnection();
$database = \Drupal::database();
$users = unserialize($form['user_list']);
$connection->delete('acl_user')
$database->delete('acl_user')
->condition('acl_id', $form['acl_id'])
->execute();
foreach ($users as $uid => $name) {
$connection->insert('acl_user')
$database->insert('acl_user')
->fields([
'acl_id' => $form['acl_id'],
'uid' => $uid,
......@@ -160,7 +159,7 @@ function acl_save_form($form, $priority = NULL) {
->execute();
}
if (isset($priority)) {
$connection->update('acl_node')
$database->update('acl_node')
->fields([
'priority' => $priority,
])
......
......@@ -11,7 +11,6 @@
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Database\Database;
use Drupal\node\NodeInterface;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormStateInterface;
......@@ -33,7 +32,7 @@ use Drupal\Core\Database\Query\SelectInterface;
* The ID of the newly created ACL.
*/
function acl_create_acl($module, $name = NULL, $figure = NULL) {
$query = Database::getConnection()->insert('acl');
$query = \Drupal::database()->insert('acl');
$query->fields([
'module' => $module,
'name' => $name,
......@@ -47,15 +46,15 @@ function acl_create_acl($module, $name = NULL, $figure = NULL) {
* Delete an existing ACL.
*/
function acl_delete_acl($acl_id) {
$connection = Database::getConnection();
$database = \Drupal::database();
$connection->delete('acl')
$database->delete('acl')
->condition('acl_id', $acl_id)
->execute();
$connection->delete('acl_user')
$database->delete('acl_user')
->condition('acl_id', $acl_id)
->execute();
$connection->delete('acl_node')
$database->delete('acl_node')
->condition('acl_id', $acl_id)
->execute();
}
......@@ -64,15 +63,15 @@ function acl_delete_acl($acl_id) {
* Add the specified UID to an ACL.
*/
function acl_add_user($acl_id, $uid) {
$connection = Database::getConnection();
$database = \Drupal::database();
$test_uid = $connection->query("SELECT uid FROM {acl_user} WHERE acl_id = :acl_id AND uid = :uid", [
$test_uid = $database->query("SELECT uid FROM {acl_user} WHERE acl_id = :acl_id AND uid = :uid", [
'acl_id' => $acl_id,
'uid' => $uid,
])->fetchField();
if (!$test_uid) {
$connection
$database
->insert('acl_user')
->fields([
'acl_id' => $acl_id,
......@@ -86,7 +85,7 @@ function acl_add_user($acl_id, $uid) {
* Remove the specified UID from an ACL.
*/
function acl_remove_user($acl_id, $uid) {
Database::getConnection()->delete('acl_user')
\Drupal::database()->delete('acl_user')
->condition('acl_id', $acl_id)
->condition('uid', $uid)
->execute();
......@@ -96,7 +95,7 @@ function acl_remove_user($acl_id, $uid) {
* Remove all users from an ACL.
*/
function acl_remove_all_users($acl_id) {
Database::getConnection()->delete('acl_user')
\Drupal::database()->delete('acl_user')
->condition('acl_id', $acl_id)
->execute();
}
......@@ -125,9 +124,9 @@ function acl_edit_form(FormStateInterface $form_state, $acl_id, $label = NULL, $
* Provide access control to all nodes selected by a subquery, based upon an ACL id.
*/
function acl_add_nodes(SelectInterface $subselect, $acl_id, $view, $update, $delete, $priority = 0) {
$connection = Database::getConnection();
$database = \Drupal::database();
$connection->delete('acl_node')
$database->delete('acl_node')
->condition('acl_id', $acl_id)
->condition('nid', $subselect, 'IN')
->execute();
......@@ -137,7 +136,7 @@ function acl_add_nodes(SelectInterface $subselect, $acl_id, $view, $update, $del
$subselect->addExpression((int) $delete, 'grant_delete');
$subselect->addExpression($priority, 'priority');
if (\Drupal::database()->driver() == 'mysql') {
$connection->insert('acl_node')
$database->insert('acl_node')
->from($subselect)
->execute();
}
......@@ -147,7 +146,7 @@ function acl_add_nodes(SelectInterface $subselect, $acl_id, $view, $update, $del
// cause an error in their respective database systems.
$results = $subselect->execute()->fetchAll(PDO::FETCH_ASSOC);
if (!empty($results)) {
$query = $connection->insert('acl_node');
$query = $database->insert('acl_node');
$query->fields([
'acl_id',
'nid',
......@@ -182,13 +181,13 @@ function acl_node_add_acl($nid, $acl_id, $view, $update, $delete, $priority = 0)
* Provide access control to a node based upon an ACL id.
*/
function acl_node_add_acl_record(array $record) {
$connection = Database::getConnection();
$database = \Drupal::database();
$connection->delete('acl_node')
$database->delete('acl_node')
->condition('acl_id', $record['acl_id'])
->condition('nid', $record['nid'])
->execute();
$connection->insert('acl_node')
$database->insert('acl_node')
->fields($record)
->execute();
}
......@@ -197,7 +196,7 @@ function acl_node_add_acl_record(array $record) {
* Remove an ACL completely from a node.
*/
function acl_node_remove_acl($nid, $acl_id) {
Database::getConnection()->delete('acl_node')
\Drupal::database()->delete('acl_node')
->condition('acl_id', $acl_id)
->condition('nid', $nid)
->execute();
......@@ -207,12 +206,12 @@ function acl_node_remove_acl($nid, $acl_id) {
* Clear all of a module's ACLs from a node.
*/
function acl_node_clear_acls($nid, $module) {
$connection = Database::getConnection();
$database = \Drupal::database();
$select = $connection->select('acl', 'a')
$select = $database->select('acl', 'a')
->fields('a', ['acl_id'])
->condition('a.module', $module);
$connection->delete('acl_node')
$database->delete('acl_node')
->condition('nid', $nid)
->condition('acl_id', $select, 'IN')
->execute();
......@@ -222,7 +221,7 @@ function acl_node_clear_acls($nid, $module) {
* Get the id of an ACL by name (+ optionally figure).
*/
function acl_get_id_by_name($module, $name, $figure = NULL) {
$query = Database::getConnection()->select('acl', 'a')
$query = \Drupal::database()->select('acl', 'a')
->fields('a', ['acl_id'])
->condition('a.module', $module)
->condition('a.name', $name);
......@@ -238,7 +237,7 @@ function acl_get_id_by_name($module, $name, $figure = NULL) {
* Get the id of an ACL by figure.
*/
function acl_get_id_by_figure($module, $figure) {
$query = Database::getConnection()->select('acl', 'a')
$query = \Drupal::database()->select('acl', 'a')
->fields('a', ['acl_id'])
->condition('a.module', $module)
->condition('a.figure', $figure);
......@@ -250,7 +249,7 @@ function acl_get_id_by_figure($module, $figure) {
* Determine whether an ACL has some assigned users.
*/
function acl_has_users($acl_id) {
return Database::getConnection()
return \Drupal::database()
->query("SELECT COUNT(uid) FROM {acl_user} WHERE acl_id = :acl_id", [
'acl_id' => $acl_id,
])
......@@ -261,7 +260,7 @@ function acl_has_users($acl_id) {
* Determine whether an ACL has a specific assigned user.
*/
function acl_has_user($acl_id, $uid) {
return Database::getConnection()
return \Drupal::database()
->query("SELECT COUNT(uid) FROM {acl_user} WHERE acl_id = :acl_id AND uid = :uid", [
'acl_id' => $acl_id,
'uid' => $uid,
......@@ -273,7 +272,7 @@ function acl_has_user($acl_id, $uid) {
* Get an array of acl_ids held by a user.
*/
function acl_get_ids_by_user($module, $uid, $name = NULL, $figure = NULL) {
$query = Database::getConnection()->select('acl', 'a');
$query = \Drupal::database()->select('acl', 'a');
$query->join('acl_user', 'au', 'a.acl_id = au.acl_id');
$query
->fields('a', ['acl_id'])
......@@ -294,7 +293,7 @@ function acl_get_ids_by_user($module, $uid, $name = NULL, $figure = NULL) {
* Get the uids of an ACL.
*/
function acl_get_uids($acl_id) {
$uids = Database::getConnection()
$uids = \Drupal::database()
->query("SELECT uid FROM {acl_user} WHERE acl_id = :acl_id", [
'acl_id' => $acl_id,
])
......@@ -311,7 +310,7 @@ function acl_node_access_records(NodeInterface $node) {
return;
}
$result = Database::getConnection()
$result = \Drupal::database()
->query("SELECT n.*, 'acl' AS realm, n.acl_id AS gid, a.module FROM {acl_node} n INNER JOIN {acl} a ON n.acl_id = a.acl_id WHERE nid = :nid", [
'nid' => $node->id(),
], ['fetch' => PDO::FETCH_ASSOC]);
......@@ -342,7 +341,7 @@ function acl_node_access_records(NodeInterface $node) {
* Implements hook_node_grants().
*/
function acl_node_grants($account, $op) {
$acl_ids = Database::getConnection()
$acl_ids = \Drupal::database()
->query("SELECT acl_id FROM {acl_user} WHERE uid = :uid", [
'uid' => $account->id(),
])
......@@ -355,7 +354,7 @@ function acl_node_grants($account, $op) {
* Implements hook_node_delete().
*/
function acl_node_delete(NodeInterface $node) {
Database::getConnection()->delete('acl_node')
\Drupal::database()->delete('acl_node')
->condition('nid', $node->id())
->execute();
}
......@@ -364,7 +363,7 @@ function acl_node_delete(NodeInterface $node) {
* Implements hook_user_cancel().
*/
function acl_user_cancel($edit, AccountInterface $account, $method) {
Database::getConnection()->delete('acl_user')
\Drupal::database()->delete('acl_user')
->condition('uid', $account->id())
->execute();
}
......@@ -374,11 +373,11 @@ function acl_user_cancel($edit, AccountInterface $account, $method) {
*/
function acl_node_access_explain($row) {
static $interpretations = array();
$connection = Database::getConnection();
$database = \Drupal::database();
if ($row->realm == 'acl') {
if (!isset($interpretations[$row->gid])) {
$acl = $connection->query("SELECT * FROM {acl} WHERE acl_id = :acl_id", [
$acl = $database->query("SELECT * FROM {acl} WHERE acl_id = :acl_id", [
'acl_id' => $row->gid,
])->fetchObject();
$acl->tag = '?';
......@@ -399,7 +398,7 @@ function acl_node_access_explain($row) {
$interpretations[$row->gid] = _acl_get_explanation("$acl->module/$acl->tag: $usernames", $acl->acl_id, $acl->module, $acl->name, $acl->figure, $usernames);
}
elseif ($row->gid == 0) {
$result = $connection->query("SELECT an.acl_id, a.module, a.name FROM {acl_node} an JOIN {acl} a ON an.acl_id = a.acl_id LEFT JOIN {acl_user} au ON a.acl_id = au.acl_id WHERE an.nid = :nid AND au.uid IS NULL", [
$result = $database->query("SELECT an.acl_id, a.module, a.name FROM {acl_node} an JOIN {acl} a ON an.acl_id = a.acl_id LEFT JOIN {acl_user} au ON a.acl_id = au.acl_id WHERE an.nid = :nid AND au.uid IS NULL", [
'nid' => $row->nid,
]);
foreach ($result as $acl) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment