Set TwoFAccountService as static behind a Facade

This commit is contained in:
Bubka 2022-07-30 11:38:20 +02:00
parent fdc944be0e
commit be632bb489
6 changed files with 42 additions and 56 deletions

View File

@ -14,30 +14,13 @@
use App\Api\v1\Resources\TwoFAccountReadResource; use App\Api\v1\Resources\TwoFAccountReadResource;
use App\Api\v1\Resources\TwoFAccountStoreResource; use App\Api\v1\Resources\TwoFAccountStoreResource;
use App\Facades\Groups; use App\Facades\Groups;
use App\Services\TwoFAccountService; use App\Facades\TwoFAccounts;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
class TwoFAccountController extends Controller class TwoFAccountController extends Controller
{ {
/**
* The TwoFAccount Service instance.
*/
protected $twofaccountService;
/**
* Create a new controller instance.
*
* @param \App\Services\TwoFAccountService $twofaccountService
* @return void
*/
public function __construct(TwoFAccountService $twofaccountService)
{
$this->twofaccountService = $twofaccountService;
}
/** /**
* List all resources * List all resources
@ -128,7 +111,7 @@ public function update(TwoFAccountUpdateRequest $request, TwoFAccount $twofaccou
public function import(TwoFAccountImportRequest $request) public function import(TwoFAccountImportRequest $request)
{ {
$request->merge(['withSecret' => true]); $request->merge(['withSecret' => true]);
$twofaccounts = $this->twofaccountService->convertMigrationFromGA($request->uri); $twofaccounts = TwoFAccounts::convertMigrationFromGA($request->uri);
return new TwoFAccountCollection($twofaccounts); return new TwoFAccountCollection($twofaccounts);
} }
@ -238,7 +221,7 @@ public function withdraw(TwoFAccountBatchRequest $request)
], 400); ], 400);
} }
$this->twofaccountService->withdraw($validated['ids']); TwoFAccounts::withdraw($validated['ids']);
return response()->json([ 'message' => 'accounts withdrawn' ], 200); return response()->json([ 'message' => 'accounts withdrawn' ], 200);
} }
@ -252,7 +235,7 @@ public function withdraw(TwoFAccountBatchRequest $request)
*/ */
public function destroy(TwoFAccount $twofaccount) public function destroy(TwoFAccount $twofaccount)
{ {
$this->twofaccountService->delete($twofaccount->id); TwoFAccounts::delete($twofaccount->id);
return response()->json(null, 204); return response()->json(null, 204);
} }
@ -275,7 +258,7 @@ public function batchDestroy(TwoFAccountBatchRequest $request)
], 400); ], 400);
} }
$this->twofaccountService->delete($validated['ids']); TwoFAccounts::delete($validated['ids']);
return response()->json(null, 204); return response()->json(null, 204);
} }

View File

@ -0,0 +1,14 @@
<?php
namespace App\Facades;
use App\Services\TwoFAccountService;
use Illuminate\Support\Facades\Facade;
class TwoFAccounts extends Facade
{
protected static function getFacadeAccessor()
{
return TwoFAccountService::class;
}
}

View File

