2FAuth/app/Api/v1/Controllers/GroupController.php

119 lines
3.2 KiB
PHP
Raw Normal View History

2020-10-25 23:50:13 +01:00
<?php
namespace App\Api\v1\Controllers;
2020-10-25 23:50:13 +01:00
use App\Api\v1\Requests\GroupAssignRequest;
2022-11-22 15:15:52 +01:00
use App\Api\v1\Requests\GroupStoreRequest;
use App\Api\v1\Resources\GroupResource;
use App\Api\v1\Resources\TwoFAccountCollection;
2022-11-22 15:15:52 +01:00
use App\Facades\Groups;
use App\Http\Controllers\Controller;
2022-11-22 15:15:52 +01:00
use App\Models\Group;
use Illuminate\Http\Request;
2020-10-25 23:50:13 +01:00
class GroupController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
2020-10-25 23:50:13 +01:00
*/
public function index(Request $request)
2020-10-25 23:50:13 +01:00
{
$groups = Groups::for($request->user())->withTheAllGroup()->all();
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.
*
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
*/
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 = Groups::for($request->user())->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.
*
2021-12-02 13:15:53 +01:00
* @param \App\Models\Group $group
* @param \Illuminate\Http\Request $request
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, Request $request)
2020-10-25 23:50:13 +01:00
{
$group = Groups::for($request->user())->get($group->id);
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
2022-11-22 15:15:52 +01:00
* @param \App\Models\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 update(GroupStoreRequest $request, Group $group)
2020-10-25 23:50:13 +01:00
{
$validated = $request->validated();
2020-10-25 23:50:13 +01:00
Groups::for($request->user())->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
*
2021-11-26 11:18:58 +01:00
* @param \App\Api\v1\Requests\GroupAssignRequest $request
2021-12-02 13:15:53 +01:00
* @param \App\Models\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 assignAccounts(GroupAssignRequest $request, Group $group)
2020-10-25 23:50:13 +01:00
{
$validated = $request->validated();
2020-10-28 17:51:32 +01:00
Groups::for($request->user())->assign($validated['ids'], $group);
2020-10-25 23:50:13 +01:00
2022-11-22 15:15:52 +01:00
return new GroupResource($group);
2020-10-25 23:50:13 +01:00
}
/**
* Get accounts assigned to the group
*
2021-12-02 13:15:53 +01:00
* @param \App\Models\Group $group
* @param \Illuminate\Http\Request $request
2021-11-26 11:18:58 +01:00
* @return \App\Api\v1\Resources\TwoFAccountCollection
*/
public function accounts(Group $group, Request $request)
{
$groups = Groups::for($request->user())->accounts($group);
return new TwoFAccountCollection($groups);
}
2020-10-25 23:50:13 +01:00
/**
* Remove the specified resource from storage.
*
2021-12-02 13:15:53 +01:00
* @param \App\Models\Group $group
* @param \Illuminate\Http\Request $request
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, Request $request)
2020-10-25 23:50:13 +01:00
{
Groups::for($request->user())->delete($group->id);
2020-10-25 23:50:13 +01:00
return response()->json(null, 204);
}
}