GroupController.php 4.2 KB

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