RoleController.php 4.6 KB

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