RoleController.php 4.0 KB

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