diff --git a/lib/Drupal/masquerade/Plugin/Block/MasqueradeBlock.php b/lib/Drupal/masquerade/Plugin/Block/MasqueradeBlock.php
index 776f873..b243609 100644
--- a/lib/Drupal/masquerade/Plugin/Block/MasqueradeBlock.php
+++ b/lib/Drupal/masquerade/Plugin/Block/MasqueradeBlock.php
@@ -8,16 +8,15 @@
namespace Drupal\masquerade\Plugin\Block;
use Drupal\block\BlockBase;
-use Drupal\Component\Annotation\Plugin;
+use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
/**
* Provides a 'Masquerade' block.
*
- * @Plugin(
+ * @Block(
* id = "masquerade",
* admin_label = @Translation("Masquerade"),
- * module = "masquerade",
* cache = false
* )
*/
diff --git a/lib/Drupal/masquerade/Tests/MasqueradeAccessTest.php b/lib/Drupal/masquerade/Tests/MasqueradeAccessTest.php
index 0195f7c..4faef2d 100644
--- a/lib/Drupal/masquerade/Tests/MasqueradeAccessTest.php
+++ b/lib/Drupal/masquerade/Tests/MasqueradeAccessTest.php
@@ -7,6 +7,9 @@
namespace Drupal\masquerade\Tests;
+use Drupal\user\UserInterface;
+use Drupal\Core\Session\UserSession;
+
/**
* Tests masquerade access mechanism.
*
@@ -35,8 +38,12 @@ class MasqueradeAccessTest extends MasqueradeWebTestBase {
'label' => 'Administrator',
));
$this->admin_role->save();
- config('user.settings')->set('admin_role', $this->admin_role->id())->save();
- user_role_grant_permissions($this->admin_role->id(), array_keys(module_invoke_all('permission')));
+ $this->container->get('config.factory')
+ ->get('user.settings')
+ ->set('admin_role', $this->admin_role->id())
+ ->save();
+ $permissions = $this->container->get('module_handler')->invokeAll('permission');
+ user_role_grant_permissions($this->admin_role->id(), array_keys($permissions));
// Create an additional 'moderator' role with some typical permissions.
$this->moderator_role = entity_create('user_role', array(
@@ -69,28 +76,28 @@ class MasqueradeAccessTest extends MasqueradeWebTestBase {
// Administrative user with User module's admin role *only*.
$this->admin_user = $this->drupalCreateUser();
- $this->admin_user->name = 'admin_user';
- $this->admin_user->roles[$this->admin_role->id()] = $this->admin_role->id();
+ $this->admin_user->setUsername('admin_user');
+ $this->admin_user->addRole($this->admin_role->id());
$this->admin_user->save();
// Moderator user.
$this->moderator_user = $this->drupalCreateUser();
- $this->moderator_user->name = 'moderator_user';
- $this->moderator_user->roles[$this->moderator_role->id()] = $this->moderator_role->id();
- $this->moderator_user->roles[$this->masquerade_role->id()] = $this->masquerade_role->id();
+ $this->moderator_user->setUsername('moderator_user');
+ $this->moderator_user->addRole($this->moderator_role->id());
+ $this->moderator_user->addRole($this->masquerade_role->id());
$this->moderator_user->save();
// Editor user.
$this->editor_user = $this->drupalCreateUser();
- $this->editor_user->name = 'editor_user';
- $this->editor_user->roles[$this->editor_role->id()] = $this->editor_role->id();
- $this->editor_user->roles[$this->masquerade_role->id()] = $this->masquerade_role->id();
+ $this->editor_user->setUsername('editor_user');
+ $this->editor_user->addRole($this->editor_role->id());
+ $this->editor_user->addRole($this->masquerade_role->id());
$this->editor_user->save();
// Masquerade user.
$this->masquerade_user = $this->drupalCreateUser();
- $this->masquerade_user->name = 'masquerade_user';
- $this->masquerade_user->roles[$this->masquerade_role->id()] = $this->masquerade_role->id();
+ $this->masquerade_user->setUsername('masquerade_user');
+ $this->masquerade_user->addRole($this->masquerade_role->id());
$this->masquerade_user->save();
// Authenticated user.
@@ -144,7 +151,7 @@ class MasqueradeAccessTest extends MasqueradeWebTestBase {
// Verify that a user cannot masquerade as himself.
$edit = array(
- 'masquerade_as' => $this->masquerade_user->name,
+ 'masquerade_as' => $this->masquerade_user->getUsername(),
);
$this->drupalPost('masquerade', $edit, t('Switch'));
$this->assertRaw(t('You cannot masquerade as yourself. Please choose a different user to masquerade as.'));
@@ -158,28 +165,34 @@ class MasqueradeAccessTest extends MasqueradeWebTestBase {
/**
* Asserts that the currently logged-in user can masquerade as a given target user.
+ *
+ * @param \Drupal\user\UserInterface|\Drupal\Core\Session\UserSession $target_account
+ * The user to masquerade or session of root_user.
*/
protected function assertCanMasqueradeAs($target_account) {
$edit = array(
- 'masquerade_as' => $target_account->name,
+ 'masquerade_as' => $target_account->getUsername(),
);
$this->drupalPost('masquerade', $edit, t('Switch'));
$this->assertNoRaw(t('You are not allowed to masquerade as %name.', array(
- '%name' => $target_account->name,
+ '%name' => $target_account->getUsername(),
)));
$this->clickLink(t('Unmasquerade'));
}
/**
* Asserts that the currently logged-in user can not masquerade as a given target user.
+ *
+ * @param \Drupal\user\UserInterface|\Drupal\Core\Session\UserSession $account
+ * The user to masquerade or session of root_user.
*/
protected function assertCanNotMasqueradeAs($target_account) {
$edit = array(
- 'masquerade_as' => $target_account->name,
+ 'masquerade_as' => $target_account->getUsername(),
);
$this->drupalPost('masquerade', $edit, t('Switch'));
$this->assertRaw(t('You are not allowed to masquerade as %name.', array(
- '%name' => $target_account->name,
+ '%name' => $target_account->getUsername(),
)));
$this->assertNoText(t('Unmasquerade'));
}
diff --git a/lib/Drupal/masquerade/Tests/MasqueradeTest.php b/lib/Drupal/masquerade/Tests/MasqueradeTest.php
index 0e3999d..a992ebb 100644
--- a/lib/Drupal/masquerade/Tests/MasqueradeTest.php
+++ b/lib/Drupal/masquerade/Tests/MasqueradeTest.php
@@ -29,31 +29,31 @@ class MasqueradeTest extends MasqueradeWebTestBase {
// Verify that a token is required.
$this->drupalGet('user/0/masquerade');
$this->assertResponse(403);
- $this->drupalGet('user/' . $this->web_user->uid . '/masquerade');
+ $this->drupalGet('user/' . $this->web_user->id() . '/masquerade');
$this->assertResponse(403);
// Verify that the admin user is able to masquerade.
- $this->assertSessionByUid($this->admin_user->uid, FALSE);
+ $this->assertSessionByUid($this->admin_user->id(), FALSE);
$this->masqueradeAs($this->web_user);
- $this->assertSessionByUid($this->web_user->uid, $this->admin_user->uid);
- $this->assertNoSessionByUid($this->admin_user->uid);
+ $this->assertSessionByUid($this->web_user->id(), $this->admin_user->id());
+ $this->assertNoSessionByUid($this->admin_user->id());
// Verify that a token is required to unmasquerade.
$this->drupalGet('unmasquerade');
$this->assertResponse(403);
// Verify that the web user cannot masquerade.
- $this->drupalGet('user/' . $this->admin_user->uid . '/masquerade', array(
+ $this->drupalGet('user/' . $this->admin_user->id() . '/masquerade', array(
'query' => array(
- 'token' => $this->drupalGetToken('user/' . $this->admin_user->uid . '/masquerade'),
+ 'token' => $this->drupalGetToken('user/' . $this->admin_user->id() . '/masquerade'),
),
));
$this->assertResponse(403);
// Verify that the user can unmasquerade.
$this->unmasquerade($this->web_user);
- $this->assertNoSessionByUid($this->web_user->uid);
- $this->assertSessionByUid($this->admin_user->uid, FALSE);
+ $this->assertNoSessionByUid($this->web_user->id());
+ $this->assertSessionByUid($this->admin_user->id(), FALSE);
}
}
diff --git a/lib/Drupal/masquerade/Tests/MasqueradeWebTestBase.php b/lib/Drupal/masquerade/Tests/MasqueradeWebTestBase.php
index 70525b0..56a7e0b 100644
--- a/lib/Drupal/masquerade/Tests/MasqueradeWebTestBase.php
+++ b/lib/Drupal/masquerade/Tests/MasqueradeWebTestBase.php
@@ -7,8 +7,9 @@
namespace Drupal\masquerade\Tests;
+use Drupal\Component\Utility\Crypt;
use Drupal\simpletest\WebTestBase;
-use Drupal\user\Plugin\Core\Entity\User;
+use Drupal\user\UserInterface;
/**
* Base test class for Masquerade module web tests.
@@ -24,13 +25,13 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
/**
* Masquerades as another user.
*
- * @param \Drupal\user\Plugin\Core\Entity\User $account
+ * @param \Drupal\user\UserInterface $account
* The user account to masquerade as.
*/
- protected function masqueradeAs(User $account) {
- $this->drupalGet('user/' . $account->uid . '/masquerade', array(
+ protected function masqueradeAs(UserInterface $account) {
+ $this->drupalGet('user/' . $account->id() . '/masquerade', array(
'query' => array(
- 'token' => $this->drupalGetToken('user/' . $account->uid . '/masquerade'),
+ 'token' => $this->drupalGetToken('user/' . $account->id() . '/masquerade'),
),
));
$this->assertResponse(200);
@@ -47,10 +48,10 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
/**
* Unmasquerades the current user.
*
- * @param \Drupal\user\Plugin\Core\Entity\User $account
+ * @param \Drupal\user\UserInterface $account
* The user account to unmasquerade from.
*/
- protected function unmasquerade(User $account) {
+ protected function unmasquerade(UserInterface $account) {
$this->drupalGet('unmasquerade', array(
'query' => array(
'token' => $this->drupalGetToken('unmasquerade'),
@@ -154,11 +155,11 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
* @see http://drupal.org/node/1555862
*/
protected function drupalGetToken($value = '') {
- $private_key = drupal_get_private_key();
+ $private_key = $this->container->get('private_key')->get();
// Use the session_id assigned by WebTestBase::drupalLogin() instead of
// $this->session_id until the core bug is fixed.
$session_id = isset($this->loggedInUser->session_id) ? $this->loggedInUser->session_id : '';
- return drupal_hmac_base64($value, $session_id . $private_key . drupal_get_hash_salt());
+ return Crypt::hmacBase64($value, $session_id . $private_key . drupal_get_hash_salt());
}
}
diff --git a/masquerade.api.php b/masquerade.api.php
index 53850ef..998f301 100644
--- a/masquerade.api.php
+++ b/masquerade.api.php
@@ -16,9 +16,9 @@
* Modules may implement this hook to control whether a user is allowed to
* masquerade as a certain target user account.
*
- * @param stdClass $user
+ * @param \Drupal\user\UserInterface $user
* The currently logged-in user.
- * @param Drupal\user\Plugin\Core\Entity\User $target_account
+ * @param \Drupal\user\UserInterface $target_account
* The target user account to check for masquerade access.
*
* @return bool
@@ -30,7 +30,7 @@
* - NULL or nothing to not affect the operation. If no module explicitly
* grants access, access is denied.
*/
-function hook_masquerade_access($user, \Drupal\user\Plugin\Core\Entity\User $target_account) {
+function hook_masquerade_access(\Drupal\user\UserInterface $user, \Drupal\user\UserInterface $target_account) {
// Explicitly deny access for uid 1.
if ($target_account->id() == 1) {
return FALSE;
diff --git a/masquerade.module b/masquerade.module
index 213d928..7f0c2ec 100644
--- a/masquerade.module
+++ b/masquerade.module
@@ -5,8 +5,8 @@
* Allows privileged users to masquerade as another user.
*/
-use Drupal\user\Plugin\Core\Entity\User;
-use Symfony\Component\HttpFoundation\JsonResponse;
+use Drupal\user\UserInterface;
+use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -117,7 +117,7 @@ function masquerade_translated_menu_link_alter(&$item, $map) {
/**
* Returns whether the current user is allowed to masquerade as a certain target user.
*
- * @param \Drupal\user\Plugin\Core\Entity\User $target_account
+ * @param \Drupal\user\UserInterface $target_account
* The user account object to masquerade as.
*
* @return bool
@@ -125,12 +125,12 @@ function masquerade_translated_menu_link_alter(&$item, $map) {
*
* @see hook_masquerade_access()
*/
-function masquerade_user_access(User $target_account) {
- global $user;
+function masquerade_user_access(UserInterface $target_account) {
+ $user = Drupal::currentUser();
// Deny access if the current user is masquerading already or tries to
// masquerade as himself.
- if (isset($_SESSION['masquerading']) || $user->uid == $target_account->id()) {
+ if (isset($_SESSION['masquerading']) || $user->id() == $target_account->id()) {
return FALSE;
}
@@ -162,9 +162,9 @@ function masquerade_user_access(User $target_account) {
* This default implementation only returns TRUE and never FALSE, since
* alternative access implementations could not work otherwise.
*/
-function masquerade_masquerade_access($user, User $target_account) {
+function masquerade_masquerade_access($user, UserInterface $target_account) {
// Uid 1 may masquerade as anyone.
- if ($user->uid == 1) {
+ if ($user->id() == 1) {
return TRUE;
}
// No one can masquerade as uid 1.
@@ -180,11 +180,13 @@ function masquerade_masquerade_access($user, User $target_account) {
// additional roles), access is granted.
// Note: array_diff*() returns all values from the first array that are NOT
// contained in the second.
- $missing_roles = array_diff_assoc($target_account->roles, $user->roles);
+ $target_account_roles = $target_account->getRoles();
+ $user_roles = $user->getRoles();
+ $missing_roles = array_diff_assoc($target_account_roles, $user_roles);
if (!$missing_roles) {
return TRUE;
}
- $additional_roles = array_diff_assoc($user->roles, $target_account->roles);
+ $additional_roles = array_diff_assoc($user_roles, $target_account_roles);
// If the current user has the identical permissions as the target user (or
// additional permissions), access is granted.
@@ -229,9 +231,7 @@ function masquerade_field_extra_fields() {
/**
* Implements hook_user_view().
*/
-function masquerade_user_view(User $account, $display, $view_mode, $langcode) {
- global $user;
-
+function masquerade_user_view(UserInterface $account, $display, $view_mode, $langcode) {
if (masquerade_user_access($account)) {
$path = 'user/' . $account->id() . '/masquerade';
$account->content['masquerade'] = array(
@@ -306,10 +306,12 @@ function masquerade_block_form_submit($form, &$form_state) {
/**
* Page callback; Masquerades as a given user.
*
- * @param \Drupal\user\Plugin\Core\Entity\User $target_account
+ * @param \Drupal\user\UserInterface $target_account
* The user account object to masquerade as.
+ *
+ * @throws Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
-function masquerade_switch_user_page(User $target_account) {
+function masquerade_switch_user_page(UserInterface $target_account) {
$token = Drupal::service('request')->query->get('token');
if (isset($token) && drupal_valid_token($token, 'user/' . $target_account->id() . '/masquerade')) {
$error = masquerade_switch_user_validate($target_account);
@@ -319,7 +321,7 @@ function masquerade_switch_user_page(User $target_account) {
else {
drupal_set_message($error, 'error');
}
- drupal_goto(Drupal::request()->server->get('HTTP_REFERER'));
+ return new RedirectResponse(Drupal::request()->server->get('HTTP_REFERER'));
}
else {
throw new AccessDeniedHttpException();
@@ -332,15 +334,15 @@ function masquerade_switch_user_page(User $target_account) {
* Use this function to generate user-friendly error messages to show in the
* user interface.
*
- * @param \Drupal\user\Plugin\Core\Entity\User $target_account
+ * @param \Drupal\user\UserInterface $target_account
* The user account object to masquerade as.
*
* @return string|null
* A string containing a validation error message, or NULL if the current user
* can masquerade as $target_account.
*/
-function masquerade_switch_user_validate(User $target_account) {
- global $user;
+function masquerade_switch_user_validate(UserInterface $target_account) {
+ $user = Drupal::currentUser();
if (masquerade_user_is_masquerading()) {
return t('You are masquerading already. Please switch back to your account to masquerade as another user.', array(
@@ -349,10 +351,10 @@ function masquerade_switch_user_validate(User $target_account) {
)),
));
}
- if ($target_account->uid == $user->uid) {
+ if ($target_account->id() == $user->id()) {
return t('You cannot masquerade as yourself. Please choose a different user to masquerade as.');
}
- if (config('system.maintenance')->get('enabled') && !user_access('access site in maintenance mode', $target_account)) {
+ if (Drupal::config('system.maintenance')->get('enabled') && !user_access('access site in maintenance mode', $target_account)) {
return t('!user is not permitted to %permission. Disable maintenance mode to masquerade as !user.', array(
'!user' => theme('username', array('account' => $target_account)),
'%permission' => t('Use the site in maintenance mode'),
@@ -372,17 +374,17 @@ function masquerade_switch_user_validate(User $target_account) {
* Access to masquerade as the target user account has to checked by all callers
* via masquerade_user_access() already.
*
- * @param \Drupal\user\Plugin\Core\Entity\User $target_account
+ * @param \Drupal\user\UserInterface $target_account
* The user account object to masquerade as.
*/
-function masquerade_switch_user(User $target_account) {
- global $user;
+function masquerade_switch_user(UserInterface $target_account) {
+ $user = Drupal::currentUser();
// Call logout hooks when switching from original user.
Drupal::moduleHandler()->invokeAll('user_logout', array($user));
drupal_session_regenerate();
- $_SESSION['masquerading'] = $user->uid;
+ $_SESSION['masquerading'] = $user->id();
watchdog('masquerade', 'User %username masqueraded as %target_username.', array(
// Use "name" here because $user object could not be fully initialized.
@@ -394,7 +396,7 @@ function masquerade_switch_user(User $target_account) {
)));
$user = $target_account;
- $user->masquerading = $target_account->uid;
+ $user->masquerading = $target_account->id();
// Call all login hooks when switching to masquerading user.
Drupal::moduleHandler()->invokeAll('user_login', array($user));
@@ -402,9 +404,11 @@ function masquerade_switch_user(User $target_account) {
/**
* Allows a user who is currently masquerading to become a new user.
+ *
+ * @throws Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
function masquerade_switch_back_page() {
- global $user;
+ $user = Drupal::currentUser();
$token = Drupal::request()->query->get('token');
if (isset($token) && drupal_valid_token($token, 'unmasquerade') && masquerade_user_is_masquerading()) {
@@ -414,7 +418,7 @@ function masquerade_switch_back_page() {
drupal_set_message(t('You are no longer masquerading as !user.', array(
'!user' => theme('username', array('account' => $old_user)),
)));
- drupal_goto(Drupal::request()->server->get('HTTP_REFERER'));
+ return new RedirectResponse(Drupal::request()->server->get('HTTP_REFERER'));
}
else {
throw new AccessDeniedHttpException();
@@ -425,7 +429,7 @@ function masquerade_switch_back_page() {
* Function for a masquerading user to switch back to the previous user.
*/
function masquerade_switch_back() {
- global $user;
+ $user = Drupal::currentUser();
$uid = $_SESSION['masquerading'];
@@ -464,7 +468,7 @@ function masquerade_form_user_admin_account_alter(&$form, &$form_state) {
// @todo Core: The already loaded accounts are not provided.
$account = user_load($uid);
if (masquerade_user_access($account)) {
- $path = 'user/' . $account->uid . '/masquerade';
+ $path = 'user/' . $account->id() . '/masquerade';
$row['operations']['data']['#links']['masquerade'] = array(
'title' => t('Masquerade'),
'href' => $path,
diff --git a/masquerade.routing.yml b/masquerade.routing.yml
index 5721a37..2bf411b 100644
--- a/masquerade.routing.yml
+++ b/masquerade.routing.yml
@@ -4,6 +4,6 @@
masquerade_autocomplete:
pattern: '/masquerade/autocomplete'
defaults:
- _controller: '\Drupal\user\UserAutocompleteController::autocompleteUser'
+ _controller: '\Drupal\user\Controller\UserAutocompleteController::autocompleteUser'
requirements:
_permission: 'masquerade'