RoleController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use Exception;
  6. use Illuminate\Contracts\Foundation\Application;
  7. use Illuminate\Contracts\View\Factory;
  8. use Illuminate\Contracts\View\View;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Spatie\Permission\Models\Permission;
  12. use Spatie\Permission\Models\Role;
  13. class RoleController extends Controller
  14. {
  15. const READ_PERMISSION = "admin.roles.read";
  16. const CREATE_PERMISSION = "admin.roles.create";
  17. const EDIT_PERMISSION = "admin.roles.edit";
  18. const DELETE_PERMISSION = "admin.roles.delete";
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @param Request $request
  23. * @return mixed
  24. * @throws Exception
  25. */
  26. public function index(Request $request)
  27. {
  28. $this->checkPermission(self::READ_PERMISSION);
  29. //datatables
  30. if ($request->ajax()) {
  31. return $this->dataTableQuery();
  32. }
  33. $html = $this->dataTable();
  34. return view('admin.roles.index', compact('html'));
  35. }
  36. /**
  37. * Show the form for creating a new resource.
  38. *
  39. * @return Application|Factory|View
  40. */
  41. public function create()
  42. {
  43. $this->checkPermission(self::CREATE_PERMISSION);
  44. $permissions = Permission::all();
  45. return view('admin.roles.edit', compact('permissions'));
  46. }
  47. /**
  48. * Store a newly created resource in storage.
  49. *
  50. * @return RedirectResponse
  51. */
  52. public function store(Request $request): RedirectResponse
  53. {
  54. $this->checkPermission(self::CREATE_PERMISSION);
  55. $role = Role::create([
  56. 'name' => $request->name,
  57. 'color' => $request->color
  58. ]);
  59. if ($request->permissions) {
  60. $role->givePermissionTo($request->permissions);
  61. }
  62. return redirect()
  63. ->route('admin.roles.index')
  64. ->with('success', __('Role saved'));
  65. }
  66. /**
  67. * Display the specified resource.
  68. */
  69. public function show()
  70. {
  71. abort(404);
  72. }
  73. /**
  74. * Show the form for editing the specified resource.
  75. *
  76. * @param Role $role
  77. * @return Application|Factory|View
  78. */
  79. public function edit(Role $role)
  80. {
  81. $this->checkPermission(self::EDIT_PERMISSION);
  82. $permissions = Permission::all();
  83. return view('admin.roles.edit', compact('role', 'permissions'));
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param Role $role
  89. * @return RedirectResponse
  90. */
  91. public function update(Request $request, Role $role)
  92. {
  93. $this->checkPermission(self::EDIT_PERMISSION);
  94. if ($request->permissions) {
  95. if($role->id != 1){ //disable admin permissions change
  96. $role->syncPermissions($request->permissions);
  97. }
  98. }
  99. //if($role->id == 1 || $role->id == 3 || $role->id == 4){ //dont let the user change the names of these roles
  100. // $role->update([
  101. // 'color' => $request->color
  102. // ]);
  103. //}else{
  104. $role->update([
  105. 'name' => $request->name,
  106. 'color' => $request->color
  107. ]);
  108. //}
  109. //if($role->id == 1){
  110. // return redirect()->route('admin.roles.index')->with('success', __('Role updated. Name and Permissions of this Role cannot be changed'));
  111. //}elseif($role->id == 4 || $role->id == 3){
  112. // return redirect()->route('admin.roles.index')->with('success', __('Role updated. Name of this Role cannot be changed'));
  113. // }else{
  114. return redirect()
  115. ->route('admin.roles.index')
  116. ->with('success', __('Role saved'));
  117. //}
  118. }
  119. /**
  120. * Remove the specified resource from storage.
  121. *
  122. * @return RedirectResponse
  123. */
  124. public function destroy(Role $role)
  125. {
  126. $this->checkPermission(self::DELETE_PERMISSION);
  127. if($role->id == 1 || $role->id == 3 || $role->id == 4){ //cannot delete the hard coded roles
  128. return back()->with("error","You cannot delete that role");
  129. }
  130. $users = User::role($role)->get();
  131. foreach($users as $user){
  132. //$user->syncRoles(['Member']);
  133. $user->syncRoles(4);
  134. }
  135. $role->delete();
  136. return redirect()
  137. ->route('admin.roles.index')
  138. ->with('success', __('Role removed'));
  139. }
  140. /**
  141. * @return mixed
  142. * @throws Exception
  143. */
  144. public function dataTable()
  145. {
  146. $query = Role::query()->withCount(['users', 'permissions']);
  147. return datatables($query)
  148. ->editColumn('id', function (Role $role) {
  149. return $role->id;
  150. })
  151. ->addColumn('actions', function (Role $role) {
  152. return '
  153. <a title="Edit" href="'.route("admin.roles.edit", $role).'" class="btn btn-sm btn-info"><i
  154. class="fa fas fa-edit"></i></a>
  155. <form class="d-inline" method="post" action="'.route("admin.roles.destroy", $role).'">
  156. ' . csrf_field() . '
  157. ' . method_field("DELETE") . '
  158. <button title="Delete" type="submit" class="btn btn-sm btn-danger confirm"><i
  159. class="fa fas fa-trash"></i></button>
  160. </form>
  161. ';
  162. })
  163. ->editColumn('name', function (Role $role) {
  164. return "<span style=\"color: $role->color\">$role->name</span>";
  165. })
  166. ->editColumn('usercount', function ($query) {
  167. return $query->users_count;
  168. })
  169. ->editColumn('permissionscount', function ($query){
  170. return $query->permissions_count;
  171. })
  172. ->rawColumns(['actions', 'name'])
  173. ->make(true);
  174. }
  175. }