AuthorityController.php 5.3 KB

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