Drop appstract/laravel-options package

This commit is contained in:
Bubka 2021-12-01 13:47:20 +01:00
parent fa489956a9
commit 65da59db64
27 changed files with 300 additions and 457 deletions

View File

@ -4,7 +4,7 @@
use App\Exceptions\DbEncryptionException; use App\Exceptions\DbEncryptionException;
use App\Services\DbEncryptionService; use App\Services\DbEncryptionService;
use App\Services\SettingServiceInterface; use App\Services\SettingService;
use App\Api\v1\Requests\SettingStoreRequest; use App\Api\v1\Requests\SettingStoreRequest;
use App\Api\v1\Requests\SettingUpdateRequest; use App\Api\v1\Requests\SettingUpdateRequest;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
@ -16,15 +16,15 @@ class SettingController extends Controller
/** /**
* The Settings Service instance. * The Settings Service instance.
*/ */
protected SettingServiceInterface $settingService; protected SettingService $settingService;
/** /**
* Create a new controller instance. * Create a new controller instance.
*/ */
public function __construct(SettingServiceInterface $SettingServiceInterface) public function __construct(SettingService $settingService)
{ {
$this->settingService = $SettingServiceInterface; $this->settingService = $settingService;
} }

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Services\SettingServiceInterface; use App\Services\SettingService;
class SinglePageController extends Controller class SinglePageController extends Controller
{ {
@ -10,16 +10,16 @@ class SinglePageController extends Controller
/** /**
* The Settings Service instance. * The Settings Service instance.
*/ */
protected SettingServiceInterface $settingService; protected SettingService $settingService;
/** /**
* Create a new controller instance. * Create a new controller instance.
* *
*/ */
public function __construct(SettingServiceInterface $SettingServiceInterface) public function __construct(SettingService $settingService)
{ {
$this->settingService = $SettingServiceInterface; $this->settingService = $settingService;
} }
@ -29,6 +29,9 @@ public function __construct(SettingServiceInterface $SettingServiceInterface)
*/ */
public function index() public function index()
{ {
return view('landing')->with('appSettings', $this->settingService->all()->toJson()); return view('landing')->with([
'appSettings' => $this->settingService->all()->toJson(),
'lang' => $this->settingService->get('lang')
]);
} }
} }

View File

@ -30,7 +30,7 @@ public function handle($request, Closure $next, $guard = null)
$inactiveFor = $now->diffInSeconds(Carbon::parse($user->last_seen_at)); $inactiveFor = $now->diffInSeconds(Carbon::parse($user->last_seen_at));
// Fetch all setting values // Fetch all setting values
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$kickUserAfterXSecond = intval($settingService->get('kickUserAfter')) * 60; $kickUserAfterXSecond = intval($settingService->get('kickUserAfter')) * 60;
// If user has been inactive longer than the allowed inactivity period // If user has been inactive longer than the allowed inactivity period

View File

@ -3,6 +3,7 @@
namespace App\Http\Middleware; namespace App\Http\Middleware;
use Closure; use Closure;
use Facades\App\Services\SettingService;
class SetLanguage class SetLanguage
{ {
@ -15,7 +16,7 @@ class SetLanguage
*/ */
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
\App::setLocale(option('lang', 'en')); \App::setLocale(SettingService::get('lang', 'en'));
return $next($request); return $next($request);
} }

36
app/Option.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Option extends Model
{
/**
* The attributes that are mass assignable.
*
* @var [type]
*/
protected $fillable = [
'key',
'value',
];
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* Casts.
*
* @var array
*/
protected $casts = [];
}

View File

@ -1,29 +0,0 @@
<?php
namespace App\Providers;
use App\Services\SettingServiceInterface;
use App\Services\AppstractOptionsService;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class TwoFAuthServiceProvider extends ServiceProvider
{
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
}
/**
* Register stuff.
*
*/
public function register() : void
{
$this->app->bind(SettingServiceInterface::class, AppstractOptionsService::class);
}
}

View File

@ -4,7 +4,7 @@
use App\Group; use App\Group;
use App\TwoFAccount; use App\TwoFAccount;
use App\Services\SettingServiceInterface; use App\Services\SettingService;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -14,16 +14,16 @@ class GroupService
/** /**
* The Settings Service instance. * The Settings Service instance.
*/ */
protected SettingServiceInterface $settingService; protected SettingService $settingService;
/** /**
* Create a new controller instance. * Create a new controller instance.
* *
*/ */
public function __construct(SettingServiceInterface $SettingServiceInterface) public function __construct(SettingService $settingService)
{ {
$this->settingService = $SettingServiceInterface; $this->settingService = $settingService;
} }

View File

