1 | <?php |
||
36 | class Saml extends AbstractPlugin |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | 75 | public function init() |
|
43 | { |
||
44 | 75 | parent::init(); |
|
45 | |||
46 | 75 | $this->initComponents(); |
|
47 | 75 | $this->initEvents(); |
|
48 | |||
49 | // Switch target to console controllers |
||
50 | 75 | if (\Craft::$app instanceof ConsoleApplication) { |
|
51 | $this->controllerNamespace = __NAMESPACE__ . '\commands'; |
||
52 | } |
||
53 | 75 | } |
|
54 | |||
55 | /** |
||
56 | * Events |
||
57 | */ |
||
58 | 75 | protected function initEvents() |
|
59 | { |
||
60 | /** |
||
61 | * CP routes |
||
62 | */ |
||
63 | 75 | Event::on( |
|
64 | 75 | UrlManager::class, |
|
65 | 75 | UrlManager::EVENT_REGISTER_CP_URL_RULES, |
|
66 | 75 | [self::class, 'onRegisterCpUrlRules'] |
|
67 | ); |
||
68 | |||
69 | /** |
||
70 | * Clean Frontend Endpoints |
||
71 | */ |
||
72 | 75 | Event::on( |
|
73 | 75 | UrlManager::class, |
|
74 | 75 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
75 | 75 | [static::class, 'onRegisterSiteUrlRules'] |
|
76 | ); |
||
77 | |||
78 | 75 | Event::on( |
|
79 | 75 | Fields::class, |
|
80 | 75 | Fields::EVENT_REGISTER_FIELD_TYPES, |
|
81 | function (RegisterComponentTypesEvent $event) { |
||
82 | $event->types[] = ExternalIdentity::class; |
||
83 | 75 | } |
|
84 | ); |
||
85 | 75 | } |
|
86 | |||
87 | /** |
||
88 | * Components |
||
89 | */ |
||
90 | 75 | public function initComponents() |
|
91 | { |
||
92 | 75 | $this->setComponents( |
|
93 | [ |
||
94 | 75 | '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 | 75 | } |
|
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 | 42 | public function getSettings(): SettingsInterface |
|
126 | { |
||
127 | 42 | return parent::getSettings(); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 42 | protected function createSettingsModel() |
|
134 | { |
||
135 | 42 | return new Settings([ |
|
136 | 42 | 'myType' => Settings::SP, |
|
137 | ]); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Components |
||
142 | */ |
||
143 | |||
144 | /** |
||
145 | * @noinspection PhpDocMissingThrowsInspection |
||
146 | * @return Provider |
||
147 | */ |
||
148 | 30 | public function getProvider() |
|
154 | |||
155 | /** |
||
156 | * @noinspection PhpDocMissingThrowsInspection |
||
157 | * @return ProviderIdentity |
||
158 | */ |
||
159 | 9 | public function getProviderIdentity() |
|
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 | 12 | public function getUser() |
|
198 | |||
199 | /** |
||
200 | * @noinspection PhpDocMissingThrowsInspection |
||
201 | * @return UserGroups |
||
202 | */ |
||
203 | 9 | public function getUserGroups() |
|
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 | 18 | public function loadSaml2Container(): AbstractContainer |
|
237 | |||
238 | /** |
||
239 | * @return string |
||
240 | */ |
||
241 | 33 | public function getProviderRecordClass() |
|
245 | |||
246 | /** |
||
247 | * @return string |
||
248 | */ |
||
249 | 6 | public function getProviderIdentityRecordClass() |
|
253 | } |
||
254 |