2020-10-25 23:50:13 +01:00
|
|
|
<?php
|
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
namespace App\Api\v1\Controllers;
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
use App\Group;
|
2021-09-21 22:46:35 +02:00
|
|
|
use App\Services\GroupService;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Requests\GroupStoreRequest;
|
|
|
|
use App\Api\v1\Requests\GroupAssignRequest;
|
|
|
|
use App\Api\v1\Resources\GroupResource;
|
|
|
|
use App\Api\v1\Resources\TwoFAccountCollection;
|
|
|
|
use App\Http\Controllers\Controller;
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
class GroupController extends Controller
|
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
/**
|
|
|
|
* The TwoFAccount Service instance.
|
|
|
|
*/
|
|
|
|
protected $groupService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Services\GroupService $groupService
|
2021-09-21 22:46:35 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(GroupService $groupService)
|
|
|
|
{
|
|
|
|
$this->groupService = $groupService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-25 23:50:13 +01:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\GroupResource
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
$groups = $this->groupService->getAll();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
return GroupResource::collection($groups);
|
2020-10-25 23:50:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\GroupStoreRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
2021-09-21 22:46:35 +02:00
|
|
|
public function store(GroupStoreRequest $request)
|
2020-10-25 23:50:13 +01:00
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2021-11-26 11:18:58 +01:00
|
|
|
$group = $this->groupService->create($validated);
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
return (new GroupResource($group))
|
|
|
|
->response()
|
|
|
|
->setStatusCode(201);
|
2020-10-25 23:50:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Group $group
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\GroupResource
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
|
|
|
public function show(Group $group)
|
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
return new GroupResource($group);
|
2020-10-25 23:50:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\GroupStoreRequest $request
|
2021-09-21 22:46:35 +02:00
|
|
|
* @param \App\Group $group
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\GroupResource
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
2021-09-21 22:46:35 +02:00
|
|
|
public function update(GroupStoreRequest $request, Group $group)
|
2020-10-25 23:50:13 +01:00
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
$this->groupService->update($group, $validated);
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
return new GroupResource($group);
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Associate the specified accounts with the group
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\GroupAssignRequest $request
|
2020-10-25 23:50:13 +01:00
|
|
|
* @param \App\Group $group
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\GroupResource
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
2021-09-21 22:46:35 +02:00
|
|
|
public function assignAccounts(GroupAssignRequest $request, Group $group)
|
2020-10-25 23:50:13 +01:00
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2020-10-28 17:51:32 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
$this->groupService->assign($validated['ids'], $group);
|
2020-10-28 17:51:32 +01:00
|
|
|
|
2021-11-13 13:26:25 +01:00
|
|
|
return new GroupResource($group);
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-22 22:50:45 +02:00
|
|
|
/**
|
|
|
|
* Get accounts assign to the group
|
|
|
|
*
|
|
|
|
* @param \App\Group $group
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\TwoFAccountCollection
|
2021-09-22 22:50:45 +02:00
|
|
|
*/
|
|
|
|
public function accounts(Group $group)
|
|
|
|
{
|
|
|
|
$groups = $this->groupService->getAccounts($group);
|
|
|
|
|
|
|
|
return new TwoFAccountCollection($groups);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-25 23:50:13 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param \App\Group $group
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
|
|
|
public function destroy(Group $group)
|
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
$this->groupService->delete($group->id);
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|