AuthorityController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 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. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  65. return redirect('maintenance/authority/create')->with('successTip',"成功录入权限“{$successName}”");
  66. }
  67. protected function validatorCreate(array $data)
  68. {
  69. return Validator::make($data, [
  70. 'name' => ['max:50', 'required_without:id_owner'],
  71. 'id_owner' => ['max:11', 'required_without:name'],
  72. 'combinedName' => ['max:50', 'unique:authorities,name'],
  73. ]);
  74. }
  75. protected function validatorUpdate(array $data)
  76. {
  77. return Validator::make($data, [
  78. 'name' => ['required', 'string', 'max:50'],
  79. ]);
  80. }
  81. /**
  82. * Display the specified resource.
  83. *
  84. * @param Authority $authority
  85. * @return Response
  86. */
  87. public function show(Authority $authority)
  88. {
  89. //
  90. }
  91. /**
  92. * Show the form for editing the specified resource.
  93. *
  94. * @param \App\Http\Controllers\Authority $authority
  95. * @return Response
  96. */
  97. public function edit(Authority $authority)
  98. {
  99. if(!Gate::allows('权限-编辑')){ return redirect(url('/')); }
  100. $owners=Owner::all();
  101. return view('maintenance.authority.edit',compact('owners','authority'));
  102. }
  103. /**
  104. * Update the specified resource in storage.
  105. *
  106. * @param Request $request
  107. * @param \App\Http\Controllers\Authority $authority
  108. * @return Response
  109. */
  110. public function update(Request $request, Authority $authority)
  111. {
  112. if(!Gate::allows('权限-编辑')){ return redirect(url('/')); }
  113. $this->validatorUpdate($request->all())->validate();
  114. $authority->fill($request->all());
  115. $authority->update();
  116. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  117. return redirect('maintenance/authority/')->with('successTip',"成功修改权限“{$authority['name']}”!");
  118. }
  119. /**
  120. * Remove the specified resource from storage.
  121. *
  122. * @param \App\Http\Controllers\Authority $authority
  123. * @return array|Response
  124. * @throws Exception
  125. */
  126. public function destroy(Authority $authority)
  127. {
  128. if(!Gate::allows('权限-删除')){ return redirect(url('/')); }
  129. $this->log(__METHOD__,__FUNCTION__,$authority->toJson(),Auth::user()['id']);
  130. $re=$authority->delete();
  131. return ['success'=>$re];
  132. }
  133. }