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
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Requests\GroupAssignRequest;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Api\v1\Requests\GroupStoreRequest;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Resources\GroupResource;
|
|
|
|
use App\Api\v1\Resources\TwoFAccountCollection;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Groups;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Models\Group;
|
2023-04-19 08:38:48 +02:00
|
|
|
use App\Models\User;
|
2023-02-23 16:40:53 +01:00
|
|
|
use Illuminate\Http\Request;
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
class GroupController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2023-03-07 15:17:07 +01:00
|
|
|
* Display all user groups.
|
2020-10-25 23:50:13 +01:00
|
|
|
*
|
2022-08-26 15:57:18 +02:00
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
2023-02-23 16:40:53 +01:00
|
|
|
public function index(Request $request)
|
2020-10-25 23:50:13 +01:00
|
|
|
{
|
2023-04-19 08:38:48 +02:00
|
|
|
// Quick fix for #176
|
|
|
|
if (config('auth.defaults.guard') === 'reverse-proxy-guard' && User::count() === 1) {
|
|
|
|
if (Group::orphans()->exists()) {
|
|
|
|
$groups = Group::orphans()->get();
|
|
|
|
Groups::setUser($groups, $request->user());
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 16:55:58 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
// We do not use fluent call all over the call chain to ease tests
|
2023-03-10 22:59:46 +01:00
|
|
|
$user = $request->user();
|
2023-03-07 15:17:07 +01:00
|
|
|
$groups = $user->groups()->withCount('twofaccounts')->get();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
return GroupResource::collection(Groups::prependTheAllGroup($groups, $request->user()));
|
2020-10-25 23:50:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @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
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->authorize('create', Group::class);
|
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$group = $request->user()->groups()->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.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\GroupResource
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function show(Group $group)
|
2020-10-25 23:50:13 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->authorize('view', $group);
|
2023-02-23 16:40:53 +01:00
|
|
|
|
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
|
|
|
* @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
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->authorize('update', $group);
|
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$group->update($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
|
|
|
* @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
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->authorize('update', $group);
|
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2020-10-28 17:51:32 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::assign($validated['ids'], $request->user(), $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
|
|
|
}
|
|
|
|
|
2021-09-22 22:50:45 +02:00
|
|
|
/**
|
2023-02-23 16:40:53 +01:00
|
|
|
* Get accounts assigned to the group
|
2021-09-22 22:50:45 +02:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\TwoFAccountCollection
|
2021-09-22 22:50:45 +02:00
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function accounts(Group $group)
|
2021-09-22 22:50:45 +02:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->authorize('view', $group);
|
2021-09-22 22:50:45 +02:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
return new TwoFAccountCollection($group->twofaccounts);
|
2021-09-22 22:50:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 23:50:13 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-10-25 23:50:13 +01:00
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public function destroy(Group $group)
|
2020-10-25 23:50:13 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->authorize('delete', $group);
|
|
|
|
|
|
|
|
$group->delete();
|
2020-10-25 23:50:13 +01:00
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
|
|
|
}
|