1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\Plugin; |
13
|
|
|
use craft\events\RegisterUrlRulesEvent; |
14
|
|
|
use craft\helpers\ArrayHelper; |
15
|
|
|
use craft\helpers\Json; |
16
|
|
|
use craft\helpers\UrlHelper; |
17
|
|
|
use craft\services\ProjectConfig; |
18
|
|
|
use craft\web\twig\variables\CraftVariable; |
19
|
|
|
use craft\web\UrlManager; |
20
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
21
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
22
|
|
|
use flipbox\craft\ember\modules\LoggerTrait; |
23
|
|
|
use flipbox\patron\events\handlers\ProjectConfigHandler; |
24
|
|
|
use flipbox\patron\events\RegisterProviderSettings; |
25
|
|
|
use flipbox\patron\models\Settings as SettingsModel; |
26
|
|
|
use flipbox\patron\queries\ProviderQuery; |
27
|
|
|
use flipbox\patron\queries\TokenQuery; |
28
|
|
|
use flipbox\patron\records\Provider; |
29
|
|
|
use flipbox\patron\records\Token; |
30
|
|
|
use flipbox\patron\settings\BaseSettings; |
31
|
|
|
use flipbox\patron\settings\SettingsInterface; |
32
|
|
|
use yii\base\Event; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Flipbox Factory <[email protected]> |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
* |
38
|
|
|
* @method SettingsModel getSettings() |
39
|
|
|
* |
40
|
|
|
* @property services\Session $session |
41
|
|
|
*/ |
42
|
|
|
class Patron extends Plugin |
43
|
|
|
{ |
44
|
|
|
use LoggerTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The before persist token event name |
48
|
|
|
*/ |
49
|
|
|
const EVENT_BEFORE_PERSIST_TOKEN = 'beforePersistToken'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The after persist token event name |
53
|
|
|
*/ |
54
|
|
|
const EVENT_AFTER_PERSIST_TOKEN = 'afterPersistToken'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected static function getLogFileName(): string |
60
|
|
|
{ |
61
|
|
|
return 'patron'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
3 |
|
public function init() |
68
|
|
|
{ |
69
|
3 |
|
parent::init(); |
70
|
|
|
|
71
|
|
|
// Components |
72
|
3 |
|
$this->setComponents([ |
73
|
3 |
|
'session' => services\Session::class |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
// Modules |
77
|
3 |
|
$this->setModules([ |
78
|
3 |
|
'cp' => cp\Cp::class |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
// Project config |
83
|
3 |
|
$this->registerProjectConfigEvents(); |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
// Template variables |
87
|
3 |
|
Event::on( |
88
|
3 |
|
CraftVariable::class, |
89
|
3 |
|
CraftVariable::EVENT_INIT, |
90
|
1 |
|
function (Event $event) { |
91
|
|
|
/** @var CraftVariable $variable */ |
92
|
|
|
$variable = $event->sender; |
93
|
|
|
$variable->set('patron', self::getInstance()); |
94
|
3 |
|
} |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
// CP routes |
98
|
3 |
|
Event::on( |
99
|
3 |
|
UrlManager::class, |
100
|
3 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
101
|
3 |
|
[self::class, 'onRegisterCpUrlRules'] |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
// Register our site routes |
105
|
3 |
|
Event::on( |
106
|
3 |
|
UrlManager::class, |
107
|
3 |
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
108
|
1 |
|
function (RegisterUrlRulesEvent $event) { |
109
|
|
|
if ($callbackUrlRule = $this->getSettings()->getCallbackUrlRule()) { |
110
|
|
|
$event->rules = array_merge( |
111
|
|
|
$event->rules, |
112
|
|
|
$callbackUrlRule |
113
|
|
|
); |
114
|
|
|
} |
115
|
3 |
|
} |
116
|
|
|
); |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register project config events, if we're able to |
121
|
|
|
*/ |
122
|
3 |
|
protected function registerProjectConfigEvents() |
123
|
|
|
{ |
124
|
3 |
|
if (!version_compare(Craft::$app->getVersion(), '3.1', '>=')) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Project Config |
129
|
3 |
|
Craft::$app->projectConfig |
130
|
3 |
|
->onAdd( |
131
|
3 |
|
'patronProviders.{uid}', |
132
|
|
|
[ |
133
|
3 |
|
ProjectConfigHandler::class, |
134
|
|
|
'handleChangedProvider' |
135
|
|
|
] |
136
|
|
|
) |
137
|
3 |
|
->onUpdate( |
138
|
3 |
|
'patronProviders.{uid}', |
139
|
3 |
|
[ProjectConfigHandler::class, |
140
|
|
|
'handleChangedProvider' |
141
|
|
|
] |
142
|
|
|
) |
143
|
3 |
|
->onRemove( |
144
|
3 |
|
'patronProviders.{uid}', |
145
|
3 |
|
[ProjectConfigHandler::class, |
146
|
|
|
'handleDeletedProvider' |
147
|
|
|
] |
148
|
|
|
) |
149
|
3 |
|
->onAdd( |
150
|
3 |
|
'patronTokens.{uid}', |
151
|
3 |
|
[ProjectConfigHandler::class, |
152
|
|
|
'handleChangedToken' |
153
|
|
|
] |
154
|
|
|
) |
155
|
3 |
|
->onUpdate( |
156
|
3 |
|
'patronTokens.{uid}', |
157
|
3 |
|
[ProjectConfigHandler::class, |
158
|
|
|
'handleChangedToken' |
159
|
|
|
] |
160
|
|
|
) |
161
|
3 |
|
->onRemove( |
162
|
3 |
|
'patronTokens.{uid}', |
163
|
3 |
|
[ProjectConfigHandler::class, |
164
|
|
|
'handleDeletedToken' |
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
|
168
|
3 |
|
Event::on( |
169
|
3 |
|
ProjectConfig::class, |
170
|
3 |
|
ProjectConfig::EVENT_REBUILD, |
171
|
|
|
[ |
172
|
3 |
|
events\handlers\ProjectConfigHandler::class, |
173
|
|
|
'rebuild' |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
|
177
|
3 |
|
Event::on( |
178
|
3 |
|
Provider::class, |
179
|
3 |
|
Provider::EVENT_AFTER_INSERT, |
180
|
|
|
[ |
181
|
3 |
|
events\ManageProviderProjectConfig::class, |
182
|
|
|
'save' |
183
|
|
|
] |
184
|
|
|
); |
185
|
|
|
|
186
|
3 |
|
Event::on( |
187
|
3 |
|
Provider::class, |
188
|
3 |
|
Provider::EVENT_AFTER_UPDATE, |
189
|
|
|
[ |
190
|
3 |
|
events\ManageProviderProjectConfig::class, |
191
|
|
|
'save' |
192
|
|
|
] |
193
|
|
|
); |
194
|
|
|
|
195
|
3 |
|
Event::on( |
196
|
3 |
|
Provider::class, |
197
|
3 |
|
Provider::EVENT_AFTER_DELETE, |
198
|
|
|
[ |
199
|
3 |
|
events\ManageProviderProjectConfig::class, |
200
|
|
|
'delete' |
201
|
|
|
] |
202
|
|
|
); |
203
|
|
|
|
204
|
3 |
|
Event::on( |
205
|
3 |
|
Token::class, |
206
|
3 |
|
Token::EVENT_AFTER_INSERT, |
207
|
|
|
[ |
208
|
3 |
|
events\ManageTokenProjectConfig::class, |
209
|
|
|
'save' |
210
|
|
|
] |
211
|
|
|
); |
212
|
|
|
|
213
|
3 |
|
Event::on( |
214
|
3 |
|
Token::class, |
215
|
3 |
|
Token::EVENT_AFTER_UPDATE, |
216
|
|
|
[ |
217
|
3 |
|
events\ManageTokenProjectConfig::class, |
218
|
|
|
'save' |
219
|
|
|
] |
220
|
|
|
); |
221
|
|
|
|
222
|
3 |
|
Event::on( |
223
|
3 |
|
Token::class, |
224
|
3 |
|
Token::EVENT_AFTER_DELETE, |
225
|
|
|
[ |
226
|
3 |
|
events\ManageTokenProjectConfig::class, |
227
|
|
|
'delete' |
228
|
|
|
] |
229
|
|
|
); |
230
|
3 |
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/******************************************* |
234
|
|
|
* NAV |
235
|
|
|
*******************************************/ |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @inheritdoc |
239
|
|
|
*/ |
240
|
|
|
public function getCpNavItem() |
241
|
|
|
{ |
242
|
|
|
return array_merge( |
243
|
|
|
parent::getCpNavItem(), |
244
|
|
|
[ |
245
|
|
|
'subnav' => [ |
246
|
|
|
'patron.providers' => [ |
247
|
|
|
'label' => Craft::t('patron', 'Providers'), |
248
|
|
|
'url' => 'patron/providers', |
249
|
|
|
], |
250
|
|
|
'patron.settings' => [ |
251
|
|
|
'label' => Craft::t('patron', 'Settings'), |
252
|
|
|
'url' => 'patron/settings', |
253
|
|
|
] |
254
|
|
|
] |
255
|
|
|
] |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/******************************************* |
261
|
|
|
* SETTINGS |
262
|
|
|
*******************************************/ |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @inheritdoc |
266
|
|
|
* @return SettingsModel |
267
|
|
|
*/ |
268
|
|
|
public function createSettingsModel() |
269
|
|
|
{ |
270
|
|
|
return new SettingsModel(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @inheritdoc |
275
|
|
|
* @throws \yii\base\ExitException |
276
|
|
|
*/ |
277
|
|
|
public function getSettingsResponse() |
278
|
|
|
{ |
279
|
|
|
Craft::$app->getResponse()->redirect( |
280
|
|
|
UrlHelper::cpUrl('patron/settings') |
281
|
|
|
); |
282
|
|
|
|
283
|
|
|
Craft::$app->end(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/******************************************* |
287
|
|
|
* QUERIES |
288
|
|
|
*******************************************/ |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param array $config |
292
|
|
|
* @return ProviderQuery |
293
|
|
|
*/ |
294
|
|
|
public function getProviders(array $config = []): ProviderQuery |
295
|
|
|
{ |
296
|
|
|
$query = new ProviderQuery(); |
297
|
|
|
|
298
|
|
|
QueryHelper::configure( |
299
|
|
|
$query, |
300
|
|
|
$config |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
return $query; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param array $config |
308
|
|
|
* @return TokenQuery |
309
|
|
|
*/ |
310
|
|
|
public function getTokens(array $config = []): TokenQuery |
311
|
|
|
{ |
312
|
|
|
$query = new TokenQuery(); |
313
|
|
|
|
314
|
|
|
QueryHelper::configure( |
315
|
|
|
$query, |
316
|
|
|
$config |
317
|
|
|
); |
318
|
|
|
|
319
|
|
|
return $query; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
/******************************************* |
324
|
|
|
* SERVICES |
325
|
|
|
*******************************************/ |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
329
|
|
|
* @return services\Session |
330
|
|
|
*/ |
331
|
3 |
|
public function getSession(): services\Session |
332
|
|
|
{ |
333
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
334
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
335
|
3 |
|
return $this->get('session'); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
/******************************************* |
340
|
|
|
* MODULES |
341
|
|
|
*******************************************/ |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
345
|
|
|
* @return cp\Cp |
346
|
|
|
*/ |
347
|
|
|
public function getCp(): cp\Cp |
348
|
|
|
{ |
349
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
350
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
351
|
|
|
return $this->getModule('cp'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
|
355
|
|
|
/******************************************* |
356
|
|
|
* PROVIDER SETTINGS |
357
|
|
|
*******************************************/ |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param string|null $class |
361
|
|
|
* @param array $settings |
362
|
|
|
* @return SettingsInterface |
363
|
|
|
* @throws \yii\base\InvalidConfigException |
364
|
|
|
*/ |
365
|
|
|
public function providerSettings(string $class = null, $settings = []): SettingsInterface |
366
|
|
|
{ |
367
|
|
|
if (null === $class) { |
368
|
|
|
return new BaseSettings(); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
$event = new RegisterProviderSettings(); |
372
|
|
|
|
373
|
|
|
RegisterProviderSettings::trigger( |
374
|
|
|
$class, |
375
|
|
|
RegisterProviderSettings::REGISTER_SETTINGS, |
376
|
|
|
$event |
377
|
|
|
); |
378
|
|
|
|
379
|
|
|
if (is_string($settings)) { |
380
|
|
|
$settings = Json::decodeIfJson($settings); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
if (!is_array($settings)) { |
384
|
|
|
$settings = ArrayHelper::toArray($settings, []); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$settings['class'] = $event->class; |
388
|
|
|
|
389
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
390
|
|
|
return ObjectHelper::create($settings, SettingsInterface::class); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
|
394
|
|
|
/******************************************* |
395
|
|
|
* EVENTS |
396
|
|
|
*******************************************/ |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @param RegisterUrlRulesEvent $event |
400
|
|
|
*/ |
401
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
402
|
|
|
{ |
403
|
|
|
$event->rules = array_merge( |
404
|
|
|
$event->rules, |
405
|
|
|
[ |
406
|
|
|
// SETTINGS |
407
|
|
|
'patron/settings' => 'patron/cp/view/settings/index', |
408
|
|
|
|
409
|
|
|
// BASE |
410
|
|
|
'patron' => |
411
|
|
|
'patron/cp/view/general/index', |
412
|
|
|
|
413
|
|
|
// PROVIDERS |
414
|
|
|
'patron/providers' => |
415
|
|
|
'patron/cp/view/providers/default/index', |
416
|
|
|
|
417
|
|
|
'patron/providers/new' => |
418
|
|
|
'patron/cp/view/providers/default/upsert', |
419
|
|
|
|
420
|
|
|
'patron/providers/<identifier:\d+>' => |
421
|
|
|
'patron/cp/view/providers/default/upsert', |
422
|
|
|
|
423
|
|
|
// TOKENS |
424
|
|
|
'patron/providers/<provider:\d+>/tokens' => |
425
|
|
|
'patron/cp/view/providers/tokens/index', |
426
|
|
|
|
427
|
|
|
'patron/providers/<provider:\d+>/tokens/<identifier:\d+>' => |
428
|
|
|
'patron/cp/view/providers/tokens/upsert', |
429
|
|
|
] |
430
|
|
|
); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|