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\events; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\patron\Patron; |
13
|
|
|
use flipbox\patron\records\Token; |
14
|
|
|
use yii\base\Event; |
15
|
|
|
use yii\db\AfterSaveEvent; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.1.0 |
20
|
|
|
*/ |
21
|
|
|
class ManageTokenProjectConfig |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param AfterSaveEvent $event |
25
|
|
|
* @throws \yii\base\ErrorException |
26
|
|
|
* @throws \yii\base\Exception |
27
|
|
|
* @throws \yii\base\NotSupportedException |
28
|
|
|
* @throws \yii\web\ServerErrorHttpException |
29
|
|
|
*/ |
30
|
|
|
public static function save(AfterSaveEvent $event) |
31
|
|
|
{ |
32
|
|
|
/** @var Token $record */ |
33
|
|
|
$record = $event->sender; |
34
|
|
|
|
35
|
|
|
if (static::bypassSavingToProjectConfig($event->changedAttributes)) { |
36
|
|
|
Patron::warning( |
37
|
|
|
"Saving Token to project config is not possible while in read-only mode.", |
38
|
|
|
__METHOD__ |
39
|
|
|
); |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
Craft::$app->getProjectConfig()->set( |
44
|
|
|
'plugins.patron.tokens.' . $record->uid, |
45
|
|
|
$record->toProjectConfig() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Event $event |
51
|
|
|
*/ |
52
|
|
|
public static function delete(Event $event) |
53
|
|
|
{ |
54
|
|
|
/** @var Token $record */ |
55
|
|
|
$record = $event->sender; |
56
|
|
|
|
57
|
|
|
Craft::$app->getProjectConfig()->remove( |
58
|
|
|
'plugins.patron.tokens.' . $record->uid |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* In some automated scenarios, we want to bypass saving to the project config. |
64
|
|
|
* |
65
|
|
|
* @param array $changedAttributes |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
protected static function bypassSavingToProjectConfig(array $changedAttributes): bool |
69
|
|
|
{ |
70
|
|
|
// If project config is not in read only mode, don't bypass |
71
|
|
|
if (!Craft::$app->getProjectConfig()->readOnly) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Also, only allow the 'accessToken' to be updated -> this would be triggered by a refresh token operation |
76
|
|
|
return count($changedAttributes) === 2 && array_key_exists('accessToken', $changedAttributes); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|