Completed
Push — master ( af1792...633e5a )
by Damien
02:56
created

Saml::loadSaml2Container()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @copyright  Copyright (c) Flipbox Digital Limited
4
 * @license    https://flipboxfactory.com/software/saml-sp/license
5
 * @link       https://www.flipboxfactory.com/software/saml-sp/
6
 */
7
8
namespace flipbox\saml\sp;
9
10
use craft\events\RegisterComponentTypesEvent;
11
use craft\events\RegisterUrlRulesEvent;
12
use craft\services\Fields;
13
use craft\web\UrlManager;
14
use flipbox\saml\core\AbstractPlugin;
15
use flipbox\saml\core\containers\Saml2Container;
16
use flipbox\saml\core\models\SettingsInterface;
17
use flipbox\saml\core\services\Session;
18
use flipbox\saml\sp\fields\ExternalIdentity;
19
use flipbox\saml\sp\models\Settings;
20
use flipbox\saml\sp\records\ProviderIdentityRecord;
21
use flipbox\saml\sp\records\ProviderRecord;
22
use flipbox\saml\sp\services\Login;
23
use flipbox\saml\sp\services\login\User;
24
use flipbox\saml\sp\services\login\UserGroups;
25
use flipbox\saml\sp\services\messages\AuthnRequest;
26
use flipbox\saml\sp\services\Provider;
27
use flipbox\saml\sp\services\ProviderIdentity;
28
use SAML2\Compat\AbstractContainer;
29
use yii\base\Event;
30
31
/**
32
 * Class Saml
33
 * @package flipbox\saml\sp
34
 */
35
class Saml extends AbstractPlugin
36
{
37
    /**
38
     * @inheritdoc
39
     */
40 57
    public function init()
41
    {
42 57
        parent::init();
43
44 57
        $this->initComponents();
45 57
        $this->initEvents();
46 57
    }
47
48
    /**
49
     * Events
50
     */
51 57
    protected function initEvents()
52
    {
53
        /**
54
         * CP routes
55
         */
56 57
        Event::on(
57 57
            UrlManager::class,
58 57
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
59 57
            [self::class, 'onRegisterCpUrlRules']
60
        );
61
62
63
        /**
64
         * Clean Frontend Endpoints
65
         */
66 57
        Event::on(
67 57
            UrlManager::class,
68 57
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
69 57
            [static::class, 'onRegisterSiteUrlRules']
70
        );
71
72 57
        Event::on(
73 57
            Fields::class,
74 57
            Fields::EVENT_REGISTER_FIELD_TYPES,
75 19
            function (RegisterComponentTypesEvent $event) {
76
                $event->types[] = ExternalIdentity::class;
77 57
            }
78
        );
79 57
    }
80
81
    /**
82
     * Components
83
     */
84 57
    public function initComponents()
85
    {
86 57
        $this->setComponents(
87
            [
88 57
                'authnRequest' => AuthnRequest::class,
89
                'login' => Login::class,
90
                'user' => User::class,
91
                'userGroups' => UserGroups::class,
92
                'provider' => Provider::class,
93
                'providerIdentity' => ProviderIdentity::class,
94
                'session' => Session::class,
95
            ]
96
        );
97 57
    }
98
99
    /**
100
     * @param RegisterUrlRulesEvent $event
101
     */
102 3
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
103
    {
104
105 3
        $event->rules = array_merge(
106 3
            $event->rules,
107 3
            static::getInstance()->getSettings()->enableCpLoginButtons ?
108
                [
109 3
                    'login' => 'saml-sp/cp/view/login',
110 3
                ] : []
111
        );
112
113 3
        parent::onRegisterCpUrlRules($event);
114 3
    }
115
116
117
    /**
118
     * @return Settings
119
     */
120 24
    public function getSettings(): SettingsInterface
121
    {
122 24
        return parent::getSettings();
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 24
    protected function createSettingsModel()
129
    {
130 24
        return new Settings([
131 24
            'myType' => Settings::SP,
132
        ]);
133
    }
134
135
    /**
136
     * Components
137
     */
138
139
    /**
140
     * @noinspection PhpDocMissingThrowsInspection
141
     * @return AuthnRequest
142
     */
143 12
    public function getAuthnRequest()
144
    {
145
        /** @noinspection PhpUnhandledExceptionInspection */
146
        /** @noinspection PhpIncompatibleReturnTypeInspection */
147 12
        return $this->get('authnRequest');
148
    }
149
150
    /**
151
     * @noinspection PhpDocMissingThrowsInspection
152
     * @return Login
153
     */
154 6
    public function getLogin()
155
    {
156
        /** @noinspection PhpUnhandledExceptionInspection */
157
        /** @noinspection PhpIncompatibleReturnTypeInspection */
158 6
        return $this->get('login');
159
    }
160
161
    /**
162
     * @noinspection PhpDocMissingThrowsInspection
163
     * @return User
164
     */
165 6
    public function getUser()
166
    {
167
        /** @noinspection PhpUnhandledExceptionInspection */
168
        /** @noinspection PhpIncompatibleReturnTypeInspection */
169 6
        return $this->get('user');
170
    }
171
172
    /**
173
     * @noinspection PhpDocMissingThrowsInspection
174
     * @return UserGroups
175
     */
176 6
    public function getUserGroups()
177
    {
178
        /** @noinspection PhpUnhandledExceptionInspection */
179
        /** @noinspection PhpIncompatibleReturnTypeInspection */
180 6
        return $this->get('userGroups');
181
    }
182
183
    /**
184
     * @noinspection PhpDocMissingThrowsInspection
185
     * @return Session
186
     * @throws \yii\base\InvalidConfigException
187
     */
188 6
    public function getSession()
189
    {
190
        /** @noinspection PhpUnhandledExceptionInspection */
191
        /** @noinspection PhpIncompatibleReturnTypeInspection */
192 6
        return $this->get('session');
193
    }
194
195
    /**
196
     * Util Methods
197
     */
198
199
    /**
200
     * @return Saml2Container
201
     */
202 12
    public function loadSaml2Container(): AbstractContainer
203
    {
204 12
        $container = new Saml2Container($this);
205
206 12
        \SAML2\Compat\ContainerSingleton::setContainer($container);
207
208 12
        return $container;
209
    }
210
211
    /**
212
     * @return string
213
     */
214 15
    public function getProviderRecordClass()
215
    {
216 15
        return ProviderRecord::class;
217
    }
218
219
    /**
220
     * @return string
221
     */
222 3
    public function getProviderIdentityRecordClass()
223
    {
224 3
        return ProviderIdentityRecord::class;
225
    }
226
}
227