diff --git a/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php b/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php index 00ef8d9..9edcf49 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php @@ -17,7 +17,19 @@ class UserPermissionsTest extends WebTestBase { * @var \Drupal\user\UserAccess */ protected $accessService; - protected $admin_user; + + /** + * A user object with administrative permissions. + * + * @var \Drupal\user\Plugin\Core\Entity\User + */ + protected $adminUser; + + /** + * The role ID of an administrative role created per test run. + * + * @var int + */ protected $rid; public static function getInfo() { @@ -31,10 +43,10 @@ public static function getInfo() { function setUp() { parent::setUp(); $this->accessService = $this->container->get('user.access'); - $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'access user profiles', 'administer site configuration', 'administer modules', 'administer users')); + $this->adminUser = $this->drupalCreateUser(array('administer permissions', 'access user profiles', 'administer site configuration', 'administer modules', 'administer users')); // Find the new role ID. - $all_rids = $this->admin_user->roles; + $all_rids = $this->adminUser->roles; unset($all_rids[DRUPAL_AUTHENTICATED_RID]); $this->rid = key($all_rids); } @@ -43,9 +55,9 @@ function setUp() { * Change user permissions and check \Drupal\user\UserAccess::check(). */ function testUserPermissionChanges() { - $this->drupalLogin($this->admin_user); + $this->drupalLogin($this->adminUser); $rid = $this->rid; - $account = $this->admin_user; + $account = $this->adminUser; // Add a permission. $this->assertFalse($this->accessService->check('administer nodes', $account), 'User does not have "administer nodes" permission.'); @@ -70,7 +82,7 @@ function testUserPermissionChanges() { * Test assigning of permissions for the administrator role. */ function testAdministratorRole() { - $this->drupalLogin($this->admin_user); + $this->drupalLogin($this->adminUser); $this->drupalGet('admin/config/people/accounts'); // Set the user's role to be the administrator role. @@ -85,7 +97,7 @@ function testAdministratorRole() { // Aggregator depends on file module, enable that as well. $edit['modules[Core][file][enable]'] = TRUE; $this->drupalPost('admin/modules', $edit, t('Save configuration')); - $this->assertTrue($this->accessService->check('administer news feeds', $this->admin_user), 'The permission was automatically assigned to the administrator role'); + $this->assertTrue($this->accessService->check('administer news feeds', $this->adminUser), 'The permission was automatically assigned to the administrator role'); } /** @@ -93,7 +105,7 @@ function testAdministratorRole() { */ function testUserRoleChangePermissions() { $rid = $this->rid; - $account = $this->admin_user; + $account = $this->adminUser; // Verify current permissions. $this->assertFalse($this->accessService->check('administer nodes', $account), 'User does not have "administer nodes" permission.'); diff --git a/core/modules/user/lib/Drupal/user/UserAccess.php b/core/modules/user/lib/Drupal/user/UserAccess.php index 2baf56c..d7000e4 100644 --- a/core/modules/user/lib/Drupal/user/UserAccess.php +++ b/core/modules/user/lib/Drupal/user/UserAccess.php @@ -2,15 +2,14 @@ /** * @file - * Contains Drupal\user\UserAccess. + * Contains \Drupal\user\UserAccess. */ namespace Drupal\user; use Drupal\Core\Database\Connection; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Core\ControllerInterface; -class UserAccess implements ControllerInterface { +class UserAccess implements UserAccessInterface { /** * A cache of permissions for a user uid. @@ -46,25 +45,6 @@ public function __construct(Connection $database) { /** * {@inheritdoc} */ - public static function create(ContainerInterface $container) { - return new static($container->get('database')); - } - - /** - * Determines whether the given user has a given privilege. - * - * @param $string - * The permission, such as "administer nodes", being checked for. - * @param $account - * The account to check. - * - * @return - * Boolean TRUE if the current user has the requested permission. - * - * All permission checks in Drupal should go through this function. This - * way, we guarantee consistent behavior, and ensure that the superuser - * can perform all actions. - */ public function check($string, $account) { // User #1 has all privileges: if ($account->uid == 1) { @@ -86,7 +66,7 @@ public function check($string, $account) { } /** - * Resets the cache of permissions and roles. + * {@inheritdoc} */ public function reset() { $this->permissionCache = array(); @@ -94,14 +74,7 @@ public function reset() { } /** - * Determines the permissions for one or more roles. - * - * @param $roles - * An array whose keys are the role IDs of interest, such as $user->roles. - * - * @return - * An array indexed by role ID. Each value is an array whose keys are the - * permission strings for the given role ID. + * {@inheritdoc} */ public function userRolePermissions($roles) { $role_permissions = $fetch = array(); @@ -121,7 +94,7 @@ public function userRolePermissions($roles) { if ($fetch) { // Get from the database permissions that were not in the cache. // Only role IDs with at least one permission assigned will return rows. - $result = db_query("SELECT rid, permission FROM {role_permission} WHERE rid IN (:fetch)", array(':fetch' => $fetch)); + $result = $this->database->query("SELECT rid, permission FROM {role_permission} WHERE rid IN (:fetch)", array(':fetch' => $fetch)); foreach ($result as $row) { $this->roleCache[$row->rid][$row->permission] = TRUE; diff --git a/core/modules/user/lib/Drupal/user/UserAccessInterface.php b/core/modules/user/lib/Drupal/user/UserAccessInterface.php new file mode 100644 index 0000000..7133bb2 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/UserAccessInterface.php @@ -0,0 +1,46 @@ +roles. + * + * @return + * An array indexed by role ID. Each value is an array whose keys are the + * permission strings for the given role ID. + */ + public function userRolePermissions($roles); + +}