Commit 45a9e859 authored by alexpott's avatar alexpott

Issue #2026347 by Berdir: Adding methods to UserInterface/AccountInterface.

parent 79e0e8da
......@@ -2772,6 +2772,13 @@ function template_preprocess_page(&$variables) {
if ($suggestions = theme_get_suggestions(arg(), 'page')) {
$variables['theme_hook_suggestions'] = $suggestions;
}
// Prepare render array for messages. drupal_get_messages() is called later,
// when this variable is rendered in a theme function or template file.
$variables['messages'] = array(
'#theme' => 'status_messages',
'#access' => $variables['show_messages'],
);
}
/**
......@@ -2797,11 +2804,6 @@ function template_process_page(&$variables) {
$variables['title'] = drupal_get_title();
}
// Generate messages last in order to capture as many as possible for the
// current page.
if (!isset($variables['messages'])) {
$variables['messages'] = $variables['show_messages'] ? array('#theme' => 'status_messages') : '';
}
}
/**
......
......@@ -55,4 +55,63 @@ public function getSecureSessionId();
*/
public function getSessionData();
/**
* Returns TRUE if the account is authenticated.
*
* @return bool
* TRUE if the account is authenticated.
*/
public function isAuthenticated();
/**
* Returns TRUE if the account is anonymous.
*
* @return bool
* TRUE if the account is anonymous.
*/
public function isAnonymous();
/**
* Returns the preferred language code of the account.
*
* @param string $default
* (optional) Default language code to return if the account
* has no valid language, defaults to the site default language.
*
* @return string
* The language code that is preferred by the account.
*/
public function getPreferredLangcode($default = NULL);
/**
* Returns the preferred administrative language code of the account.
*
* Defines which language is used on administrative pages.
*
* @param string $default
* (optional) Default language code to return if the account
* has no valid language, defaults to the site default language.
*
* @return string
* The language code that is preferred by the account.
*/
public function getPreferredAdminLangcode($default = NULL);
/**
* Returns the username of this account.
*
* By default, the passed-in object's 'name' property is used if it exists, or
* else, the site-defined value for the 'anonymous' variable. However, a module
* may override this by implementing
* hook_user_format_name_alter(&$name, $account).
*
* @see hook_user_format_name_alter()
*
* @return
* An unsanitized string with the username to display. The code receiving
* this result must ensure that check_plain() is called on it before it is
* printed to the page.
*/
public function getUsername();
}
......@@ -65,6 +65,27 @@ class UserSession implements AccountInterface {
*/
public $timestamp;
/**
* The name of this account.
*
* @var string
*/
public $name;
/**
* The preferred language code of the account.
*
* @var string
*/
protected $preferred_langcode;
/**
* The preferred administrative language code of the account.
*
* @var string
*/
protected $preferred_admin_langcode;
/**
* Constructs a new user session.
*
......@@ -112,4 +133,53 @@ public function getSessionId() {
return $this->sid;
}
/**
* {@inheritdoc}
*/
public function isAuthenticated() {
return $this->uid > 0;
}
/**
* {@inheritdoc}
*/
public function isAnonymous() {
return $this->uid == 0;
}
/**
* {@inheritdoc}
*/
function getPreferredLangcode($default = NULL) {
$language_list = language_list();
if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
return $language_list[$this->preferred_langcode]->id;
}
else {
return $default ? $default : language_default()->id;
}
}
/**
* {@inheritdoc}
*/
function getPreferredAdminLangcode($default = NULL) {
$language_list = language_list();
if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
return $language_list[$this->preferred_admin_langcode]->id;
}
else {
return $default ? $default : language_default()->id;
}
}
/**
* {@inheritdoc}
*/
public function getUsername() {
$name = $this->name ?: \Drupal::config('user.settings')->get('anonymous');
\Drupal::moduleHandler()->alter('user_format_name', $name, $this);
return $name;
}
}
......@@ -86,7 +86,7 @@ public function execute($entity = NULL) {
$recipient_accounts = $this->storageController->loadByProperties(array('mail' => $recipient));
$recipient_account = reset($recipient_accounts);
if ($recipient_account) {
$langcode = user_preferred_langcode($recipient_account);
$langcode = $recipient_account->getPreferredLangcode();
}
else {
$langcode = language_default()->id;
......
......@@ -164,7 +164,7 @@ public function save(array $form, array &$form_state) {
elseif ($recipient = $message->getPersonalRecipient()) {
// Send to the user in the user's preferred language.
$to = $recipient->mail->value;
$recipient_langcode = user_preferred_langcode($recipient);
$recipient_langcode = $recipient->getPreferredLangcode();
$params['recipient'] = $recipient->getBCEntity();
}
else {
......
......@@ -99,7 +99,7 @@ function dblog_event($id) {
}
$username = array(
'#theme' => 'username',
'#account' => $dblog,
'#account' => user_load($dblog->uid),
);
$rows = array(
array(
......
......@@ -124,7 +124,6 @@ public function overview() {
$query = $this->database->select('watchdog', 'w')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('Drupal\Core\Database\Query\TableSortExtender');
$query->leftJoin('users', 'u', 'w.uid = u.uid');
$query->fields('w', array(
'wid',
'uid',
......@@ -134,8 +133,7 @@ public function overview() {
'message',
'variables',
'link',
));
$query->addField('u', 'name');
));
if (!empty($filter['where'])) {
$query->where($filter['where'], $filter['args']);
......@@ -164,7 +162,7 @@ public function overview() {
}
$username = array(
'#theme' => 'username',
'#account' => $dblog,
'#account' => user_load($dblog->uid),
);
$rows[] = array(
'data' => array(
......
......@@ -1211,7 +1211,7 @@ function template_preprocess_forum_icon(&$variables) {
* - topic: The topic object.
*/
function template_preprocess_forum_submitted(&$variables) {
$variables['author'] = isset($variables['topic']->uid) ? theme('username', array('account' => $variables['topic'])) : '';
$variables['author'] = isset($variables['topic']->uid) ? theme('username', array('account' => user_load($variables['topic']->uid))) : '';
$variables['time'] = isset($variables['topic']->created) ? format_interval(REQUEST_TIME - $variables['topic']->created) : '';
}
......
......@@ -463,8 +463,9 @@ function language_get_default_langcode($entity_type, $bundle) {
case 'authors_default':
global $user;
if (!empty($user->preferred_langcode)) {
$default_value = $user->preferred_langcode;
$language_code = $user->getPreferredLangcode();
if (!empty($language_code)) {
$default_value = $language_code;
}
else {
$default_value = $language_interface->id;
......
......@@ -191,8 +191,12 @@ function language_from_user($languages) {
// User preference (only for authenticated users).
global $user;
if ($user->uid && !empty($user->preferred_langcode) && isset($languages[$user->preferred_langcode])) {
return $user->preferred_langcode;
if ($user->id()) {
$langcode = $user->getPreferredLangcode();
$default_langcode = language_default()->id;
if (!empty($langcode) && $langcode != $default_langcode && isset($languages[$langcode])) {
return $langcode;
}
}
// No language preference from the user.
......@@ -216,9 +220,13 @@ function language_from_user_admin(array $languages, Request $request = NULL) {
// User preference (only for authenticated users).
global $user;
$request_path = $request ? urldecode(trim($request->getPathInfo(), '/')) : _current_path();
if ($user->uid && !empty($user->preferred_admin_langcode) && isset($languages[$user->preferred_admin_langcode]) && path_is_admin($request_path)) {
return $user->preferred_admin_langcode;
if ($user->id()) {
$request_path = $request ? urldecode(trim($request->getPathInfo(), '/')) : _current_path();
$langcode = $user->getPreferredAdminLangcode();
$default_langcode = language_default()->id;
if (!empty($langcode) && $langcode != $default_langcode && isset($languages[$langcode]) && path_is_admin($request_path)) {
return $langcode;
}
}
// No language preference from the user or not on an admin path.
......@@ -241,7 +249,7 @@ function language_from_session($languages) {
// an authenticated user.
if (isset($_GET[$param]) && isset($languages[$langcode = $_GET[$param]])) {
global $user;
if ($user->uid) {
if ($user->id()) {
$_SESSION[$param] = $langcode;
}
return $langcode;
......@@ -479,7 +487,7 @@ function language_url_rewrite_session(&$path, &$options) {
// request processing.
if (!isset($query_rewrite)) {
global $user;
if (!$user->uid) {
if (!$user->id()) {
$languages = language_list();
$query_param = check_plain(config('language.negotiation')->get('session.parameter'));
$query_value = isset($_GET[$query_param]) ? check_plain($_GET[$query_param]) : NULL;
......
......@@ -70,7 +70,7 @@ public function configContext(ConfigEvent $event) {
$context->set('locale.language', $language);
}
elseif ($account = $context->get('user.account')) {
$context->set('locale.language', language_load(user_preferred_langcode($account)));
$context->set('locale.language', language_load($account->getPreferredLangcode()));
}
elseif ($language = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)) {
$context->set('locale.language', $language);
......
......@@ -276,7 +276,7 @@ function node_admin_nodes() {
);
$form['nodes'][$node->nid]['author'] = array(
'#theme' => 'username',
'#account' => $node,
'#account' => user_load($node->uid),
);
$form['nodes'][$node->nid]['status'] = array(
'#markup' => $node->status ? t('published') : t('not published'),
......
......@@ -693,7 +693,7 @@ function template_preprocess_node(&$variables) {
// http://drupal.org/node/1941286.
$username = array(
'#theme' => 'username',
'#account' => $node,
'#account' => user_load($node->uid),
'#link_options' => array('attributes' => array('rel' => 'author')),
);
$variables['name'] = drupal_render($username);
......@@ -951,7 +951,7 @@ function node_search_execute($keys = NULL, $conditions = NULL) {
$uri = $node->uri();
$username = array(
'#theme' => 'username',
'#account' => $node,
'#account' => user_load($node->uid),
);
$results[] = array(
'link' => url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE, 'language' => $language))),
......
......@@ -253,7 +253,7 @@ function node_revision_overview($node) {
if ($revision->current_vid > 0) {
$username = array(
'#theme' => 'username',
'#account' => $revision,
'#account' => user_load($revision->uid),
);
$row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->revision_timestamp, 'short'), "node/$node->nid"), '!username' => drupal_render($username)))
. (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
......@@ -263,7 +263,7 @@ function node_revision_overview($node) {
else {
$username = array(
'#theme' => 'username',
'#account' => $revision,
'#account' => user_load($revision->uid),
);
$row[] = t('!date by !username', array('!date' => l(format_date($revision->revision_timestamp, 'short'), "node/$node->nid/revisions/$revision->vid/view"), '!username' => drupal_render($username)))
. (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
......
......@@ -50,7 +50,7 @@ public function access(Route $route, Request $request) {
// 2. the user was successfully authenticated and
// 3. the request comes with a session cookie.
if (!in_array($method, array('GET', 'HEAD', 'OPTIONS', 'TRACE'))
&& user_is_logged_in()
&& $GLOBALS['user']->isAuthenticated()
&& $cookie
) {
$csrf_token = $request->headers->get('X-CSRF-Token');
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Session\UserSession;
use Drupal\simpletest\WebTestBase;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
......@@ -148,15 +149,12 @@ protected function setUp() {
// Ensure that the session is not written to the new environment and replace
// the global $user session with uid 1 from the new test site.
drupal_save_session(FALSE);
// Login as uid 1.
$user = db_query('SELECT * FROM {users} WHERE uid = :uid', array(':uid' => 1))->fetchObject();
// Load roles for the user object.
$roles = array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID);
$result = db_query('SELECT rid, uid FROM {users_roles} WHERE uid = :uid', array(':uid' => 1));
foreach ($result as $record) {
$roles[$record->rid] = $record->rid;
}
$user->roles = $roles;
// Load values for uid 1.
$values = db_query('SELECT * FROM {users} WHERE uid = :uid', array(':uid' => 1))->fetchAssoc();
// Load rolest.
$values['roles'] = array_merge(array(DRUPAL_AUTHENTICATED_RID), db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => 1))->fetchCol());
// Create a new user session object.
$user = new UserSession($values);
// Generate and set a D8-compatible session cookie.
$this->prepareD8Session();
......
......@@ -83,7 +83,7 @@ function tracker_page($account = NULL, $set_title = FALSE) {
'type' => check_plain(node_get_type_label($node)),
// Do not use $node->label(), because $node comes from the database.
'title' => array('data' => l($node->title, 'node/' . $node->nid) . ' ' . drupal_render($mark_build)),
'author' => array('data' => array('#theme' => 'username', '#account' => $node)),
'author' => array('data' => array('#theme' => 'username', '#account' => user_load($node->uid))),
'replies' => array('class' => array('replies'), 'data' => $comments),
'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))),
);
......
......@@ -355,7 +355,7 @@ function _update_cron_notify() {
$default_langcode = language_default()->id;
foreach ($notify_list as $target) {
if ($target_user = user_load_by_mail($target)) {
$target_langcode = user_preferred_langcode($target_user);
$target_langcode = $target_user->getPreferredLangcode();
}
else {
$target_langcode = $default_langcode;
......
......@@ -176,9 +176,9 @@ public function form(array $form, array &$form_state) {
'#format' => isset($account->signature_format) ? $account->signature_format : NULL,
);
$user_preferred_langcode = $register ? $language_interface->id : user_preferred_langcode($account);
$user_preferred_langcode = $register ? $language_interface->id : $account->getPreferredLangcode();
$user_preferred_admin_langcode = $register ? $language_interface->id : user_preferred_langcode($account, 'admin');
$user_preferred_admin_langcode = $register ? $language_interface->id : $account->getPreferredAdminLangcode();
// Is default the interface language?
include_once DRUPAL_ROOT . '/core/includes/language.inc';
......
......@@ -29,7 +29,7 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
$path = $request->attributes->get('system_path');
if ($site_status == MENU_SITE_OFFLINE) {
// If the site is offline, log out unprivileged users.
if (user_is_logged_in() && !user_access('access site in maintenance mode')) {
if ($GLOBALS['user']->isAuthenticated() && !user_access('access site in maintenance mode')) {
user_logout();
// Redirect to homepage.
$event->setResponse(new RedirectResponse(url('<front>', array('absolute' => TRUE))));
......@@ -56,7 +56,7 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
}
}
}
if (user_is_logged_in()) {
if ($GLOBALS['user']->isAuthenticated()) {
if ($path == 'user/login') {
// If user is logged in, redirect to 'user' instead of giving 403.
$event->setResponse(new RedirectResponse(url('user', array('absolute' => TRUE))));
......@@ -64,7 +64,7 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
}
if ($path == 'user/register') {
// Authenticated user should be redirected to user edit page.
$event->setResponse(new RedirectResponse(url('user/' . $GLOBALS['user']->uid . '/edit', array('absolute' => TRUE))));
$event->setResponse(new RedirectResponse(url('user/' . $GLOBALS['user']->id() . '/edit', array('absolute' => TRUE))));
return;
}
}
......
......@@ -66,12 +66,12 @@ public function blockSubmit($form, &$form_state) {
*/
public function build() {
// Retrieve a list of new users who have accessed the site successfully.
$items = db_query_range('SELECT uid, name FROM {users} WHERE status <> 0 AND access <> 0 ORDER BY created DESC', 0, $this->configuration['whois_new_count'])->fetchAll();
$uids = db_query_range('SELECT uid FROM {users} WHERE status <> 0 AND access <> 0 ORDER BY created DESC', 0, $this->configuration['whois_new_count'])->fetchCol();
$build = array(
'#theme' => 'item_list__user__new',
'#items' => array(),
);
foreach ($items as $account) {
foreach (user_load_multiple($uids) as $account) {
$username = array(
'#theme' => 'username',
'#account' => $account,
......
......@@ -51,134 +51,6 @@
*/
class User extends EntityNG implements UserInterface {
/**
* The user ID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $uid;
/**
* The user UUID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $uuid;
/**
* The unique user name.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $name;
/**
* The user's password (hashed).
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $pass;
/**
* The user's email address.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $mail;
/**
* The user's default theme.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $theme;
/**
* The user's signature.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $signature;
/**
* The user's signature format.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $signature_format;
/**
* The timestamp when the user was created.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $created;
/**
* The timestamp when the user last accessed the site. A value of 0 means the
* user has never accessed the site.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $access;
/**
* The timestamp when the user last logged in. A value of 0 means the user has
* never logged in.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $login;
/**
* Whether the user is active (1) or blocked (0).
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $status;
/**
* The user's timezone.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $timezone;
/**
* The user's langcode.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $langcode;
/**
* The user's preferred langcode for receiving emails and viewing the site.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $preferred_langcode;
/**
* The user's preferred langcode for viewing administration pages.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $preferred_admin_langcode;
/**
* The email address used for initial account creation.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $init;
/**
* The user's roles.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $roles;
/**
* {@inheritdoc}
*/
......@@ -186,30 +58,6 @@ public function id() {
return $this->get('uid')->value;
}
/**
* {@inheritdoc}
*/
protected function init() {
parent::init();
unset($this->access);
unset($this->created);
unset($this->init);
unset($this->login);
unset($this->mail);
unset($this->name);
unset($this->pass);
unset($this->preferred_admin_langcode);
unset($this->preferred_langcode);
unset($this->roles);
unset($this->signature);
unset($this->signature_format);
unset($this->status);
unset($this->theme);
unset($this->timezone);
unset($this->uid);
unset($this->uuid);
}
/**
* {@inheritdoc}
*/
......@@ -300,6 +148,7 @@ public static function postDelete(EntityStorageControllerInterface $storage_cont
$storage_controller->deleteUserRoles($uids);
}
/**
* {@inheritdoc}
*/
......@@ -336,7 +185,6 @@ public function getSecureSessionId() {
public function getSessionData() {
return array();
}
/**
* {@inheritdoc}
*/
......@@ -367,4 +215,182 @@ public function removeRole($rid) {
$this->set('roles', array_diff($this->getRoles(), array($rid)));
}
/**
* {@inheritdoc}
*/
public function getPassword() {
return $this->get('pass')->value;
}
/**
* {@inheritdoc}
*/
public function setPassword($password) {
$this->get('pass')->value = $password;
}
/**
* {@inheritdoc}
*/
public function getEmail() {
return $this->get('mail')->value;
}
/**
* {@inheritdoc}
*/
public function setEmail($mail) {
$this->get('mail')->value = $mail;
}
/**
* {@inheritdoc}
*/