@ -4,7 +4,6 @@
use App\Services\LogoService; use App\Services\LogoService;
use App\Services\SettingService; use App\Services\SettingService;
use App\Services\TwoFAccountService;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Contracts\Support\DeferrableProvider;
@ -24,10 +23,6 @@ public function register()
$this->app->singleton(LogoService::class, function () { $this->app->singleton(LogoService::class, function () {
return new LogoService(); return new LogoService();
}); });
$this->app->singleton(TwoFAccountService::class, function () {
return new TwoFAccountService();
});
} }
/** /**
@ -50,7 +45,6 @@ public function provides()
{ {
return [ return [
LogoService::class, LogoService::class,
TwoFAccountService::class,
]; ];
} }
} }

View File

@ -23,11 +23,11 @@ class TwoFAccountService
* *
* @param int|array|string $ids twofaccount ids to free * @param int|array|string $ids twofaccount ids to free
*/ */
public function withdraw($ids) : void public static function withdraw($ids) : void
{ {
// $ids as string could be a comma-separated list of ids // $ids as string could be a comma-separated list of ids
// so in this case we explode the string to an array // so in this case we explode the string to an array
$ids = $this->commaSeparatedToArray($ids); $ids = self::commaSeparatedToArray($ids);
// whereIn() expects an array // whereIn() expects an array
$ids = is_array($ids) ? $ids : func_get_args(); $ids = is_array($ids) ? $ids : func_get_args();
@ -48,11 +48,11 @@ public function withdraw($ids) : void
* *
* @return int The number of deleted * @return int The number of deleted
*/ */
public function delete($ids) : int public static function delete($ids) : int
{ {
// $ids as string could be a comma-separated list of ids // $ids as string could be a comma-separated list of ids
// so in this case we explode the string to an array // so in this case we explode the string to an array
$ids = $this->commaSeparatedToArray($ids); $ids = self::commaSeparatedToArray($ids);
Log::info(sprintf('Deletion of TwoFAccounts #%s requested', is_array($ids) ? implode(',#', $ids) : $ids )); Log::info(sprintf('Deletion of TwoFAccounts #%s requested', is_array($ids) ? implode(',#', $ids) : $ids ));
$deleted = TwoFAccount::destroy($ids); $deleted = TwoFAccount::destroy($ids);
@ -67,7 +67,7 @@ public function delete($ids) : int
* *
* @return \Illuminate\Support\Collection The converted accounts * @return \Illuminate\Support\Collection The converted accounts
*/ */
public function convertMigrationFromGA($migrationUri) : Collection public static function convertMigrationFromGA($migrationUri) : Collection
{ {
try { try {
$migrationData = base64_decode(urldecode(Str::replace('otpauth-migration://offline?data=', '', $migrationUri))); $migrationData = base64_decode(urldecode(Str::replace('otpauth-migration://offline?data=', '', $migrationUri)));
@ -116,7 +116,7 @@ public function convertMigrationFromGA($migrationUri) : Collection
} }
} }
return $this->markAsDuplicate(collect($twofaccounts)); return self::markAsDuplicate(collect($twofaccounts));
} }
@ -124,7 +124,7 @@ public function convertMigrationFromGA($migrationUri) : Collection
/** /**
* *
*/ */
private function commaSeparatedToArray($ids) private static function commaSeparatedToArray($ids)
{ {
if(is_string($ids)) if(is_string($ids))
{ {
@ -144,7 +144,7 @@ private function commaSeparatedToArray($ids)
* @param \Illuminate\Support\Collection * @param \Illuminate\Support\Collection
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
private function markAsDuplicate($twofaccounts) : Collection private static function markAsDuplicate($twofaccounts) : Collection
{ {
$storage = TwoFAccount::all(); $storage = TwoFAccount::all();

View File

@ -229,6 +229,9 @@
'URL' => Illuminate\Support\Facades\URL::class, 'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class, 'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class, 'View' => Illuminate\Support\Facades\View::class,
'QrCode' => App\Facades\QrCode::class,
'Groups' => App\Facades\Groups::class,
'TwoFAccounts' => App\Facades\TwoFAccounts::class,
], ],

View File

@ -6,7 +6,7 @@
use App\Models\TwoFAccount; use App\Models\TwoFAccount;
use Tests\FeatureTestCase; use Tests\FeatureTestCase;
use Tests\Classes\OtpTestData; use Tests\Classes\OtpTestData;
use App\Services\TwoFAccountService; use App\Facades\TwoFAccounts;
/** /**
@ -14,12 +14,6 @@
*/ */
class TwoFAccountServiceTest extends FeatureTestCase class TwoFAccountServiceTest extends FeatureTestCase
{ {
/**
* App\Services\SettingService $settingService
*/
protected $twofaccountService;
/** /**
* App\Models\TwoFAccount $customTotpTwofaccount * App\Models\TwoFAccount $customTotpTwofaccount
*/ */
@ -45,8 +39,6 @@ public function setUp() : void
{ {
parent::setUp(); parent::setUp();
$this->twofaccountService = $this->app->make(TwoFAccountService::class);
$this->customTotpTwofaccount = new TwoFAccount; $this->customTotpTwofaccount = new TwoFAccount;
$this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI; $this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
$this->customTotpTwofaccount->service = OtpTestData::SERVICE; $this->customTotpTwofaccount->service = OtpTestData::SERVICE;
@ -88,7 +80,7 @@ public function test_withdraw_comma_separated_ids_deletes_relation()
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]); $twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
$this->group->twofaccounts()->saveMany($twofaccounts); $this->group->twofaccounts()->saveMany($twofaccounts);
$this->twofaccountService->withdraw($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id); TwoFAccounts::withdraw($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id);
$this->assertDatabaseHas('twofaccounts', [ $this->assertDatabaseHas('twofaccounts', [
'id' => $this->customTotpTwofaccount->id, 'id' => $this->customTotpTwofaccount->id,
@ -110,7 +102,7 @@ public function test_withdraw_array_of_model_ids_deletes_relation()
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]); $twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
$this->group->twofaccounts()->saveMany($twofaccounts); $this->group->twofaccounts()->saveMany($twofaccounts);
$this->twofaccountService->withdraw([$this->customHotpTwofaccount->id, $this->customTotpTwofaccount->id]); TwoFAccounts::withdraw([$this->customHotpTwofaccount->id, $this->customTotpTwofaccount->id]);
$this->assertDatabaseHas('twofaccounts', [ $this->assertDatabaseHas('twofaccounts', [
'id' => $this->customTotpTwofaccount->id, 'id' => $this->customTotpTwofaccount->id,
@ -132,7 +124,7 @@ public function test_withdraw_single_id_deletes_relation()
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]); $twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
$this->group->twofaccounts()->saveMany($twofaccounts); $this->group->twofaccounts()->saveMany($twofaccounts);
$this->twofaccountService->withdraw($this->customTotpTwofaccount->id); TwoFAccounts::withdraw($this->customTotpTwofaccount->id);
$this->assertDatabaseHas('twofaccounts', [ $this->assertDatabaseHas('twofaccounts', [
'id' => $this->customTotpTwofaccount->id, 'id' => $this->customTotpTwofaccount->id,
@ -146,7 +138,7 @@ public function test_withdraw_single_id_deletes_relation()
*/ */
public function test_withdraw_missing_ids_returns_void() public function test_withdraw_missing_ids_returns_void()
{ {
$this->assertNull($this->twofaccountService->withdraw(null)); $this->assertNull(TwoFAccounts::withdraw(null));
} }
@ -155,7 +147,7 @@ public function test_withdraw_missing_ids_returns_void()
*/ */
public function test_delete_comma_separated_ids() public function test_delete_comma_separated_ids()
{ {
$this->twofaccountService->delete($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id); TwoFAccounts::delete($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id);
$this->assertDatabaseMissing('twofaccounts', [ $this->assertDatabaseMissing('twofaccounts', [
'id' => $this->customTotpTwofaccount->id, 'id' => $this->customTotpTwofaccount->id,
@ -171,7 +163,7 @@ public function test_delete_comma_separated_ids()
*/ */
public function test_delete_array_of_ids() public function test_delete_array_of_ids()
{ {
$this->twofaccountService->delete([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]); TwoFAccounts::delete([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]);
$this->assertDatabaseMissing('twofaccounts', [ $this->assertDatabaseMissing('twofaccounts', [
'id' => $this->customTotpTwofaccount->id, 'id' => $this->customTotpTwofaccount->id,
@ -187,7 +179,7 @@ public function test_delete_array_of_ids()
*/ */
public function test_delete_single_id() public function test_delete_single_id()
{ {
$this->twofaccountService->delete($this->customTotpTwofaccount->id); TwoFAccounts::delete($this->customTotpTwofaccount->id);
$this->assertDatabaseMissing('twofaccounts', [ $this->assertDatabaseMissing('twofaccounts', [
'id' => $this->customTotpTwofaccount->id, 'id' => $this->customTotpTwofaccount->id,
@ -200,7 +192,7 @@ public function test_delete_single_id()
*/ */
public function test_convert_migration_from_gauth_returns_correct_accounts() public function test_convert_migration_from_gauth_returns_correct_accounts()
{ {
$twofaccounts = $this->twofaccountService->convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI); $twofaccounts = TwoFAccounts::convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI);
$this->assertCount(2, $twofaccounts); $this->assertCount(2, $twofaccounts);
@ -248,7 +240,7 @@ public function test_convert_migration_from_gauth_returns_flagged_duplicates()
$twofaccount = new TwoFAccount; $twofaccount = new TwoFAccount;
$twofaccount->fillWithOtpParameters($parameters)->save(); $twofaccount->fillWithOtpParameters($parameters)->save();
$twofaccounts = $this->twofaccountService->convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI); $twofaccounts = TwoFAccounts::convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI);
$this->assertEquals(-1, $twofaccounts->first()->id); $this->assertEquals(-1, $twofaccounts->first()->id);
$this->assertEquals(-1, $twofaccounts->last()->id); $this->assertEquals(-1, $twofaccounts->last()->id);
@ -261,7 +253,7 @@ public function test_convert_migration_from_gauth_returns_flagged_duplicates()
public function test_convert_invalid_migration_from_gauth_returns_InvalidGoogleAuthMigration_excpetion() public function test_convert_invalid_migration_from_gauth_returns_InvalidGoogleAuthMigration_excpetion()
{ {
$this->expectException(\App\Exceptions\InvalidGoogleAuthMigration::class); $this->expectException(\App\Exceptions\InvalidGoogleAuthMigration::class);
$twofaccounts = $this->twofaccountService->convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA); $twofaccounts = TwoFAccounts::convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA);
} }
} }