-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Switch user during RESTful execution
Allow other code to rely on the global $user object when reacting to a RESTful thread execution.
- Loading branch information
Mateu Aguiló Bosch
committed
Feb 20, 2016
1 parent
7e21136
commit eadbca2
Showing
12 changed files
with
249 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\restful\Authentication\UserSessionState. | ||
*/ | ||
|
||
namespace Drupal\restful\Authentication; | ||
|
||
/** | ||
* Class UserSessionState. | ||
* | ||
* @package Drupal\restful\Authentication | ||
*/ | ||
class UserSessionState implements UserSessionStateInterface { | ||
|
||
/** | ||
* Boolean holding if this is the first switch. | ||
* | ||
* @var bool | ||
*/ | ||
protected static $isSwitched = FALSE; | ||
|
||
/** | ||
* Boolean holding if the session needs to be saved. | ||
* | ||
* @var bool | ||
*/ | ||
protected $needsSaving = FALSE; | ||
|
||
/** | ||
* Object holding the original user. | ||
* | ||
* This is saved for switch back purposes. | ||
* | ||
* @var object | ||
*/ | ||
protected $originalUser; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function isSwitched() { | ||
return static::$isSwitched; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function switchUser($account) { | ||
global $user; | ||
|
||
if (!static::isSwitched() && !$this->originalUser && !$this->needsSaving) { | ||
// This is the first time a user switched, and there isn't an original | ||
// user session. | ||
$this->needsSaving = drupal_save_session(); | ||
$this->originalUser = $user; | ||
|
||
// Don't allow a session to be saved. Provider that require a session to | ||
// be saved, like the cookie provider, need to explicitly set | ||
// drupal_save_session(TRUE). | ||
// @see LoginCookie__1_0::loginUser(). | ||
drupal_save_session(FALSE); | ||
} | ||
|
||
// Set the global user. | ||
$user = $account; | ||
} | ||
|
||
/** | ||
* Switch the user to the authenticated user, and back. | ||
* | ||
* This should be called only for an API call. It should not be used for calls | ||
* via the menu system, as it might be a login request, so we avoid switching | ||
* back to the anonymous user. | ||
*/ | ||
public function switchUserBack() { | ||
global $user; | ||
if (!$this->originalUser) { | ||
return; | ||
} | ||
|
||
$user = $this->originalUser; | ||
drupal_save_session($this->needsSaving); | ||
$this->reset(); | ||
} | ||
|
||
/** | ||
* Reset the initial values. | ||
*/ | ||
protected function reset() { | ||
// Reset initial values. | ||
static::$isSwitched = FALSE; | ||
$this->originalUser = NULL; | ||
$this->needsSaving = FALSE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\restful\Authentication\UserSessionStateInterface. | ||
*/ | ||
|
||
namespace Drupal\restful\Authentication; | ||
|
||
/** | ||
* Class UserSessionStateInterface. | ||
* | ||
* @package Drupal\restful\Authentication | ||
*/ | ||
interface UserSessionStateInterface { | ||
|
||
/** | ||
* Check if the user has already been switched. | ||
* | ||
* We need this information to perform additional actions the first time a | ||
* user is switched. | ||
* | ||
* @return bool | ||
* TRUE if the user has been switched previously. FALSE otherwise. | ||
*/ | ||
public static function isSwitched(); | ||
|
||
/** | ||
* Make the passed in user to be the account for the Drupal thread. | ||
* | ||
* @param object $account | ||
* The account to switch to. | ||
*/ | ||
public function switchUser($account); | ||
|
||
/** | ||
* Switch the user to the authenticated user, and back. | ||
* | ||
* This should be called only for an API call. It should not be used for calls | ||
* via the menu system, as it might be a login request, so we avoid switching | ||
* back to the anonymous user. | ||
*/ | ||
public function switchUserBack(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.