2019-05-20 07:37:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-11-02 22:41:40 +01:00
|
|
|
use App\Group;
|
2019-05-24 14:44:41 +02:00
|
|
|
use App\TwoFAccount;
|
2020-10-25 23:51:50 +01:00
|
|
|
use App\Classes\Options;
|
2021-09-07 23:10:17 +02:00
|
|
|
use App\Http\Requests\TwoFAccountReorderRequest;
|
|
|
|
use App\Http\Requests\TwoFAccountStoreRequest;
|
2021-09-18 00:00:39 +02:00
|
|
|
use App\Http\Requests\TwoFAccountUpdateRequest;
|
|
|
|
use App\Http\Resources\TwoFAccountReadResource;
|
|
|
|
use App\Http\Resources\TwoFAccountStoreResource;
|
|
|
|
use App\Http\Requests\TwoFAccountDeleteRequest;
|
|
|
|
use App\Http\Requests\TwoFAccountUriRequest;
|
|
|
|
use App\Http\Requests\TwoFAccountDynamicRequest;
|
|
|
|
use App\Services\TwoFAccountService;
|
|
|
|
use Illuminate\Support\Arr;
|
2019-05-20 07:37:41 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
class TwoFAccountController extends Controller
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* The TwoFAccount Service instance.
|
|
|
|
*/
|
|
|
|
protected $twofaccountService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
2021-09-18 00:00:39 +02:00
|
|
|
* @param TwoFAccountService $twofaccountService
|
|
|
|
* @return void
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function __construct(TwoFAccountService $twofaccountService)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$this->twofaccountService = $twofaccountService;
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* List all resources
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
2021-09-18 00:00:39 +02:00
|
|
|
* @return \App\Http\Resources\TwoFAccountReadResource
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function index(Request $request)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$request->merge(['hideSecret' => true]);
|
|
|
|
|
|
|
|
return TwoFAccountReadResource::collection(TwoFAccount::ordered()->get());
|
|
|
|
}
|
2020-01-09 16:33:32 +01:00
|
|
|
|
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
/**
|
|
|
|
* Display a resource
|
|
|
|
*
|
|
|
|
* @param \App\TwoFAccount $twofaccount
|
|
|
|
*
|
|
|
|
* @return \App\Http\Resources\TwoFAccountReadResource
|
|
|
|
*/
|
|
|
|
public function show(TwoFAccount $twofaccount)
|
|
|
|
{
|
|
|
|
return new TwoFAccountReadResource($twofaccount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\TwoFAccountDynamicRequest $request
|
|
|
|
*
|
|
|
|
* @return \App\Http\Resources\TwoFAccountReadResource
|
|
|
|
*/
|
|
|
|
public function store(TwoFAccountDynamicRequest $request)
|
|
|
|
{
|
2020-11-13 15:45:17 +01:00
|
|
|
// Two possible cases :
|
2021-09-18 00:00:39 +02:00
|
|
|
// - The most common case, an URI is provided by the QuickForm, thanks to a QR code live scan or file upload
|
|
|
|
// -> We use that URI to define the account
|
|
|
|
// - The advanced form has been used and all individual parameters
|
|
|
|
// -> We use the parameters array to define the account
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
$twofaccount = Arr::has($validated, 'uri')
|
|
|
|
? $this->twofaccountService->createFromUri($validated['uri'])
|
|
|
|
: $this->twofaccountService->createFromParameters($validated);
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2020-11-02 22:41:40 +01:00
|
|
|
// Possible group association
|
|
|
|
$groupId = Options::get('defaultGroup') === '-1' ? (int) Options::get('activeGroup') : (int) Options::get('defaultGroup');
|
|
|
|
|
|
|
|
// 0 is the pseudo group 'All', only groups with id > 0 are true user groups
|
|
|
|
if( $groupId > 0 ) {
|
|
|
|
$group = Group::find($groupId);
|
|
|
|
|
|
|
|
if($group) {
|
|
|
|
$group->twofaccounts()->save($twofaccount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
return (new TwoFAccountReadResource($twofaccount))
|
|
|
|
->response()
|
|
|
|
->setStatusCode(201);
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* Update the specified resource in storage.
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
2021-09-18 00:00:39 +02:00
|
|
|
* @param \App\Http\TwoFAccountUpdateRequest $request
|
2019-05-24 14:44:41 +02:00
|
|
|
* @param \App\TwoFAccount $twofaccount
|
2019-05-20 07:37:41 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function update(TwoFAccountUpdateRequest $request, TwoFAccount $twofaccount)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$validated = $request->validated();
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
$this->twofaccountService->update($twofaccount, $validated);
|
|
|
|
|
|
|
|
return response()->json($twofaccount, 200);
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2020-11-16 14:10:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-27 22:36:01 +01:00
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* Set new order.
|
2020-03-27 22:36:01 +01:00
|
|
|
*
|
2021-09-07 23:10:17 +02:00
|
|
|
* @param App\Http\Requests\TwoFAccountReorderRequest $request
|
2020-03-27 22:36:01 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2021-09-07 23:10:17 +02:00
|
|
|
public function reorder(TwoFAccountReorderRequest $request)
|
2020-03-27 22:36:01 +01:00
|
|
|
{
|
2021-09-07 23:10:17 +02:00
|
|
|
$validated = $request->validated();
|
2020-03-27 22:36:01 +01:00
|
|
|
|
2021-09-18 12:31:24 +02:00
|
|
|
$this->twofaccountService->saveOrder($validated['orderedIds']);
|
2021-09-18 00:00:39 +02:00
|
|
|
|
|
|
|
return response()->json(['message' => 'order saved'], 200);
|
|
|
|
}
|
2020-03-27 22:36:01 +01:00
|
|
|
|
2020-11-20 14:11:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preview account using an uri, without any db moves
|
|
|
|
*
|
2021-09-18 00:00:39 +02:00
|
|
|
* @param \App\Http\Requests\TwoFAccountUriRequest $request
|
|
|
|
*
|
|
|
|
* @return \App\Http\Resources\TwoFAccountStoreResource
|
2020-11-20 14:11:32 +01:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function preview(TwoFAccountUriRequest $request)
|
2020-11-20 14:11:32 +01:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$twofaccount = $this->twofaccountService->createFromUri($request->uri, $saveToDB = false);
|
2020-11-20 14:11:32 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
return new TwoFAccountStoreResource($twofaccount);
|
2020-11-20 14:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-26 16:42:09 +02:00
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* Get a One-Time Password
|
2019-05-26 16:42:09 +02:00
|
|
|
*
|
2020-02-04 17:06:11 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-05-26 16:42:09 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function otp(Request $request, $id = null)
|
2019-05-26 16:42:09 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$inputs = $request->all();
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
// The request input is the ID of an existing account
|
|
|
|
if ( $id ) {
|
|
|
|
$otp = $this->twofaccountService->getOTP((int) $id);
|
2020-02-04 17:06:11 +01:00
|
|
|
}
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
// The request input is an uri
|
|
|
|
else if ( count($inputs) === 1 && $request->has('uri') ) {
|
|
|
|
$validatedData = $request->validate((new TwoFAccountUriRequest)->rules());
|
|
|
|
$otp = $this->twofaccountService->getOTP($validatedData['uri']);
|
2020-11-13 15:45:17 +01:00
|
|
|
}
|
2021-09-18 00:00:39 +02:00
|
|
|
|
|
|
|
else if ( count($inputs) > 1 && $request->has('uri')) {
|
|
|
|
return response()->json([
|
|
|
|
'message' => 'bad request',
|
|
|
|
'reason' => ['uri' => __('validation.single', ['attribute' => 'uri'])]
|
|
|
|
], 400);
|
2020-11-14 18:55:10 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
// The request inputs should define an account
|
2020-11-21 21:46:31 +01:00
|
|
|
else {
|
2021-09-18 00:00:39 +02:00
|
|
|
$validatedData = $request->validate((new TwoFAccountStoreRequest)->rules());
|
|
|
|
$otp = $this->twofaccountService->getOTP($validatedData);
|
2020-02-04 17:06:11 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
return response()->json($otp, 200);
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-11-21 19:41:36 +01:00
|
|
|
/**
|
|
|
|
* A simple and light method to get the account count.
|
|
|
|
*
|
|
|
|
* @param \App\TwoFAccount $twofaccount
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function count(Request $request)
|
|
|
|
{
|
|
|
|
return response()->json([ 'count' => TwoFAccount::count() ], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2019-05-24 14:44:41 +02:00
|
|
|
* @param \App\TwoFAccount $twofaccount
|
2019-05-20 07:37:41 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-01-31 23:05:06 +01:00
|
|
|
public function destroy(TwoFAccount $twofaccount)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$this->twofaccountService->delete($twofaccount->id);
|
2020-01-10 00:22:45 +01:00
|
|
|
|
2020-01-31 23:05:06 +01:00
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
2020-01-10 00:22:45 +01:00
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-01-31 23:05:06 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resources from storage.
|
|
|
|
*
|
2021-09-18 00:00:39 +02:00
|
|
|
* @param \App\Http\Requests\TwoFAccountDeleteRequest $request
|
2020-01-31 23:05:06 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function batchDestroy(TwoFAccountDeleteRequest $request)
|
2020-01-31 23:05:06 +01:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$this->twofaccountService->delete($request->ids);
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-01-26 21:49:39 +01:00
|
|
|
return response()->json(null, 204);
|
2019-05-24 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|