RoleController.php
3.83 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
<?php
namespace App\Http\Container\AdminSection\Controllers\System;
use App\Enums\RoleStatusEnum;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\RoleRequest;
use App\Models\SystemRole;
use App\Support\Controller;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
class RoleController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function index(Request $request): Response
{
$pageSize = $request->get('pageSize', 20);
$filter = $request->except('page', 'pageSize', 'fetchType');
$fetchType = $request->get('fetchType', 'page');
$build = SystemRole::filter($filter);
return match ($fetchType) {
'all' => $this->successWithData($build->get()),
default => $this->successWithData($build->paginate($pageSize))
};
}
/**
* @param \App\Http\Container\AdminSection\Requests\System\RoleRequest $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function store(RoleRequest $request): Response
{
$attribute = $request->safe()->toArray();
$role = SystemRole::query()->create($attribute + ['user_id' => Auth::id(), 'guard' => 'Admin']);
OperationLog::admin()->createAction()->subject($role)->content('角色《%s》', $role->getAttribute('name'));
return $this->successWithData($role, ApiCode::CREATE_SUCCESS);
}
/**
* @param \App\Http\Container\AdminSection\Requests\System\RoleRequest $request
* @param \App\Models\SystemRole $role
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function update(RoleRequest $request, SystemRole $role): Response
{
$role->update($request->safe()->toArray());
OperationLog::admin()->updateAction()->subject($role)->content('角色《%s》', $role->getAttribute('name'));
return $this->successWithData($role, ApiCode::UPDATE_SUCCESS);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\SystemRole $role
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function changeStatus(Request $request, SystemRole $role): Response
{
$request->validate(['status' => 'required|in:0,1'], ['status.required' => '请选择状态', 'status.in' => '状态值不在指定范围内']);
$status = RoleStatusEnum::from($request->offsetGet('status'));
$role->update(['status' => $status->value]);
OperationLog::admin()->statusAction()->subject($role)->content('%s角色《%s》', $status->label(), $role->getAttribute('name'));
return $this->success(ApiCode::UPDATE_SUCCESS);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\SystemRole $role
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function changePermission(Request $request, SystemRole $role): Response
{
$request->validate(['permission' => 'present|array'], ['permission.present' => '请选择分配的权限']);
$role->permissions()->sync($request->get('permission', []));
OperationLog::admin()->updateAction()->subject($role)->content('角色《%s》权限内容', $role->getAttribute('name'));
return $this->success(ApiCode::UPDATE_SUCCESS);
}
/**
* @param \App\Models\SystemRole $role
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function destroy(SystemRole $role): Response
{
SystemRole::query()->whereKey($role->getKey())->limit(1)->delete();
OperationLog::admin()->deleteAction()->subject($role)->content('角色《%s》', $role->getAttribute('name'));
return $this->success(ApiCode::DELETE_SUCCESS);
}
}