InitController.php
8.41 KB
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
<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
use backend\models\Admin;
/**
* Site controller
*/
class InitController extends Controller
{
/**
* 创建/修改后台用户
*/
public function actionAdmin()
{
$this->stdout("1: Create admin account\n2: Modify admin account\n");
$action = $this->prompt('Which:');
if ($action == 1){
echo "创建一个新用户 ...\n"; // 提示当前操作
$username = $this->prompt('User Name:'); // 接收用户名
$email = $this->prompt('Email:'); // 接收Email
$password = $this->prompt('Password:'); // 接收密码
$model = new Admin(); // 创建一个新用户
$model->username = $username; // 完成赋值
$model->email = $email;
$model->setPassword($password);
$model->generateAuthKey();
$model->generatePasswordResetToken();
}elseif ($action == 2){
echo "修改一个已存在用户 ...\n"; // 提示当前操作
$username = $this->prompt('User Name:'); // 接收用户名
if (!$username){
echo '请输入用户名';
return 1;
}
// 修改一个已存在用户
if (!$model = Admin::findByUsername($username)){
echo '找不到该用户';
return 1;
}
$email = $this->prompt('Email:'); // 接收Email
$password = $this->prompt('Password:'); // 接收密码
if (!$email && !$password){
echo '没有任何修改';
return 0;
}
if ($email){
$model->email = $email;
}
if ($password){
$model->setPassword($password);
$model->generateAuthKey();
$model->generatePasswordResetToken();
}
}else{
$this->stderr('Not a predefined option');
return 1;
}
if (!$model->save()) // 保存新的用户
{
foreach ($model->getErrors() as $error) // 如果保存失败,说明有错误,那就输出错误信息。
{
foreach ($error as $e)
{
echo "$e\n";
}
}
return 1; // 命令行返回1表示有异常
}
return 0; // 返回0表示一切OK
}
public function actionTest(){
echo "init/test";
}
/**
* 创建/移除后台权限标签(controller/action)
*/
public function actionRbac(){
$this->stdout("1: Create permission\n2: Remove permission\n3: Create role\n4: Remove role\n5: Grant permission to a role\n6: Revoke permission from a role\n7: Grant role to a user\n8: Revoke role from a a\n");
$action = $this->prompt('Which:');
$auth = Yii::createObject('yii\rbac\DbManager');
//$auth = Yii::$app->authManager;
if ($action == 1){
echo "创建一个新权限标签 ...\n"; // 提示当前操作
$name = $this->prompt('Permission Name:'); // 权限名称
$description = $this->prompt('Description(default is as name):'); // 描述
if ($name){
$permission = $auth->createPermission($name);
$permission->description = $description ? : ucwords(strtr($name,array('/'=>' ')));
if ($auth->add($permission)){
$this->stdout('create ok');
}
}
}elseif ($action == 2){
echo "移除一个权限标签 ...\n"; // 提示当前操作
$name = $this->prompt('Permission Name:'); // 权限名称
if ($name){
if (!$permission = $auth->getPermission($name)){
$this->stderr('Not a predefined permission');
return 1;
}
if ($auth->remove($permission)){
$this->stdout('remove ok');
}
}
}elseif ($action == 3){
echo "创建一个新角色 ...\n"; // 提示当前操作
$name = $this->prompt('Role Name:'); // 角色名称
$description = $this->prompt('Description(default is as name):'); // 描述
if ($name){
$role = $auth->createRole($name);
$role->description = $description ? : ucfirst($name);
if ($auth->add($role)){
$this->stdout('create ok');
}
}
}elseif ($action == 4){
echo "移除一个角色 ...\n"; // 提示当前操作
$name = $this->prompt('Role Name:'); // 角色名称
if ($name){
if (!$role = $auth->getRole($name)){
$this->stderr('Not a predefined role');
return 1;
}
if ($auth->remove($role)){
$this->stdout('remove ok');
}
}
}elseif ($action == 5){
echo "向某个角色赋予权限 ...\n"; // 提示当前操作
$name = $this->prompt('Role Name:'); // 接收用户名
if (!$role = $auth->getRole($name)){
$this->stderr('Not a predefined role');
return 1;
}
$name = $this->prompt('Permission Name:'); // 接收用户名
if (!$permission = $auth->getPermission($name)){
$this->stderr('Not a predefined permission');
return 1;
}
if ($auth->addChild($role,$permission)){
$this->stdout('grant permission ok');
}
}elseif ($action == 6){
echo "从某个角色剥夺权限 ...\n"; // 提示当前操作
$name = $this->prompt('Role Name:'); // 接收用户名
if (!$role = $auth->getRole($name)){
$this->stderr('Not a predefined role');
return 1;
}
$name = $this->prompt('Permission Name:'); // 接收用户名
if (!$permission = $auth->getPermission($name)){
$this->stderr('Not a predefined permission');
return 1;
}
if ($auth->removeChild($role,$permission)){
$this->stdout('revoke permission ok');
}
}elseif ($action == 7){
echo "向某个用户赋予角色 ...\n"; // 提示当前操作
$username = $this->prompt('User Name:'); // 接收用户名
if (!$user = Admin::findByUsername($username)){
echo '找不到该用户';
return 1;
}
$name = $this->prompt('Role Name:'); // 接收用户名
if (!$role = $auth->getRole($name)){
$this->stderr('Not a predefined role');
return 1;
}
if ($auth->assign($role,$user->getId())){
$this->stdout('grant role ok');
}
}elseif ($action == 8){
echo "从某个用户剥夺角色 ...\n"; // 提示当前操作
$username = $this->prompt('User Name:'); // 接收用户名
if (!$user = Admin::findByUsername($username)){
echo '找不到该用户';
return 1;
}
$name = $this->prompt('Role Name:'); // 接收用户名
if (!$role = $auth->getRole($name)){
$this->stderr('Not a predefined role');
return 1;
}
if ($auth->revoke($role,$user->getId())){
$this->stdout('revoke role ok');
}
}else{
$this->stderr('Not a predefined option');
return 1;
}
return 0;
}
}