add api authentication
This commit is contained in:
@ -3,10 +3,10 @@
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\web\IdentityInterface;
|
||||
use yii\web\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* User model
|
||||
@ -64,14 +64,25 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function findIdentityByAccessToken($token, $type = null)
|
||||
public function generateAccessToken()
|
||||
{
|
||||
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
|
||||
$this->access_token = Yii::$app->security->generateRandomString();
|
||||
return $this->access_token;
|
||||
}
|
||||
|
||||
public static function findIdentityByAccessToken($token, $type = null)
|
||||
{
|
||||
$user = static::find()->where(['access_token' => $token, 'status' => self::STATUS_ACTIVE])->one();
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
if (strtotime($user->access_token_expired_at) < time()) {
|
||||
throw new UnauthorizedHttpException('the access - token expired ', -1);
|
||||
} else {
|
||||
return $user;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Finds user by username
|
||||
*
|
||||
@ -186,4 +197,16 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
{
|
||||
$this->password_reset_token = null;
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (parent::beforeSave($insert)) {
|
||||
if ($this->isNewRecord) {
|
||||
$this->auth_key = Yii::$app->security->generateRandomString();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user