Identity.php 2.3 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

/**
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * Identity is the interface that should be implemented by a class providing identity information.
 *
 * This interface can typically be implemented by a user model class. For example, the following
 * code shows how to implement this interface by a User ActiveRecord class:
 *
 * ~~~
 * class User extends ActiveRecord implements Identity
 * {
 *     public static function findIdentity($id)
 *     {
 *         return static::find($id);
 *     }
 *
 *     public function getId()
 *     {
 *         return $this->id;
 *     }
 *
 *     public function getAuthKey()
 *     {
 *         return $this->authKey;
 *     }
 *
 *     public function validateAuthKey($authKey)
 *     {
 *         return $this->authKey === $authKey;
 *     }
 * }
 * ~~~
Qiang Xue committed
40 41 42 43 44 45
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
interface Identity
{
Qiang Xue committed
46 47 48 49 50 51 52 53
	/**
	 * Finds an identity by the given ID.
	 * @param string|integer $id the ID to be looked for
	 * @return Identity the identity object that matches the given ID.
	 * Null should be returned if such an identity cannot be found
	 * or the identity is not in an active state (disabled, deleted, etc.)
	 */
	public static function findIdentity($id);
Qiang Xue committed
54 55
	/**
	 * Returns an ID that can uniquely identify a user identity.
56
	 * @return string|integer an ID that uniquely identifies a user identity.
Qiang Xue committed
57 58 59 60
	 */
	public function getId();
	/**
	 * Returns a key that can be used to check the validity of a given identity ID.
61 62 63 64 65
	 *
	 * The key should be unique for each individual user, and should be persistent
	 * so that it can be used to check the validity of the user identity.
	 *
	 * The space of such keys should be big enough to defeat potential identity attacks.
Qiang Xue committed
66 67
	 *
	 * This is required if [[User::enableAutoLogin]] is enabled.
68
	 * @return string a key that is used to check the validity of a given identity ID.
Qiang Xue committed
69 70 71 72 73
	 * @see validateAuthKey()
	 */
	public function getAuthKey();
	/**
	 * Validates the given auth key.
Qiang Xue committed
74 75
	 *
	 * This is required if [[User::enableAutoLogin]] is enabled.
Qiang Xue committed
76 77 78 79 80
	 * @param string $authKey the given auth key
	 * @return boolean whether the given auth key is valid.
	 * @see getAuthKey()
	 */
	public function validateAuthKey($authKey);
Zander Baldwin committed
81
}