AuthorityController.php 5.3 KB

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