Skip to content

Commit 33d0f67

Browse files
author
Bui Sy Nguyen
committed
[Done] Support AuthLogoutAction and AuthRevokeAction
1 parent f236e86 commit 33d0f67

File tree

5 files changed

+179
-27
lines changed

5 files changed

+179
-27
lines changed

fproject/authclient/AuthAction.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@
2626

2727
class AuthAction extends \yii\authclient\AuthAction
2828
{
29-
/**
30-
* @inheritdoc
31-
*/
32-
public function init()
33-
{
34-
parent::init();
35-
$this->successCallback = [$this, 'onAuthSuccess'];
36-
}
37-
3829
/**
3930
* @inheritdoc
4031
*/
@@ -65,11 +56,26 @@ public function run()
6556
throw new NotFoundHttpException();
6657
}
6758

59+
/** @inheritdoc */
60+
protected function authSuccess($client)
61+
{
62+
if (!is_callable($this->successCallback))
63+
{
64+
$this->successCallback = [$this, 'onAuthSuccess'];
65+
parent::authSuccess($client);
66+
}
67+
else
68+
{
69+
/** @var OAuth2 $client */
70+
$this->onAuthSuccess($client);
71+
parent::authSuccess($client);
72+
}
73+
}
6874

6975
/**
7076
* @param OAuth2 $client
7177
*/
72-
public function onAuthSuccess($client)
78+
protected function onAuthSuccess($client)
7379
{
7480
$attributes = $client->getUserAttributes();
7581
$identity = new UserIdentity($attributes);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
///////////////////////////////////////////////////////////////////////////////
3+
//
4+
// © Copyright f-project.net 2010-present. All Rights Reserved.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
///////////////////////////////////////////////////////////////////////////////
19+
20+
namespace fproject\authclient;
21+
use Yii;
22+
use yii\authclient\Collection;
23+
24+
class AuthLogoutAction extends AuthLogoutActionBase
25+
{
26+
/** @var bool */
27+
public $globalLogout = true;
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function run()
33+
{
34+
parent::run();
35+
36+
$b = $this->client->logout($this->globalLogout);
37+
38+
if (is_callable($this->afterActionCallback))
39+
{
40+
call_user_func($this->afterActionCallback, $this->client);
41+
}
42+
43+
return $b;
44+
}
45+
46+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Bui
5+
* Date: 9/30/2015
6+
* Time: 1:36 PM
7+
*/
8+
9+
namespace fproject\authclient;
10+
11+
use Yii;
12+
use yii\authclient\Collection;
13+
use yii\base\Action;
14+
15+
abstract class AuthLogoutActionBase extends Action
16+
{
17+
/**
18+
* @var callable PHP callback, which should be triggered before this action is run.
19+
* This callback should accept [[ClientInterface]] instance as an argument.
20+
* For example:
21+
*
22+
* ~~~
23+
* public function onBeforeLogout($client)
24+
* {
25+
* $attributes = $client->getUserAttributes();
26+
* // user login or signup comes here
27+
* }
28+
* ~~~
29+
*
30+
* If this callback returns [[Response]] instance, it will be used as action response,
31+
* otherwise redirection to [[successUrl]] will be performed.
32+
*
33+
*/
34+
public $beforeActionCallback;
35+
36+
/**
37+
* @var callable PHP callback, which should be triggered after this action is run.
38+
* This callback should accept [[ClientInterface]] instance as an argument.
39+
* For example:
40+
*
41+
* ~~~
42+
* public function onAfterLogout($client)
43+
* {
44+
* $attributes = $client->getUserAttributes();
45+
* // user login or signup comes here
46+
* }
47+
* ~~~
48+
*
49+
* If this callback returns [[Response]] instance, it will be used as action response,
50+
* otherwise redirection to [[successUrl]] will be performed.
51+
*
52+
*/
53+
public $afterActionCallback;
54+
55+
/**
56+
* @var string $clientCollection name of the auth client collection application component.
57+
* It should point to [[Collection]] instance.
58+
*/
59+
public $clientCollection = 'authClientCollection';
60+
61+
/** @var string $authClientId the auth client ID*/
62+
public $authClientId;
63+
64+
/** @var OAuth2 $client */
65+
protected $client;
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function run()
71+
{
72+
/** @var Collection $collection */
73+
$collection = Yii::$app->get($this->clientCollection);
74+
$this->client = $collection->getClient($this->authClientId);
75+
if (is_callable($this->beforeActionCallback))
76+
{
77+
call_user_func($this->beforeActionCallback, $this->client);
78+
}
79+
}
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
///////////////////////////////////////////////////////////////////////////////
3+
//
4+
// © Copyright f-project.net 2010-present. All Rights Reserved.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
///////////////////////////////////////////////////////////////////////////////
19+
20+
namespace fproject\authclient;
21+
use Yii;
22+
use yii\authclient\Collection;
23+
24+
class AuthRevokeAction extends AuthLogoutActionBase
25+
{
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function run()
30+
{
31+
/** @var Collection $collection */
32+
$collection = Yii::$app->get($this->clientCollection);
33+
/** @var OAuth2 $client */
34+
$client = $collection->getClient($this->authClientId);
35+
}
36+
37+
}

fproject/authclient/OAuth2.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,6 @@ public function buildAuthUrl(array $params = [])
7373
return parent::buildAuthUrl($params);
7474
}
7575

76-
77-
/**
78-
* @inheritdoc
79-
*/
80-
protected function defaultName()
81-
{
82-
return 'project-kit';
83-
}
84-
85-
/**
86-
* @inheritdoc
87-
*/
88-
protected function defaultTitle()
89-
{
90-
return 'ProjectKit';
91-
}
92-
9376
/**
9477
* @inheritdoc
9578
*/

0 commit comments

Comments
 (0)