-
Notifications
You must be signed in to change notification settings - Fork 2
/
InitController.php
225 lines (168 loc) · 8.89 KB
/
InitController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace wdmg\rbac\commands;
use Yii;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\helpers\Console;
use yii\rbac\DbManager;
use \wdmg\rbac\rules\AuthorRule;
use \wdmg\rbac\rules\OwnerRule;
class InitController extends Controller
{
/**
* @inheritdoc
*/
public $choice = null;
/**
* @inheritdoc
*/
public $defaultAction = 'index';
public function options($actionID)
{
return ['choice', 'color', 'interactive', 'help'];
}
public function actionIndex($params = null)
{
$version = Yii::$app->controller->module->version;
$welcome =
'╔════════════════════════════════════════════════╗'. "\n" .
'║ ║'. "\n" .
'║ RBAC MODULE, v.'.$version.' ║'. "\n" .
'║ by Alexsander Vyshnyvetskyy ║'. "\n" .
'║ (c) 2019-2023 W.D.M.Group, Ukraine ║'. "\n" .
'║ ║'. "\n" .
'╚════════════════════════════════════════════════╝';
echo $name = $this->ansiFormat($welcome . "\n\n", Console::FG_GREEN);
echo "Select the operation you want to perform:\n";
echo " 1) Apply all module migrations\n";
echo " 2) Add base roles and rules for users\n";
echo " 3) Revert all module migrations\n";
echo "Your choice: ";
if(!is_null($this->choice))
$selected = $this->choice;
else
$selected = trim(fgets(STDIN));
if ($selected == "1") {
Yii::$app->runAction('migrate/up', ['migrationPath' => '@vendor/wdmg/yii2-rbac/migrations', 'interactive' => true]);
} else if($selected == "2") {
// Get auth manager
$authManager = Yii::$app->getAuthManager();
// Check if auth manager configured
if (!$authManager instanceof DbManager) {
echo $this->ansiFormat("Error! You should configure \"authManager\" component to use database before add roles and rules.\n\n", Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
echo $this->ansiFormat("\nDelete the old data from the db... ", Console::FG_YELLOW);
// Delete the old data from the db
if($authManager->removeAll())
echo $this->ansiFormat("Done.\n", Console::FG_GREEN);
else
echo $this->ansiFormat("Error!\n", Console::FG_RED);
// Create and add the roles of admin, editor, manager and simple users
echo $this->ansiFormat("Create and add the roles... ", Console::FG_YELLOW);
$role = $authManager->createRole('admin');
$role->description = 'Administrator role';
$role->createdAt = \date("Y-m-d H:i:s");
$role->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($role);
$role = $authManager->createRole('editor');
$role->description = 'Editor role';
$role->createdAt = \date("Y-m-d H:i:s");
$role->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($role);
$role = $authManager->createRole('manager');
$role->description = 'Content manager role';
$role->createdAt = \date("Y-m-d H:i:s");
$role->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($role);
$role = $authManager->createRole('user');
$role->description = 'Default user role';
$role->createdAt = \date("Y-m-d H:i:s");
$role->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($role);
$role = $authManager->createRole('banned');
$role->description = 'Blocked user role';
$role->createdAt = \date("Y-m-d H:i:s");
$role->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($role);
echo $this->ansiFormat("Done.\n", Console::FG_GREEN);
// Create and add permissions
echo $this->ansiFormat("Create and add permissions... ", Console::FG_YELLOW);
$permission = $authManager->createPermission('viewDashboard');
$permission->description = 'Access permission to view all of modules in dashboard';
$permission->createdAt = \date("Y-m-d H:i:s");
$permission->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($permission);
$permission = $authManager->createPermission('updateDashboard');
$permission->description = 'Access permission to edit all of modules in dashboard';
$permission->createdAt = \date("Y-m-d H:i:s");
$permission->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($permission);
$permission = $authManager->createPermission('updatePosts');
$permission->description = 'Access permission to edit all posts';
$permission->createdAt = \date("Y-m-d H:i:s");
$permission->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($permission);
echo $this->ansiFormat("Done.\n", Console::FG_GREEN);
// Create and add custom access rules
echo $this->ansiFormat("Create access rules... ", Console::FG_YELLOW);
$rule = new AuthorRule();
$rule->createdAt = \date("Y-m-d H:i:s");
$rule->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($rule);
$permission = $authManager->createPermission('updateOwnerPosts');
$permission->description = 'Access permission to edit posts by owner';
$permission->ruleName = $rule->name;
$permission->createdAt = \date("Y-m-d H:i:s");
$permission->updatedAt = \date("Y-m-d H:i:s");
$authManager->add($permission);
echo $this->ansiFormat("Done.\n\n", Console::FG_GREEN);
// Add permissions as childs of roles
echo $this->ansiFormat("Add permissions as childs of roles... ", Console::FG_YELLOW);
$admin = $authManager->getRole('admin');
$manager = $authManager->getRole('editor');
$editor = $authManager->getRole('manager');
$viewDashboard = $authManager->getPermission('viewDashboard');
$updateDashboard = $authManager->getPermission('updateDashboard');
// Content manager can view everything
$authManager->addChild($manager, $viewDashboard);
// Content manager can update self posts
$updatePosts = $authManager->getPermission('updatePosts');
$updateOwnerPosts = $authManager->getPermission('updateOwnerPosts');
$authManager->addChild($updateOwnerPosts, $updatePosts);
// Editor can update everything posts
$authManager->addChild($editor, $updatePosts);
// Editor can do everything that a content manager can
$authManager->addChild($editor, $manager);
// Anyone who can edit everything can also view everything
$authManager->addChild($updateDashboard, $viewDashboard);
// Editor can edit everything
$authManager->addChild($admin, $updateDashboard);
// Administrator can do everything that a content manager can
$authManager->addChild($admin, $updatePosts);
$authManager->addChild($admin, $editor);
echo $this->ansiFormat("Done.\n\n", Console::FG_GREEN);
// Assign roles to users
echo $this->ansiFormat("Assign roles to users... ", Console::FG_YELLOW);
$admin = $authManager->getRole('admin');
$authManager->assign($admin, 100);
$editor = $authManager->getRole('editor');
$authManager->assign($editor, 101);
$manager = $authManager->getRole('manager');
$authManager->assign($manager, 102);
$user = $authManager->getRole('user');
$authManager->assign($user, 103);
$banned = $authManager->getRole('banned');
$authManager->assign($banned, 104);
echo $this->ansiFormat("Done.\n\n", Console::FG_GREEN);
echo $this->ansiFormat("Base roles and rules for users inserted successfully.\n\n", Console::FG_GREEN);
} else if($selected == "3") {
Yii::$app->runAction('migrate/down', ['migrationPath' => '@vendor/wdmg/yii2-rbac/migrations', 'interactive' => true]);
} else {
echo $this->ansiFormat("Error! Your selection has not been recognized.\n\n", Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
echo "\n";
return ExitCode::OK;
}
}