RolesController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers\Panel\Authorization;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\View;
  6. use Illuminate\Support\Facades\Config;
  7. use Illuminate\Support\Facades\Session;
  8. class RolesController
  9. {
  10. protected $rolesModel;
  11. protected $permissionModel;
  12. public function __construct()
  13. {
  14. $this->rolesModel = Config::get('laratrust.models.role');
  15. $this->permissionModel = Config::get('laratrust.models.permission');
  16. }
  17. public function index()
  18. {
  19. return View::make('panel.authorization.roles.index', [
  20. 'roles' => $this->rolesModel::withCount('permissions')
  21. ->simplePaginate(10),
  22. ]);
  23. }
  24. public function create()
  25. {
  26. return View::make('panel.authorization.edit', [
  27. 'model' => null,
  28. 'permissions' => $this->permissionModel::all(['id', 'name']),
  29. 'type' => 'role',
  30. ]);
  31. }
  32. public function store(Request $request)
  33. {
  34. $data = $request->validate([
  35. 'name' => 'required|string|unique:roles,name',
  36. 'display_name' => 'nullable|string',
  37. 'description' => 'nullable|string',
  38. ]);
  39. $role = $this->rolesModel::create($data);
  40. $role->syncPermissions($request->get('permissions') ?? []);
  41. Session::flash('laratrust-success', 'Role created successfully');
  42. return redirect(route('roles.index'));
  43. }
  44. public function edit($id)
  45. {
  46. $role = $this->rolesModel::query()
  47. ->with('permissions:id')
  48. ->findOrFail($id);
  49. $permissions = $this->permissionModel::all(['id', 'name'])
  50. ->map(function ($permission) use ($role) {
  51. $permission->assigned = $role->permissions
  52. ->pluck('id')
  53. ->contains($permission->id);
  54. return $permission;
  55. });
  56. return View::make('panel.authorization.edit', [
  57. 'model' => $role,
  58. 'permissions' => $permissions,
  59. 'type' => 'role',
  60. ]);
  61. }
  62. public function update(Request $request, $id)
  63. {
  64. $role = $this->rolesModel::findOrFail($id);
  65. $data = $request->validate([
  66. 'display_name' => 'nullable|string',
  67. 'description' => 'nullable|string',
  68. ]);
  69. $role->update($data);
  70. $role->syncPermissions($request->get('permissions') ?? []);
  71. Session::flash('laratrust-success', 'Role updated successfully');
  72. return redirect(route('roles.index'));
  73. }
  74. public function destroy($id)
  75. {
  76. $usersAssignedToRole = DB::table(Config::get('laratrust.tables.role_user'))
  77. ->where(Config::get('laratrust.foreign_keys.role'), $id)
  78. ->count();
  79. if ($usersAssignedToRole > 0) {
  80. Session::flash('laratrust-warning', 'Role is attached to one or more users. It can not be deleted');
  81. } else {
  82. Session::flash('laratrust-success', 'Role deleted successfully');
  83. $this->rolesModel::destroy($id);
  84. }
  85. return redirect(route('roles.index'));
  86. }
  87. }