SchemaFactory::getPermissionsSchema()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 37
c 1
b 0
f 1
rs 8.8571
cc 2
eloc 27
nc 1
nop 0
1
<?php
2
/**
3
 * CakePHP permission handling library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\CakePermission;
7
8
use Cake\Core\Configure;
9
use Cake\Database\Schema\TableSchema;
10
11
class SchemaFactory
12
{
13
    /**
14
     * Gets the users table schema
15
     * @return TableSchema
16
     */
17
    public static function getUsersSchema()
18
    {
19
        $schema = new TableSchema(Configure::read('Permission.tableNameMap.users') ?: 'users');
20
21
        $schema->addColumn('id', [
22
            'type' => 'integer',
23
            'autoIncrement' => true
24
        ])
25
            ->addConstraint('primary', [
26
                'type' => 'primary',
27
                'columns' => ['id']
28
            ]);
29
        $schema->addColumn('name', [
30
            'type' => 'string',
31
            'length'  => 255,
32
            'default' => '',
33
            'null' => false
34
        ]);
35
        return $schema;
36
    }
37
38
    /**
39
     * Gets the roles table schema
40
     * @return TableSchema
41
     */
42
    public static function getRolesSchema()
43
    {
44
        $schema = new TableSchema(Configure::read('Permission.tableNameMap.roles') ?: 'roles');
45
46
        $schema->addColumn('id', [
47
                'type' => 'integer',
48
                'autoIncrement' => true
49
            ])
50
            ->addConstraint('primary', [
51
                'type' => 'primary',
52
                'columns' => ['id']
53
            ]);
54
        $schema->addColumn('name', [
55
            'type' => 'string',
56
            'length'  => 255,
57
            'default' => '',
58
            'null' => false
59
        ]);
60
        $schema->addColumn('slug', [
61
            'type' => 'string',
62
            'length'  => 255,
63
            'default' => '',
64
            'null' => false
65
        ]);
66
        $schema->addColumn('created', [
67
            'type' => 'datetime',
68
            'default' => null,
69
            'null' => false
70
        ]);
71
        $schema->addColumn('modified', [
72
            'type' => 'datetime',
73
            'default' => null,
74
            'null' => false
75
        ]);
76
        return $schema;
77
    }
78
79
    /**
80
     * Gets the roles table schema
81
     * @return TableSchema
82
     */
83
    public static function getPermissionsSchema()
84
    {
85
        $schema = new TableSchema(Configure::read('Permission.tableNameMap.permissions') ?: 'permissions');
86
87
        $schema->addColumn('id', [
88
            'type' => 'integer',
89
            'autoIncrement' => true
90
        ])
91
            ->addConstraint('primary', [
92
                'type' => 'primary',
93
                'columns' => ['id']
94
            ]);
95
96
        $schema->addColumn('name', [
97
            'type' => 'string',
98
            'length'  => 255,
99
            'default' => '',
100
            'null' => false
101
        ]);
102
        $schema->addColumn('slug', [
103
            'type' => 'string',
104
            'length'  => 255,
105
            'default' => '',
106
            'null' => false
107
        ]);
108
        $schema->addColumn('created', [
109
            'type' => 'datetime',
110
            'default' => null,
111
            'null' => false
112
        ]);
113
        $schema->addColumn('modified', [
114
            'type' => 'datetime',
115
            'default' => null,
116
            'null' => false
117
        ]);
118
        return $schema;
119
    }
120
121
    /**
122
     * Gets the join table schema between users and roles
123
     * @return TableSchema
124
     */
125
    public static function getUsersRolesSchema()
126
    {
127
        $schema = new TableSchema(Configure::read('Permission.tableNameMap.users_roles') ?: 'users_roles');
128
129
        $schema->addColumn('user_id', [
130
            'type' => 'integer',
131
            'default' => null,
132
            'null' => false
133
        ]);
134
135
        $schema->addColumn('role_id', [
136
            'type' => 'integer',
137
            'default' => null,
138
            'null' => false
139
        ]);
140
141
        $schema->addConstraint('primary', [
142
            'type' => 'primary', 'columns' => ['user_id', 'role_id']
143
        ]);
144
        return $schema;
145
    }
146
147
    /**
148
     * Gets the join table schema between roles and permissions
149
     * @return TableSchema
150
     */
151
    public static function getRolesPermissionsSchema()
152
    {
153
        $schema = new TableSchema(Configure::read('Permission.tableNameMap.roles_permissions') ?: 'roles_permissions');
154
155
        $schema->addColumn('role_id', [
156
            'type' => 'integer',
157
            'default' => null,
158
            'null' => false
159
        ]);
160
161
        $schema->addColumn('permission_id', [
162
            'type' => 'integer',
163
            'default' => null,
164
            'null' => false
165
        ]);
166
167
        $schema->addConstraint('primary', [
168
            'type' => 'primary', 'columns' => ['role_id', 'permission_id']
169
        ]);
170
        return $schema;
171
    }
172
}