Completed
Push — master ( 80f949...488989 )
by Xu
63:17 queued 57:43
created

m180301_104125_rbac_init   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 4 1
B up() 0 24 1
A getAuthManager() 0 8 2
1
<?php
2
3
use yii\base\InvalidConfigException;
4
use yii\db\Migration;
5
use yii\rbac\DbManager;
6
use yuncms\rbac\rules\RouteRule;
7
use yuncms\rbac\rules\GuestRule;
8
9
class m180301_104125_rbac_init extends Migration
10
{
11
    /**
12
     * @throws yii\base\InvalidConfigException
13
     * @return DbManager
14
     */
15
    protected function getAuthManager()
16
    {
17
        $authManager = Yii::$app->getAuthManager();
18
        if (!$authManager instanceof DbManager) {
19
            throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
20
        }
21
22
        return $authManager;
23
    }
24
25
    /*
26
    public function safeUp()
27
    {
28
29
    }
30
31
    public function safeDown()
32
    {
33
34
    }*/
35
36
    // Use up()/down() to run migration code without a transaction.
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function up()
42
    {
43
        $authManager = $this->getAuthManager();
44
45
        $routeRule = new RouteRule();
46
        $authManager->add($routeRule);
47
48
        $guestRule = new GuestRule();
49
        $authManager->add($guestRule);
50
51
        $superAdmin = $authManager->createRole('SuperAdministrator');
52
        $superAdmin->description = '超级管理员对系统有不受限制的完全访问权。';
53
        $superAdmin->ruleName = $routeRule->name;
54
        $authManager->add($superAdmin);
55
56
        $admin = $authManager->createRole('Administrator');
57
        $admin->description = '防止管理员进行有意或无意的系统范围的更改,但是可以执行大部分管理操作。';
58
        $admin->ruleName = $routeRule->name;
59
        $authManager->add($admin);
60
61
        $defaultPermission = $authManager->createPermission('/*');
62
        $authManager->add($defaultPermission);
63
64
        $authManager->addChild($superAdmin, $defaultPermission);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function down()
71
    {
72
        $authManager = $this->getAuthManager();
73
        $authManager->removeAll();
74
    }
75
76
}
77