GroupController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Api\v1\Controllers;
  3. use App\Api\v1\Requests\GroupAssignRequest;
  4. use App\Api\v1\Requests\GroupStoreRequest;
  5. use App\Api\v1\Resources\GroupResource;
  6. use App\Api\v1\Resources\TwoFAccountCollection;
  7. use App\Facades\Groups;
  8. use App\Http\Controllers\Controller;
  9. use App\Models\Group;
  10. use App\Models\User;
  11. use Illuminate\Auth\Access\AuthorizationException;
  12. use Illuminate\Database\Eloquent\ModelNotFoundException;
  13. use Illuminate\Http\Request;
  14. class GroupController extends Controller
  15. {
  16. /**
  17. * Display all user groups.
  18. *
  19. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
  20. */
  21. public function index(Request $request)
  22. {
  23. // Quick fix for #176
  24. if (config('auth.defaults.guard') === 'reverse-proxy-guard' && User::count() === 1) {
  25. if (Group::orphans()->exists()) {
  26. $groups = Group::orphans()->get();
  27. Groups::setUser($groups, $request->user());
  28. }
  29. }
  30. // We do not use fluent call all over the call chain to ease tests
  31. $user = $request->user();
  32. $groups = $user->groups()->withCount('twofaccounts')->get();
  33. return GroupResource::collection(Groups::prependTheAllGroup($groups, $request->user()));
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @return \Illuminate\Http\JsonResponse
  39. */
  40. public function store(GroupStoreRequest $request)
  41. {
  42. $this->authorize('create', Group::class);
  43. $validated = $request->validated();
  44. $group = $request->user()->groups()->create($validated);
  45. return (new GroupResource($group))
  46. ->response()
  47. ->setStatusCode(201);
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @return \App\Api\v1\Resources\GroupResource
  53. */
  54. public function show(Request $request, Group $group)
  55. {
  56. $this->authorize('view', $group);
  57. // group with id==0 is the 'All' virtual group.
  58. // Eloquent specifically returns a non-persisted Group instance
  59. // with just the name property. The twofaccounts_count has to be
  60. // set here.
  61. if ($group->id === 0) {
  62. $group->twofaccounts_count = $request->user()->twofaccounts->count();
  63. }
  64. return new GroupResource($group);
  65. }
  66. /**
  67. * Update the specified resource in storage.
  68. *
  69. * @return \App\Api\v1\Resources\GroupResource
  70. */
  71. public function update(GroupStoreRequest $request, Group $group)
  72. {
  73. $this->authorize('update', $group);
  74. $validated = $request->validated();
  75. $group->update($validated);
  76. return new GroupResource($group);
  77. }
  78. /**
  79. * Associate the specified accounts with the group
  80. *
  81. * @return \App\Api\v1\Resources\GroupResource
  82. */
  83. public function assignAccounts(GroupAssignRequest $request, Group $group)
  84. {
  85. $this->authorize('update', $group);
  86. $validated = $request->validated();
  87. try {
  88. Groups::assign($validated['ids'], $request->user(), $group);
  89. $group->loadCount('twofaccounts');
  90. } catch (ModelNotFoundException $exc) {
  91. abort(404);
  92. } catch (AuthorizationException $exc) {
  93. abort(403);
  94. } catch (\Throwable $th) {
  95. abort(409, 'Conflict');
  96. }
  97. return new GroupResource($group);
  98. }
  99. /**
  100. * Get accounts assigned to the group
  101. *
  102. * @return \App\Api\v1\Resources\TwoFAccountCollection
  103. */
  104. public function accounts(Request $request, Group $group)
  105. {
  106. $this->authorize('view', $group);
  107. // group with id==0 is the 'All' virtual group that lists
  108. // all the user's twofaccounts. From the db pov the accounts
  109. // are not assigned to any group record.
  110. if ($group->id === 0) {
  111. $twofaccounts = $request->user()->twofaccounts;
  112. }
  113. else {
  114. $twofaccounts = $group->twofaccounts;
  115. }
  116. return new TwoFAccountCollection($twofaccounts);
  117. }
  118. /**
  119. * Remove the specified resource from storage.
  120. *
  121. * @return \Illuminate\Http\JsonResponse
  122. */
  123. public function destroy(Group $group)
  124. {
  125. $this->authorize('delete', $group);
  126. $group->delete();
  127. return response()->json(null, 204);
  128. }
  129. }