Commit 8a403411 authored by shajiaiming's avatar shajiaiming

验证token

parent a4d4a964
......@@ -8,27 +8,32 @@
namespace common\behaviors;
use Yii;
use common\models\Admin;
use api\base\ResponseMsg;
use common\components\Response;
use yii\base\ActionFilter;
use Yii;
class LoginStatusAuthInterceptor extends ActionFilter
{
public function beforeAction($action)
{
return true;
$auth_string = Yii::$app->request->headers->get('Authorization');
if (strstr($auth_string, 'Bearer')) {
return true;
#return $this->frontAuth();
$token_string = Yii::$app->request->headers->get('access_token');
if(false == $token_string){
$message = 'platform auth error';
$code = '40004';
goto doEnd;
}
if (!$auth_string) {
$message = 'cms auth error';
$model = new Admin();
$user = $model->loginByAccessToken($token_string,'');
if(false == $user){
$message = 'user auth error';
$code = '40004';
goto doEnd;
}
return true;
doEnd :
// 返回错误
$response_message = new ResponseMsg();
......
......@@ -36,14 +36,6 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* 根据用户名获取账号信息
*
* @param string $username
......@@ -159,6 +151,28 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
$user = static::find()->where(['access_token' => $token, 'status' => self::STATUS_ACTIVE])->one();
if (!$user) {
return false;
}
// if ($user->expire_at < time()) {
// throw new UnauthorizedHttpException('the access - token expired ', -1);
// } else {
// return $user;
// }
return $user;
}
public function loginByAccessToken($accessToken, $type) {
return static::findIdentityByAccessToken($accessToken, $type);
}
/**
* Generate accessToken string
* @return string
* @throws \yii\base\Exception
......
......@@ -11,6 +11,7 @@ class LoginForm extends Model
{
public $username;
public $password;
public $token;
public $rememberMe = true;
private $_user;
......@@ -89,4 +90,18 @@ class LoginForm extends Model
return $this->_user;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getToken()
{
if ($this->_user === null) {
$this->_user = Admin::findIdentityByAccessToken($this->token);
}
return $this->_user;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment