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\migrations; |
10
|
|
|
|
11
|
|
|
use craft\db\Migration; |
12
|
|
|
use craft\helpers\Json; |
13
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
14
|
|
|
use flipbox\patron\records\Provider; |
15
|
|
|
use yii\db\Query; |
16
|
|
|
|
17
|
|
|
class m190426_101341_project_config extends Migration |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @return bool |
21
|
|
|
* @throws \yii\base\NotSupportedException |
22
|
|
|
*/ |
23
|
|
|
public function safeUp() |
24
|
|
|
{ |
25
|
|
|
$schema = $this->getDb()->getSchema(); |
26
|
|
|
$table = $schema->getTableSchema($schema->getRawTableName(Provider::tableName())); |
27
|
|
|
|
28
|
|
|
if (!array_key_exists('clientId', $table->columns)) { |
29
|
|
|
$this->addColumn( |
30
|
|
|
Provider::tableName(), |
31
|
|
|
'clientId', |
32
|
|
|
$this->string(Provider::CLIENT_ID_LENGTH)->notNull() |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!array_key_exists('clientSecret', $table->columns)) { |
37
|
|
|
$this->addColumn( |
38
|
|
|
Provider::tableName(), |
39
|
|
|
'clientSecret', |
40
|
|
|
$this->string(Provider::CLIENT_SECRET_LENGTH) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!array_key_exists('settings', $table->columns)) { |
45
|
|
|
$this->addColumn( |
46
|
|
|
Provider::tableName(), |
47
|
|
|
'settings', |
48
|
|
|
$this->text() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->getDb()->getSchema()->refresh(); |
53
|
|
|
|
54
|
|
|
if (!$this->providerInstances()) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->deleteLegacyTables(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws \yii\base\NotSupportedException |
63
|
|
|
*/ |
64
|
|
|
protected function providerInstances(): bool |
65
|
|
|
{ |
66
|
|
|
$tableName = '{{%patron_provider_instances}}'; |
67
|
|
|
|
68
|
|
|
$schema = $this->getDb()->getSchema(); |
69
|
|
|
if ($schema->getTableSchema($schema->getRawTableName($tableName)) === null) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$providers = Provider::findAll([ |
74
|
|
|
'enabled' => null |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
foreach ($providers as $provider) { |
78
|
|
|
if (!$query = (new Query()) |
79
|
|
|
->select([ |
80
|
|
|
'clientId', |
81
|
|
|
'clientSecret', |
82
|
|
|
'settings' |
83
|
|
|
]) |
84
|
|
|
->from([$tableName]) |
85
|
|
|
->where([ |
86
|
|
|
'providerId' => $provider->id |
87
|
|
|
])->one() |
88
|
|
|
) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$provider->clientId = $query['clientId']; |
93
|
|
|
$provider->clientSecret = $query['clientSecret']; |
94
|
|
|
|
95
|
|
|
if ($settings = $query['settings']) { |
96
|
|
|
ObjectHelper::populate( |
97
|
|
|
$provider->getSettings(), |
98
|
|
|
Json::decodeIfJson($settings) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$provider->save(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
protected function deleteLegacyTables(): bool |
112
|
|
|
{ |
113
|
|
|
$this->dropTableIfExists('{{%patron_provider_environments}}'); |
114
|
|
|
$this->dropTableIfExists('{{%patron_token_environments}}'); |
115
|
|
|
$this->dropTableIfExists('{{%patron_provider_instances}}'); |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
public function safeDown() |
124
|
|
|
{ |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|