2FAuth/app/Http/Controllers/GroupController.php

140 lines
3.0 KiB
PHP
Raw Normal View History

2020-10-25 23:50:13 +01:00
<?php
namespace App\Http\Controllers;
use App\Group;
use App\Services\GroupService;
use App\Http\Requests\GroupStoreRequest;
use App\Http\Requests\GroupAssignRequest;
use App\Http\Resources\GroupResource;
use App\Http\Resources\TwoFAccountCollection;
2020-10-25 23:50:13 +01:00
class GroupController extends Controller
{
/**
* The TwoFAccount Service instance.
*/
protected $groupService;
/**
* Create a new controller instance.
*
* @param GroupService $groupService
* @return void
*/
public function __construct(GroupService $groupService)
{
$this->groupService = $groupService;
}
2020-10-25 23:50:13 +01:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$groups = $this->groupService->getAll();
2020-10-25 23:50:13 +01:00
return GroupResource::collection($groups);
2020-10-25 23:50:13 +01:00
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\GroupRequest $request
* @return \App\Http\Resources\GroupResource
2020-10-25 23:50:13 +01:00
*/
public function store(GroupStoreRequest $request)
2020-10-25 23:50:13 +01:00
{
$validated = $request->validated();
2020-10-25 23:50:13 +01:00
$group = $this->groupService->Create($validated);
2020-10-25 23:50:13 +01:00
return (new GroupResource($group))
->response()
->setStatusCode(201);
2020-10-25 23:50:13 +01:00
}
/**
* Display the specified resource.
*
* @param \App\Group $group
* @return \Illuminate\Http\Response
*/
public function show(Group $group)
{
return new GroupResource($group);
2020-10-25 23:50:13 +01:00
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\GroupRequest $request
* @param \App\Group $group
* @return \App\Http\Resources\GroupResource
2020-10-25 23:50:13 +01:00
*/
public function update(GroupStoreRequest $request, Group $group)
2020-10-25 23:50:13 +01:00
{
$validated = $request->validated();
2020-10-25 23:50:13 +01:00
$this->groupService->update($group, $validated);
2020-10-25 23:50:13 +01:00
return new GroupResource($group);
2020-10-25 23:50:13 +01:00
}
/**
* Associate the specified accounts with the group
*
* @param \App\Http\Requests\GroupAssignRequest $request
2020-10-25 23:50:13 +01:00
* @param \App\Group $group
* @return \Illuminate\Http\Response
*/
public function assignAccounts(GroupAssignRequest $request, Group $group)
2020-10-25 23:50:13 +01:00
{
$validated = $request->validated();
2020-10-28 17:51:32 +01:00
$this->groupService->assign($validated['ids'], $group);
2020-10-28 17:51:32 +01:00
return response()->json($group, 200);
2020-10-25 23:50:13 +01:00
}
/**
* Get accounts assign to the group
*
* @param \App\Group $group
* @return \App\Http\Resources\TwoFAccountCollection
*/
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
* @return \Illuminate\Http\Response
*/
public function destroy(Group $group)
{
$this->groupService->delete($group->id);
2020-10-25 23:50:13 +01:00
return response()->json(null, 204);
}
}