Add groups accounts fetching with dedicated route

This commit is contained in:
Bubka 2021-09-22 22:50:45 +02:00
parent 9a7ef19177
commit 037ebaa8ba
6 changed files with 74 additions and 6 deletions

View File

@ -8,6 +8,7 @@
use App\Http\Requests\GroupStoreRequest;
use App\Http\Requests\GroupAssignRequest;
use App\Http\Resources\GroupResource;
use App\Http\Resources\TwoFAccountCollection;
use Illuminate\Http\Request;
class GroupController extends Controller
@ -109,6 +110,21 @@ public function assignAccounts(GroupAssignRequest $request, Group $group)
}
/**
* 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);
}
/**
* Remove the specified resource from storage.
*

View File

@ -8,11 +8,12 @@
use App\Http\Requests\TwoFAccountReorderRequest;
use App\Http\Requests\TwoFAccountStoreRequest;
use App\Http\Requests\TwoFAccountUpdateRequest;
use App\Http\Resources\TwoFAccountReadResource;
use App\Http\Resources\TwoFAccountStoreResource;
use App\Http\Requests\TwoFAccountBatchRequest;
use App\Http\Requests\TwoFAccountUriRequest;
use App\Http\Requests\TwoFAccountDynamicRequest;
use App\Http\Resources\TwoFAccountCollection;
use App\Http\Resources\TwoFAccountReadResource;
use App\Http\Resources\TwoFAccountStoreResource;
use App\Services\GroupService;
use App\Services\TwoFAccountService;
use Illuminate\Support\Arr;
@ -51,9 +52,7 @@ public function __construct(TwoFAccountService $twofaccountService, GroupService
*/
public function index(Request $request)
{
$request->merge(['hideSecret' => true]);
return TwoFAccountReadResource::collection(TwoFAccount::ordered()->get());
return new TwoFAccountCollection(TwoFAccount::ordered()->get());
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class TwoFAccountCollection extends ResourceCollection
{
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects = 'App\Http\Resources\TwoFAccountReadResource';
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
// By default we want this collection to not return the secret.
// The underlying TwoFAccountReadResource hides the secret only when withSecret == false.
// When withSecret is provided the underlying resource will return secret according to the parameter value
// If no withSecret is set we force it to false to ensure the secret will not being returned.
if (!$request->has('withSecret')) {
$request->merge(['withSecret' => false]);
}
return $this->collection;
}
}

View File

@ -19,7 +19,10 @@ public function toArray($request)
'account' => $this->account,
'service' => $this->service,
'icon' => $this->icon,
'secret' => $this->when((int) filter_var($request->input('hideSecret'), FILTER_VALIDATE_BOOLEAN) == 0, $this->secret),
'secret' => $this->when(
!$request->has('withSecret') || (int) filter_var($request->input('withSecret'), FILTER_VALIDATE_BOOLEAN) == 1,
$this->secret
),
'digits' => $this->digits,
'algorithm' => $this->algorithm,
'period' => $this->period,

View File

@ -110,6 +110,20 @@ public function assign(mixed $ids, Group $group = null) : Group
return $group;
}
/**
* Finds twofaccounts assigned to the group
*
* @param Group $group The group
*
* @return Collection The assigned accounts
*/
public function getAccounts(Group $group) : Collection
{
$twofaccounts = $group->twofaccounts()->where('group_id', $group->id)->get();
return $twofaccounts;
}
/**
* Determines the destination group

View File

@ -45,6 +45,7 @@
Route::get('twofaccounts/{id}/otp', 'TwoFAccountController@otp')->where('id', '[0-9]+');;
Route::post('twofaccounts/otp', 'TwoFAccountController@otp');
Route::apiResource('twofaccounts', 'TwoFAccountController');
Route::get('groups/{group}/twofaccounts', 'GroupController@accounts');
Route::post('groups/{group}/assign', 'GroupController@assignAccounts');
Route::apiResource('groups', 'GroupController');