Completed
Push — master ( 72ecff...582962 )
by Damien
11:32
created

AbstractInstall::addForeignKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 9.216
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 2/9/18
6
 * Time: 2:23 PM
7
 */
8
9
namespace flipbox\saml\core\migrations;
10
11
use craft\db\Migration;
12
use craft\records\User;
13
use flipbox\keychain\records\KeyChainRecord;
14
use flipbox\saml\core\AbstractPlugin;
15
use flipbox\saml\core\models\SettingsInterface;
16
use flipbox\saml\core\records\AbstractProvider;
17
use flipbox\saml\core\records\LinkRecord;
18
19
abstract class AbstractInstall extends Migration
20
{
21
22
    const PROVIDER_AFTER_COLUMN = 'sha256';
23
24
    /**
25
     * @return string
26
     */
27
    abstract protected function getIdentityTableName(): string;
28
29
    /**
30
     * @return string
31
     */
32
    abstract protected function getProviderTableName(): string;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function safeUp()
38
    {
39
40
        $this->installKeyChain();
41
        $this->createTables();
42
        $this->createIndexes();
43
        $this->addForeignKeys();
44
45
        return true;
46
    }
47
48
    protected function installKeyChain()
49
    {
50
        if (! \Craft::$app->plugins->getPlugin('keychain')) {
51
            \Craft::$app->plugins->installPlugin('keychain');
52
        }
53
    }
54
55
    protected function getProviderFields()
56
    {
57
        return [
58
            'id' => $this->primaryKey(),
59
            'label' => $this->string(64),
60
            'entityId' => $this->string()->notNull(),
61
            'metadata' => $this->mediumText()->notNull(),
62
            'sha256' => $this->string()->notNull(),
63
            'providerType' => $this->enum('providerType', [
64
                SettingsInterface::SP,
65
                SettingsInterface::IDP,
66
            ])->notNull(),
67
            'encryptAssertions' => $this->boolean()->defaultValue(false)->notNull(),
68
            'encryptionMethod' => $this->string(64)->null(),
69
            'groupOptions' => $this->text(),
70
            'metadataOptions' => $this->text(),
71
            'syncGroups' => $this->boolean()->defaultValue(true)->notNull(),
72
            'groupsAttributeName' => $this->string(64)->defaultValue(AbstractProvider::DEFAULT_GROUPS_ATTRIBUTE_NAME),
73
            'nameIdOverride' => $this->text(),
74
            'mapping' => $this->text(),
75
            'enabled' => $this->boolean()->defaultValue(true)->notNull(),
76
            'dateUpdated' => $this->dateTime()->notNull(),
77
            'dateCreated' => $this->dateTime()->notNull(),
78
            'uid' => $this->uid(),
79
        ];
80
    }
81
82
    /**
83
     * Creates the tables.
84
     *
85
     * @return void
86
     */
87
    protected function createTables()
88
    {
89
90
91
        $this->createTable($this->getProviderTableName(), $this->getProviderFields());
92
93
        $this->createTable(LinkRecord::tableName(), [
94
            'id' => $this->primaryKey(),
95
            'providerId' => $this->integer()->notNull(),
96
            'keyChainId' => $this->integer()->notNull(),
97
            'dateUpdated' => $this->dateTime()->notNull(),
98
            'dateCreated' => $this->dateTime()->notNull(),
99
            'uid' => $this->uid(),
100
        ]);
101
102
        $this->createTable($this->getIdentityTableName(), [
103
            'id' => $this->primaryKey(),
104
            'providerId' => $this->integer()->notNull(),
105
            'userId' => $this->integer()->notNull(),
106
            'nameId' => $this->string()->notNull(),
107
            'sessionId' => $this->string()->null(),
108
            'enabled' => $this->boolean()->defaultValue(true)->notNull(),
109
            'lastLoginDate' => $this->dateTime()->notNull(),
110
            'dateUpdated' => $this->dateTime()->notNull(),
111
            'dateCreated' => $this->dateTime()->notNull(),
112
            'uid' => $this->uid(),
113
        ]);
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function safeDown()
120
    {
121
122
123
        // Delete tables
124
        $this->dropTableIfExists($this->getIdentityTableName());
125
        $this->dropTableIfExists(LinkRecord::tableName());
126
        $this->dropTableIfExists($this->getProviderTableName());
127
        return true;
128
    }
129
130
    /**
131
     * Creates the indexes.
132
     *
133
     * @return void
134
     */
135
    protected function createIndexes()
136
    {
137
138
        $this->createIndex(
139
            $this->db->getIndexName($this->getProviderTableName(), 'entityId', false, true),
140
            $this->getProviderTableName(),
141
            'entityId',
142
            false
143
        );
144
        $this->createIndex(
145
            $this->db->getIndexName($this->getProviderTableName(), [
146
                'sha256',
147
            ], true, true),
148
            $this->getProviderTableName(),
149
            [
150
                'sha256',
151
            ],
152
            true
153
        );
154
        $this->createIndex(
155
            $this->db->getIndexName(LinkRecord::tableName(), [
156
                'providerId',
157
                'keyChainId',
158
            ], true, true),
159
            LinkRecord::tableName(),
160
            [
161
                'providerId',
162
                'keyChainId',
163
            ],
164
            true
165
        );
166
        $this->createIndex(
167
            $this->db->getIndexName($this->getIdentityTableName(), 'nameId', false, true),
168
            $this->getIdentityTableName(),
169
            'nameId',
170
            false
171
        );
172
173
        $this->createIndex(
174
            $this->db->getIndexName(
175
                $this->getIdentityTableName(),
176
                [
177
                    'providerId',
178
                    'nameId',
179
                    'userId',
180
                ],
181
                true
182
            ),
183
            $this->getIdentityTableName(),
184
            [
185
                'providerId',
186
                'nameId',
187
                'userId',
188
            ],
189
            true
190
        );
191
    }
192
193
    /**
194
     * Adds the foreign keys.
195
     *
196
     * @return void
197
     */
198
    protected function addForeignKeys()
199
    {
200
201
        /**
202
         * Link Provider
203
         */
204
        $this->addForeignKey(
205
            $this->db->getForeignKeyName(LinkRecord::tableName(), 'providerId'),
206
            LinkRecord::tableName(),
207
            'providerId',
208
            $this->getProviderTableName(),
209
            'id',
210
            'CASCADE'
211
        );
212
213
        /**
214
         * Link KeyChain
215
         */
216
        $this->addForeignKey(
217
            $this->db->getForeignKeyName(LinkRecord::tableName(), 'keyChainId'),
218
            LinkRecord::tableName(),
219
            'keyChainId',
220
            KeyChainRecord::tableName(),
221
            'id',
222
            'CASCADE'
223
        );
224
225
        $this->addForeignKey(
226
            $this->db->getForeignKeyName($this->getIdentityTableName(), 'userId'),
227
            $this->getIdentityTableName(),
228
            'userId',
229
            User::tableName(),
230
            'id',
231
            'CASCADE'
232
        );
233
        $this->addForeignKey(
234
            $this->db->getForeignKeyName($this->getIdentityTableName(), 'providerId'),
235
            $this->getIdentityTableName(),
236
            'providerId',
237
            $this->getProviderTableName(),
238
            'id',
239
            'CASCADE'
240
        );
241
    }
242
}
243