1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 3/10/18 |
6
|
|
|
* Time: 8:40 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\core\controllers\cp\view\metadata; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\keychain\KeyChain; |
13
|
|
|
use flipbox\saml\core\AbstractPlugin; |
14
|
|
|
use flipbox\saml\core\controllers\cp\view\AbstractController; |
15
|
|
|
use craft\helpers\UrlHelper; |
16
|
|
|
use flipbox\saml\core\records\ProviderInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AbstractEditController |
20
|
|
|
* @package flipbox\saml\core\controllers\cp\view\metadata |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractEditController extends AbstractController |
23
|
|
|
{ |
24
|
|
|
const TEMPLATE_INDEX = DIRECTORY_SEPARATOR . '_cp' . DIRECTORY_SEPARATOR . 'metadata'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string|null $providerId |
28
|
|
|
* @return \yii\web\Response |
29
|
|
|
*/ |
30
|
|
|
public function actionIndex($providerId = null, $overwriteVariables = []) |
31
|
|
|
{ |
32
|
|
|
$variables = $this->prepVariables($providerId); |
33
|
|
|
$provider = $variables['provider']; |
34
|
|
|
|
35
|
|
|
$variables['title'] = Craft::t( |
36
|
|
|
$this->getSamlPlugin()->getHandle(), |
37
|
|
|
'Edit ' . $this->getTitle($provider->getType()) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$variables['createType'] = $variables['remoteType']; |
41
|
|
|
|
42
|
|
|
if (isset($variables['provider']) && $variables['provider'] instanceof ProviderInterface) { |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Actions |
46
|
|
|
*/ |
47
|
|
|
$variables['actions'] = $this->getActions($variables['provider']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$variables = array_merge($variables, $overwriteVariables); |
51
|
|
|
return $this->renderTemplate( |
52
|
|
|
$this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit', |
53
|
|
|
$variables |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \yii\web\Response |
59
|
|
|
*/ |
60
|
|
|
public function actionNewIdp() |
61
|
|
|
{ |
62
|
|
|
/** @var AbstractPlugin $plugin */ |
63
|
|
|
$plugin = $this->getSamlPlugin(); |
64
|
|
|
return $this->actionIndex(null, [ |
65
|
|
|
'title' => 'New ' . $this->getTitle($plugin::IDP), |
66
|
|
|
'createType' => $plugin::IDP, |
67
|
|
|
'crumbs' => [ |
68
|
|
|
[ |
69
|
|
|
'url' => UrlHelper::cpUrl( |
70
|
|
|
implode( |
71
|
|
|
'/', |
72
|
|
|
[ |
73
|
|
|
$plugin->getHandle(), |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
), |
77
|
|
|
'label' => $plugin->name, |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
'url' => UrlHelper::cpUrl( |
81
|
|
|
implode( |
82
|
|
|
'/', |
83
|
|
|
[ |
84
|
|
|
$plugin->getHandle(), |
85
|
|
|
'metadata', |
86
|
|
|
] |
87
|
|
|
) |
88
|
|
|
), |
89
|
|
|
'label' => 'Provider List', |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
'url' => UrlHelper::cpUrl( |
93
|
|
|
implode( |
94
|
|
|
'/', |
95
|
|
|
[ |
96
|
|
|
$plugin->getHandle(), |
97
|
|
|
'metadata', |
98
|
|
|
'new-idp' |
99
|
|
|
] |
100
|
|
|
) |
101
|
|
|
), |
102
|
|
|
'label' => 'New IDP', |
103
|
|
|
], |
104
|
|
|
], |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return \yii\web\Response |
110
|
|
|
*/ |
111
|
|
|
public function actionNewSp() |
112
|
|
|
{ |
113
|
|
|
/** @var AbstractPlugin $plugin */ |
114
|
|
|
$plugin = $this->getSamlPlugin(); |
115
|
|
|
return $this->actionIndex(null, [ |
116
|
|
|
'title' => 'New ' . $this->getTitle($plugin::SP), |
117
|
|
|
'createType' => $plugin::SP, |
118
|
|
|
'crumbs' => [ |
119
|
|
|
[ |
120
|
|
|
'url' => UrlHelper::cpUrl( |
121
|
|
|
implode( |
122
|
|
|
'/', |
123
|
|
|
[ |
124
|
|
|
$plugin->getHandle(), |
125
|
|
|
] |
126
|
|
|
) |
127
|
|
|
), |
128
|
|
|
'label' => $plugin->name, |
129
|
|
|
], |
130
|
|
|
[ |
131
|
|
|
'url' => UrlHelper::cpUrl( |
132
|
|
|
implode( |
133
|
|
|
'/', |
134
|
|
|
[ |
135
|
|
|
$plugin->getHandle(), |
136
|
|
|
'metadata', |
137
|
|
|
] |
138
|
|
|
) |
139
|
|
|
), |
140
|
|
|
'label' => 'Provider List', |
141
|
|
|
], |
142
|
|
|
[ |
143
|
|
|
'url' => UrlHelper::cpUrl( |
144
|
|
|
implode( |
145
|
|
|
'/', |
146
|
|
|
[ |
147
|
|
|
$plugin->getHandle(), |
148
|
|
|
'metadata', |
149
|
|
|
'new-sp' |
150
|
|
|
] |
151
|
|
|
) |
152
|
|
|
), |
153
|
|
|
'label' => 'New SP', |
154
|
|
|
], |
155
|
|
|
], |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \yii\web\Response |
161
|
|
|
*/ |
162
|
|
|
public function actionMyProvider() |
163
|
|
|
{ |
164
|
|
|
$provider = $this->getSamlPlugin()->getProvider()->findOwn(); |
165
|
|
|
$variables = $this->prepVariables( |
166
|
|
|
$provider ? $provider->id : null |
|
|
|
|
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
if ($provider) { |
170
|
|
|
$variables['provider'] = $provider; |
171
|
|
|
|
172
|
|
|
$variables = array_merge( |
173
|
|
|
$variables, |
174
|
|
|
$this->addUrls($provider) |
175
|
|
|
); |
176
|
|
|
} else { |
177
|
|
|
$provider = $variables['provider']; |
178
|
|
|
$variables['provider']->entityId = $this->getSamlPlugin()->getSettings()->getEntityId(); |
179
|
|
|
$variables['provider']->providerType = $this->getSamlPlugin()->getMyType(); |
180
|
|
|
$variables['provider']->label = 'My Provider'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Actions |
185
|
|
|
*/ |
186
|
|
|
$variables['actions'] = $this->getActions($provider); |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Edit Title |
190
|
|
|
*/ |
191
|
|
|
$variables['title'] = Craft::t( |
192
|
|
|
$this->getSamlPlugin()->getHandle(), |
193
|
|
|
'My Provider (' . strtoupper($variables['provider']->providerType) . ')' |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$variables['createType'] = $variables['myType']; |
197
|
|
|
|
198
|
|
|
return $this->renderTemplate( |
199
|
|
|
$this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit', |
200
|
|
|
$variables |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param ProviderInterface $provider |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
protected function getActions(ProviderInterface $provider) |
209
|
|
|
{ |
210
|
|
|
$actions = []; |
211
|
|
|
|
212
|
|
|
if ($provider->id) { |
|
|
|
|
213
|
|
|
$actions = [ |
214
|
|
|
[ |
215
|
|
|
//action list 1 |
216
|
|
|
[ |
217
|
|
|
'action' => $this->getSamlPlugin()->getHandle() . '/metadata/change-status', |
218
|
|
|
'label' => $provider->enabled ? 'Disable' : 'Enable', |
|
|
|
|
219
|
|
|
], |
220
|
|
|
[ |
221
|
|
|
'action' => $this->getSamlPlugin()->getHandle() . '/metadata/delete', |
222
|
|
|
'label' => 'Delete', |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
return $actions; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
protected function getBaseVariables() |
234
|
|
|
{ |
235
|
|
|
|
236
|
|
|
return array_merge( |
237
|
|
|
parent::getBaseVariables(), |
238
|
|
|
[ |
239
|
|
|
'autoCreate' => false, |
240
|
|
|
'myEntityId' => $this->getSamlPlugin()->getSettings()->getEntityId(), |
241
|
|
|
'myType' => $this->getSamlPlugin()->getSettings(), |
242
|
|
|
] |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string|null $providerId |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
protected function prepVariables($providerId = null) |
251
|
|
|
{ |
252
|
|
|
$variables = $this->getBaseVariables(); |
253
|
|
|
|
254
|
|
|
$variables['title'] = Craft::t( |
255
|
|
|
$this->getSamlPlugin()->getHandle(), |
256
|
|
|
$this->getSamlPlugin()->name |
|
|
|
|
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* TYPES |
261
|
|
|
*/ |
262
|
|
|
$variables['myType'] = $this->getSamlPlugin()->getMyType(); |
263
|
|
|
$variables['remoteType'] = $this->getSamlPlugin()->getRemoteType(); |
264
|
|
|
|
265
|
|
|
if ($providerId) { |
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @var ProviderInterface $provider |
268
|
|
|
*/ |
269
|
|
|
$provider = $variables['provider'] = $provider = $this->getProviderRecord()::find()->where([ |
|
|
|
|
270
|
|
|
'id' => $providerId, |
271
|
|
|
])->one(); |
272
|
|
|
|
273
|
|
|
$variables['title'] .= ': Edit'; |
274
|
|
|
|
275
|
|
|
$crumb = [ |
276
|
|
|
[ |
277
|
|
|
|
278
|
|
|
'url' => UrlHelper::cpUrl( |
279
|
|
|
implode( |
280
|
|
|
'/', |
281
|
|
|
[ |
282
|
|
|
$this->getSamlPlugin()->getHandle(), |
283
|
|
|
'metadata', |
284
|
|
|
] |
285
|
|
|
) |
286
|
|
|
), |
287
|
|
|
'label' => 'Provider List', |
288
|
|
|
], [ |
289
|
|
|
'url' => UrlHelper::cpUrl( |
290
|
|
|
$this->getSamlPlugin()->getHandle() . '/metadata/' . $providerId |
291
|
|
|
), |
292
|
|
|
'label' => $provider->label ?: $provider->entityId, |
|
|
|
|
293
|
|
|
] |
294
|
|
|
]; |
295
|
|
|
$variables['keypair'] = $provider->keychain; |
|
|
|
|
296
|
|
|
|
297
|
|
|
$variables = array_merge( |
298
|
|
|
$variables, |
299
|
|
|
$this->addUrls($provider) |
300
|
|
|
); |
301
|
|
|
} else { |
302
|
|
|
$record = $this->getProviderRecord(); |
303
|
|
|
|
304
|
|
|
$provider = $variables['provider'] = new $record([ |
|
|
|
|
305
|
|
|
'providerType' => 'idp', |
306
|
|
|
]); |
307
|
|
|
|
308
|
|
|
$variables['title'] .= ': Create'; |
309
|
|
|
|
310
|
|
|
$crumb = [ |
311
|
|
|
[ |
312
|
|
|
'url' => UrlHelper::cpUrl( |
313
|
|
|
$this->getSamlPlugin()->getHandle() . '/new' |
314
|
|
|
), |
315
|
|
|
'label' => 'New', |
316
|
|
|
] |
317
|
|
|
]; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$variables['allkeypairs'] = []; |
321
|
|
|
|
322
|
|
|
$keypairs = KeyChain::getInstance()->getService()->findByPlugin($this->getSamlPlugin())->all(); |
323
|
|
|
|
324
|
|
|
foreach ($keypairs as $keypair) { |
325
|
|
|
$variables['allkeypairs'][] = [ |
326
|
|
|
'label' => $keypair->description, |
327
|
|
|
'value' => $keypair->id, |
328
|
|
|
]; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$variables['crumbs'] = array_merge([ |
332
|
|
|
[ |
333
|
|
|
'url' => UrlHelper::cpUrl($this->getSamlPlugin()->getHandle()), |
334
|
|
|
'label' => Craft::t($this->getSamlPlugin()->getHandle(), $this->getSamlPlugin()->name), |
|
|
|
|
335
|
|
|
], |
336
|
|
|
], $crumb); |
337
|
|
|
|
338
|
|
|
return $variables; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.