Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wallet
token
Commits
8a403411
Commit
8a403411
authored
Jun 24, 2019
by
shajiaiming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
验证token
parent
a4d4a964
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
16 deletions
+50
-16
LoginStatusAuthInterceptor.php
common/behaviors/LoginStatusAuthInterceptor.php
+13
-8
Admin.php
common/models/Admin.php
+22
-8
LoginForm.php
common/models/LoginForm.php
+15
-0
No files found.
common/behaviors/LoginStatusAuthInterceptor.php
View file @
8a403411
...
@@ -8,27 +8,32 @@
...
@@ -8,27 +8,32 @@
namespace
common\behaviors
;
namespace
common\behaviors
;
use
Yii
;
use
common\models\Admin
;
use
api\base\ResponseMsg
;
use
api\base\ResponseMsg
;
use
common\components\Response
;
use
common\components\Response
;
use
yii\base\ActionFilter
;
use
yii\base\ActionFilter
;
use
Yii
;
class
LoginStatusAuthInterceptor
extends
ActionFilter
class
LoginStatusAuthInterceptor
extends
ActionFilter
{
{
public
function
beforeAction
(
$action
)
public
function
beforeAction
(
$action
)
{
{
return
true
;
$token_string
=
Yii
::
$app
->
request
->
headers
->
get
(
'access_token'
)
;
$auth_string
=
Yii
::
$app
->
request
->
headers
->
get
(
'Authorization'
);
if
(
false
==
$token_string
){
if
(
strstr
(
$auth_string
,
'Bearer'
))
{
$message
=
'platform auth error'
;
return
true
;
$code
=
'40004'
;
#return $this->frontAuth()
;
goto
doEnd
;
}
}
if
(
!
$auth_string
)
{
$model
=
new
Admin
();
$message
=
'cms auth error'
;
$user
=
$model
->
loginByAccessToken
(
$token_string
,
''
);
if
(
false
==
$user
){
$message
=
'user auth error'
;
$code
=
'40004'
;
$code
=
'40004'
;
goto
doEnd
;
goto
doEnd
;
}
}
return
true
;
doEnd
:
doEnd
:
// 返回错误
// 返回错误
$response_message
=
new
ResponseMsg
();
$response_message
=
new
ResponseMsg
();
...
...
common/models/Admin.php
View file @
8a403411
...
@@ -36,14 +36,6 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
...
@@ -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
* @param string $username
...
@@ -159,6 +151,28 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
...
@@ -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
* Generate accessToken string
* @return string
* @return string
* @throws \yii\base\Exception
* @throws \yii\base\Exception
...
...
common/models/LoginForm.php
View file @
8a403411
...
@@ -11,6 +11,7 @@ class LoginForm extends Model
...
@@ -11,6 +11,7 @@ class LoginForm extends Model
{
{
public
$username
;
public
$username
;
public
$password
;
public
$password
;
public
$token
;
public
$rememberMe
=
true
;
public
$rememberMe
=
true
;
private
$_user
;
private
$_user
;
...
@@ -89,4 +90,18 @@ class LoginForm extends Model
...
@@ -89,4 +90,18 @@ class LoginForm extends Model
return
$this
->
_user
;
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
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment