Completed
Push — master ( 71c7f7...f45e5c )
by vistart
04:22
created

M170310150337CreateAuthTables   B

Complexity

Total Complexity 7

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 16
dl 0
loc 150
rs 8.4614

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthManager() 0 8 2
A down() 0 9 1
A addRules() 0 19 1
B addRoles() 0 40 1
A up() 0 52 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\rbac\migrations;
14
15
use rhosocial\user\migrations\Migration;
16
use rhosocial\user\rbac\roles\Admin;
17
use rhosocial\user\rbac\roles\User;
18
use rhosocial\user\rbac\rules\CreateUserRule;
19
use rhosocial\user\rbac\rules\DeleteMyselfRule;
20
use rhosocial\user\rbac\rules\DeleteUserRule;
21
use rhosocial\user\rbac\rules\ViewUserRule;
22
use rhosocial\user\rbac\rules\UpdateMyselfRule;
23
use rhosocial\user\rbac\rules\UpdateUserRule;
24
use rhosocial\user\rbac\permissions\CreateAdminUser;
25
use rhosocial\user\rbac\permissions\CreateUser;
26
use rhosocial\user\rbac\permissions\DeleteAdminUser;
27
use rhosocial\user\rbac\permissions\DeleteMyself;
28
use rhosocial\user\rbac\permissions\DeleteUser;
29
use rhosocial\user\rbac\permissions\ViewUser;
30
use rhosocial\user\rbac\permissions\UpdateAdminUser;
31
use rhosocial\user\rbac\permissions\UpdateMyself;
32
use rhosocial\user\rbac\permissions\UpdateUser;
33
use Yii;
34
use yii\base\InvalidConfigException;
35
use yii\rbac\DbManager;
36
37
/**
38
 * Create following four tables in order:
39
 * `{{%auth_rule}}`
40
 * `{{%auth_item}}`
41
 * `{{%auth_item_child}}`
42
 * `{{%auth_assignment}}`
43
 *
44
```SQL
45
CREATE TABLE `auth_rule` (
46
  `name` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Rule Name',
47
  `data` blob,
48
  `created_at` datetime NOT NULL,
49
  `updated_at` datetime NOT NULL,
50
  PRIMARY KEY (`name`)
51
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Rule';
52
53
CREATE TABLE `auth_item` (
54
  `name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
55
  `type` smallint(6) NOT NULL,
56
  `description` text COLLATE utf8_unicode_ci COMMENT 'Description',
57
  `rule_name` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Rule Name',
58
  `data` blob,
59
  `color` int(11) NOT NULL DEFAULT '-1' COMMENT 'Color',
60
  `created_at` datetime NOT NULL,
61
  `updated_at` datetime NOT NULL,
62
  PRIMARY KEY (`name`),
63
  KEY `rule_name_fk` (`rule_name`),
64
  KEY `idx-auth_item-type` (`type`),
65
  CONSTRAINT `rule_name_fk` FOREIGN KEY (`rule_name`) REFERENCES `auth_rule` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
66
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Item';
67
68
CREATE TABLE `auth_item_child` (
69
  `parent` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
70
  `child` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
71
  PRIMARY KEY (`parent`,`child`),
72
  KEY `child_name_fk` (`child`),
73
  CONSTRAINT `child_name_fk` FOREIGN KEY (`child`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,
74
  CONSTRAINT `parent_name_fk` FOREIGN KEY (`parent`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
75
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Item Child';
76
77
CREATE TABLE `auth_assignment` (
78
  `item_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
79
  `user_guid` varbinary(16) NOT NULL,
80
  `created_at` datetime NOT NULL,
81
  `failed_at` datetime DEFAULT NULL,
82
  PRIMARY KEY (`item_name`,`user_guid`),
83
  KEY `user_assignment_fk` (`user_guid`),
84
  CONSTRAINT `user_assignment_fk` FOREIGN KEY (`user_guid`) REFERENCES `user` (`guid`) ON DELETE CASCADE ON UPDATE CASCADE
85
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Assignment';
86
``
87
 *
88
 * @codeCoverageIgnore
89
 * @version 1.0
90
 * @author vistart <[email protected]>
91
 */
92
class M170310150337CreateAuthTables extends Migration
93
{
94
    /**
95
     * @throws yii\base\InvalidConfigException
96
     * @return DbManager
97
     */
98
    protected function getAuthManager()
99
    {
100
        $authManager = Yii::$app->getAuthManager();
101
        if (!$authManager instanceof DbManager) {
102
            throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
103
        }
104
        return $authManager;
105
    }
106
    
107
    public function up()
108
    {
109
        $authManager = $this->getAuthManager();
110
        $this->db = $authManager->db;
111
112
        $tableOptions = null;
0 ignored issues
show
Unused Code introduced by
$tableOptions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
        if ($this->db->driverName === 'mysql') {
114
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
115
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
116
117
            $this->createTable($authManager->ruleTable, [
118
                'name' => $this->varchar(64)->notNull()->comment('Rule Name'),
119
                'data' => $this->blob(),
120
                'created_at' => $this->dateTime()->notNull(),
121
                'updated_at' => $this->dateTime()->notNull(),
122
            ], $tableOptions . " COMMENT 'Auth Rule'");
123
            $this->addPrimaryKey('rule_name_pk', $authManager->ruleTable, 'name');
124
125
            $this->createTable($authManager->itemTable, [
126
                'name' => $this->varchar(64)->notNull(),
127
                'type' => $this->smallInteger()->notNull(),
128
                'description' => $this->text()->comment('Description'),
129
                'rule_name' => $this->varchar(64)->comment('Rule Name'),
130
                'data' => $this->blob(),
131
                'color' => $this->integer()->defaultValue(-1)->notNull()->comment('Color'),
132
                'created_at' => $this->dateTime()->notNull(),
133
                'updated_at' => $this->dateTime()->notNull(),
134
            ], $tableOptions . " COMMENT 'Auth Item'");
135
            $this->addPrimaryKey('item_name_pk', $authManager->itemTable, 'name');
136
            $this->addForeignKey('rule_name_fk', $authManager->itemTable, 'rule_name', $authManager->ruleTable, 'name', 'CASCADE', 'CASCADE');
137
            $this->createIndex('idx-auth_item-type', $authManager->itemTable, 'type');
138
139
            $this->createTable($authManager->itemChildTable, [
140
                'parent' => $this->varchar(64)->notNull(),
141
                'child' => $this->varchar(64)->notNull(),
142
            ], $tableOptions . " COMMENT 'Auth Item Child'");
143
            $this->addPrimaryKey('parent_child_pk', $authManager->itemChildTable, ['parent', 'child']);
144
            $this->addForeignKey('parent_name_fk', $authManager->itemChildTable, 'parent', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE');
145
            $this->addForeignKey('child_name_fk', $authManager->itemChildTable, 'child', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE');
146
147
            $this->createTable($authManager->assignmentTable, [
148
                'item_name' => $this->varchar(64)->notNull(),
149
                'user_guid' => $this->varbinary(16)->notNull(),
150
                'created_at' => $this->dateTime()->notNull(),
151
                'failed_at' => $this->dateTime(),
152
            ], $tableOptions . " COMMENT 'Auth Assignment'");
153
            $this->addPrimaryKey('user_item_name_pk', $authManager->assignmentTable, ['item_name', 'user_guid']);
154
            $this->addForeignKey('user_assignment_fk', $authManager->assignmentTable, 'user_guid', '{{%user}}', 'guid', 'CASCADE', 'CASCADE');
155
        }
156
        //$this->addRules();
157
        $this->addRoles();
158
    }
159
160
    public function down()
161
    {
162
        $authManager = $this->getAuthManager();
163
        $this->db = $authManager->db;
164
        $this->dropTable($authManager->assignmentTable);
165
        $this->dropTable($authManager->itemChildTable);
166
        $this->dropTable($authManager->itemTable);
167
        $this->dropTable($authManager->ruleTable);
168
    }
169
    
170
    protected function addRules()
171
    {
172
        $authManager = $this->getAuthManager();
173
        $this->db = $authManager->db;
174
        
175
        $createUserRule = new CreateUserRule();
176
        $deleteMyselfRule = new DeleteMyselfRule();
177
        $deleteUserRule = new DeleteUserRule();
178
        $viewUserRule = new ViewUserRule();
179
        $updateMyselfRule = new UpdateMyselfRule();
180
        $updateUserRule = new UpdateUserRule();
181
        
182
        $authManager->add($createUserRule);
183
        $authManager->add($deleteMyselfRule);
184
        $authManager->add($deleteUserRule);
185
        $authManager->add($viewUserRule);
186
        $authManager->add($updateMyselfRule);        
187
        $authManager->add($updateUserRule);
188
    }
189
    
190
    protected function addRoles()
191
    {
192
        $authManager = $this->getAuthManager();
193
        $this->db = $authManager->db;
194
        
195
        $createUser = new CreateUser();
196
        $viewUser = new ViewUser();
197
        $updateUser = new UpdateUser();
198
        $deleteUser = new DeleteUser();
199
        $updateMyself = new UpdateMyself();
200
        $deleteMyself = new DeleteMyself();
201
        $createAdminUser = new CreateAdminUser();
202
        $updateAdminUser = new UpdateAdminUser();
203
        $deleteAdminUser = new DeleteAdminUser();
204
        
205
        $authManager->add($createUser);
0 ignored issues
show
Documentation introduced by
$createUser is of type object<rhosocial\user\rb...permissions\CreateUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
206
        $authManager->add($viewUser);
0 ignored issues
show
Documentation introduced by
$viewUser is of type object<rhosocial\user\rbac\permissions\ViewUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
207
        $authManager->add($updateUser);
0 ignored issues
show
Documentation introduced by
$updateUser is of type object<rhosocial\user\rb...permissions\UpdateUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
208
        $authManager->add($deleteUser);
0 ignored issues
show
Documentation introduced by
$deleteUser is of type object<rhosocial\user\rb...permissions\DeleteUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
209
        $authManager->add($updateMyself);
0 ignored issues
show
Documentation introduced by
$updateMyself is of type object<rhosocial\user\rb...rmissions\UpdateMyself>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
210
        $authManager->add($deleteMyself);
0 ignored issues
show
Documentation introduced by
$deleteMyself is of type object<rhosocial\user\rb...rmissions\DeleteMyself>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
211
        $authManager->add($createAdminUser);
0 ignored issues
show
Documentation introduced by
$createAdminUser is of type object<rhosocial\user\rb...ssions\CreateAdminUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
        $authManager->add($updateAdminUser);
0 ignored issues
show
Documentation introduced by
$updateAdminUser is of type object<rhosocial\user\rb...ssions\UpdateAdminUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
213
        $authManager->add($deleteAdminUser);
0 ignored issues
show
Documentation introduced by
$deleteAdminUser is of type object<rhosocial\user\rb...ssions\DeleteAdminUser>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
214
        
215
        $admin = new Admin();
216
        $user = new User();
217
        
218
        $authManager->add($admin);
0 ignored issues
show
Documentation introduced by
$admin is of type object<rhosocial\user\rbac\roles\Admin>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
219
        $authManager->add($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<rhosocial\user\rbac\roles\User>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
220
        
221
        $authManager->addChild($user, $updateMyself);
222
        $authManager->addChild($user, $deleteMyself);
223
        $authManager->addChild($admin, $user);
224
        
225
        $authManager->addChild($admin, $createUser);
226
        $authManager->addChild($admin, $viewUser);
227
        $authManager->addChild($admin, $updateUser);
228
        $authManager->addChild($admin, $deleteUser);
229
    }
230
231
    /*
232
    // Use safeUp/safeDown to run migration code within a transaction
233
    public function safeUp()
234
    {
235
    }
236
237
    public function safeDown()
238
    {
239
    }
240
    */
241
}
242