Commit 241348d5 authored by catch's avatar catch
Browse files

Issue #2073531 by tim.plunkett, dawehner, Berdir, catch, plach: Use current...

Issue #2073531 by tim.plunkett, dawehner, Berdir, catch, plach: Use current user service instead of _account, remove _account from the request object.
parent 7b8e2041
......@@ -407,7 +407,7 @@ services:
class: Drupal\Core\Access\CsrfTokenGenerator
arguments: ['@private_key']
calls:
- [setRequest, ['@?request']]
- [setCurrentUser, ['@?current_user']]
access_manager:
class: Drupal\Core\Access\AccessManager
arguments: ['@router.route_provider', '@url_generator', '@paramconverter_manager']
......
......@@ -9,7 +9,7 @@
use Drupal\Component\Utility\Crypt;
use Drupal\Core\PrivateKey;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Session\AccountInterface;
/**
* Generates and validates CSRF tokens.
......@@ -26,11 +26,11 @@ class CsrfTokenGenerator {
protected $privateKey;
/**
* The current request object.
* The current user.
*
* @var \Symfony\Component\HttpFoundation\Request
* @var \Drupal\Core\Session\AccountInterface
*/
protected $request;
protected $currentUser;
/**
* Constructs the token generator.
......@@ -43,13 +43,13 @@ public function __construct(PrivateKey $private_key) {
}
/**
* Sets the $request property.
* Sets the current user.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The HttpRequest object representing the current request.
* @param \Drupal\Core\Session\AccountInterface|null $current_user
* The current user service.
*/
public function setRequest(Request $request) {
$this->request = $request;
public function setCurrentUser(AccountInterface $current_user = NULL) {
$this->currentUser = $current_user;
}
/**
......@@ -84,9 +84,7 @@ public function get($value = '') {
* is TRUE, the return value will always be TRUE for anonymous users.
*/
public function validate($token, $value = '', $skip_anonymous = FALSE) {
$user = $this->request->attributes->get('_account');
return ($skip_anonymous && $user->isAnonymous()) || ($token == $this->get($value));
return ($skip_anonymous && $this->currentUser->isAnonymous()) || ($token == $this->get($value));
}
}
......@@ -110,7 +110,6 @@ public function authenticate(Request $request) {
// Save the authenticated account and the provider that supplied it
// for later access.
$request->attributes->set('_account', $account);
$request->attributes->set('_authentication_provider', $this->triggeredProviderId);
// The global $user object is included for backward compatibility only and
......
......@@ -23,7 +23,6 @@ class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
*/
protected function alterRoutes(RouteCollection $collection, $module) {
$special_variables = array(
'_account',
'system_path',
'_maintenance',
'_legacy',
......
......@@ -178,7 +178,7 @@ public function setRequest(Request $request) {
* The current user.
*/
protected function currentUser() {
return $this->getRequest()->attributes->get('_account');
return \Drupal::currentUser();
}
/**
......
......@@ -55,8 +55,6 @@ public function enhance(array $defaults, Request $request) {
$anonymous_user = drupal_anonymous_user();
$this->container->set('current_user', $anonymous_user, 'request');
// @todo Remove this in https://drupal.org/node/2073531
$request->attributes->set('_account', $anonymous_user);
// The global $user object is included for backward compatibility only
// and should be considered deprecated.
......
......@@ -1175,9 +1175,7 @@ function comment_load($cid, $reset = FALSE) {
* The number of new comments or FALSE if the user is not logged in.
*/
function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) {
global $user;
if ($user->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) {
if (\Drupal::currentUser()->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) {
// Retrieve the timestamp at which the current user last viewed this entity.
if (!$timestamp) {
if ($entity_type == 'node') {
......
......@@ -46,6 +46,9 @@ function setUp() {
'node test view',
'skip comment approval',
));
// Set the author of the created node to the web_user uid.
$this->node->setAuthorId($this->web_user->id())->save();
}
/**
......
......@@ -155,7 +155,7 @@ protected function assertPublishedStatus() {
* Tests translate link on comment content admin page.
*/
function testTranslateLinkCommentAdminPage() {
$this->admin_user = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer comments')));
$this->admin_user = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer comments', 'skip comment approval')));
$this->drupalLogin($this->admin_user);
$cid_translatable = $this->createEntity(array(), $this->langcodes[0]);
......
......@@ -36,7 +36,7 @@ function setUp() {
// Add two users, create a node with the user1 as author and another node
// with user2 as author. For the second node add a comment from user1.
$this->account = $this->drupalCreateUser();
$this->account = $this->drupalCreateUser(array('skip comment approval'));
$this->account2 = $this->drupalCreateUser();
$this->drupalLogin($this->account);
......
......@@ -234,7 +234,14 @@ public function entityFormAlter(array &$form, array &$form_state, EntityInterfac
);
}
$name = $new_translation ? $GLOBALS['user']->getUsername() : user_load($entity->translation[$form_langcode]['uid'])->getUsername();
// Default to the anonymous user.
$name = '';
if ($new_translation) {
$name = $GLOBALS['user']->getUsername();
}
elseif ($entity->translation[$form_langcode]['uid']) {
$name = user_load($entity->translation[$form_langcode]['uid'])->getUsername();
}
$form['content_translation']['name'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
......
......@@ -119,8 +119,7 @@ public function testNodeHandler() {
// Test as a non-admin.
$normal_user = $this->drupalCreateUser(array('access content'));
$request = $this->container->get('request');
$request->attributes->set('_account', $normal_user);
$this->container->set('current_user', $normal_user);
$referenceable_tests = array(
array(
'arguments' => array(
......@@ -172,7 +171,7 @@ public function testNodeHandler() {
// Test as an admin.
$admin_user = $this->drupalCreateUser(array('access content', 'bypass node access'));
$request->attributes->set('_account', $admin_user);
$this->container->set('current_user', $admin_user);
$referenceable_tests = array(
array(
'arguments' => array(
......@@ -266,8 +265,7 @@ public function testUserHandler() {
}
// Test as a non-admin.
$request = $this->container->get('request');
$request->attributes->set('_account', $users['non_admin']);
$this->container->set('current_user', $users['non_admin']);
$referenceable_tests = array(
array(
'arguments' => array(
......@@ -306,7 +304,7 @@ public function testUserHandler() {
);
$this->assertReferenceable($instance, $referenceable_tests, 'User handler');
$request->attributes->set('_account', $users['admin']);
$this->container->set('current_user', $users['admin']);
$referenceable_tests = array(
array(
'arguments' => array(
......@@ -448,8 +446,7 @@ public function testCommentHandler() {
// Test as a non-admin.
$normal_user = $this->drupalCreateUser(array('access content', 'access comments'));
$request = $this->container->get('request');
$request->attributes->set('_account', $normal_user);
$this->container->set('current_user', $normal_user);
$referenceable_tests = array(
array(
'arguments' => array(
......@@ -488,7 +485,7 @@ public function testCommentHandler() {
// Test as a comment admin.
$admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments'));
$request->attributes->set('_account', $admin_user);
$this->container->set('current_user', $admin_user);
$referenceable_tests = array(
array(
'arguments' => array(
......@@ -506,7 +503,7 @@ public function testCommentHandler() {
// Test as a node and comment admin.
$admin_user = $this->drupalCreateUser(array('access content', 'access comments', 'administer comments', 'bypass node access'));
$request->attributes->set('_account', $admin_user);
$this->container->set('current_user', $admin_user);
$referenceable_tests = array(
array(
'arguments' => array(
......
......@@ -303,9 +303,8 @@ function filter_get_formats_by_role($rid) {
* @see filter_fallback_format()
*/
function filter_default_format(AccountInterface $account = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
$account = \Drupal::currentUser();
}
// Get a list of formats for this user, ordered by weight. The first one
// available is the user's default format.
......
......@@ -62,6 +62,9 @@ public function testForumIntegration() {
$nodes[] = $node;
}
$account = $this->drupalCreateUser(array('skip comment approval'));
$this->drupalLogin($account);
$comments = array();
foreach ($nodes as $index => $node) {
for ($i = 0; $i <= $index; $i++) {
......
......@@ -55,6 +55,7 @@ public function testCommentPager() {
'comment_body' => array(
array('value' => $this->randomName()),
),
'status' => COMMENT_PUBLISHED,
));
$comment->save();
}
......
......@@ -1735,11 +1735,9 @@ function node_access_view_all_nodes($account = NULL) {
* @endcode
*/
function node_query_node_access_alter(AlterableInterface $query) {
global $user;
// Read meta-data from query, if provided.
if (!$account = $query->getMetaData('account')) {
$account = $user;
$account = \Drupal::currentUser();
}
if (!$op = $query->getMetaData('op')) {
$op = 'view';
......
......@@ -23,9 +23,7 @@ interface SearchInterface extends PluginInspectionInterface {
* Array of parameters as am associative array. This is expected to
* be the query string from the current request.
* @param array $attributes
* Array of attributes, usually from the current request object. The search
* plugin may use the '_account' attribute if present to personalize the
* search, or use attributes from the current route variables.
* Array of attributes, usually from the current request object.
*
* @return \Drupal\search\Plugin\SearchInterface
* A search plugin object for chaining.
......
......@@ -46,6 +46,9 @@ function setUp() {
// Create searching user.
$this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
// Login with sufficient privileges.
$this->drupalLogin($this->searching_user);
// Add a comment field.
$this->container->get('comment.manager')->addDefaultField('node', 'article');
// Create initial nodes.
......@@ -54,9 +57,6 @@ function setUp() {
$this->searchable_nodes['1 comment'] = $this->drupalCreateNode($node_params);
$this->searchable_nodes['0 comments'] = $this->drupalCreateNode($node_params);
// Login with sufficient privileges.
$this->drupalLogin($this->searching_user);
// Create a comment array
$edit_comment = array();
$edit_comment['subject'] = $this->randomName();
......
......@@ -1041,7 +1041,7 @@ protected function rebuildContainer() {
// different object, so we need to replace the instance on this test class.
$this->container = \Drupal::getContainer();
// The global $user is set in TestBase::prepareEnvironment().
$this->container->get('request')->attributes->set('_account', $GLOBALS['user']);
$this->container->set('current_user', $GLOBALS['user']);
}
/**
......
......@@ -277,7 +277,7 @@ protected function drupalCreateNode(array $settings = array()) {
$settings['uid'] = $this->loggedInUser->id();
}
else {
global $user;
$user = \Drupal::currentUser() ?: $GLOBALS['user'];
$settings['uid'] = $user->id();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment