RoleController.php 5.2 KB

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