2002-01-11 21:00:37 +00:00
<?php
2013-05-02 04:46:53 +00:00
use Drupal\Component\Utility\Crypt;
2012-01-23 04:47:08 +00:00
use Drupal\Core\Database\Query\SelectInterface;
2012-09-12 09:18:04 +00:00
use Drupal\Core\Entity\EntityInterface;
2013-06-11 14:04:34 +00:00
use Drupal\Core\Session\AccountInterface;
2013-01-08 19:16:16 +00:00
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
2012-10-30 20:37:18 +00:00
use Drupal\file\Plugin\Core\Entity\File;
2013-01-08 19:16:16 +00:00
use Drupal\user\Plugin\Core\Entity\User;
2013-06-06 10:20:38 +00:00
use Drupal\user\UserInterface;
2013-06-06 19:10:20 +00:00
use Drupal\user\RoleInterface;
2012-09-04 13:32:47 +00:00
use Drupal\Core\Template\Attribute;
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
use Symfony\Component\HttpFoundation\RedirectResponse;
2012-06-02 19:41:40 +00:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2013-03-04 15:28:39 +00:00
use Drupal\menu_link\Plugin\Core\Entity\MenuLink;
2011-12-28 20:04:40 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Enables the user registration and login system.
*/
2008-05-26 17:12:55 +00:00
/**
* Maximum length of username text field.
*/
2011-11-29 09:56:53 +00:00
const USERNAME_MAX_LENGTH = 60;
2008-05-26 17:12:55 +00:00
/**
* Maximum length of user e-mail text field.
*/
2011-11-29 09:56:53 +00:00
const EMAIL_MAX_LENGTH = 254;
2006-12-25 10:04:03 +00:00
2010-05-27 12:29:39 +00:00
/**
* Only administrators can create user accounts.
*/
2012-08-26 21:15:58 +00:00
const USER_REGISTER_ADMINISTRATORS_ONLY = 'admin_only';
2010-05-27 12:29:39 +00:00
/**
* Visitors can create their own accounts.
*/
2012-08-26 21:15:58 +00:00
const USER_REGISTER_VISITORS = 'visitors';
2010-05-27 12:29:39 +00:00
/**
* Visitors can create accounts, but they don't become active without
* administrative approval.
*/
2012-08-26 21:15:58 +00:00
const USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL = 'visitors_admin_approval';
2010-05-27 12:29:39 +00:00
2009-12-07 03:45:16 +00:00
/**
* Implement hook_help().
*/
function user_help($path, $arg) {
global $user;
switch ($path) {
case 'admin/help#user':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2012-03-21 05:51:30 +00:00
$output .= '<p>' . t('The User module allows users to register, log in, and log out. It also allows users with proper permissions to manage user roles (used to classify users) and permissions associated with those roles. For more information, see the online handbook entry for <a href="@user">User module</a>.', array('@user' => 'http://drupal.org/documentation/modules/user')) . '</p>';
2009-12-07 03:45:16 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Creating and managing users') . '</dt>';
2010-02-07 17:29:09 +00:00
$output .= '<dd>' . t('The User module allows users with the appropriate <a href="@permissions">permissions</a> to create user accounts through the <a href="@people">People administration page</a>, where they can also assign users to one or more roles, and block or delete user accounts. If allowed, users without accounts (anonymous users) can create their own accounts on the <a href="@register">Create new account</a> page.', array('@permissions' => url('admin/people/permissions', array('fragment' => 'module-user')), '@people' => url('admin/people'), '@register' => url('user/register'))) . '</dd>';
2009-12-07 03:45:16 +00:00
$output .= '<dt>' . t('User roles and permissions') . '</dt>';
2012-06-17 02:23:56 +00:00
$output .= '<dd>' . t('<em>Roles</em> are used to group and classify users; each user can be assigned one or more roles. By default there are two roles: <em>anonymous user</em> (users that are not logged in) and <em>authenticated user</em> (users that are registered and logged in). Depending on choices you made when you installed Drupal, the installation process may have defined more roles, and you can create additional custom roles on the <a href="@roles">Roles page</a>. After creating roles, you can set permissions for each role on the <a href="@permissions_user">Permissions page</a>. Granting a permission allows users who have been assigned a particular role to perform an action on the site, such as viewing a particular type of content, editing or creating content, administering settings for a particular module, or using a particular function of the site (such as search).', array('@permissions_user' => url('admin/people/permissions'), '@roles' => url('admin/people/roles'))) . '</dd>';
2009-12-07 03:45:16 +00:00
$output .= '<dt>' . t('Account settings') . '</dt>';
2012-11-26 10:38:45 +00:00
$output .= '<dd>' . t('The <a href="@accounts">Account settings page</a> allows you to manage settings for the displayed name of the anonymous user role, personal contact forms, user registration, and account cancellation. On this page you can also manage settings for account personalization (including signatures), and adapt the text for the e-mail messages that are sent automatically during the user registration process.', array('@accounts' => url('admin/config/people/accounts'))) . '</dd>';
2009-12-07 03:45:16 +00:00
$output .= '</dl>';
return $output;
case 'admin/people/create':
return '<p>' . t("This web page allows administrators to register new users. Users' e-mail addresses and usernames must be unique.") . '</p>';
2010-02-07 17:29:09 +00:00
case 'admin/people/permissions':
2012-06-17 02:23:56 +00:00
return '<p>' . t('Permissions let you control what users can do and see on your site. You can define a specific set of permissions for each role. (See the <a href="@role">Roles</a> page to create a role). Two important roles to consider are Authenticated Users and Administrators. Any permissions granted to the Authenticated Users role will be given to any user who can log into your site. You can make any role the Administrator role for the site, meaning this will be granted all new permissions automatically. You can do this on the <a href="@settings">User Settings</a> page. You should be careful to ensure that only trusted users are given this access and level of control of your site.', array('@role' => url('admin/people/roles'), '@settings' => url('admin/config/people/accounts'))) . '</p>';
case 'admin/people/roles':
2010-03-24 07:34:10 +00:00
$output = '<p>' . t('Roles allow you to fine tune the security and administration of Drupal. A role defines a group of users that have certain privileges as defined on the <a href="@permissions">permissions page</a>. Examples of roles include: anonymous user, authenticated user, moderator, administrator and so on. In this area you will define the names and order of the roles on your site. It is recommended to order your roles from least permissive (anonymous user) to most permissive (administrator). To delete a role choose "edit role".', array('@permissions' => url('admin/people/permissions'))) . '</p>';
2011-11-23 01:59:26 +00:00
$output .= '<p>' . t('Drupal has three special user roles:') . '</p>';
2009-12-07 03:45:16 +00:00
$output .= '<ul>';
$output .= '<li>' . t("Anonymous user: this role is used for users that don't have a user account or that are not authenticated.") . '</li>';
$output .= '<li>' . t('Authenticated user: this role is automatically granted to all logged in users.') . '</li>';
2011-11-23 01:59:26 +00:00
$output .= '<li>' . t('Administrator role: this role is automatically granted all new permissions when you install a new module. Configure which role is the administrator role on the <a href="@account_settings">Account settings page</a>.', array('@account_settings' => url('admin/config/people/accounts'))) . '</li>';
2009-12-07 03:45:16 +00:00
$output .= '</ul>';
return $output;
case 'admin/config/people/accounts/fields':
2013-06-27 21:24:39 +00:00
return '<p>' . t('This form lets administrators add and edit fields for storing user data.') . '</p>';
case 'admin/config/people/accounts/form-display':
return '<p>' . t('This form lets administrators configure how form fields should be displayed when editing a user profile.') . '</p>';
2009-12-07 03:45:16 +00:00
case 'admin/config/people/accounts/display':
return '<p>' . t('This form lets administrators configure how fields should be displayed when rendering a user profile page.') . '</p>';
case 'admin/people/search':
return '<p>' . t('Enter a simple pattern ("*" may be used as a wildcard match) to search for a username or e-mail address. For example, one may search for "br" and Drupal might return "brian", "brad", and "brenda@example.com".') . '</p>';
}
}
2009-01-08 08:42:13 +00:00
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function user_theme() {
return array(
2012-12-13 12:11:18 +00:00
'user' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2009-05-21 23:07:16 +00:00
'file' => 'user.pages.inc',
2013-05-24 16:55:47 +00:00
'template' => 'user',
2007-06-21 04:38:41 +00:00
),
2009-07-05 18:07:04 +00:00
'user_admin_permissions' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'form',
2009-05-21 23:07:16 +00:00
'file' => 'user.admin.inc',
2007-04-06 13:27:23 +00:00
),
2010-03-21 21:20:43 +00:00
'user_permission_description' => array(
'variables' => array('permission_item' => NULL, 'hide' => NULL),
'file' => 'user.admin.inc',
),
2007-04-23 16:58:10 +00:00
'user_signature' => array(
2009-10-23 22:24:19 +00:00
'variables' => array('signature' => NULL),
2007-04-23 16:58:10 +00:00
),
2011-11-10 14:57:14 +00:00
'username' => array(
2013-06-15 09:10:01 +00:00
'variables' => array('account' => NULL, 'attributes' => array()),
2011-11-10 14:57:14 +00:00
),
2007-04-06 13:27:23 +00:00
);
}
2012-12-27 22:26:37 +00:00
/**
* Implements hook_page_build().
*/
function user_page_build(&$page) {
$path = drupal_get_path('module', 'user');
2013-06-07 10:48:55 +00:00
$page['#attached']['css'][$path . '/css/user.module.css'] = array('every_page' => TRUE);
2012-12-27 22:26:37 +00:00
}
2013-01-23 17:46:47 +00:00
/**
* Implements hook_entity_bundle_info().
*/
function user_entity_bundle_info() {
2013-05-05 00:24:24 +00:00
$bundles['user']['user']['label'] = t('User');
2013-01-23 17:46:47 +00:00
return $bundles;
}
2010-01-13 06:26:49 +00:00
/**
2012-09-02 04:50:06 +00:00
* Entity URI callback.
2010-01-13 06:26:49 +00:00
*/
2010-02-11 15:52:13 +00:00
function user_uri($user) {
return array(
2013-06-06 10:20:38 +00:00
'path' => 'user/' . $user->id(),
2010-02-11 15:52:13 +00:00
);
2010-01-13 06:26:49 +00:00
}
2011-10-26 13:45:52 +00:00
/**
* Entity label callback.
*
2012-06-16 15:39:01 +00:00
* This label callback has langcode for security reasons. The username is the
* visual identifier for a user and needs to be consistent in all languages.
*
2011-10-26 13:45:52 +00:00
* @param $entity_type
* The entity type.
* @param $entity
* The entity object.
*
* @return
* The name of the user.
*
2012-01-16 02:18:26 +00:00
* @see user_format_name()
2011-10-26 13:45:52 +00:00
*/
function user_label($entity_type, $entity) {
2013-07-09 13:46:44 +00:00
return $entity->getUsername();
2011-10-26 13:45:52 +00:00
}
2012-11-26 10:38:45 +00:00
/**
* Populates $entity->account for each prepared entity.
*
2013-01-08 19:16:16 +00:00
* Called by Drupal\Core\Entity\EntityRenderControllerInterface::buildContent()
* implementations.
2012-11-26 10:38:45 +00:00
*
* @param array $entities
* The entities keyed by entity ID.
*/
function user_attach_accounts(array $entities) {
$uids = array();
foreach ($entities as $entity) {
$uids[] = $entity->uid;
}
$uids = array_unique($uids);
$accounts = user_load_multiple($uids);
$anonymous = drupal_anonymous_user();
foreach ($entities as $id => $entity) {
if (isset($accounts[$entity->uid])) {
$entities[$id]->account = $accounts[$entity->uid];
}
else {
$entities[$id]->account = $anonymous;
}
}
}
/**
* Returns whether this site supports the default user picture feature.
*
* This approach preserves compatibility with node/comment templates. Alternate
* user picture implementations (e.g., Gravatar) should provide their own
* add/edit/delete forms and populate the 'picture' variable during the
* preprocess stage.
*/
function user_picture_enabled() {
return (bool) field_info_instance('user', 'user_picture', 'user');
}
2009-08-19 13:31:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_extra_fields().
2009-08-19 13:31:14 +00:00
*/
2010-01-02 15:00:34 +00:00
function user_field_extra_fields() {
Issue #967566 by Letharion, mariusz.slonina, indytechcook, adnasa, joestewart, oriol_e9g, Fabianx, tim.plunkett, valthebald, tsvenson, rbayliss, sun, xjm | DocuAnt: Several important Core User settings need to implement hook_field_extra_fields().
2013-02-18 21:21:42 +00:00
$fields['user']['user']['form']['account'] = array(
'label' => t('User name and password'),
'description' => t('User module account form elements.'),
'weight' => -10,
);
if (config('user.settings')->get('signatures')) {
$fields['user']['user']['form']['signature_settings'] = array(
'label' => t('Signature settings'),
'description' => t('User module form element.'),
'weight' => 1,
);
}
$fields['user']['user']['form']['language'] = array(
'label' => t('Language settings'),
'description' => t('User module form element.'),
'weight' => 0,
);
if (config('system.timezone')->get('user.configurable')) {
$fields['user']['user']['form']['timezone'] = array(
'label' => t('Timezone'),
'description' => t('System module form element.'),
'weight' => 6,
);
}
$fields['user']['user']['display']['member_for'] = array(
'label' => t('Member for'),
'description' => t('User module \'member for\' view element.'),
'weight' => 5,
2010-01-02 15:00:34 +00:00
);
2009-08-19 13:31:14 +00:00
Issue #967566 by Letharion, mariusz.slonina, indytechcook, adnasa, joestewart, oriol_e9g, Fabianx, tim.plunkett, valthebald, tsvenson, rbayliss, sun, xjm | DocuAnt: Several important Core User settings need to implement hook_field_extra_fields().
2013-02-18 21:21:42 +00:00
return $fields;
2009-08-19 13:31:14 +00:00
}
2004-05-24 18:37:50 +00:00
/**
2012-04-26 04:07:55 +00:00
* Loads multiple users based on certain conditions.
2004-05-24 18:37:50 +00:00
*
2009-03-14 23:01:38 +00:00
* This function should be used whenever you need to load more than one user
* from the database. Users are loaded into memory and will not require
* database access if loaded again during the same page request.
2004-05-24 18:37:50 +00:00
*
2012-08-21 15:38:04 +00:00
* @param array $uids
* (optional) An array of entity IDs. If omitted, all entities are loaded.
2012-04-26 04:07:55 +00:00
* @param bool $reset
2009-03-14 23:01:38 +00:00
* A boolean indicating that the internal cache should be reset. Use this if
* loading a user object which has been altered during the page request.
2010-11-20 09:43:43 +00:00
*
2012-04-26 04:07:55 +00:00
* @return array
2009-03-14 23:01:38 +00:00
* An array of user objects, indexed by uid.
*
2012-04-26 04:07:55 +00:00
* @see entity_load_multiple()
2009-03-14 23:01:38 +00:00
* @see user_load()
* @see user_load_by_mail()
* @see user_load_by_name()
2012-10-30 10:41:42 +00:00
* @see \Drupal\Core\Entity\Query\QueryInterface
2004-05-24 18:37:50 +00:00
*/
2012-08-21 15:38:04 +00:00
function user_load_multiple(array $uids = NULL, $reset = FALSE) {
2013-06-06 10:20:38 +00:00
$entities = entity_load_multiple('user', $uids, $reset);
// Return BC-entities.
foreach ($entities as $id => $entity) {
$entities[$id] = $entity->getBCEntity();
}
return $entities;
2009-08-25 21:53:48 +00:00
}
2009-03-14 23:01:38 +00:00
/**
2010-04-28 19:47:45 +00:00
* Loads a user object.
*
* Drupal has a global $user object, which represents the currently-logged-in
* user. So to avoid confusion and to avoid clobbering the global $user object,
* it is a good idea to assign the result of this function to a different local
* variable, generally $account. If you actually do want to act as the user you
2011-02-19 00:14:03 +00:00
* are loading, it is essential to call drupal_save_session(FALSE); first.
* See
* @link http://drupal.org/node/218104 Safely impersonating another user @endlink
* for more information.
2009-03-14 23:01:38 +00:00
*
2012-04-26 04:07:55 +00:00
* @param int $uid
2010-04-28 19:47:45 +00:00
* Integer specifying the user ID to load.
2012-04-26 04:07:55 +00:00
* @param bool $reset
2010-04-28 19:47:45 +00:00
* TRUE to reset the internal cache and load from the database; FALSE
* (default) to load from the internal cache, if set.
*
2012-04-26 04:07:55 +00:00
* @return object
2013-07-01 00:09:20 +00:00
* A fully-loaded user object upon successful user load, or NULL if the user
2009-03-14 23:01:38 +00:00
* cannot be loaded.
*
* @see user_load_multiple()
*/
function user_load($uid, $reset = FALSE) {
2013-06-06 10:20:38 +00:00
$user = entity_load('user', $uid, $reset);
2013-07-01 00:09:20 +00:00
return $user ? $user->getBCEntity() : NULL;
2009-03-14 23:01:38 +00:00
}
/**
2012-04-26 04:07:55 +00:00
* Fetches a user object by email address.
2009-03-14 23:01:38 +00:00
*
2012-04-26 04:07:55 +00:00
* @param string $mail
2009-03-14 23:01:38 +00:00
* String with the account's e-mail address.
2012-04-26 04:07:55 +00:00
* @return object|bool
2009-03-14 23:01:38 +00:00
* A fully-loaded $user object upon successful user load or FALSE if user
* cannot be loaded.
*
* @see user_load_multiple()
*/
function user_load_by_mail($mail) {
2012-08-21 15:38:04 +00:00
$users = entity_load_multiple_by_properties('user', array('mail' => $mail));
2013-06-06 10:20:38 +00:00
return $users ? reset($users)->getBCEntity() : FALSE;
2009-03-14 23:01:38 +00:00
}
/**
2012-04-26 04:07:55 +00:00
* Fetches a user object by account name.
2009-03-14 23:01:38 +00:00
*
2012-04-26 04:07:55 +00:00
* @param string $name
2009-03-14 23:01:38 +00:00
* String with the account's user name.
2012-04-26 04:07:55 +00:00
* @return object|bool
2009-03-14 23:01:38 +00:00
* A fully-loaded $user object upon successful user load or FALSE if user
* cannot be loaded.
*
* @see user_load_multiple()
*/
function user_load_by_name($name) {
2012-08-21 15:38:04 +00:00
$users = entity_load_multiple_by_properties('user', array('name' => $name));
2013-06-06 10:20:38 +00:00
return $users ? reset($users)->getBCEntity() : FALSE;
2002-01-11 21:00:37 +00:00
}
2004-05-24 18:37:50 +00:00
/**
* Verify the syntax of the given name.
*/
2002-01-11 21:00:37 +00:00
function user_validate_name($name) {
2008-06-27 07:25:11 +00:00
if (!$name) {
return t('You must enter a username.');
}
if (substr($name, 0, 1) == ' ') {
return t('The username cannot begin with a space.');
}
if (substr($name, -1) == ' ') {
return t('The username cannot end with a space.');
}
if (strpos($name, ' ') !== FALSE) {
return t('The username cannot contain multiple spaces in a row.');
}
if (preg_match('/[^\x{80}-\x{F7} a-z0-9@_.\'-]/i', $name)) {
return t('The username contains an illegal character.');
}
2008-04-14 17:48:46 +00:00
if (preg_match('/[\x{80}-\x{A0}' . // Non-printable ISO-8859-1 + NBSP
2009-05-24 17:39:35 +00:00
'\x{AD}' . // Soft-hyphen
'\x{2000}-\x{200F}' . // Various space characters
'\x{2028}-\x{202F}' . // Bidirectional text overrides
'\x{205F}-\x{206F}' . // Various text hinting characters
'\x{FEFF}' . // Byte order mark
'\x{FF01}-\x{FF60}' . // Full-width latin
'\x{FFF9}-\x{FFFD}' . // Replacement characters
'\x{0}-\x{1F}]/u', // NULL byte and control characters
$name)) {
2005-07-31 21:54:39 +00:00
return t('The username contains an illegal character.');
}
2008-06-27 07:25:11 +00:00
if (drupal_strlen($name) > USERNAME_MAX_LENGTH) {
return t('The username %name is too long: it must be %max characters or less.', array('%name' => $name, '%max' => USERNAME_MAX_LENGTH));
}
2002-01-11 21:00:37 +00:00
}
2004-05-24 18:37:50 +00:00
/**
* Generate a random alphanumeric password.
*/
2003-06-13 17:34:18 +00:00
function user_password($length = 10) {
// This variable contains the list of allowable characters for the
2006-05-07 00:08:36 +00:00
// password. Note that the number 0 and the letter 'O' have been
// removed to avoid confusion between the two. The same is true
2008-02-03 19:23:01 +00:00
// of 'I', 1, and 'l'.
2006-01-15 16:57:56 +00:00
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
2004-05-24 18:37:50 +00:00
2006-01-15 16:57:56 +00:00
// Zero-based count of characters in the allowable list:
$len = strlen($allowable_characters) - 1;
2003-06-13 17:34:18 +00:00
2004-05-24 18:37:50 +00:00
// Declare the password as a blank string.
$pass = '';
2003-06-13 17:34:18 +00:00
2004-05-24 18:37:50 +00:00
// Loop the number of times specified by $length.
2003-06-13 17:34:18 +00:00
for ($i = 0; $i < $length; $i++) {
// Each iteration, pick a random character from the
// allowable string and append it to the password:
2006-01-15 16:57:56 +00:00
$pass .= $allowable_characters[mt_rand(0, $len)];
2003-06-13 17:34:18 +00:00
}
return $pass;
2002-01-11 21:00:37 +00:00
}
2008-05-07 19:34:24 +00:00
/**
* Determine the permissions for one or more roles.
*
2013-06-24 16:38:32 +00:00
* @param array $roles
* An array of role IDs.
2008-05-07 19:34:24 +00:00
*
2013-06-24 16:38:32 +00:00
* @return array
* An array indexed by role ID. Each value is an array of permission strings
* for the given role.
2008-05-07 19:34:24 +00:00
*/
2013-06-24 16:38:32 +00:00
function user_role_permissions(array $roles) {
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
return _user_role_permissions_update($roles);
}
$entities = entity_load_multiple('user_role', $roles);
$role_permissions = array();
2013-06-06 10:20:38 +00:00
foreach ($roles as $rid) {
2013-06-24 16:38:32 +00:00
$role_permissions[$rid] = isset($entities[$rid]) ? $entities[$rid]->getPermissions() : array();
2013-01-10 18:50:19 +00:00
}
2013-06-24 16:38:32 +00:00
return $role_permissions;
}
2008-05-07 19:34:24 +00:00
2013-06-24 16:38:32 +00:00
/**
* Determine the permissions for one or more roles during update.
*
* A separate version is needed because during update the entity system can't
* be used and in non-update situations the entity system is preferred because
* of the hook system.
*
* @param array $roles
* An array of role IDs.
*
* @return array
* An array indexed by role ID. Each value is an array of permission strings
* for the given role.
*/
function _user_role_permissions_update($roles) {
$role_permissions = array();
foreach ($roles as $rid) {
$role_permissions[$rid] = Drupal::config("user.role.$rid")->get('permissions') ?: array();
2008-05-07 19:34:24 +00:00
}
return $role_permissions;
}
2004-05-24 18:37:50 +00:00
/**
* Determine whether the user has a given privilege.
*
* @param $string
* The permission, such as "administer nodes", being checked for.
2013-06-11 14:04:34 +00:00
* @param \Drupal\Core\Session\AccountInterface $account
2004-10-18 18:29:15 +00:00
* (optional) The account to check, if not given use currently logged in user.
2004-05-24 18:37:50 +00:00
*
* @return
2007-11-04 15:42:52 +00:00
* Boolean TRUE if the current user has the requested permission.
2004-05-24 18:37:50 +00:00
*
* All permission checks in Drupal should go through this function. This
* way, we guarantee consistent behavior, and ensure that the superuser
* can perform all actions.
*/
2013-06-11 14:04:34 +00:00
function user_access($string, AccountInterface $account = NULL) {
2002-01-11 21:00:37 +00:00
global $user;
2007-11-04 15:42:52 +00:00
2009-08-04 06:19:44 +00:00
if (!isset($account)) {
2013-06-29 16:26:55 +00:00
// In the installer request session is not set, so we have to fall back
// to the global $user. In all other cases the session key is preferred.
$account = Drupal::request()->attributes->get('account') ?: $user;
2004-11-02 12:20:59 +00:00
}
2004-10-19 18:02:31 +00:00
// User #1 has all privileges:
2013-07-09 13:46:44 +00:00
if ($account->id() == 1) {
2005-06-21 18:45:30 +00:00
return TRUE;
2003-06-11 18:16:32 +00:00
}
2004-05-24 18:37:50 +00:00
// To reduce the number of SQL queries, we cache the user's permissions
// in a static variable.
2009-11-22 04:22:54 +00:00
// Use the advanced drupal_static() pattern, since this is called very often.
2010-01-07 04:54:18 +00:00
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['perm'] = &drupal_static(__FUNCTION__);
}
$perm = &$drupal_static_fast['perm'];
2013-07-09 13:46:44 +00:00
if (!isset($perm[$account->id()])) {
$perm[$account->id()] = array();
2013-06-24 16:38:32 +00:00
foreach (user_role_permissions($account->getRoles()) as $role_permissions) {
2013-07-09 13:46:44 +00:00
$perm[$account->id()] += array_fill_keys($role_permissions, TRUE);
2004-05-10 20:34:25 +00:00
}
2002-01-11 21:00:37 +00:00
}
2005-06-21 18:45:30 +00:00
2013-07-09 13:46:44 +00:00
return isset($perm[$account->id()][$string]);
2002-01-11 21:00:37 +00:00
}
2005-06-21 18:21:08 +00:00
/**
2008-02-03 19:23:01 +00:00
* Checks for usernames blocked by user administration.
2005-06-21 18:21:08 +00:00
*
2012-06-26 16:31:20 +00:00
* @param $name
* A string containing a name of the user.
*
* @return
* Object with property 'name' (the user name), if the user is blocked;
* FALSE if the user is not blocked.
2005-06-21 18:21:08 +00:00
*/
function user_is_blocked($name) {
2010-01-13 06:44:31 +00:00
return db_select('users')
->fields('users', array('name'))
->condition('name', db_like($name), 'LIKE')
->condition('status', 0)
->execute()->fetchObject();
2005-06-21 18:21:08 +00:00
}
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2004-05-24 18:37:50 +00:00
*/
2009-07-05 18:00:11 +00:00
function user_permission() {
2009-05-24 17:39:35 +00:00
return array(
'administer permissions' => array(
'title' => t('Administer permissions'),
2010-03-21 21:20:43 +00:00
'restrict access' => TRUE,
2009-05-24 17:39:35 +00:00
),
'administer users' => array(
'title' => t('Administer users'),
2010-03-21 21:20:43 +00:00
'restrict access' => TRUE,
2009-05-24 17:39:35 +00:00
),
'access user profiles' => array(
2009-12-01 13:14:43 +00:00
'title' => t('View user profiles'),
2009-05-24 17:39:35 +00:00
),
'change own username' => array(
'title' => t('Change own username'),
),
'cancel account' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Cancel own user account'),
2012-08-26 21:15:58 +00:00
'description' => t('Note: content may be kept, unpublished, deleted or transferred to the %anonymous-name user depending on the configured <a href="@user-settings-url">user settings</a>.', array('%anonymous-name' => config('user.settings')->get('anonymous'), '@user-settings-url' => url('admin/config/people/accounts'))),
2009-05-24 17:39:35 +00:00
),
'select account cancellation method' => array(
'title' => t('Select method for cancelling own account'),
2010-03-21 21:20:43 +00:00
'restrict access' => TRUE,
2009-05-24 17:39:35 +00:00
),
);
2002-01-11 21:00:37 +00:00
}
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_search_info().
2004-05-24 18:37:50 +00:00
*/
2009-08-29 21:05:16 +00:00
function user_search_info() {
return array(
'title' => 'Users',
);
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_search_access().
2009-08-29 21:05:16 +00:00
*/
function user_search_access() {
return user_access('access user profiles');
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_search_execute().
2009-08-29 21:05:16 +00:00
*/
2010-08-11 14:21:39 +00:00
function user_search_execute($keys = NULL, $conditions = NULL) {
2009-08-29 21:05:16 +00:00
$find = array();
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\*+!', '%', $keys);
2012-06-05 05:42:19 +00:00
$query = db_select('users')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
2010-11-21 18:52:05 +00:00
$query->fields('users', array('uid'));
2009-08-29 21:05:16 +00:00
if (user_access('administer users')) {
2012-12-27 02:29:38 +00:00
// Administrators can also search in the otherwise private email field, and
// they don't need to be restricted to only active users.
2010-05-07 12:55:25 +00:00
$query->fields('users', array('mail'));
2009-08-29 21:05:16 +00:00
$query->condition(db_or()->
2010-02-28 20:10:34 +00:00
condition('name', '%' . db_like($keys) . '%', 'LIKE')->
condition('mail', '%' . db_like($keys) . '%', 'LIKE'));
2009-08-29 21:05:16 +00:00
}
else {
2012-12-27 02:29:38 +00:00
// Regular users can only search via usernames, and we do not show them
// blocked accounts.
$query->condition('name', '%' . db_like($keys) . '%', 'LIKE')
->condition('status', 1);
2009-08-29 21:05:16 +00:00
}
2010-11-21 18:52:05 +00:00
$uids = $query
2009-08-29 21:05:16 +00:00
->limit(15)
2010-11-21 18:52:05 +00:00
->execute()
->fetchCol();
$accounts = user_load_multiple($uids);
$results = array();
foreach ($accounts as $account) {
$result = array(
2012-01-16 02:18:26 +00:00
'title' => user_format_name($account),
2013-07-09 13:46:44 +00:00
'link' => url('user/' . $account->id(), array('absolute' => TRUE)),
2010-11-21 18:52:05 +00:00
);
if (user_access('administer users')) {
2013-07-09 13:46:44 +00:00
$result['title'] .= ' (' . $account->getEmail() . ')';
2010-05-07 12:55:25 +00:00
}
2010-11-21 18:52:05 +00:00
$results[] = $result;
2002-01-11 21:00:37 +00:00
}
2010-11-21 18:52:05 +00:00
return $results;
2002-01-11 21:00:37 +00:00
}
2004-05-24 18:37:50 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_view().
2004-05-24 18:37:50 +00:00
*/
2013-06-06 10:20:38 +00:00
function user_user_view(UserInterface $account, EntityDisplay $display) {
2013-01-08 19:16:16 +00:00
if ($display->getComponent('member_for')) {
$account->content['member_for'] = array(
'#type' => 'item',
'#title' => t('Member for'),
'#markup' => format_interval(REQUEST_TIME - $account->created),
);
}
2008-10-06 11:30:12 +00:00
}
2012-04-24 04:01:42 +00:00
/**
* Sets the value of the user register and profile forms' langcode element.
*/
function _user_language_selector_langcode_value($element, $input, &$form_state) {
// Only add to the description if the form element have a description.
if (isset($form_state['complete_form']['language']['preferred_langcode']['#description'])) {
$form_state['complete_form']['language']['preferred_langcode']['#description'] .= ' ' . t("This is also assumed to be the primary language of this account's profile information.");
}
return $form_state['values']['preferred_langcode'];
2009-10-10 16:48:39 +00:00
}
2010-02-11 03:19:21 +00:00
/**
2012-08-16 11:30:43 +00:00
* Form validation handler for the current password on the user account form.
2010-05-27 12:29:39 +00:00
*
2012-08-16 11:30:43 +00:00
* @see AccountFormController::form()
2010-02-11 03:19:21 +00:00
*/
function user_validate_current_pass(&$form, &$form_state) {
2012-08-16 11:30:43 +00:00
$account = $form_state['user'];
2010-02-11 03:19:21 +00:00
foreach ($form_state['values']['current_pass_required_values'] as $key => $name) {
// This validation only works for required textfields (like mail) or
// form values like password_confirm that have their own validation
// that prevent them from being empty if they are changed.
if ((strlen(trim($form_state['values'][$key])) > 0) && ($form_state['values'][$key] != $account->$key)) {
2012-11-24 08:12:43 +00:00
$current_pass_failed = empty($form_state['values']['current_pass']) || !drupal_container()->get('password')->check($form_state['values']['current_pass'], $account);
2010-02-11 03:19:21 +00:00
if ($current_pass_failed) {
form_set_error('current_pass', t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => $name)));
form_set_error($key);
}
// We only need to check the password once.
break;
}
}
}
2012-01-10 03:22:24 +00:00
/**
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* Implements hook_preprocess_HOOK() for block.html.twig.
2012-01-10 03:22:24 +00:00
*/
function user_preprocess_block(&$variables) {
2013-05-11 10:01:25 +00:00
if ($variables['configuration']['module'] == 'user') {
switch ($variables['elements']['#plugin_id']) {
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
case 'user_login_block':
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'form';
2012-01-10 03:22:24 +00:00
break;
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
case 'user_new_block':
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'complementary';
2012-01-10 03:22:24 +00:00
break;
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
case 'user_online_block':
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'complementary';
2012-01-10 03:22:24 +00:00
break;
}
}
}
2011-11-10 14:57:14 +00:00
/**
* Format a username.
*
2013-07-09 13:46:44 +00:00
* @param \Drupal\Core\Session\Interface $account
2011-11-10 14:57:14 +00:00
* The account object for the user whose name is to be formatted.
*
* @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.
2013-07-09 13:46:44 +00:00
*
* @deprecated Use \Drupal\Core\Session\Interface::getUsername() instead.
2011-11-10 14:57:14 +00:00
*/
2013-07-09 13:46:44 +00:00
function user_format_name(AccountInterface $account) {
return $account->getUsername();
2011-11-10 14:57:14 +00:00
}
2012-11-11 23:52:37 +00:00
/**
* Implements hook_template_preprocess_default_variables_alter().
*
* @see user_user_login()
* @see user_user_logout()
*/
function user_template_preprocess_default_variables_alter(&$variables) {
global $user;
2013-04-07 20:06:33 +00:00
// If this function is called from the installer after Drupal has been
// installed then $user will not be set.
if (!is_object($user)) {
return;
}
2012-11-11 23:52:37 +00:00
$variables['user'] = clone $user;
// Remove password and session IDs, since themes should not need nor see them.
unset($variables['user']->pass, $variables['user']->sid, $variables['user']->ssid);
$variables['is_admin'] = user_access('access administration pages');
2013-07-09 13:46:44 +00:00
$variables['logged_in'] = $user->isAuthenticated();
2012-11-11 23:52:37 +00:00
}
2011-11-10 14:57:14 +00:00
/**
* Preprocesses variables for theme_username().
*
2013-06-17 06:46:04 +00:00
* Modules that make any changes to variables like 'name' or 'extra' must ensure
2011-11-10 14:57:14 +00:00
* that the final string is safe to include directly in the output by using
* check_plain() or filter_xss().
*
* @see template_process_username()
*/
function template_preprocess_username(&$variables) {
2013-07-09 13:46:44 +00:00
$account = $variables['account'] ?: drupal_anonymous_user();
2011-11-10 14:57:14 +00:00
$variables['extra'] = '';
2013-07-09 13:46:44 +00:00
$variables['uid'] = $account->id();
if (empty($variables['uid'])) {
if (theme_get_setting('features.comment_user_verification')) {
$variables['extra'] = ' (' . t('not verified') . ')';
}
2011-11-10 14:57:14 +00:00
}
// Set the name to a formatted name that is safe for printing and
// that won't break tables by being too long. Keep an unshortened,
// unsanitized version, in case other preprocess functions want to implement
// their own shortening logic or add markup. If they do so, they must ensure
// that $variables['name'] is safe for printing.
2013-07-09 13:46:44 +00:00
$name = $variables['name_raw'] = $account->getUsername();
2011-11-10 14:57:14 +00:00
if (drupal_strlen($name) > 20) {
$name = drupal_substr($name, 0, 15) . '...';
}
$variables['name'] = check_plain($name);
$variables['profile_access'] = user_access('access user profiles');
2011-12-04 23:35:24 +00:00
2011-11-10 14:57:14 +00:00
// Populate link path and attributes if appropriate.
if ($variables['uid'] && $variables['profile_access']) {
// We are linking to a local user.
2013-06-14 22:27:57 +00:00
$variables['link_options']['attributes']['title'] = t('View user profile.');
2011-11-10 14:57:14 +00:00
$variables['link_path'] = 'user/' . $variables['uid'];
}
elseif (!empty($account->homepage)) {
// Like the 'class' attribute, the 'rel' attribute can hold a
// space-separated set of values, so initialize it as an array to make it
// easier for other preprocess functions to append to it.
2013-06-14 22:27:57 +00:00
$variables['link_options']['attributes']['rel'] = 'nofollow';
2011-11-10 14:57:14 +00:00
$variables['link_path'] = $account->homepage;
$variables['homepage'] = $account->homepage;
}
// We do not want the l() function to check_plain() a second time.
$variables['link_options']['html'] = TRUE;
// Set a default class.
2013-06-14 22:27:57 +00:00
$variables['link_options']['attributes']['class'] = array('username');
2011-11-10 14:57:14 +00:00
}
/**
* Returns HTML for a username, potentially linked to the user's page.
*
* @param $variables
* An associative array containing:
* - account: The user object to format.
* - name: The user's name, sanitized.
* - extra: Additional text to append to the user's name, sanitized.
* - link_path: The path or URL of the user's profile page, home page, or
* other desired page to link to for more information about the user.
* - link_options: An array of options to pass to the l() function's $options
* parameter if linking the user's name to the user's page.
2012-09-04 13:32:47 +00:00
* - attributes: An array of attributes to instantiate the
* Drupal\Core\Template\Attribute class if not linking to the user's page.
2011-11-10 14:57:14 +00:00
*
* @see template_preprocess_username()
* @see template_process_username()
*/
function theme_username($variables) {
if (isset($variables['link_path'])) {
// We have a link path, so we should generate a link using l().
// Additional classes may be added as array elements like
// $variables['link_options']['attributes']['class'][] = 'myclass';
$output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
}
else {
// Modules may have added important attributes so they must be included
// in the output. Additional classes may be added as array elements like
2012-08-03 15:31:18 +00:00
// $variables['attributes']['class'][] = 'myclass';
2012-09-04 13:32:47 +00:00
$output = '<span' . new Attribute($variables['attributes']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
2011-11-10 14:57:14 +00:00
}
return $output;
}
2012-12-17 21:42:24 +00:00
/**
* Determines if the current user is anonymous.
*
* @return bool
* TRUE if the user is anonymous, FALSE if the user is authenticated.
*/
2007-01-24 14:48:36 +00:00
function user_is_anonymous() {
2007-11-17 14:25:23 +00:00
// Menu administrators can see items for anonymous when administering.
2013-07-09 13:46:44 +00:00
return $GLOBALS['user']->isAnonymous() || !empty($GLOBALS['menu_admin']);
2007-01-24 14:48:36 +00:00
}
2012-12-17 21:42:24 +00:00
/**
* Determines if the current user is logged in.
*
* @return bool
* TRUE if the user is logged in, FALSE if the user is anonymous.
2013-07-09 13:46:44 +00:00
*
* @deprecated Use \Drupal\Core\Session\UserSession::isAuthenticated().
2012-12-17 21:42:24 +00:00
*/
2007-01-24 14:48:36 +00:00
function user_is_logged_in() {
2013-07-09 13:46:44 +00:00
return $GLOBALS['user']->isAuthenticated();
2007-01-24 14:48:36 +00:00
}
2012-12-17 21:42:24 +00:00
/**
* Determines if the current user has access to the user registration page.
*
* @return bool
* TRUE if the user is not already logged in and can register for an account.
*/
2007-01-24 14:48:36 +00:00
function user_register_access() {
2012-08-26 21:15:58 +00:00
return user_is_anonymous() && (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY);
2007-01-24 14:48:36 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2004-04-21 13:56:38 +00:00
*/
2007-01-24 14:48:36 +00:00
function user_menu() {
// Registration and login pages.
2007-02-11 09:30:51 +00:00
$items['user'] = array(
2007-12-17 13:46:36 +00:00
'title' => 'User account',
2010-10-08 05:28:30 +00:00
'title callback' => 'user_menu_title',
'weight' => -10,
2013-06-27 08:23:39 +00:00
'route_name' => 'user_page',
2012-10-25 15:53:18 +00:00
'menu_name' => 'account',
2007-02-11 09:30:51 +00:00
);
$items['user/login'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Log in',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
);
2012-09-30 16:53:26 +00:00
// Other authentication methods may add pages below user/login/.
$items['user/login/default'] = array(
'title' => 'Username and password',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
2004-04-21 13:56:38 +00:00
2007-01-24 14:48:36 +00:00
$items['user/register'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Create new account',
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
2013-03-09 05:46:28 +00:00
'route_name' => 'user_register',
2007-01-24 14:48:36 +00:00
);
$items['user/password'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Request new password',
2013-06-20 19:02:06 +00:00
'route_name' => 'user_pass',
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
);
$items['user/reset/%/%/%'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Reset password',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('user_pass_reset', 2, 3, 4),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
2009-08-24 00:14:23 +00:00
'file' => 'user.pages.inc',
2007-01-24 14:48:36 +00:00
);
2008-11-29 09:33:51 +00:00
$items['user/logout'] = array(
'title' => 'Log out',
2013-05-26 02:46:09 +00:00
'route_name' => 'user_logout',
2008-11-29 09:33:51 +00:00
'weight' => 10,
2012-10-25 15:53:18 +00:00
'menu_name' => 'account',
2008-11-29 09:33:51 +00:00
);
2009-08-21 14:27:47 +00:00
// User listing pages.
2009-08-11 11:47:58 +00:00
$items['admin/people'] = array(
'title' => 'People',
2010-10-04 14:54:10 +00:00
'description' => 'Manage user accounts, roles, and permissions.',
2013-06-13 20:57:33 +00:00
'page callback' => 'user_admin_account',
2007-09-10 13:14:38 +00:00
'access arguments' => array('administer users'),
2009-08-21 07:59:47 +00:00
'position' => 'left',
2009-07-31 19:01:03 +00:00
'weight' => -4,
2009-08-24 00:14:23 +00:00
'file' => 'user.admin.inc',
2007-09-10 13:14:38 +00:00
);
2010-03-24 07:34:10 +00:00
// Permissions and role forms.
2010-02-07 17:29:09 +00:00
$items['admin/people/permissions'] = array(
'title' => 'Permissions',
'description' => 'Determine access to features by selecting permissions for roles.',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_admin_permissions'),
'access arguments' => array('administer permissions'),
'file' => 'user.admin.inc',
'type' => MENU_LOCAL_TASK,
);
2012-06-17 02:23:56 +00:00
$items['admin/people/roles'] = array(
2010-02-07 17:29:09 +00:00
'title' => 'Roles',
'description' => 'List, edit, or add user roles.',
2013-05-17 17:11:57 +00:00
'route_name' => 'user_role_list',
2010-02-07 17:29:09 +00:00
'type' => MENU_LOCAL_TASK,
);
2013-05-10 10:23:45 +00:00
$items['admin/people/roles/add'] = array(
'title' => 'Add role',
2013-05-17 17:11:57 +00:00
'route_name' => 'user_role_add',
2013-05-10 10:23:45 +00:00
'type' => MENU_LOCAL_ACTION,
);
$items['admin/people/roles/manage/%user_role'] = array(
2010-02-07 17:29:09 +00:00
'title' => 'Edit role',
2013-05-17 17:11:57 +00:00
'route_name' => 'user_role_edit',
2010-02-07 17:29:09 +00:00
);
2013-05-10 10:23:45 +00:00
$items['admin/people/roles/manage/%user_role/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/people/roles/manage/%user_role/delete'] = array(
2010-03-26 22:24:31 +00:00
'title' => 'Delete role',
2013-05-17 17:11:57 +00:00
'route_name' => 'user_role_delete',
2013-05-10 10:23:45 +00:00
'weight' => 10,
'context' => MENU_CONTEXT_INLINE,
2010-03-26 22:24:31 +00:00
);
2010-02-07 17:29:09 +00:00
2009-08-11 11:47:58 +00:00
$items['admin/people/create'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add user',
2013-06-13 20:57:33 +00:00
'route_name' => 'user_admin_create',
2009-08-23 01:05:12 +00:00
'type' => MENU_LOCAL_ACTION,
2007-01-24 14:48:36 +00:00
);
2013-06-06 19:10:20 +00:00
$items['admin/people/cancel'] = array(
'title' => 'Cancel user',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_multiple_cancel_confirm'),
'access arguments' => array('administer users'),
'file' => 'user.admin.inc',
2013-06-13 20:57:33 +00:00
'type' => MENU_CALLBACK,
2013-06-06 19:10:20 +00:00
);
2009-08-21 14:27:47 +00:00
// Administration pages.
$items['admin/config/people'] = array(
2010-02-07 17:29:09 +00:00
'title' => 'People',
2010-02-17 08:54:51 +00:00
'description' => 'Configure user accounts.',
2010-03-30 07:17:19 +00:00
'position' => 'left',
'weight' => -20,
2009-08-21 14:27:47 +00:00
'page callback' => 'system_admin_menu_block_page',
2009-08-28 06:42:54 +00:00
'access arguments' => array('access administration pages'),
2009-08-29 04:55:32 +00:00
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
2009-08-21 14:27:47 +00:00
);
$items['admin/config/people/accounts'] = array(
'title' => 'Account settings',
2012-11-26 10:38:45 +00:00
'description' => 'Configure default behavior of users, including registration requirements, e-mails, and fields.',
2009-08-21 14:27:47 +00:00
'weight' => -10,
2013-04-10 23:35:47 +00:00
'route_name' => 'user_account_settings',
2007-01-24 14:48:36 +00:00
);
2009-08-21 14:27:47 +00:00
$items['admin/config/people/accounts/settings'] = array(
2009-08-19 13:31:14 +00:00
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
2007-01-24 14:48:36 +00:00
2010-10-08 05:28:30 +00:00
$items['user/%user'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'My account',
2007-12-27 14:03:37 +00:00
'title callback' => 'user_page_title',
'title arguments' => array(1),
2010-01-14 13:45:33 +00:00
'page callback' => 'user_view_page',
2007-01-24 14:48:36 +00:00
'page arguments' => array(1),
2013-01-24 20:04:49 +00:00
'access callback' => 'entity_page_access',
2007-01-24 14:48:36 +00:00
'access arguments' => array(1),
);
2007-02-11 09:30:51 +00:00
$items['user/%user/view'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'View',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
);
2009-01-08 08:42:13 +00:00
$items['user/%user/cancel'] = array(
'title' => 'Cancel account',
2007-08-16 13:07:31 +00:00
'page callback' => 'drupal_get_form',
2009-01-08 08:42:13 +00:00
'page arguments' => array('user_cancel_confirm_form', 1),
2013-01-24 20:04:49 +00:00
'access callback' => 'entity_page_access',
'access arguments' => array(1, 'delete'),
2009-08-24 00:14:23 +00:00
'file' => 'user.pages.inc',
2009-01-08 08:42:13 +00:00
);
$items['user/%user/cancel/confirm/%/%'] = array(
'title' => 'Confirm account cancellation',
'page callback' => 'user_cancel_confirm',
'page arguments' => array(1, 4, 5),
2013-01-24 20:04:49 +00:00
'access callback' => 'entity_page_access',
'access arguments' => array(1, 'delete'),
2009-08-24 00:14:23 +00:00
'file' => 'user.pages.inc',
2007-01-24 14:48:36 +00:00
);
2008-09-08 15:44:57 +00:00
$items['user/%user/edit'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit',
2012-08-16 11:30:43 +00:00
'page callback' => 'entity_get_form',
2013-06-27 21:24:39 +00:00
'page arguments' => array(1),
2013-01-24 20:04:49 +00:00
'access callback' => 'entity_page_access',
'access arguments' => array(1, 'update'),
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
2009-08-24 00:14:23 +00:00
'file' => 'user.pages.inc',
2007-01-24 14:48:36 +00:00
);
2004-06-18 15:04:37 +00:00
return $items;
2002-01-11 21:00:37 +00:00
}
2010-10-08 05:28:30 +00:00
/**
2013-02-08 23:55:25 +00:00
* Implements hook_menu_link_presave().
2010-10-08 05:28:30 +00:00
*/
2013-03-04 15:28:39 +00:00
function user_menu_link_presave(MenuLink $menu_link) {
2010-10-23 03:20:40 +00:00
// The path 'user' must be accessible for anonymous users, but only visible
// for authenticated users. Authenticated users should see "My account", but
// anonymous users should not see it at all. Therefore, invoke
2013-02-08 23:55:25 +00:00
// user_menu_link_load() to conditionally hide the link.
2013-03-04 15:28:39 +00:00
if ($menu_link->link_path == 'user' && $menu_link->module == 'system') {
$menu_link->options['alter'] = TRUE;
2010-10-23 03:20:40 +00:00
}
2012-10-25 15:53:18 +00:00
// Force the Logout link to appear on the top-level of 'account' menu by
2010-10-08 05:28:30 +00:00
// default (i.e., unless it has been customized).
2013-03-04 15:28:39 +00:00
if ($menu_link->link_path == 'user/logout' && $menu_link->module == 'system' && empty($menu_link->customized)) {
$menu_link->plid = 0;
2010-10-08 05:28:30 +00:00
}
}
2012-08-01 17:39:02 +00:00
/**
* Implements hook_menu_breadcrumb_alter().
*/
function user_menu_breadcrumb_alter(&$active_trail, $item) {
// Remove "My account" from the breadcrumb when $item is descendant-or-self
// of system path user/%.
if (isset($active_trail[1]['module']) && $active_trail[1]['module'] == 'system' && $active_trail[1]['link_path'] == 'user' && strpos($item['path'], 'user/%') === 0) {
array_splice($active_trail, 1, 1);
}
}
2010-10-23 03:20:40 +00:00
/**
2013-02-08 23:55:25 +00:00
* Implements hook_menu_link_load().
2010-10-23 03:20:40 +00:00
*/
2013-02-08 23:55:25 +00:00
function user_menu_link_load($menu_links) {
2010-10-23 03:20:40 +00:00
// Hide the "User account" link for anonymous users.
2013-02-08 23:55:25 +00:00
foreach ($menu_links as $link) {
2013-07-09 13:46:44 +00:00
if ($link['link_path'] == 'user' && $link['module'] == 'system' && !$GLOBALS['user']->id()) {
2013-02-08 23:55:25 +00:00
$link['hidden'] = 1;
}
2010-10-23 03:20:40 +00:00
}
}
2010-08-03 23:07:01 +00:00
/**
* Implements hook_admin_paths().
*/
function user_admin_paths() {
$paths = array(
'user/*/cancel' => TRUE,
'user/*/edit' => TRUE,
'user/*/edit/*' => TRUE,
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
'user/*/translations' => TRUE,
'user/*/translations/*' => TRUE,
2010-08-03 23:07:01 +00:00
);
return $paths;
}
2010-10-23 03:20:40 +00:00
/**
* Returns $arg or the user ID of the current user if $arg is '%' or empty.
*
* Deprecated. Use %user_uid_optional instead.
*
* @todo D8: Remove.
*/
function user_uid_only_optional_to_arg($arg) {
return user_uid_optional_to_arg($arg);
}
2009-10-10 19:54:15 +00:00
/**
2009-11-07 14:27:20 +00:00
* Load either a specified or the current user account.
2009-10-10 19:54:15 +00:00
*
* @param $uid
* An optional user ID of the user to load. If not provided, the current
* user's ID will be used.
* @return
2013-07-01 00:09:20 +00:00
* A fully-loaded $user object upon successful user load, NULL if user
2009-10-10 19:54:15 +00:00
* cannot be loaded.
*
* @see user_load()
2010-01-14 13:45:33 +00:00
* @todo rethink the naming of this in Drupal 8.
2009-10-10 19:54:15 +00:00
*/
function user_uid_optional_load($uid = NULL) {
if (!isset($uid)) {
2013-07-09 13:46:44 +00:00
$uid = $GLOBALS['user']->id();
2009-10-10 19:54:15 +00:00
}
return user_load($uid);
2007-02-11 09:30:51 +00:00
}
2007-11-06 08:51:23 +00:00
/**
2010-01-14 13:45:33 +00:00
* Returns $arg or the user ID of the current user if $arg is '%' or empty.
*
* @todo rethink the naming of this in Drupal 8.
2007-11-06 08:51:23 +00:00
*/
2008-04-23 20:01:56 +00:00
function user_uid_optional_to_arg($arg) {
2007-11-06 08:51:23 +00:00
// Give back the current user uid when called from eg. tracker, aka.
// with an empty arg. Also use the current user uid when called from
// the menu with a % for the current account link.
2013-07-09 13:46:44 +00:00
return empty($arg) || $arg == '%' ? $GLOBALS['user']->id() : $arg;
2007-02-11 09:30:51 +00:00
}
2010-01-14 13:45:33 +00:00
/**
2010-10-08 05:28:30 +00:00
* Menu item title callback for the 'user' path.
2010-01-14 13:45:33 +00:00
*
2012-09-27 11:04:02 +00:00
* Anonymous users should see a title based on the requested page, but
* authenticated users are expected to see "My account".
2010-01-14 13:45:33 +00:00
*/
2010-10-08 05:28:30 +00:00
function user_menu_title() {
2013-07-09 13:46:44 +00:00
if ($GLOBALS['user']->isAnonymous()) {
2012-09-27 11:04:02 +00:00
switch (current_path()) {
case 'user' :
case 'user/login' :
return t('Log in');
case 'user/register' :
return t('Create new account');
case 'user/password' :
return t('Request new password');
default :
return t('User account');
}
}
else {
return t('My account');
}
2010-01-14 13:45:33 +00:00
}
2007-12-27 14:03:37 +00:00
/**
2009-09-10 12:33:46 +00:00
* Menu item title callback - use the user name.
2007-12-27 14:03:37 +00:00
*/
2013-07-09 13:46:44 +00:00
function user_page_title(UserInterface $account = NULL) {
return $account ? $account->getUsername() : '';
2007-12-27 14:03:37 +00:00
}
2007-06-15 06:53:26 +00:00
/**
2009-08-08 20:52:33 +00:00
* Try to validate the user's login credentials locally.
2007-12-13 12:53:47 +00:00
*
2009-08-08 20:52:33 +00:00
* @param $name
* User name to authenticate.
* @param $password
* A plain-text password, such as trimmed text from form values.
* @return
* The user's uid on success, or FALSE on failure to authenticate.
2007-09-10 13:14:38 +00:00
*/
2009-08-08 20:52:33 +00:00
function user_authenticate($name, $password) {
$uid = FALSE;
if (!empty($name) && !empty($password)) {
2010-12-18 00:56:18 +00:00
$account = user_load_by_name($name);
2008-03-31 20:50:05 +00:00
if ($account) {
2012-11-24 08:12:43 +00:00
$password_hasher = drupal_container()->get('password');
if ($password_hasher->check($password, $account)) {
2009-08-08 20:52:33 +00:00
// Successful authentication.
2013-07-09 13:46:44 +00:00
$uid = $account->id();
2009-07-21 07:27:00 +00:00
2009-06-30 11:32:08 +00:00
// Update user to new password scheme if needed.
2012-11-24 08:12:43 +00:00
if ($password_hasher->userNeedsNewHash($account)) {
2013-07-09 13:46:44 +00:00
$account->setPassword($password);
2012-04-26 03:51:09 +00:00
$account->save();
2008-03-31 20:50:05 +00:00
}
}
}
2005-11-21 09:17:04 +00:00
}
2009-08-08 20:52:33 +00:00
return $uid;
2005-11-21 09:17:04 +00:00
}
2007-12-13 12:53:47 +00:00
/**
2013-06-27 08:23:39 +00:00
* Finalizes the login process and logs in a user.
2007-12-13 12:53:47 +00:00
*
2013-06-27 08:23:39 +00:00
* The function logs in the user, records a watchdog message about the new
* session, saves the login timestamp, calls hook_user_login(), and generates a
* new session.
2013-05-03 16:07:05 +00:00
*
2013-06-27 08:23:39 +00:00
* The global $user object is replaced with the passed in account.
*
2013-07-09 13:46:44 +00:00
* @param \Drupal\user\UserInterface $account
2013-06-27 08:23:39 +00:00
* The account to log in.
2013-05-03 16:07:05 +00:00
*
* @see hook_user_login()
2007-12-13 12:53:47 +00:00
*/
2013-07-09 13:46:44 +00:00
function user_login_finalize(UserInterface $account) {
2007-12-13 12:53:47 +00:00
global $user;
2013-06-27 08:23:39 +00:00
$user = $account;
2013-07-09 13:46:44 +00:00
watchdog('user', 'Session opened for %name.', array('%name' => $user->getUsername()));
2007-12-13 12:53:47 +00:00
// Update the user table timestamp noting user has logged in.
// This is also used to invalidate one-time login links.
2013-07-09 13:46:44 +00:00
$account->setLastLoginTime(REQUEST_TIME);
2009-04-13 12:14:57 +00:00
db_update('users')
2013-07-09 13:46:44 +00:00
->fields(array('login' => $user->getLastLoginTime()))
->condition('uid', $user->id())
2009-04-13 12:14:57 +00:00
->execute();
2009-06-02 06:58:17 +00:00
2008-11-24 06:12:46 +00:00
// Regenerate the session ID to prevent against session fixation attacks.
// This is called before hook_user in case one of those functions fails
// or incorrectly does a redirect which would leave the old session in place.
2008-09-15 15:18:59 +00:00
drupal_session_regenerate();
2009-06-02 06:58:17 +00:00
2013-01-07 11:11:52 +00:00
module_invoke_all('user_login', $user);
2007-12-13 12:53:47 +00:00
}
2012-11-11 23:52:37 +00:00
/**
* Implements hook_user_login().
*/
2013-01-07 11:11:52 +00:00
function user_user_login($account) {
2012-11-11 23:52:37 +00:00
// Reset static cache of default variables in template_preprocess() to reflect
// the new user.
drupal_static_reset('template_preprocess');
}
/**
* Implements hook_user_logout().
*/
function user_user_logout($account) {
// Reset static cache of default variables in template_preprocess() to reflect
// the new user.
drupal_static_reset('template_preprocess');
}
2010-02-17 03:49:02 +00:00
/**
* Generates a unique URL for a user to login and reset their password.
*
* @param object $account
2013-05-21 19:17:10 +00:00
* An object containing the user account, which must contain at least the
* following properties:
* - uid: The user ID number.
* - login: The UNIX timestamp of the user's last login.
2012-08-26 17:45:51 +00:00
* @param array $options
* (optional) A keyed array of settings. Supported options are:
* - langcode: A language code to be used when generating locale-sensitive
2012-09-02 04:50:06 +00:00
* URLs. If langcode is NULL the users preferred language is used.
2010-02-17 03:49:02 +00:00
*
* @return
* A unique URL that provides a one-time log in for the user, from which
* they can change their password.
*/
2012-08-26 17:45:51 +00:00
function user_pass_reset_url($account, $options = array()) {
2008-09-17 07:11:59 +00:00
$timestamp = REQUEST_TIME;
2013-07-09 13:46:44 +00:00
$langcode = isset($options['langcode']) ? $options['langcode'] : $account->getPreferredLangcode();
2012-09-13 08:48:24 +00:00
$url_options = array('absolute' => TRUE, 'language' => language_load($langcode));
2013-07-09 13:46:44 +00:00
return url("user/reset/" . $account->id() . "/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime()), $url_options);
2005-04-24 11:43:08 +00:00
}
2009-01-08 08:42:13 +00:00
/**
2011-06-27 06:01:42 +00:00
* Generates a URL to confirm an account cancellation request.
*
* @param object $account
* The user account object, which must contain at least the following
* properties:
2013-05-21 19:17:10 +00:00
* - uid: The user ID number.
2011-06-27 06:01:42 +00:00
* - pass: The hashed user password string.
2013-05-21 19:17:10 +00:00
* - login: The UNIX timestamp of the user's last login.
2012-08-26 17:45:51 +00:00
* @param array $options
* (optional) A keyed array of settings. Supported options are:
* - langcode: A language code to be used when generating locale-sensitive
2012-09-02 04:50:06 +00:00
* URLs. If langcode is NULL the users preferred language is used.
2011-06-27 06:01:42 +00:00
*
* @return
* A unique URL that may be used to confirm the cancellation of the user
* account.
2009-01-08 08:42:13 +00:00
*
* @see user_mail_tokens()
* @see user_cancel_confirm()
*/
2012-08-26 17:45:51 +00:00
function user_cancel_url($account, $options = array()) {
2009-01-08 08:42:13 +00:00
$timestamp = REQUEST_TIME;
2013-07-09 13:46:44 +00:00
$langcode = isset($options['langcode']) ? $options['langcode'] : $account->getPreferredLangcode();
2012-09-13 08:48:24 +00:00
$url_options = array('absolute' => TRUE, 'language' => language_load($langcode));
2013-07-09 13:46:44 +00:00
return url("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account->getPassword(), $timestamp, $account->login), $url_options);
2009-01-08 08:42:13 +00:00
}
2011-06-30 06:47:22 +00:00
/**
* Creates a unique hash value for use in time-dependent per-user URLs.
*
* This hash is normally used to build a unique and secure URL that is sent to
* the user by email for purposes such as resetting the user's password. In
* order to validate the URL, the same hash can be generated again, from the
* same information, and compared to the hash value from the URL. The URL
* normally contains both the time stamp and the numeric user ID. The login
2013-05-21 19:17:10 +00:00
* timestamp and hashed password are retrieved from the database as necessary.
* For a usage example, see user_cancel_url() and user_cancel_confirm().
2011-06-30 06:47:22 +00:00
*
2013-05-21 19:17:10 +00:00
* @param string $password
2011-06-30 06:47:22 +00:00
* The hashed user account password value.
2013-05-21 19:17:10 +00:00
* @param int $timestamp
* A UNIX timestamp, typically REQUEST_TIME.
* @param int $login
* The UNIX timestamp of the user's last login.
2011-06-30 06:47:22 +00:00
*
* @return
* A string that is safe for use in URLs and SQL statements.
*/
2005-10-22 15:14:46 +00:00
function user_pass_rehash($password, $timestamp, $login) {
2013-05-02 04:46:53 +00:00
return Crypt::hmacBase64($timestamp . $login, drupal_get_hash_salt() . $password);
2005-04-24 11:43:08 +00:00
}
2007-08-16 13:07:31 +00:00
/**
2009-01-08 08:42:13 +00:00
* Cancel a user account.
*
* Since the user cancellation process needs to be run in a batch, either
* Form API will invoke it, or batch_process() needs to be invoked after calling
* this function and should define the path to redirect to.
*
* @param $edit
* An array of submitted form values.
* @param $uid
* The user ID of the user account to cancel.
* @param $method
* The account cancellation method to use.
2007-08-16 13:07:31 +00:00
*
2009-01-08 08:42:13 +00:00
* @see _user_cancel()
2007-08-16 13:07:31 +00:00
*/
2009-01-08 08:42:13 +00:00
function user_cancel($edit, $uid, $method) {
global $user;
2009-03-14 23:01:38 +00:00
$account = user_load($uid);
2009-01-08 08:42:13 +00:00
if (!$account) {
drupal_set_message(t('The user account %id does not exist.', array('%id' => $uid)), 'error');
2011-07-04 16:58:33 +00:00
watchdog('user', 'Attempted to cancel non-existing user account: %id.', array('%id' => $uid), WATCHDOG_ERROR);
2009-01-08 08:42:13 +00:00
return;
}
// Initialize batch (to set title).
$batch = array(
'title' => t('Cancelling account'),
'operations' => array(),
);
batch_set($batch);
2011-12-17 12:43:37 +00:00
// When the 'user_cancel_delete' method is used, user_delete() is called,
// which invokes hook_user_predelete() and hook_user_delete(). Modules
// should use those hooks to respond to the account deletion.
2010-03-12 15:56:30 +00:00
if ($method != 'user_cancel_delete') {
// Allow modules to add further sets to this batch.
module_invoke_all('user_cancel', $edit, $account, $method);
}
2009-01-08 08:42:13 +00:00
// Finish the batch and actually cancel the account.
$batch = array(
'title' => t('Cancelling user account'),
'operations' => array(
array('_user_cancel', array($edit, $account, $method)),
),
);
2013-04-29 10:50:44 +00:00
// After cancelling account, ensure that user is logged out.
2013-07-09 13:46:44 +00:00
if ($account->id() == $user->id()) {
2013-04-29 10:50:44 +00:00
// Batch API stores data in the session, so use the finished operation to
// manipulate the current user's session id.
$batch['finished'] = '_user_cancel_session_regenerate';
}
2009-01-08 08:42:13 +00:00
batch_set($batch);
// Batch processing is either handled via Form API or has to be invoked
// manually.
}
/**
* Last batch processing step for cancelling a user account.
*
* Since batch and session API require a valid user account, the actual
* cancellation of a user account needs to happen last.
*
* @see user_cancel()
*/
function _user_cancel($edit, $account, $method) {
global $user;
switch ($method) {
case 'user_cancel_block':
case 'user_cancel_block_unpublish':
default:
// Send account blocked notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_blocked', $account);
}
2012-04-26 03:51:09 +00:00
$account->status = 0;
$account->save();
2009-01-08 08:42:13 +00:00
drupal_set_message(t('%name has been disabled.', array('%name' => $account->name)));
2011-07-04 16:58:33 +00:00
watchdog('user', 'Blocked user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
2009-01-08 08:42:13 +00:00
break;
case 'user_cancel_reassign':
case 'user_cancel_delete':
// Send account canceled notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_canceled', $account);
}
2013-07-09 13:46:44 +00:00
$account->delete();
2009-01-08 08:42:13 +00:00
drupal_set_message(t('%name has been deleted.', array('%name' => $account->name)));
2011-07-04 16:58:33 +00:00
watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
2009-01-08 08:42:13 +00:00
break;
}
2013-04-29 10:50:44 +00:00
// After cancelling account, ensure that user is logged out. We can't destroy
// their session though, as we might have information in it, and we can't
// regenerate it because batch API uses the session ID, we will regenerate it
// in _user_cancel_session_regenerate().
2013-07-09 13:46:44 +00:00
if ($account->id() == $user->id()) {
2013-04-29 10:50:44 +00:00
$user = drupal_anonymous_user();
2009-01-08 08:42:13 +00:00
}
// Clear the cache for anonymous users.
2012-11-28 21:36:29 +00:00
cache_invalidate_tags(array('content' => TRUE));
2007-08-16 13:07:31 +00:00
}
2013-04-29 10:50:44 +00:00
/**
* Finished batch processing callback for cancelling a user account.
*
* @see user_cancel()
*/
function _user_cancel_session_regenerate() {
// Regenerate the users session instead of calling session_destroy() as we
// want to preserve any messages that might have been set.
drupal_session_regenerate();
}
2010-03-12 15:56:30 +00:00
/**
* Delete a user.
*
* @param $uid
* A user ID.
*/
function user_delete($uid) {
user_delete_multiple(array($uid));
}
/**
* Delete multiple user accounts.
*
* @param $uids
* An array of user IDs.
2011-12-17 12:43:37 +00:00
*
* @see hook_user_predelete()
* @see hook_user_delete()
2010-03-12 15:56:30 +00:00
*/
function user_delete_multiple(array $uids) {
2012-10-31 23:19:29 +00:00
entity_delete_multiple('user', $uids);
2010-03-12 15:56:30 +00:00
}
2010-01-14 13:45:33 +00:00
/**
* Page callback wrapper for user_view().
*/
2010-10-08 05:28:30 +00:00
function user_view_page($account) {
2012-04-16 19:26:30 +00:00
if (is_object($account)) {
return user_view($account);
}
2010-01-14 13:45:33 +00:00
// An administrator may try to view a non-existent account,
// so we give them a 404 (versus a 403 for non-admins).
2012-06-02 19:41:40 +00:00
throw new NotFoundHttpException();
2010-01-14 13:45:33 +00:00
}
2007-06-21 04:38:41 +00:00
/**
2009-09-10 12:33:46 +00:00
* Generate an array for rendering the given user.
*
* When viewing a user profile, the $page array contains:
*
2011-11-03 07:34:18 +00:00
* - $page['content']['member_for']:
* Contains the default "Member for" profile data for a user.
2012-12-13 12:11:18 +00:00
* - $page['content']['#user']:
2009-09-10 12:33:46 +00:00
* The user account of the profile being viewed.
*
2012-12-13 12:11:18 +00:00
* To theme user profiles, copy modules/user/user.tpl.php
2009-10-09 08:13:17 +00:00
* to your theme directory, and edit it as instructed in that file's comments.
2007-06-21 04:38:41 +00:00
*
* @param $account
* A user object.
2009-12-26 16:50:09 +00:00
* @param $view_mode
* View mode, e.g. 'full'.
2010-10-03 01:15:34 +00:00
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
2007-06-21 04:38:41 +00:00
*
* @return
2009-09-10 12:33:46 +00:00
* An array as expected by drupal_render().
*/
2010-10-03 01:15:34 +00:00
function user_view($account, $view_mode = 'full', $langcode = NULL) {
2012-10-14 06:40:03 +00:00
return entity_view($account, $view_mode, $langcode);
2009-09-10 12:33:46 +00:00
}
/**
2012-10-14 06:40:03 +00:00
* Constructs a drupal_render() style array from an array of loaded users.
2009-09-10 12:33:46 +00:00
*
2012-10-14 06:40:03 +00:00
* @param $accounts
* An array of user accounts as returned by user_load_multiple().
2009-12-26 16:50:09 +00:00
* @param $view_mode
2012-10-14 06:40:03 +00:00
* (optional) View mode, e.g., 'full', 'teaser'... Defaults to 'teaser.'
2010-10-03 01:15:34 +00:00
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
2012-10-14 06:40:03 +00:00
*
* @return
* An array in the format expected by drupal_render().
2007-06-21 04:38:41 +00:00
*/
2012-10-14 06:40:03 +00:00
function user_view_multiple($accounts, $view_mode = 'full', $langcode = NULL) {
return entity_view($accounts, $view_mode, $langcode);
2002-01-11 21:00:37 +00:00
}
2007-07-01 19:49:19 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_mail().
2007-07-01 19:49:19 +00:00
*/
function user_mail($key, &$message, $params) {
2013-04-18 07:24:35 +00:00
$token_service = Drupal::token();
2012-09-13 08:48:24 +00:00
$langcode = $message['langcode'];
2009-10-02 14:49:10 +00:00
$variables = array('user' => $params['account']);
2002-01-11 21:00:37 +00:00
2013-02-27 09:49:26 +00:00
// Get configuration objects customized for the user specified in $params as
// this user is not necessarily the same as the one triggering the mail. This
// allows the configuration objects to be localized for the user's language if
// the locale module is enabled.
2013-03-08 17:37:37 +00:00
$user_config_context = config_context_enter('Drupal\user\UserConfigContext');
2013-02-27 09:49:26 +00:00
$user_config_context->setAccount($params['account']);
$mail_config = config('user.mail');
// We do not sanitize the token replacement, since the output of this
// replacement is intended for an e-mail message, not a web browser.
$token_options = array('langcode' => $langcode, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE);
2013-04-18 07:24:35 +00:00
$message['subject'] .= $token_service->replace($mail_config->get($key . '.subject'), $variables, $token_options);
$message['body'][] = $token_service->replace($mail_config->get($key . '.body'), $variables, $token_options);
2013-02-27 09:49:26 +00:00
// Return the previous config context.
config_context_leave();
2009-10-02 14:49:10 +00:00
}
/**
* Token callback to add unsafe tokens for user mails.
*
2013-04-18 07:24:35 +00:00
* This function is used by \Drupal\Core\Utility\Token::replace() to set up
* some additional tokens that can be used in email messages generated by
* user_mail().
2011-06-27 06:14:10 +00:00
*
* @param $replacements
* An associative array variable containing mappings from token names to
* values (for use with strtr()).
* @param $data
* An associative array of token replacement values. If the 'user' element
* exists, it must contain a user account object with the following
* properties:
2013-05-21 19:17:10 +00:00
* - login: The UNIX timestamp of the user's last login.
2011-06-27 06:14:10 +00:00
* - pass: The hashed account login password.
* @param $options
2013-04-18 07:24:35 +00:00
* Unused parameter required by \Drupal\Core\Utility\Token::replace().
2009-10-02 14:49:10 +00:00
*/
function user_mail_tokens(&$replacements, $data, $options) {
if (isset($data['user'])) {
2012-08-26 17:45:51 +00:00
$replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user'], $options);
$replacements['[user:cancel-url]'] = user_cancel_url($data['user'], $options);
2005-01-28 20:49:00 +00:00
}
}
2007-09-10 13:14:38 +00:00
/*** Administrative features ***********************************************/
2006-04-26 11:07:25 +00:00
/**
2006-05-02 13:26:30 +00:00
* Retrieve an array of roles matching specified conditions.
*
* @param $membersonly
* Set this to TRUE to exclude the 'anonymous' role.
* @param $permission
2008-02-03 19:23:01 +00:00
* A string containing a permission. If set, only roles containing that
* permission are returned.
2006-05-02 13:26:30 +00:00
*
* @return
2008-02-03 19:23:01 +00:00
* An associative array with the role id as the key and the role name as
* value.
2006-05-02 13:26:30 +00:00
*/
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
function user_role_names($membersonly = FALSE, $permission = NULL) {
2013-06-24 16:38:32 +00:00
return array_map(function ($item) {
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
return $item->label();
}, user_roles($membersonly, $permission));
}
2013-06-06 19:10:20 +00:00
/**
* Implements hook_user_role_insert().
*/
function user_user_role_insert(RoleInterface $role) {
// Ignore the authenticated and anonymous roles.
if (in_array($role->id(), array(DRUPAL_AUTHENTICATED_RID, DRUPAL_ANONYMOUS_RID))) {
return;
}
$action = entity_create('action', array(
'id' => 'user_add_role_action.' . $role->id(),
'type' => 'user',
'label' => t('Add the @label role to the selected users', array('@label' => $role->label())),
'configuration' => array(
'rid' => $role->id(),
),
'plugin' => 'user_add_role_action',
));
$action->save();
$action = entity_create('action', array(
'id' => 'user_remove_role_action.' . $role->id(),
'type' => 'user',
'label' => t('Remove the @label role from the selected users', array('@label' => $role->label())),
'configuration' => array(
'rid' => $role->id(),
),
'plugin' => 'user_remove_role_action',
));
$action->save();
}
/**
* Implements hook_user_role_delete().
*/
function user_user_role_delete(RoleInterface $role) {
// Ignore the authenticated and anonymous roles.
if (in_array($role->id(), array(DRUPAL_AUTHENTICATED_RID, DRUPAL_ANONYMOUS_RID))) {
return;
}
$actions = entity_load_multiple('action', array(
'user_add_role_action.' . $role->id(),
'user_remove_role_action.' . $role->id(),
));
foreach ($actions as $action) {
$action->delete();
}
}
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
/**
* Retrieve an array of roles matching specified conditions.
*
* @param $membersonly
* Set this to TRUE to exclude the 'anonymous' role.
* @param $permission
* A string containing a permission. If set, only roles containing that
* permission are returned.
*
* @return
* An associative array with the role id as the key and the role object as
* value.
*/
2008-01-10 20:22:57 +00:00
function user_roles($membersonly = FALSE, $permission = NULL) {
2011-09-28 02:31:34 +00:00
$user_roles = &drupal_static(__FUNCTION__);
// Do not cache roles for specific permissions. This data is not requested
// frequently enough to justify the additional memory use.
if (empty($permission)) {
$cid = $membersonly ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
if (isset($user_roles[$cid])) {
return $user_roles[$cid];
}
}
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
$roles = entity_load_multiple('user_role');
Revert "Issue #1479454 by Hugo Wetterberg, galooph, dawehner, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun: Change notice: Convert user roles to configurables."
Broke testbot. :(
This reverts commit 2ae1597227b962f5563944fdc30b17a18692a217.
2013-01-19 07:46:12 +00:00
if ($membersonly) {
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
unset($roles[DRUPAL_ANONYMOUS_RID]);
}
if (!empty($permission)) {
2013-06-24 16:38:32 +00:00
$roles = array_filter($roles, function ($role) use ($permission) {
return $role->hasPermission($permission);
});
2002-01-11 21:00:37 +00:00
}
2008-01-10 16:00:44 +00:00
2011-09-28 02:31:34 +00:00
if (empty($permission)) {
$user_roles[$cid] = $roles;
}
2010-03-24 07:34:10 +00:00
return $roles;
2002-01-11 21:00:37 +00:00
}
2009-08-27 20:25:29 +00:00
/**
2010-03-23 19:22:58 +00:00
* Fetches a user role by role ID.
*
* @param $rid
2012-06-05 12:19:14 +00:00
* A string representing the role ID.
2010-03-23 19:22:58 +00:00
*
* @return
2013-07-01 00:09:20 +00:00
* A fully-loaded role object if a role with the given ID exists, or NULL
2010-03-23 19:22:58 +00:00
* otherwise.
*/
function user_role_load($rid) {
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
return entity_load('user_role', $rid);
2009-08-27 20:25:29 +00:00
}
2010-02-05 21:07:56 +00:00
/**
* Determine the modules that permissions belong to.
*
* @return
* An associative array in the format $permission => $module.
*/
function user_permission_get_modules() {
$permissions = array();
foreach (module_implements('permission') as $module) {
$perms = module_invoke($module, 'permission');
foreach ($perms as $key => $value) {
$permissions[$key] = $module;
}
}
return $permissions;
}
2009-08-27 20:25:29 +00:00
/**
2009-09-19 10:54:36 +00:00
* Change permissions for a user role.
2009-08-27 20:25:29 +00:00
*
2009-09-19 10:54:36 +00:00
* This function may be used to grant and revoke multiple permissions at once.
* For example, when a form exposes checkboxes to configure permissions for a
2010-10-20 00:42:42 +00:00
* role, the form submit handler may directly pass the submitted values for the
* checkboxes form element to this function.
2009-09-19 10:54:36 +00:00
*
* @param $rid
* The ID of a user role to alter.
2009-08-27 20:25:29 +00:00
* @param $permissions
2010-10-20 00:42:42 +00:00
* An associative array, where the key holds the permission name and the value
* determines whether to grant or revoke that permission. Any value that
* evaluates to TRUE will cause the permission to be granted. Any value that
* evaluates to FALSE will cause the permission to be revoked.
2009-09-19 10:54:36 +00:00
* @code
* array(
2010-10-20 00:42:42 +00:00
* 'administer nodes' => 0, // Revoke 'administer nodes'
* 'administer blocks' => FALSE, // Revoke 'administer blocks'
* 'access user profiles' => 1, // Grant 'access user profiles'
* 'access content' => TRUE, // Grant 'access content'
* 'access comments' => 'access comments', // Grant 'access comments'
2009-09-19 10:54:36 +00:00
* )
* @endcode
* Existing permissions are not changed, unless specified in $permissions.
*
* @see user_role_grant_permissions()
* @see user_role_revoke_permissions()
2009-08-27 20:25:29 +00:00
*/
2009-09-19 10:54:36 +00:00
function user_role_change_permissions($rid, array $permissions = array()) {
// Grant new permissions for the role.
$grant = array_filter($permissions);
if (!empty($grant)) {
user_role_grant_permissions($rid, array_keys($grant));
2009-08-27 20:25:29 +00:00
}
2009-09-19 10:54:36 +00:00
// Revoke permissions for the role.
$revoke = array_diff_assoc($permissions, $grant);
if (!empty($revoke)) {
user_role_revoke_permissions($rid, array_keys($revoke));
}
}
2009-08-27 20:25:29 +00:00
2009-09-19 10:54:36 +00:00
/**
* Grant permissions to a user role.
*
* @param $rid
* The ID of a user role to alter.
* @param $permissions
* A list of permission names to grant.
*
* @see user_role_change_permissions()
* @see user_role_revoke_permissions()
*/
function user_role_grant_permissions($rid, array $permissions = array()) {
// Grant new permissions for the role.
2013-06-24 16:38:32 +00:00
$role = entity_load('user_role', $rid);
foreach ($permissions as $permission) {
$role->grantPermission($permission);
2009-08-27 20:25:29 +00:00
}
2013-06-24 16:38:32 +00:00
$role->save();
2009-08-27 20:25:29 +00:00
// Clear the user access cache.
2009-09-11 06:20:49 +00:00
drupal_static_reset('user_access');
2009-09-19 10:54:36 +00:00
}
/**
* Revoke permissions from a user role.
*
* @param $rid
* The ID of a user role to alter.
* @param $permissions
* A list of permission names to revoke.
*
* @see user_role_change_permissions()
* @see user_role_grant_permissions()
*/
function user_role_revoke_permissions($rid, array $permissions = array()) {
// Revoke permissions for the role.
2013-06-24 16:38:32 +00:00
$role = entity_load('user_role', $rid);
foreach ($permissions as $permission) {
$role->revokePermission($permission);
}
$role->save();
2009-09-19 10:54:36 +00:00
// Clear the user access cache.
drupal_static_reset('user_access');
2009-08-27 20:25:29 +00:00
}
2009-09-18 00:12:48 +00:00
function user_multiple_cancel_confirm($form, &$form_state) {
2013-06-06 19:10:20 +00:00
// Retrieve the accounts to be canceled from the temp store.
2013-07-09 13:46:44 +00:00
$accounts = Drupal::service('user.tempstore')->get('user_user_operations_cancel')->get($GLOBALS['user']->id());
2006-08-05 18:56:35 +00:00
$form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
2013-06-06 19:10:20 +00:00
foreach ($accounts as $account) {
$uid = $account->id();
2010-01-14 19:21:55 +00:00
// Prevent user 1 from being canceled.
if ($uid <= 1) {
continue;
}
$form['accounts'][$uid] = array(
'#type' => 'hidden',
'#value' => $uid,
'#prefix' => '<li>',
2013-06-06 19:10:20 +00:00
'#suffix' => check_plain($account->name->value) . "</li>\n",
2010-01-14 19:21:55 +00:00
);
}
// Output a notice that user 1 cannot be canceled.
if (isset($accounts[1])) {
$redirect = (count($accounts) == 1);
2013-06-06 19:10:20 +00:00
$message = t('The user account %name cannot be cancelled.', array('%name' => $accounts[1]->name->value));
2010-01-14 19:21:55 +00:00
drupal_set_message($message, $redirect ? 'error' : 'warning');
// If only user 1 was selected, redirect to the overview.
if ($redirect) {
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
return new RedirectResponse(url('admin/people', array('absolute' => TRUE)));
2010-01-14 19:21:55 +00:00
}
2006-08-05 18:56:35 +00:00
}
2009-01-08 08:42:13 +00:00
$form['operation'] = array('#type' => 'hidden', '#value' => 'cancel');
2012-12-02 15:33:15 +00:00
form_load_include($form_state, 'inc', 'user', 'user.pages');
2009-01-08 08:42:13 +00:00
$form['user_cancel_method'] = array(
2012-12-02 15:33:15 +00:00
'#type' => 'radios',
2009-01-08 08:42:13 +00:00
'#title' => t('When cancelling these accounts'),
);
$form['user_cancel_method'] += user_cancel_methods();
// Allow to send the account cancellation confirmation mail.
$form['user_cancel_confirm'] = array(
'#type' => 'checkbox',
'#title' => t('Require e-mail confirmation to cancel account.'),
'#default_value' => FALSE,
'#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
);
// Also allow to send account canceled notification mail, if enabled.
$form['user_cancel_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify user when account is canceled.'),
'#default_value' => FALSE,
2012-08-26 21:15:58 +00:00
'#access' => config('user.settings')->get('notify.status_canceled'),
2009-01-08 08:42:13 +00:00
'#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
);
2006-08-05 18:56:35 +00:00
2006-08-18 18:58:47 +00:00
return confirm_form($form,
2009-01-08 08:42:13 +00:00
t('Are you sure you want to cancel these user accounts?'),
2009-08-11 11:47:58 +00:00
'admin/people', t('This action cannot be undone.'),
2009-01-08 08:42:13 +00:00
t('Cancel accounts'), t('Cancel'));
2006-08-05 18:56:35 +00:00
}
2009-01-08 08:42:13 +00:00
/**
* Submit handler for mass-account cancellation form.
*
* @see user_multiple_cancel_confirm()
* @see user_cancel_confirm_form_submit()
*/
function user_multiple_cancel_confirm_submit($form, &$form_state) {
global $user;
2013-06-06 19:10:20 +00:00
// Clear out the accounts from the temp store.
2013-07-09 13:46:44 +00:00
Drupal::service('user.tempstore')->get('user_user_operations_cancel')->delete($user->id());
2007-06-04 07:22:23 +00:00
if ($form_state['values']['confirm']) {
2007-09-10 13:14:38 +00:00
foreach ($form_state['values']['accounts'] as $uid => $value) {
2010-01-14 19:21:55 +00:00
// Prevent programmatic form submissions from cancelling user 1.
if ($uid <= 1) {
continue;
}
2009-01-08 08:42:13 +00:00
// Prevent user administrators from deleting themselves without confirmation.
2013-07-09 13:46:44 +00:00
if ($uid == $user->id()) {
2009-01-08 08:42:13 +00:00
$admin_form_state = $form_state;
unset($admin_form_state['values']['user_cancel_confirm']);
2012-04-26 03:51:09 +00:00
// The $user global is not a complete user entity, so load the full
// entity.
2013-07-09 13:46:44 +00:00
$admin_form_state['values']['_account'] = user_load($user->id());
2009-01-08 08:42:13 +00:00
user_cancel_confirm_form_submit(array(), $admin_form_state);
}
else {
user_cancel($form_state['values'], $uid, $form_state['values']['user_cancel_method']);
}
2007-09-10 13:14:38 +00:00
}
2002-01-11 21:00:37 +00:00
}
2009-08-11 11:47:58 +00:00
$form_state['redirect'] = 'admin/people';
2002-01-11 21:00:37 +00:00
}
2004-05-24 18:37:50 +00:00
2007-04-09 13:41:10 +00:00
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a user signature.
*
* @param $variables
* An associative array containing:
* - signature: The user's signature.
2007-04-09 13:41:10 +00:00
*
* @ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_user_signature($variables) {
$signature = $variables['signature'];
2007-04-09 13:41:10 +00:00
$output = '';
2009-10-09 01:00:08 +00:00
2007-04-09 13:41:10 +00:00
if ($signature) {
$output .= '<div class="clear">';
2007-07-04 21:42:39 +00:00
$output .= '<div>—</div>';
2007-04-09 13:41:10 +00:00
$output .= $signature;
$output .= '</div>';
}
return $output;
}
2007-05-20 12:34:48 +00:00
/**
* Conditionally create and send a notification email when a certain
* operation happens on the given user account.
*
* @see user_mail_tokens()
* @see drupal_mail()
*
* @param $op
2012-01-31 08:12:04 +00:00
* The operation being performed on the account. Possible values:
* - 'register_admin_created': Welcome message for user created by the admin.
* - 'register_no_approval_required': Welcome message when user
* self-registers.
* - 'register_pending_approval': Welcome message, user pending admin
* approval.
* - 'password_reset': Password recovery request.
* - 'status_activated': Account activated.
* - 'status_blocked': Account blocked.
* - 'cancel_confirm': Account cancellation request.
* - 'status_canceled': Account canceled.
2007-05-20 12:34:48 +00:00
*
* @param $account
2012-01-31 08:12:04 +00:00
* The user object of the account being notified. Must contain at
* least the fields 'uid', 'name', and 'mail'.
2012-09-13 08:48:24 +00:00
* @param $langcode
* Optional language code to use for the notification, overriding account
* language.
2012-01-31 08:12:04 +00:00
*
2007-05-20 12:34:48 +00:00
* @return
2012-01-31 08:12:04 +00:00
* The return value from drupal_mail_system()->mail(), if ends up being
* called.
2007-05-20 12:34:48 +00:00
*/
2012-09-13 08:48:24 +00:00
function _user_mail_notify($op, $account, $langcode = NULL) {
2009-01-08 08:42:13 +00:00
// By default, we always notify except for canceled and blocked.
2012-08-26 21:15:58 +00:00
$notify = config('user.settings')->get('notify.' . $op);
if ($notify || ($op != 'status_canceled' && $op != 'status_blocked')) {
2007-07-01 19:49:19 +00:00
$params['account'] = $account;
2013-07-09 13:46:44 +00:00
$langcode = $langcode ? $langcode : $account->getPreferredLangcode();
2013-02-19 08:30:38 +00:00
// Get the custom site notification email to use as the from email address
// if it has been set.
$site_mail = config('system.site')->get('mail_notification');
// If the custom site notification email has not been set, we use the site
// default for this.
if (empty($site_mail)) {
$site_mail = config('system.site')->get('mail');
}
if (empty($site_mail)) {
$site_mail = ini_get('sendmail_from');
}
$mail = drupal_mail('user', $op, $account->mail, $langcode, $params, $site_mail);
2007-05-20 12:34:48 +00:00
if ($op == 'register_pending_approval') {
// If a user registered requiring admin approval, notify the admin, too.
2007-07-01 19:49:19 +00:00
// We use the site default language for this.
2013-06-29 10:56:53 +00:00
drupal_mail('user', 'register_pending_approval_admin', $site_mail, language_default()->id, $params);
2007-05-20 12:34:48 +00:00
}
}
2007-07-01 19:49:19 +00:00
return empty($mail) ? NULL : $mail['result'];
2007-05-20 12:34:48 +00:00
}
2007-06-08 06:04:15 +00:00
/**
2009-11-13 10:21:50 +00:00
* Form element process handler for client-side password validation.
2007-06-08 06:04:15 +00:00
*
2009-11-13 10:21:50 +00:00
* This #process handler is automatically invoked for 'password_confirm' form
* elements to add the JavaScript and string translations for dynamic password
* validation.
*
* @see system_element_info()
2007-06-08 06:04:15 +00:00
*/
2009-11-13 10:21:50 +00:00
function user_form_process_password_confirm($element) {
2013-02-16 17:36:42 +00:00
$password_settings = array(
'confirmTitle' => t('Passwords match:'),
'confirmSuccess' => t('yes'),
'confirmFailure' => t('no'),
'showStrengthIndicator' => FALSE,
);
2009-11-13 10:21:50 +00:00
2013-02-16 17:36:42 +00:00
if (config('user.settings')->get('password_strength')) {
global $user;
$password_settings['showStrengthIndicator'] = TRUE;
$password_settings += array(
2009-11-13 10:21:50 +00:00
'strengthTitle' => t('Password strength:'),
'hasWeaknesses' => t('To make your password stronger:'),
'tooShort' => t('Make it at least 6 characters'),
'addLowerCase' => t('Add lowercase letters'),
'addUpperCase' => t('Add uppercase letters'),
'addNumbers' => t('Add numbers'),
'addPunctuation' => t('Add punctuation'),
'sameAsUsername' => t('Make it different from your username'),
'weak' => t('Weak'),
'fair' => t('Fair'),
'good' => t('Good'),
'strong' => t('Strong'),
'username' => (isset($user->name) ? $user->name : ''),
2013-02-16 17:36:42 +00:00
);
}
$js_settings = array(
'password' => $password_settings,
2009-11-13 10:21:50 +00:00
);
2012-08-30 19:24:38 +00:00
$element['#attached']['library'][] = array('user', 'drupal.user');
2010-07-20 10:17:59 +00:00
// Ensure settings are only added once per page.
static $already_added = FALSE;
if (!$already_added) {
$already_added = TRUE;
$element['#attached']['js'][] = array('data' => $js_settings, 'type' => 'setting');
}
2009-11-13 10:21:50 +00:00
return $element;
2007-06-08 06:04:15 +00:00
}
2007-06-29 18:06:51 +00:00
2009-05-30 12:14:27 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_load().
2012-11-26 10:38:45 +00:00
*
* @todo Deprecated by $node->author, attached in
* \Drupal\node\NodeRenderController::buildContent(). Update code that
* depends on these properties.
2009-05-30 12:14:27 +00:00
*/
function user_node_load($nodes, $types) {
// Build an array of all uids for node authors, keyed by nid.
$uids = array();
foreach ($nodes as $nid => $node) {
$uids[$nid] = $node->uid;
}
2012-11-26 10:38:45 +00:00
// Fetch name and data for these users.
2012-11-27 22:26:22 +00:00
$user_names = db_query("SELECT uid, name FROM {users} WHERE uid IN (:uids)", array(':uids' => $uids))->fetchAllKeyed();
2009-05-30 12:14:27 +00:00
// Add these values back into the node objects.
foreach ($uids as $nid => $uid) {
2012-11-27 22:26:22 +00:00
$nodes[$nid]->name = $user_names[$uid];
2009-05-30 12:14:27 +00:00
}
}
2009-08-24 00:14:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_modules_installed().
2009-08-24 00:14:23 +00:00
*/
function user_modules_installed($modules) {
// Assign all available permissions to the administrator role.
2012-08-26 21:15:58 +00:00
$rid = config('user.settings')->get('admin_role');
2009-08-24 00:14:23 +00:00
if ($rid) {
2010-01-03 20:29:00 +00:00
$permissions = array();
2009-08-24 00:14:23 +00:00
foreach ($modules as $module) {
2010-01-03 20:29:00 +00:00
if ($module_permissions = module_invoke($module, 'permission')) {
$permissions = array_merge($permissions, array_keys($module_permissions));
2009-08-24 00:14:23 +00:00
}
}
2010-01-03 20:29:00 +00:00
if (!empty($permissions)) {
user_role_grant_permissions($rid, $permissions);
}
2009-08-24 00:14:23 +00:00
}
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_modules_uninstalled().
2009-08-24 00:14:23 +00:00
*/
function user_modules_uninstalled($modules) {
2012-11-27 22:26:22 +00:00
// Remove any potentially orphan module data stored for users.
drupal_container()->get('user.data')->delete($modules);
2009-08-24 00:14:23 +00:00
}
2009-09-17 03:16:16 +00:00
2009-10-09 15:39:12 +00:00
/**
* Saves visitor information as a cookie so it can be reused.
*
* @param $values
2010-04-07 16:35:03 +00:00
* An array of key/value pairs to be saved into a cookie.
*/
function user_cookie_save(array $values) {
foreach ($values as $field => $value) {
// Set cookie for 365 days.
setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, '/');
2009-10-09 15:39:12 +00:00
}
}
2009-10-19 18:28:16 +00:00
2010-03-22 18:55:45 +00:00
/**
* Delete a visitor information cookie.
*
* @param $cookie_name
* A cookie name such as 'homepage'.
*/
function user_cookie_delete($cookie_name) {
setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, '/');
}
2010-08-23 14:53:50 +00:00
/**
* Implements hook_file_download_access().
*/
2012-08-21 15:19:09 +00:00
function user_file_download_access($field, EntityInterface $entity, File $file) {
if ($entity->entityType() == 'user') {
2013-01-24 20:04:49 +00:00
return $entity->access('view');
2010-08-23 14:53:50 +00:00
}
}
2012-08-30 19:24:38 +00:00
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
/**
* Implements hook_toolbar().
*/
function user_toolbar() {
global $user;
// Add logout & user account links or login link.
2013-07-09 13:46:44 +00:00
if ($user->isAuthenticated()) {
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
$links = array(
'account' => array(
'title' => t('View profile'),
'href' => 'user',
'html' => TRUE,
'attributes' => array(
'title' => t('User account'),
),
),
2013-04-21 19:40:45 +00:00
'account_edit' => array(
'title' => t('Edit profile'),
2013-07-09 13:46:44 +00:00
'href' => 'user/' . $user->id() . '/edit',
2013-04-21 19:40:45 +00:00
'html' => TRUE,
'attributes' => array(
'title' => t('Edit user account'),
),
),
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
'logout' => array(
'title' => t('Log out'),
'href' => 'user/logout',
),
);
}
else {
$links = array(
'login' => array(
'title' => t('Log in'),
'href' => 'user',
),
);
}
$items['user'] = array(
2013-02-01 16:31:47 +00:00
'#type' => 'toolbar_item',
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
'tab' => array(
2013-02-01 16:31:47 +00:00
'#type' => 'link',
2013-07-09 13:46:44 +00:00
'#title' => $user->getUsername(),
2013-02-01 16:31:47 +00:00
'#href' => 'user',
'#options' => array(
'attributes' => array(
'title' => t('My account'),
2013-07-18 10:28:54 +00:00
'class' => array('toolbar-icon', 'toolbar-icon-user'),
2013-02-01 16:31:47 +00:00
),
),
),
'tray' => array(
'#heading' => t('User account actions'),
'user_links' => array(
'#theme' => 'links__toolbar_user',
'#links' => $links,
'#attributes' => array(
'class' => array('menu'),
),
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
),
),
2013-02-01 16:31:47 +00:00
'#weight' => 100,
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
);
return $items;
}
2012-08-30 19:24:38 +00:00
/**
* Implements hook_library_info().
*/
function user_library_info() {
$libraries['drupal.user'] = array(
'title' => 'User',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'user') . '/user.js' => array(),
),
2013-01-19 01:27:33 +00:00
'css' => array(
2013-06-07 10:48:55 +00:00
drupal_get_path('module', 'user') . '/css/user.module.css' => array(),
2013-01-19 01:27:33 +00:00
),
2012-08-30 19:24:38 +00:00
'dependencies' => array(
array('system', 'jquery'),
array('system', 'drupal'),
array('system', 'jquery.once'),
),
);
$libraries['drupal.user.permissions'] = array(
'title' => 'User permissions',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'user') . '/user.permissions.js' => array(),
),
'dependencies' => array(
array('system', 'jquery'),
array('system', 'drupal'),
2012-09-24 13:12:09 +00:00
array('system', 'drupalSettings'),
2012-08-30 19:24:38 +00:00
),
);
return $libraries;
}
2013-06-13 22:53:50 +00:00
/**
* Logs the current user out.
*/
function user_logout() {
global $user;
watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
Drupal::moduleHandler()->invokeAll('user_logout', array($user));
// Destroy the current session, and reset $user to the anonymous user.
session_destroy();
}