Interface yii\web\IdentityInterface
自版本起可用 | 2.0 |
---|---|
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/web/IdentityInterface.php |
IdentityInterface 介面應由提供身分資訊的類別實作。
此介面通常可由使用者模型類別實作。例如,以下程式碼示範如何由 User ActiveRecord 類別實作此介面
class User extends ActiveRecord implements IdentityInterface
{
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
}
在某些情況下,並非所有這些方法都需要實作。例如,如果您的應用程式是純粹無狀態的 RESTful 應用程式,您只需要實作 findIdentityByAccessToken() 和 getId(),而將所有其他方法留空即可。或者,如果您的應用程式僅使用 session 驗證,您則需要實作除了 findIdentityByAccessToken() 之外的所有方法。
公開方法
方法 | 描述 | 定義於 |
---|---|---|
findIdentity() | 透過給定的 ID 尋找身分。 | yii\web\IdentityInterface |
findIdentityByAccessToken() | 透過給定的權杖尋找身分。 | yii\web\IdentityInterface |
getAuthKey() | 返回可用於檢查給定身分 ID 有效性的金鑰。 | yii\web\IdentityInterface |
getId() | 返回可以唯一識別使用者身分的 ID。 | yii\web\IdentityInterface |
validateAuthKey() | 驗證給定的授權金鑰。 | yii\web\IdentityInterface |
方法詳情
透過給定的 ID 尋找身分。
public abstract static yii\web\IdentityInterface|null findIdentity ( $id ) | ||
$id | string|integer |
要尋找的 ID |
return | yii\web\IdentityInterface|null |
符合給定 ID 的身分物件。如果找不到此身分或身分未處於活動狀態(停用、刪除等),則應返回 Null。 |
---|
public static function findIdentity($id);
透過給定的權杖尋找身分。
public abstract static yii\web\IdentityInterface|null findIdentityByAccessToken ( $token, $type = null ) | ||
$token | mixed |
要尋找的權杖 |
$type | mixed |
權杖的類型。此參數的值取決於實作。例如,yii\filters\auth\HttpBearerAuth 會將此參數設定為 |
return | yii\web\IdentityInterface|null |
符合給定權杖的身分物件。如果找不到此身分或身分未處於活動狀態(停用、刪除等),則應返回 Null。 |
---|
public static function findIdentityByAccessToken($token, $type = null);
返回可用於檢查給定身分 ID 有效性的金鑰。
金鑰對於每個個別使用者應是唯一的,並且應是持久的,以便可以用於檢查使用者身分的有效性。
此類金鑰的空間應足夠大,以擊敗潛在的身分攻擊。
返回的金鑰用於驗證 session 和自動登入(如果啟用 yii\web\User::$enableAutoLogin)。
當您實作強制使用者登出、密碼變更和其他需要強制撤銷舊 session 存取權限的情境時,請務必使先前發行的 authKeys 失效。
另請參閱 validateAuthKey()。
public abstract string|null getAuthKey ( ) | ||
return | string|null |
用於檢查給定身分 ID 有效性的金鑰。 |
---|
public function getAuthKey();
返回可以唯一識別使用者身分的 ID。
public abstract string|integer getId ( ) | ||
return | string|integer |
唯一識別使用者身分的 ID。 |
---|
public function getId();
驗證給定的授權金鑰。
另請參閱 getAuthKey()。
public abstract boolean|null validateAuthKey ( $authKey ) | ||
$authKey | string |
給定的授權金鑰 |
return | boolean|null |
給定的授權金鑰是否有效。 |
---|
public function validateAuthKey($authKey);
註冊 或 登入 以發表評論。