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