RolesAssignmentController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\Panel\Authorization;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\View;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\Session;
  7. class RolesAssignmentController
  8. {
  9. protected $rolesModel;
  10. protected $permissionModel;
  11. protected $assignPermissions;
  12. public function __construct()
  13. {
  14. $this->rolesModel = Config::get('laratrust.models.role');
  15. $this->permissionModel = Config::get('laratrust.models.permission');
  16. $this->assignPermissions = Config::get('laratrust.panel.assign_permisions_to_user');
  17. }
  18. public function index(Request $request)
  19. {
  20. /*$modelKey = $request->get('model');
  21. $userModel = Config::get('laratrust.user_models')[$modelKey] ?? null;*/
  22. $userModel = Config::get('laratrust.user_models')['users'] ?? null;
  23. $users = $userModel
  24. ? $userModel::query()
  25. ->withCount(['roles', 'permissions'])
  26. ->simplePaginate(10)
  27. : [];
  28. return View::make('panel.authorization.roles-assignment.index', [
  29. 'models' => array_keys(Config::get('laratrust.user_models')),
  30. 'modelKey' => 'users',
  31. 'users' => $users,
  32. ]);
  33. }
  34. public function edit(Request $request, $modelId)
  35. {
  36. $modelKey = $request->get('model');
  37. $userModel = Config::get('laratrust.user_models')[$modelKey] ?? null;
  38. if (!$userModel) {
  39. Session::flash('laratrust-error', 'Model was not specified in the request');
  40. return redirect(route('roles-assignment.index'));
  41. }
  42. $user = $userModel::query()
  43. ->with(['roles:id,name', 'permissions:id,name'])
  44. ->findOrFail($modelId);
  45. $roles = $this->rolesModel::all(['id', 'name'])
  46. ->map(function ($role) use ($user) {
  47. $role->assigned = $user->roles
  48. ->pluck('id')
  49. ->contains($role->id);
  50. return $role;
  51. });
  52. if ($this->assignPermissions) {
  53. $permissions = $this->permissionModel::all(['id', 'name'])
  54. ->map(function ($permission) use ($user) {
  55. $permission->assigned = $user->permissions
  56. ->pluck('id')
  57. ->contains($permission->id);
  58. return $permission;
  59. });
  60. }
  61. return View::make('panel.authorization.roles-assignment.edit', [
  62. 'modelKey' => $modelKey,
  63. 'roles' => $roles,
  64. 'permissions' => $this->assignPermissions ? $permissions : null,
  65. 'user' => $user,
  66. ]);
  67. }
  68. public function update(Request $request, $modelId)
  69. {
  70. $modelKey = $request->get('model');
  71. $userModel = Config::get('laratrust.user_models')[$modelKey] ?? null;
  72. if (!$userModel) {
  73. Session::flash('laratrust-error', 'Model was not specified in the request');
  74. return redirect()->back();
  75. }
  76. $user = $userModel::findOrFail($modelId);
  77. $user->syncRoles($request->get('roles') ?? []);
  78. if ($this->assignPermissions) {
  79. $user->syncPermissions($request->get('permissions') ?? []);
  80. }
  81. Session::flash('laratrust-success', 'Roles and permissions assigned successfully');
  82. return redirect(route('roles-assignment.index', ['model' => $modelKey]));
  83. }
  84. }