Skip to content

Commit 9606433

Browse files
committed
add(role): 权限和角色相关后台处理内容
1 parent 4cd8c26 commit 9606433

File tree

6 files changed

+496
-0
lines changed

6 files changed

+496
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Permission\Store;
7+
use App\Http\Requests\Permission\Update;
8+
use App\Permission;
9+
use App\Role;
10+
use Illuminate\Http\Request;
11+
12+
13+
class PermissionController extends Controller
14+
{
15+
16+
/**
17+
* @var int 默认分页条数
18+
*/
19+
public $perPage = 10;
20+
21+
public function __construct(Request $request)
22+
{
23+
// $this->middleware(['auth', 'isAdmin']); // isAdmin 中间件让具备指定权限的用户才能访问该资源
24+
25+
$perPage = intval($request->input('perPage'));
26+
$this->perPage = $perPage ?? 11;
27+
}
28+
29+
/**
30+
* 权限列表
31+
*
32+
* @return \Illuminate\Http\Response
33+
*/
34+
public function index()
35+
{
36+
$list = Permission::orderBy('sort')->paginate($this->perPage);
37+
return $this->out(200, $list);
38+
}
39+
40+
/**
41+
* 显示创建权限表单
42+
*
43+
* @return \Illuminate\Http\Response
44+
*/
45+
public function create()
46+
{
47+
$roles = Role::get(); // 获取所有角色
48+
49+
return $this->out(200, ['roles' => $roles, 'method' => 'create']);
50+
}
51+
52+
/**
53+
* 保存新创建的权限
54+
*
55+
* @param Store $store
56+
*
57+
* @return \Illuminate\Http\Response
58+
*/
59+
public function store(Store $request)
60+
{
61+
$name = $request['name'];
62+
$permission = new Permission();
63+
$permission->name = $name;
64+
65+
if ($permission->save()) {
66+
if (!empty($request['roles'])) { // 如果选择了角色
67+
$roles = $request['roles'];
68+
foreach ($roles as $role) {
69+
$r = Role::where('id', '=', $role)->firstOrFail(); // 将输入角色和数据库记录进行匹配
70+
71+
$permission = Permission::where('name', '=', $name)->first(); // 将输入权限与数据库记录进行匹配
72+
$r->givePermissionTo($permission);
73+
}
74+
}
75+
76+
return $this->out(200, ['data' => ['id' => $permission->id]]);
77+
} else {
78+
return $this->out(4000);
79+
}
80+
81+
}
82+
83+
/**
84+
* 显示给定权限
85+
*
86+
* @param Permission $permission
87+
*
88+
* @return \Illuminate\Http\Response
89+
*/
90+
public function show(Permission $permission)
91+
{
92+
return $this->out(200, $permission);
93+
}
94+
95+
/**
96+
* 显示编辑权限表单
97+
*
98+
* @param int $id
99+
* @return \Illuminate\Http\Response
100+
*/
101+
public function edit($id)
102+
{
103+
$permission = Permission::findOrFail($id);
104+
105+
return $this->out(200, $permission);
106+
}
107+
108+
/**
109+
* Update the specified resource in storage.
110+
* 更新数据
111+
*
112+
* @param Update $request
113+
* @param int $id
114+
* @return \Illuminate\Http\Response
115+
*/
116+
public function update(Update $request, $id)
117+
{
118+
$input = $request->all();
119+
// $model = new Category();$model->save($input, ['id' => $id]);
120+
// 老版本更新操作如下,新版本先查询再更新
121+
// Category::where('id', $id)->update($input)
122+
$model = Permission::findOrFail($id);
123+
if ($model->update($input)) {
124+
return $this->out(200, ['data' => ['id' => $id]]);
125+
} else {
126+
return $this->out(4000);
127+
}
128+
}
129+
130+
/**
131+
* 删除给定权限
132+
*
133+
* @param int $id
134+
* @return \Illuminate\Http\Response
135+
*/
136+
public function destroy($id)
137+
{
138+
$permission = Permission::findOrFail($id);
139+
140+
// 让特定权限无法删除
141+
if ($permission->name == "Administer roles & permissions") {
142+
return $this->out(200, [], 'Cannot delete this Permission!');
143+
}
144+
145+
if ($permission->delete()) {
146+
$data = ['msg' => '删除成功', 'errno' => 0];
147+
} else {
148+
$data = ['msg' => '删除失败', 'errno' => 2];
149+
}
150+
return $this->out(200, $data);
151+
152+
}
153+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Role\Store;
7+
use App\Http\Requests\Role\Update;
8+
use App\Permission;
9+
use App\Role;
10+
use Illuminate\Http\Request;
11+
12+
13+
class RoleController extends Controller
14+
{
15+
16+
/**
17+
* @var int 默认分页条数
18+
*/
19+
public $perPage = 10;
20+
21+
public function __construct(Request $request)
22+
{
23+
// $this->middleware(['auth', 'isAdmin']); // isAdmin 中间件让具备指定权限的用户才能访问该资源
24+
25+
$perPage = intval($request->input('perPage'));
26+
$this->perPage = $perPage ?? 11;
27+
}
28+
29+
/**
30+
* 角色列表
31+
*
32+
* @return \Illuminate\Http\Response
33+
*/
34+
public function index()
35+
{
36+
$list = Role::orderBy('sort')->paginate($this->perPage);
37+
return $this->out(200, $list);
38+
}
39+
40+
/**
41+
* 显示创建角色
42+
*
43+
* @return \Illuminate\Http\Response
44+
*/
45+
public function create()
46+
{
47+
$roles = Permission::get(); // 获取所有权限
48+
49+
return $this->out(200, ['roles' => $roles, 'method' => 'create']);
50+
}
51+
52+
/**
53+
* 保存新创建的角色
54+
*
55+
* @param Store $store
56+
*
57+
* @return \Illuminate\Http\Response
58+
*/
59+
public function store(Store $request)
60+
{
61+
$name = $request['name'];
62+
$role = new Role();
63+
$role->name = $name;
64+
65+
if ($role->save()) {
66+
67+
$permissions = $request['permissions'];
68+
// 遍历选择的权限
69+
foreach ($permissions as $permission) {
70+
$p = Permission::where('id', '=', $permission)->firstOrFail();
71+
// 获取新创建的角色并分配权限
72+
$role = Role::where('name', '=', $name)->first();
73+
$role->givePermissionTo($p);
74+
}
75+
76+
return $this->out(200, ['data' => ['id' => $role->id]]);
77+
} else {
78+
return $this->out(4000);
79+
}
80+
81+
}
82+
83+
/**
84+
* 显示给定角色
85+
*
86+
* @param Role $role
87+
*
88+
* @return \Illuminate\Http\Response
89+
*/
90+
public function show(Role $role)
91+
{
92+
return $this->out(200, $role);
93+
}
94+
95+
/**
96+
* 显示编辑角色
97+
*
98+
* @param int $id
99+
* @return \Illuminate\Http\Response
100+
*/
101+
public function edit($id)
102+
{
103+
$role = Role::findOrFail($id);
104+
$permissions = Permission::all();
105+
106+
return $this->out(200, [$role, $permissions]);
107+
}
108+
109+
/**
110+
* Update the specified resource in storage.
111+
* 更新数据
112+
*
113+
* @param Update $request
114+
* @param int $id
115+
* @return \Illuminate\Http\Response
116+
*/
117+
public function update(Update $request, $id)
118+
{
119+
$role = Role::findOrFail($id); // 通过给定id获取角色
120+
// 验证 name 和 permission 字段
121+
$this->validate($request, [
122+
'name' => 'required|max:10|unique:roles,name,'.$id,
123+
'permissions' => 'required',
124+
]);
125+
126+
$input = $request->except(['permissions']);
127+
$permissions = $request['permissions'];
128+
if ($role->fill($input)->save()) {
129+
130+
$p_all = Permission::all();//获取所有权限
131+
132+
foreach ($p_all as $p) {
133+
$role->revokePermissionTo($p); // 移除与角色关联的所有权限
134+
}
135+
136+
foreach ($permissions as $permission) {
137+
$p = Permission::where('id', '=', $permission)->firstOrFail(); //从数据库中获取相应权限
138+
$role->givePermissionTo($p); // 分配权限到角色
139+
}
140+
141+
return $this->out(200, ['data' => ['id' => $id]]);
142+
} else {
143+
return $this->out(4000);
144+
}
145+
}
146+
147+
/**
148+
* 删除给定角色
149+
*
150+
* @param int $id
151+
* @return \Illuminate\Http\Response
152+
*/
153+
public function destroy($id)
154+
{
155+
$role = Role::findOrFail($id);
156+
157+
if ($role->delete()) {
158+
$data = ['msg' => '删除成功', 'errno' => 0];
159+
} else {
160+
$data = ['msg' => '删除失败', 'errno' => 2];
161+
}
162+
return $this->out(200, $data);
163+
164+
}
165+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Permission;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class Store extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'name'=>'required|string|max:100',
28+
];
29+
}
30+
31+
/**
32+
* 中文错误提示
33+
*
34+
* @return array
35+
*/
36+
public function messages()
37+
{
38+
return [
39+
'name.required' => '名称不能为空',
40+
'name.string' => '名称必须是字符串',
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)