IdentityInterface.php 2.98 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
 * IdentityInterface is the interface that should be implemented by a class providing identity information.
12 13 14 15 16
 *
 * 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:
 *
 * ~~~
17
 * class User extends ActiveRecord implements IdentityInterface
18 19 20
 * {
 *     public static function findIdentity($id)
 *     {
Alexander Makarov committed
21
 *         return static::findOne($id);
22 23
 *     }
 *
24 25 26 27 28
 *     public static function findIdentityByAccessToken($token)
 *     {
 *         return static::findOne(['access_token' => $token]);
 *     }
 *
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *     public function getId()
 *     {
 *         return $this->id;
 *     }
 *
 *     public function getAuthKey()
 *     {
 *         return $this->authKey;
 *     }
 *
 *     public function validateAuthKey($authKey)
 *     {
 *         return $this->authKey === $authKey;
 *     }
 * }
 * ~~~
Qiang Xue committed
45 46 47 48
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
49
interface IdentityInterface
Qiang Xue committed
50
{
51 52
    /**
     * Finds an identity by the given ID.
53
     * @param string|integer $id the ID to be looked for
54
     * @return IdentityInterface the identity object that matches the given ID.
55 56
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
57 58 59 60
     */
    public static function findIdentity($id);
    /**
     * Finds an identity by the given secrete token.
61
     * @param string $token the secrete token
62
     * @return IdentityInterface the identity object that matches the given token.
63 64
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
     */
    public static function findIdentityByAccessToken($token);
    /**
     * Returns an ID that can uniquely identify a user identity.
     * @return string|integer an ID that uniquely identifies a user identity.
     */
    public function getId();
    /**
     * Returns a key that can be used to check the validity of a given identity ID.
     *
     * 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.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @return string a key that is used to check the validity of a given identity ID.
     * @see validateAuthKey()
     */
    public function getAuthKey();
    /**
     * Validates the given auth key.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
89
     * @param string $authKey the given auth key
90 91 92 93
     * @return boolean whether the given auth key is valid.
     * @see getAuthKey()
     */
    public function validateAuthKey($authKey);
Zander Baldwin committed
94
}