class UserSwitch in User Switch 8
Defines a UserSwitch service to switch user account.
Hierarchy
- class \Drupal\userswitch\UserSwitch uses StringTranslationTrait
Expanded class hierarchy of UserSwitch
1 file declares its use of UserSwitch
- UserSwitchController.php in src/
Controller/ UserSwitchController.php
2 string references to 'UserSwitch'
1 service uses UserSwitch
File
- src/
UserSwitch.php, line 15
Namespace
Drupal\userswitchView source
class UserSwitch {
use StringTranslationTrait;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The session manager.
*
* @var \Drupal\Core\Session\SessionManagerInterface
*/
protected $sessionManager;
/**
* The session manager.
*
* @var Symfony\Component\HttpFoundation\Session\Session
*/
protected $session;
/**
* Constructs.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Session\SessionManagerInterface $session_manager
* The session manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param Symfony\Component\HttpFoundation\Session\Session $session
* The session manager.
*/
public function __construct(AccountInterface $current_user, ModuleHandlerInterface $module_handler, SessionManagerInterface $session_manager, EntityTypeManagerInterface $entityTypeManager, Session $session) {
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
$this->sessionManager = $session_manager;
$this->entityTypeManager = $entityTypeManager;
$this->session = $session;
}
/**
* Check session.
*
* @return bool
* TRUE when user switch account, FALSE otherwise.
*/
public function isSwitchUser() {
return !empty($_SESSION['SwitchCurrentUser']);
}
/**
* Return original user id, FALSE otherwise.
*/
public function getUserId() {
if (isset($_SESSION['SwitchCurrentUser'])) {
return $_SESSION['SwitchCurrentUser'];
}
else {
return FALSE;
}
}
/**
* User account switch.
*/
public function switchToOther($target_account) {
$account = $this->currentUser
->getAccount();
$this->moduleHandler
->invokeAll('user_logout', [
$account,
]);
$this->sessionManager
->regenerate();
$_SESSION['SwitchCurrentUser'] = $account
->id();
$t_account = $this->entityTypeManager
->getStorage('user')
->load($target_account);
$this->currentUser
->setAccount($t_account);
$this->session
->set('uid', $t_account
->id());
$this->moduleHandler
->invokeAll('user_login', [
$t_account,
]);
return TRUE;
}
/**
* Switching back to previous user.
*
* @return bool
* TRUE when switched back previous account.
*/
public function switchUserBack() {
if (empty($_SESSION['SwitchCurrentUser'])) {
return FALSE;
}
$new_user = $this->entityTypeManager
->getStorage('user')
->load($_SESSION['SwitchCurrentUser']);
unset($_SESSION['SwitchCurrentUser']);
if (!$new_user) {
return FALSE;
}
$account = $this->currentUser
->getAccount();
$this->moduleHandler
->invokeAll('user_logout', [
$account,
]);
$this->sessionManager
->regenerate();
$this->currentUser
->setAccount($new_user);
$this->session
->set('uid', $new_user
->id());
$this->moduleHandler
->invokeAll('user_login', [
$new_user,
]);
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UserSwitch:: |
protected | property | The current user. | |
UserSwitch:: |
protected | property | The entity type manager. | |
UserSwitch:: |
protected | property | The module handler. | |
UserSwitch:: |
protected | property | The session manager. | |
UserSwitch:: |
protected | property | The session manager. | |
UserSwitch:: |
public | function | Return original user id, FALSE otherwise. | |
UserSwitch:: |
public | function | Check session. | |
UserSwitch:: |
public | function | User account switch. | |
UserSwitch:: |
public | function | Switching back to previous user. | |
UserSwitch:: |
public | function | Constructs. |