@ -4,17 +4,20 @@
use Throwable; use Throwable;
use Exception; use Exception;
use App\Option;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Crypt;
use App\Exceptions\DbEncryptionException; use App\Exceptions\DbEncryptionException;
class AppstractOptionsService implements SettingServiceInterface class SettingService
{ {
/** /**
* @inheritDoc * Get a setting
*
* @param string|array $setting A single setting name or an associative array of name:value settings
* @return mixed string|int|boolean|null
*/ */
public function get(string $setting) public function get(string $setting)
{ {
@ -26,7 +29,9 @@ public function get(string $setting)
/** /**
* @inheritDoc * Get all settings
*
* @return mixed Collection of settings
*/ */
public function all() : Collection public function all() : Collection
{ {
@ -42,7 +47,10 @@ public function all() : Collection
/** /**
* @inheritDoc * Set a setting
*
* @param string|array $setting A single setting name or an associative array of name:value settings
* @param string|int|boolean|null $value The value for single setting
*/ */
public function set($setting, $value = null) : void public function set($setting, $value = null) : void
{ {
@ -57,21 +65,21 @@ public function set($setting, $value = null) : void
$settings[$setting] = $this->replaceBoolean($value); $settings[$setting] = $this->replaceBoolean($value);
} }
option($settings);
foreach ($settings as $setting => $value) { foreach ($settings as $setting => $value) {
Option::updateOrCreate(['key' => $setting], ['value' => $value]);
Log::info(sprintf('Setting %s is now %s', var_export($setting, true), var_export($this->restoreType($value), true))); Log::info(sprintf('Setting %s is now %s', var_export($setting, true), var_export($this->restoreType($value), true)));
} }
} }
/** /**
* @inheritDoc * Delete a setting
*
* @param string $name The setting name
*/ */
public function delete(string $name) : void public function delete(string $name) : void
{ {
option()->remove($name); Option::where('key', $name)->delete();
Log::info(sprintf('Setting %s deleted', var_export($name, true))); Log::info(sprintf('Setting %s deleted', var_export($name, true)));
} }

View File

@ -1,41 +0,0 @@
<?php
namespace App\Services;
use Illuminate\Support\Collection;
interface SettingServiceInterface
{
/**
* Get a setting
*
* @param string|array $setting A single setting name or an associative array of name:value settings
* @return mixed string|int|boolean|null
*/
public function get(string $setting);
/**
* Get all settings
*
* @return mixed Collection of settings
*/
public function all() : Collection;
/**
* Set a setting
*
* @param string|array $setting A single setting name or an associative array of name:value settings
* @param string|int|boolean|null $value The value for single setting
*/
public function set($setting, $value = null) : void;
/**
* Delete a setting
*
* @param string $name The setting name
*/
public function delete(string $name) : void;
}

View File

@ -4,7 +4,7 @@
use Exception; use Exception;
use App\Events\TwoFAccountDeleted; use App\Events\TwoFAccountDeleted;
use Facades\App\Services\SettingServiceInterface; use Facades\App\Services\SettingService;
use Spatie\EloquentSortable\Sortable; use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait; use Spatie\EloquentSortable\SortableTrait;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -171,7 +171,7 @@ public function setSecretAttribute($value)
private function decryptOrReturn($value) private function decryptOrReturn($value)
{ {
// Decipher when needed // Decipher when needed
if ( SettingServiceInterface::get('useEncryption') ) if ( SettingService::get('useEncryption') )
{ {
try { try {
return Crypt::decryptString($value); return Crypt::decryptString($value);
@ -192,7 +192,7 @@ private function decryptOrReturn($value)
private function encryptOrReturn($value) private function encryptOrReturn($value)
{ {
// should be replaced by laravel 8 attribute encryption casting // should be replaced by laravel 8 attribute encryption casting
return SettingServiceInterface::get('useEncryption') ? Crypt::encryptString($value) : $value; return SettingService::get('useEncryption') ? Crypt::encryptString($value) : $value;
} }
} }

View File

@ -9,7 +9,6 @@
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "^7.4", "php": "^7.4",
"appstract/laravel-options": "^4.1.1",
"chillerlan/php-qrcode": "^4.3", "chillerlan/php-qrcode": "^4.3",
"doctrine/dbal": "^2.10", "doctrine/dbal": "^2.10",
"fakerphp/faker": "^1.16", "fakerphp/faker": "^1.16",

527
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -183,7 +183,6 @@
// App\Providers\BroadcastServiceProvider::class, // App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
App\Providers\TwoFAuthServiceProvider::class
], ],

View File

@ -31,7 +31,7 @@ public function up()
$twofaccounts = DB::table('twofaccounts')->select('id', 'legacy_uri')->get(); $twofaccounts = DB::table('twofaccounts')->select('id', 'legacy_uri')->get();
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
foreach ($twofaccounts as $twofaccount) { foreach ($twofaccounts as $twofaccount) {
try { try {

View File

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html class="has-background-black-ter" lang="{{ option('lang', 'en') }}"> <html class="has-background-black-ter" lang="{!! $lang !!}">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">

View File

@ -95,7 +95,7 @@ public function test_update_user_returns_success()
*/ */
public function test_update_user_in_demo_mode_returns_unchanged_user() public function test_update_user_in_demo_mode_returns_unchanged_user()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set('isDemoApp', true); $settingService->set('isDemoApp', true);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')

View File

@ -74,7 +74,7 @@ public function test_show_native_unchanged_setting_returns_consistent_value()
*/ */
public function test_show_native_changed_setting_returns_consistent_value() public function test_show_native_changed_setting_returns_consistent_value()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE); $settingService->set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
@ -92,7 +92,7 @@ public function test_show_native_changed_setting_returns_consistent_value()
*/ */
public function test_show_custom_user_setting_returns_consistent_value() public function test_show_custom_user_setting_returns_consistent_value()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
@ -153,7 +153,7 @@ public function test_store_invalid_custom_user_setting_returns_validation_error(
*/ */
public function test_store_existing_custom_user_setting_returns_validation_error() public function test_store_existing_custom_user_setting_returns_validation_error()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
@ -187,7 +187,7 @@ public function test_update_unchanged_native_setting_returns_updated_setting()
*/ */
public function test_update_custom_user_setting_returns_updated_setting() public function test_update_custom_user_setting_returns_updated_setting()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
@ -224,7 +224,7 @@ public function test_update_missing_user_setting_returns_created_setting()
*/ */
public function test_destroy_user_setting_returns_success() public function test_destroy_user_setting_returns_success()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')

View File

@ -438,7 +438,7 @@ public function test_store_with_invalid_uri_returns_validation_error()
public function test_store_assigns_created_account_when_default_group_is_a_specific_one() public function test_store_assigns_created_account_when_default_group_is_a_specific_one()
{ {
// Set the default group to a specific one // Set the default group to a specific one
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set('defaultGroup', $this->group->id); $settingService->set('defaultGroup', $this->group->id);
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
@ -456,7 +456,7 @@ public function test_store_assigns_created_account_when_default_group_is_a_speci
*/ */
public function test_store_assigns_created_account_when_default_group_is_the_active_one() public function test_store_assigns_created_account_when_default_group_is_the_active_one()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
// Set the default group to be the active one // Set the default group to be the active one
$settingService->set('defaultGroup', -1); $settingService->set('defaultGroup', -1);
@ -478,7 +478,7 @@ public function test_store_assigns_created_account_when_default_group_is_the_act
*/ */
public function test_store_assigns_created_account_when_default_group_is_no_group() public function test_store_assigns_created_account_when_default_group_is_no_group()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
// Set the default group to No group // Set the default group to No group
$settingService->set('defaultGroup', 0); $settingService->set('defaultGroup', 0);
@ -498,7 +498,7 @@ public function test_store_assigns_created_account_when_default_group_is_no_grou
*/ */
public function test_store_assigns_created_account_when_default_group_does_not_exist() public function test_store_assigns_created_account_when_default_group_does_not_exist()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
// Set the default group to a non-existing one // Set the default group to a non-existing one
$settingService->set('defaultGroup', 1000); $settingService->set('defaultGroup', 1000);
@ -786,7 +786,7 @@ public function test_get_otp_by_posting_multiple_inputs_returns_bad_request()
*/ */
public function test_get_otp_using_indecipherable_twofaccount_id_returns_bad_request() public function test_get_otp_using_indecipherable_twofaccount_id_returns_bad_request()
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set('useEncryption', true); $settingService->set('useEncryption', true);
$twofaccount = factory(TwoFAccount::class)->create(); $twofaccount = factory(TwoFAccount::class)->create();

View File

@ -69,7 +69,7 @@ public function provideValidData() : array
*/ */
public function test_invalid_data(array $data) : void public function test_invalid_data(array $data) : void
{ {
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set($this->uniqueKey, 'uniqueValue'); $settingService->set($this->uniqueKey, 'uniqueValue');
$request = new SettingStoreRequest(); $request = new SettingStoreRequest();

View File

@ -167,7 +167,7 @@ public function test_user_logout_returns_validation_success()
public function test_user_logout_after_inactivity_returns_unauthorized() public function test_user_logout_after_inactivity_returns_unauthorized()
{ {
// Set the autolock period to 1 minute // Set the autolock period to 1 minute
$settingService = resolve('App\Services\SettingServiceInterface'); $settingService = resolve('App\Services\SettingService');
$settingService->set('kickUserAfter', 1); $settingService->set('kickUserAfter', 1);
$response = $this->json('POST', '/user/login', [ $response = $this->json('POST', '/user/login', [

View File

@ -21,7 +21,7 @@ class GroupServiceTest extends FeatureTestCase
/** /**
* App\Services\SettingServiceInterface $settingService * App\Services\SettingService $settingService
*/ */
protected $settingService; protected $settingService;
@ -59,7 +59,7 @@ public function setUp() : void
parent::setUp(); parent::setUp();
$this->groupService = $this->app->make('App\Services\GroupService'); $this->groupService = $this->app->make('App\Services\GroupService');
$this->settingService = $this->app->make('App\Services\SettingServiceInterface'); $this->settingService = $this->app->make('App\Services\SettingService');
$this->groupOne = new Group; $this->groupOne = new Group;
$this->groupOne->name = 'MyGroupOne'; $this->groupOne->name = 'MyGroupOne';

View File

@ -9,12 +9,12 @@
/** /**
* @covers \App\Services\AppstractOptionsService * @covers \App\Services\SettingService
*/ */
class SettingServiceTest extends FeatureTestCase class SettingServiceTest extends FeatureTestCase
{ {
/** /**
* App\Services\SettingServiceInterface $settingService * App\Services\SettingService $settingService
*/ */
protected $settingService; protected $settingService;
@ -50,7 +50,7 @@ public function setUp() : void
{ {
parent::setUp(); parent::setUp();
$this->settingService = $this->app->make('App\Services\SettingServiceInterface'); $this->settingService = $this->app->make('App\Services\SettingService');
$this->twofaccountOne = new TwoFAccount; $this->twofaccountOne = new TwoFAccount;
$this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI; $this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI;

View File

@ -14,7 +14,7 @@
class TwoFAccountServiceTest extends FeatureTestCase class TwoFAccountServiceTest extends FeatureTestCase
{ {
/** /**
* App\Services\SettingServiceInterface $settingService * App\Services\SettingService $settingService
*/ */
protected $twofaccountService; protected $twofaccountService;

View File

@ -151,7 +151,7 @@ public function test_accounts_returns_api_resources_fetched_using_groupService()
{ {
$group = factory(Group::class)->make(); $group = factory(Group::class)->make();
\Facades\App\Services\SettingServiceInterface::shouldReceive('get') \Facades\App\Services\SettingService::shouldReceive('get')
->with('useEncryption') ->with('useEncryption')
->andReturn(false); ->andReturn(false);

View File

@ -17,7 +17,7 @@ class TwoFAccountDeletedTest extends TestCase
*/ */
public function test_event_constructor() public function test_event_constructor()
{ {
\Facades\App\Services\SettingServiceInterface::shouldReceive('get') \Facades\App\Services\SettingService::shouldReceive('get')
->with('useEncryption') ->with('useEncryption')
->andReturn(false); ->andReturn(false);

View File

@ -16,7 +16,7 @@ class CleanIconStorageTest extends TestCase
{ {
public function test_it_stores_time_to_session() public function test_it_stores_time_to_session()
{ {
\Facades\App\Services\SettingServiceInterface::shouldReceive('get') \Facades\App\Services\SettingService::shouldReceive('get')
->with('useEncryption') ->with('useEncryption')
->andReturn(false); ->andReturn(false);

View File

@ -44,7 +44,7 @@ public function test_model_configuration()
*/ */
public function test_sensitive_attributes_are_stored_encrypted(string $attribute) public function test_sensitive_attributes_are_stored_encrypted(string $attribute)
{ {
\Facades\App\Services\SettingServiceInterface::shouldReceive('get') \Facades\App\Services\SettingService::shouldReceive('get')
->with('useEncryption') ->with('useEncryption')
->andReturn(true); ->andReturn(true);
@ -80,7 +80,7 @@ public function provideSensitiveAttributes() : array
*/ */
public function test_sensitive_attributes_are_returned_clear(string $attribute) public function test_sensitive_attributes_are_returned_clear(string $attribute)
{ {
\Facades\App\Services\SettingServiceInterface::shouldReceive('get') \Facades\App\Services\SettingService::shouldReceive('get')
->with('useEncryption') ->with('useEncryption')
->andReturn(false); ->andReturn(false);
@ -97,7 +97,7 @@ public function test_sensitive_attributes_are_returned_clear(string $attribute)
*/ */
public function test_indecipherable_attributes_returns_masked_value(string $attribute) public function test_indecipherable_attributes_returns_masked_value(string $attribute)
{ {
\Facades\App\Services\SettingServiceInterface::shouldReceive('get') \Facades\App\Services\SettingService::shouldReceive('get')
->with('useEncryption') ->with('useEncryption')
->andReturn(true); ->andReturn(true);