| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 9 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 40
RoleController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 9 | 306.00 | 0.00% covered (danger) | 0.00% | 0 / 40
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 3
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 3
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 10
validatorCreate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
validatorUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
show | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
edit | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 4
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 11
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 4

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\Authority;
6 | use App\Role;
7 | use Exception;
8 | use Illuminate\Http\Request;
9 | use Illuminate\Http\Response;
10 | use Illuminate\Support\Facades\Auth;
11 | use Illuminate\Support\Facades\Gate;
12 | use Illuminate\Support\Facades\Validator;
13 |
14 | class RoleController extends Controller
15 | {
16 | /**
17 | * Display a listing of the resource.
18 | *
19 | * @return Response
20 | */
21 | public function index()
22 | {
23 | if(!Gate::allows('角色-查询')){ return redirect(url('/')); }
24 | $roles=Role::with('authorities')->orderBy('id','desc')->paginate(35);
25 | return view('maintenance.role.index',['roles'=>$roles]);
26 | }
27 |
28 | /**
29 | * Show the form for creating a new resource.
30 | *
31 | * @return Response
32 | */
33 | public function create()
34 | {
35 | if(!Gate::allows('角色-录入')){ return redirect(url('/')); }
36 | $authoritiesAll=Authority::orderBy('alias_name','desc')->get();
37 | return view('maintenance.role.create',compact('authoritiesAll'));
38 | }
39 |
40 | /**
41 | * Store a newly created resource in storage.
42 | *
43 | * @param Request $request
44 | * @return Response
45 | */
46 | public function store(Request $request)
47 | {
48 | if(!Gate::allows('角色-录入')){ return redirect(url('/')); }
49 | $this->validatorCreate($request->all())->validate();
50 | $role=new Role($request->all());
51 | $role->save();
52 | $authorityIds=$request->input('authority')??'';
53 | if($authorityIds){
54 | $authorityIdArr=explode(',',$authorityIds);
55 | $role->authorities()->sync($authorityIdArr);
56 | }
57 |
58 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
59 | return redirect('maintenance/role/create')->with('successTip',"成功录入角色“{$request->input('name')}”");
60 | }
61 | protected function validatorCreate(array $data)
62 | {
63 | return Validator::make($data, [
64 | 'name' => ['required', 'string', 'max:50', 'unique:roles'],
65 | ]);
66 | }
67 | protected function validatorUpdate(array $data)
68 | {
69 | return Validator::make($data, [
70 | 'name' => ['required', 'string', 'max:50'],
71 | ]);
72 | }
73 | /**
74 | * Display the specified resource.
75 | *
76 | * @param Role $role
77 | * @return Response
78 | */
79 | public function show(Role $role)
80 | {
81 | //
82 | }
83 |
84 | /**
85 | * Show the form for editing the specified resource.
86 | *
87 | * @param Role $role
88 | * @return Response
89 | */
90 | public function edit(Role $role)
91 | {
92 | if(!Gate::allows('角色-编辑')){ return redirect(url('/')); }
93 | $authoritiesAll=Authority::all();
94 | $authorities=$role->authorities()->get();
95 | return view('maintenance.role.edit',compact('role','authorities','authoritiesAll'));
96 | }
97 |
98 | /**
99 | * Update the specified resource in storage.
100 | *
101 | * @param Request $request
102 | * @param Role $role
103 | * @return Response
104 | */
105 | public function update(Request $request, Role $role)
106 | {
107 | if(!Gate::allows('角色-编辑')){ return redirect(url('/')); }
108 | $this->validatorUpdate($request->all())->validate();
109 | $role->fill($request->all());
110 | $role->update();
111 | $authorityIds=$request->input('authority')??'';
112 | if($authorityIds){
113 | $authorityIdArr=explode(',',$authorityIds);
114 | $role->authorities()->sync($authorityIdArr);
115 | }else{
116 | $role->authorities()->sync([]);
117 | }
118 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
119 | return redirect('maintenance/role/')->with('successTip',"成功修改角色“{$role['name']}”！");
120 | }
121 |
122 | /**
123 | * Remove the specified resource from storage.
124 | *
125 | * @param Role $role
126 | * @return array|Response
127 | * @throws Exception
128 | */
129 | public function destroy(Role $role)
130 | {
131 | if(!Gate::allows('角色-删除')){ return redirect(url('/')); }
132 | $this->log(__METHOD__,__FUNCTION__,$role->toJson(),Auth::user()['id']);
133 | $re=$role->delete();
134 | return ['success'=>$re];
135 | }
136 | }