PermissionsController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 PermissionsController
  9. {
  10. protected $permissionModel;
  11. public function __construct()
  12. {
  13. $this->permissionModel = Config::get('laratrust.models.permission');
  14. }
  15. public function index()
  16. {
  17. return View::make('panel.authorization.permissions.index', [
  18. 'permissions' => $this->permissionModel::simplePaginate(10),
  19. ]);
  20. }
  21. public function create()
  22. {
  23. return View::make('panel.authorization.edit', [
  24. 'model' => null,
  25. 'type' => 'permission',
  26. ]);
  27. }
  28. public function store(Request $request)
  29. {
  30. $data = $request->validate([
  31. 'name' => 'required|string|unique:permissions,name',
  32. 'display_name' => 'nullable|string',
  33. 'description' => 'nullable|string',
  34. ]);
  35. $this->permissionModel::create($data);
  36. Session::flash('laratrust-success', 'Permission created successfully');
  37. return redirect(route('permissions.index'));
  38. }
  39. public function edit($id)
  40. {
  41. $permission = $this->permissionModel::findOrFail($id);
  42. return View::make('panel.authorization.edit', [
  43. 'model' => $permission,
  44. 'type' => 'permission',
  45. ]);
  46. }
  47. public function update(Request $request, $id)
  48. {
  49. $permission = $this->permissionModel::findOrFail($id);
  50. $data = $request->validate([
  51. 'display_name' => 'nullable|string',
  52. 'description' => 'nullable|string',
  53. ]);
  54. $permission->update($data);
  55. Session::flash('laratrust-success', 'Permission updated successfully');
  56. return redirect(route('permissions.index'));
  57. }
  58. public function destroy($id)
  59. {
  60. $usersAssignedToPermission = DB::table(Config::get('laratrust.tables.permission_user'))
  61. ->where(Config::get('laratrust.foreign_keys.permission'), $id)
  62. ->count();
  63. if ($usersAssignedToPermission > 0) {
  64. Session::flash('laratrust-warning', 'Permission is attached to one or more users. It can not be deleted');
  65. } else {
  66. Session::flash('laratrust-success', 'Permission deleted successfully');
  67. $this->permissionModel::destroy($id);
  68. }
  69. return redirect(route('permissions.index'));
  70. }
  71. }