Completed
Push — master ( 93d695...69007e )
by vistart
05:29
created

SSOIdentity   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 6
c 5
b 1
f 0
lcom 1
cbo 3
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B loginRequired() 0 16 6
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\components;
14
15
use Yii;
16
use yii\web\ForbiddenHttpException;
17
18
/**
19
 * Description of SSOIdentity
20
 * This component needs MultipleDomainsManager component.
21
 * Usage:
22
 * config/web.php (basic template) or config/main.php (advanced template):
23
 * ```php
24
 * $config = [
25
 *     ...
26
 *     'components' => [
27
 *         ...
28
 *         'multipleDomainsManager' => [
29
 *              'baseDomain' => <Base Domain>,
30
 *         ],
31
 *         'user' => [
32
 *             'class' => 'vistart\Models\components\SSOIdentity',
33
 *             'enableAutoLogin' => true,
34
 *             'identityClass' => <User Identity Class>,
35
 *             'identityCookie' => [
36
 *                 'name' => '_identity',
37
 *                 'httpOnly' => true,
38
 *                 'domain' => '.' . <Base Domain>,    // same as Multiple Domains Manager's `baseDomain` property.
39
 *             ],
40
 *         ],
41
 *         'session' => [
42
 *             ...
43
 *             'cookieParams' => [
44
 *                 'domain' => '.' . <Base Domain>,    // same as Multiple Domains Manager's `baseDomain` property.
45
 *                 'lifetime' => 0,
46
 *             ],
47
 *             ...
48
 *         ],
49
 *         ...
50
 *     ],
51
 * ];
52
 * ```
53
 * @since 2.0
54
 * @author vistart <[email protected]>
55
 */
56
class SSOIdentity extends \yii\web\User
57
{
58
59
    public $ssoDomain = 'sso';
60
    public $loginUrl = ['sso/login'];
61
62 1
    public function loginRequired($checkAjax = true)
63
    {
64 1
        $request = Yii::$app->getRequest();
65
66 1
        if ($this->enableSession && (!$checkAjax || !$request->getIsAjax())) {
67 1
            $this->setReturnUrl($request->getAbsoluteUrl());
68 1
        }
69 1
        if ($this->loginUrl !== null) {
70 1
            $loginUrl = (array) $this->loginUrl;
71 1
            if ($loginUrl[0] !== Yii::$app->requestedRoute) {
72 1
                $ssoUrlManager = Yii::$app->multipleDomainsManager->get($this->ssoDomain);
73 1
                return Yii::$app->getResponse()->redirect($ssoUrlManager->createAbsoluteUrl($this->loginUrl));
74
            }
75 1
        }
76 1
        throw new ForbiddenHttpException(Yii::t('yii', 'Login Required'));
77
    }
78
}
79