Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

C H A P T E R 6

■ ■ ■

Working with Users

Users are the reason for using Drupal. Drupal can help users create, collaborate, communicate, and form an online community. In this chapter, we look behind the scenes and see how users are authenticated, logged in, and represented internally. We start with an examination of what the $user object is and how it’s constructed. Then we walk through the process of user registration, user login, and user authentication. We finish by examining how Drupal ties in with external authentication systems such as Lightweight Directory Access Protocol (LDAP) and Pubcookie.

The $user Object

Drupal requires that the user have cookies enabled in order to log in; a user with cookies turned off can still interact with Drupal as an anonymous user.

During the session phase of the bootstrap process, Drupal creates a global $user object that represents the identity of the current user. If the user is not logged in (and so does not have a session cookie), then he or she is treated as an anonymous user. The code that creates an anonymous user looks like this (and lives in includes/bootstrap.inc):

function drupal_anonymous_user($session = '') { $user = new stdClass();

$user->uid = 0; $user->hostname = ip_address(); $user->roles = array();

$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; $user->session = $session;

$user->cache = 0; return $user;

}

On the other hand, if the user is currently logged in, the $user object is created by joining the users table, roles, and sessions tables on the user’s ID. Values of all fields in both tables are placed into the $user object.

115

CHAPTER 6 WORKING WITH USERS

Note The user’s ID is an integer that is assigned when the user registers or the user account is created by the administrator. This ID is the primary key of the users table.

The $user object is easily inspected by adding global $user; print_r($user); to index.php. The following is what a $user object generally looks like for a logged-in user:

stdClass Object (

 

 

 

[uid] => 1

 

 

 

[name] =>

admin

 

 

[pass] =>

$S$CnUvfOYdoxl/Usy.X/Y9/SCmOLLY6Qldrzjf7EOW0fR4LG7rCAmR

[mail] =>

joe@example.com

 

 

[theme] =>

 

 

 

[signature] =>

 

 

[signature_format] => 0

 

 

[created]

=> 1277957059

 

 

[access] => 1278254230

 

 

[login] => 1277990573

 

 

[status] => 1

 

 

[timezone] =>

 

 

[language] =>

 

 

[picture]

=> 0

 

 

[init] =>

joe@example.com

 

 

[data] =>

 

 

 

[sid] => 8cnG9e0jsCC7I7IYwfWB0rmRozIbaLlk35IQGN5fz9k

[ssid] =>

 

 

 

[hostname] => ::1

 

 

[timestamp] => 1278254231

 

 

[cache] => 0

 

 

[session]

=> batches|a:1:{i:3;b:1;}

 

[roles] => Array

(

 

[2] => authenticated user

 

[3] => administrator

)

}

 

 

 

In the $user object just displayed, italicized field names denote that the origin of the data is the sessions table. The components of the $user object are explained in Table 6-1.

116

CHAPTER 6 WORKING WITH USERS

Table 6-1. Components of the $user Object

Component

Description

 

 

Provided by the users Table

 

uid

The user ID of this user. This is the primary key of the users table and

 

is unique to this Drupal installation.

name

The user’s username, typed by the user when logging in.

pass

An sha512 hash of the user’s password, which is compared when the

 

user logs in. Since the actual passwords aren’t saved, they can only be

 

reset and not restored.

mail

The user’s current e-mail address.

theme

This field is deprecated but left in the object for compatibility

 

purposes.

signature

The signature the user entered on his or her account page. Used

 

when the user adds a comment and only visible when the comment

 

module is enabled.

Signature format

The format of the users signature (e.g., filtered text, full text)

created

A Unix timestamp of when this user account was created.

access

A Unix timestamp denoting the user’s last access time.

login

A Unix timestamp denoting the user’s last successful login.

status

Contains 1 if the user is in good standing or 0 if the user has been

 

blocked.

timezone

The number of seconds that the user’s time zone is offset from GMT.

language

The user’s default language. Empty unless multiple languages are

 

enabled on a site and the user has chosen a language by editing

 

account preferences.

picture

The path to the image file the user has associated with the account.

init

The initial e-mail address the user provided when registering.

data

Arbitrary data can be stored here by modules (see the next section,

 

“Storing Data in the $user Object”).

117

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]