GroupController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Group;
  4. use App\Services\GroupService;
  5. use App\Http\Requests\GroupStoreRequest;
  6. use App\Http\Requests\GroupAssignRequest;
  7. use App\Http\Resources\GroupResource;
  8. use App\Http\Resources\TwoFAccountCollection;
  9. class GroupController extends Controller
  10. {
  11. /**
  12. * The TwoFAccount Service instance.
  13. */
  14. protected $groupService;
  15. /**
  16. * Create a new controller instance.
  17. *
  18. * @param GroupService $groupService
  19. * @return void
  20. */
  21. public function __construct(GroupService $groupService)
  22. {
  23. $this->groupService = $groupService;
  24. }
  25. /**
  26. * Display a listing of the resource.
  27. *
  28. * @return \Illuminate\Http\Response
  29. */
  30. public function index()
  31. {
  32. $groups = $this->groupService->getAll();
  33. return GroupResource::collection($groups);
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @param \App\Http\Requests\GroupRequest $request
  39. * @return \App\Http\Resources\GroupResource
  40. */
  41. public function store(GroupStoreRequest $request)
  42. {
  43. $validated = $request->validated();
  44. $group = $this->groupService->Create($validated);
  45. return (new GroupResource($group))
  46. ->response()
  47. ->setStatusCode(201);
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param \App\Group $group
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function show(Group $group)
  56. {
  57. return new GroupResource($group);
  58. }
  59. /**
  60. * Update the specified resource in storage.
  61. *
  62. * @param \App\Http\Requests\GroupRequest $request
  63. * @param \App\Group $group
  64. * @return \App\Http\Resources\GroupResource
  65. */
  66. public function update(GroupStoreRequest $request, Group $group)
  67. {
  68. $validated = $request->validated();
  69. $this->groupService->update($group, $validated);
  70. return new GroupResource($group);
  71. }
  72. /**
  73. * Associate the specified accounts with the group
  74. *
  75. * @param \App\Http\Requests\GroupAssignRequest $request
  76. * @param \App\Group $group
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function assignAccounts(GroupAssignRequest $request, Group $group)
  80. {
  81. $validated = $request->validated();
  82. $this->groupService->assign($validated['ids'], $group);
  83. return response()->json($group, 200);
  84. }
  85. /**
  86. * Get accounts assign to the group
  87. *
  88. * @param \App\Group $group
  89. * @return \App\Http\Resources\TwoFAccountCollection
  90. */
  91. public function accounts(Group $group)
  92. {
  93. $groups = $this->groupService->getAccounts($group);
  94. return new TwoFAccountCollection($groups);
  95. }
  96. /**
  97. * Remove the specified resource from storage.
  98. *
  99. * @param \App\Group $group
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function destroy(Group $group)
  103. {
  104. $this->groupService->delete($group->id);
  105. return response()->json(null, 204);
  106. }
  107. }