Completed
Push — master ( 89c2c1...306671 )
by Damien
07:22
created

Saml::getProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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