class LoginCookie__1_0 in RESTful 7.2
Class LoginCookie__1_0 @package Drupal\restful\Plugin\resource
Plugin annotation
@Resource(
name = "login_cookie:1.0",
resource = "login_cookie",
label = "Login",
description = "Login a user and return a JSON along with the authentication cookie.",
authenticationTypes = {
"basic_auth"
},
dataProvider = {
"entityType": "user",
"bundles": {
"user"
},
},
menuItem = "login",
majorVersion = 1,
minorVersion = 0
)
Hierarchy
- class \Drupal\restful\Plugin\resource\Resource extends \Drupal\Component\Plugin\PluginBase implements ResourceInterface uses ConfigurablePluginTrait
- class \Drupal\restful\Plugin\resource\ResourceEntity
- class \Drupal\restful\Plugin\resource\LoginCookie__1_0 implements ResourceInterface
- class \Drupal\restful\Plugin\resource\ResourceEntity
Expanded class hierarchy of LoginCookie__1_0
File
- src/
Plugin/ resource/ LoginCookie__1_0.php, line 36 - Contains \Drupal\restful\Plugin\resource\LoginCookie__1_0.
Namespace
Drupal\restful\Plugin\resourceView source
class LoginCookie__1_0 extends ResourceEntity implements ResourceInterface {
/**
* {@inheritdoc}
*/
public function publicFields() {
$public_fields = parent::publicFields();
$public_fields['id']['methods'] = array();
// Just return the hidden ID.
return array(
'id' => $public_fields['id'],
);
}
/**
* Overrides \RestfulBase::controllersInfo().
*/
public function controllersInfo() {
return array(
'' => array(
RequestInterface::METHOD_GET => 'loginAndRespondWithCookie',
),
);
}
/**
* Login a user and return a JSON along with the authentication cookie.
*
* @return array
* Array with the public fields populated.
*/
public function loginAndRespondWithCookie() {
// Login the user.
$account = $this
->getAccount();
$this
->loginUser($account);
$user_resource = restful()
->getResourceManager()
->getPlugin('users:1.0');
// User resource may be disabled.
$output = $user_resource ? $user_resource
->view($account->uid) : array();
if ($resource_field_collection = reset($output)) {
/* @var $resource_field_collection \Drupal\restful\Plugin\resource\Field\ResourceFieldCollectionInterface */
$resource_field_collection
->set('X-CSRF-Token', ResourceField::create(array(
'public_name' => 'X-CSRF-Token',
'callback' => '\\Drupal\\restful\\Plugin\\resource\\LoginCookie__1_0::getCSRFTokenValue',
)));
}
return $output;
}
/**
* Log the user in.
*
* @param object $account
* The user object that was retrieved by the AuthenticationManager.
*/
public function loginUser($account) {
global $user;
$this->authenticationManager
->switchUserBack();
// Explicitly allow a session to be saved, as it was disabled in
// UserSessionState::switchUser. However this resource is a special one, in
// the sense that we want to keep the user authenticated after login.
drupal_save_session(TRUE);
// Override the global user.
$user = user_load($account->uid);
$login_array = array(
'name' => $account->name,
);
user_login_finalize($login_array);
}
/**
* Get the CSRF token string.
*
* @return string
* The token.
*/
public static function getCSRFTokenValue() {
$token = array_values(restful_csrf_session_token());
return reset($token);
}
/**
* {@inheritdoc}
*/
public function switchUserBack() {
// We don't want to switch back in this case!
drupal_save_session(TRUE);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigurablePluginTrait:: |
protected | property | Plugin instance configuration. | |
ConfigurablePluginTrait:: |
public | function | ||
ConfigurablePluginTrait:: |
public | function | ||
ConfigurablePluginTrait:: |
public | function | ||
LoginCookie__1_0:: |
public | function |
Overrides \RestfulBase::controllersInfo(). Overrides Resource:: |
|
LoginCookie__1_0:: |
public static | function | Get the CSRF token string. | |
LoginCookie__1_0:: |
public | function | Login a user and return a JSON along with the authentication cookie. | |
LoginCookie__1_0:: |
public | function | Log the user in. | |
LoginCookie__1_0:: |
public | function |
Public fields. Overrides ResourceEntity:: |
|
LoginCookie__1_0:: |
public | function |
Switches the user back from the original user for the session. Overrides Resource:: |
|
Resource:: |
protected | property | The authentication manager. | |
Resource:: |
protected | property | The data provider. | |
Resource:: |
protected | property | Indicates if the resource is enabled. | |
Resource:: |
protected | property | The field definition object. | |
Resource:: |
protected | property | The requested path. | |
Resource:: |
protected | property | The current request. | |
Resource:: |
public | function |
Determine if user can access the handler. Overrides ResourceInterface:: |
1 |
Resource:: |
protected | function | Checks access based on the referer header and the allowOrigin setting. | |
Resource:: |
public | function |
Basic implementation for create. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Overrides ConfigurablePluginTrait:: |
|
Resource:: |
public | function |
Disable the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Discovery controller callback. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick DELETE request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick GET request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick PATCH request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick POST request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick PUT request. Overrides ResourceInterface:: |
|
Resource:: |
private | function | ||
Resource:: |
public | function |
Enable the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Get the user from for request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Return the controller for a given path. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the controllers for this resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the data provider. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the field definitions. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the path of the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Get the request object. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the resource machine name. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the resource name. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Helper method; Get the URL of the resource and query strings. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Return array keyed with the major and minor version of the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for listing. Overrides ResourceInterface:: |
1 |
Resource:: |
protected | function | Initializes the authentication manager and adds the appropriate providers. | |
Resource:: |
public | function |
Checks if the resource is enabled. Overrides ResourceInterface:: |
|
Resource:: |
protected | function | Adds the Allowed-Origin headers. | |
Resource:: |
public | function |
Controller function that passes the data along and executes right action. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for update. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for update. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the data provider. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the field definitions. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the path of the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the plugin definition to the provided array. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the request object. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for update. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets a resource URL based on the current version. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for view. Overrides ResourceInterface:: |
|
ResourceEntity:: |
protected | property | The entity bundles. | |
ResourceEntity:: |
protected | property | The entity type. | |
ResourceEntity:: |
protected | function |
Data provider class. Overrides Resource:: |
3 |
ResourceEntity:: |
public | function |
Data provider factory. Overrides Resource:: |
|
ResourceEntity:: |
public | function | Gets the entity bundle. | |
ResourceEntity:: |
public | function | Get the "self" url. | |
ResourceEntity:: |
public | function | Gets the entity type. | |
ResourceEntity:: |
protected | function |
Get the public fields with the default values applied to them. Overrides Resource:: |
2 |
ResourceEntity:: |
protected | function | Get the public fields with default values based on view mode information. | |
ResourceEntity:: |
public | function |
Constructs a Drupal\Component\Plugin\PluginBase object. Overrides Resource:: |
2 |
ResourceInterface:: |
constant | The string that separates multiple ids. |