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 @@ namespace App\Api\v1\Controllers;
use App\Exceptions\DbEncryptionException;
use App\Services\DbEncryptionService;
use App\Services\SettingServiceInterface;
use App\Services\SettingService;
use App\Api\v1\Requests\SettingStoreRequest;
use App\Api\v1\Requests\SettingUpdateRequest;
use App\Http\Controllers\Controller;
@ -16,15 +16,15 @@ class SettingController extends Controller
/**
* The Settings Service instance.
*/
protected SettingServiceInterface $settingService;
protected SettingService $settingService;
/**
* 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;
use App\Services\SettingServiceInterface;
use App\Services\SettingService;
class SinglePageController extends Controller
{
@ -10,16 +10,16 @@ class SinglePageController extends Controller
/**
* The Settings Service instance.
*/
protected SettingServiceInterface $settingService;
protected SettingService $settingService;
/**
* 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 @@ class SinglePageController extends Controller
*/
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 @@ class KickOutInactiveUser
$inactiveFor = $now->diffInSeconds(Carbon::parse($user->last_seen_at));
// Fetch all setting values
$settingService = resolve('App\Services\SettingServiceInterface');
$settingService = resolve('App\Services\SettingService');
$kickUserAfterXSecond = intval($settingService->get('kickUserAfter')) * 60;
// If user has been inactive longer than the allowed inactivity period

View File

@ -3,6 +3,7 @@
namespace App\Http\Middleware;
use Closure;
use Facades\App\Services\SettingService;
class SetLanguage
{
@ -15,7 +16,7 @@ class SetLanguage
*/
public function handle($request, Closure $next)
{
\App::setLocale(option('lang', 'en'));
\App::setLocale(SettingService::get('lang', 'en'));
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 @@ namespace App\Services;
use App\Group;
use App\TwoFAccount;
use App\Services\SettingServiceInterface;
use App\Services\SettingService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
@ -14,16 +14,16 @@ class GroupService
/**
* The Settings Service instance.
*/
protected SettingServiceInterface $settingService;
protected SettingService $settingService;
/**
* 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 @@ namespace App\Services;
use Throwable;
use Exception;
use App\Option;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Crypt;
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)
{
@ -26,7 +29,9 @@ class AppstractOptionsService implements SettingServiceInterface
/**
* @inheritDoc
* Get all settings
*
* @return mixed Collection of settings
*/
public function all() : Collection
{
@ -42,7 +47,10 @@ class AppstractOptionsService implements SettingServiceInterface
/**
* @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
{
@ -57,21 +65,21 @@ class AppstractOptionsService implements SettingServiceInterface
$settings[$setting] = $this->replaceBoolean($value);
}
option($settings);
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)));
}
}
/**
* @inheritDoc
* Delete a setting
*
* @param string $name The setting name
*/
public function delete(string $name) : void
{
option()->remove($name);
Option::where('key', $name)->delete();
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 @@ namespace App;
use Exception;
use App\Events\TwoFAccountDeleted;
use Facades\App\Services\SettingServiceInterface;
use Facades\App\Services\SettingService;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Illuminate\Database\Eloquent\Model;
@ -171,7 +171,7 @@ class TwoFAccount extends Model implements Sortable
private function decryptOrReturn($value)
{
// Decipher when needed
if ( SettingServiceInterface::get('useEncryption') )
if ( SettingService::get('useEncryption') )
{
try {
return Crypt::decryptString($value);
@ -192,7 +192,7 @@ class TwoFAccount extends Model implements Sortable
private function encryptOrReturn($value)
{
// 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",
"require": {
"php": "^7.4",
"appstract/laravel-options": "^4.1.1",
"chillerlan/php-qrcode": "^4.3",
"doctrine/dbal": "^2.10",
"fakerphp/faker": "^1.16",

527
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

@ -95,7 +95,7 @@ class UserControllerTest extends FeatureTestCase
*/
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);
$response = $this->actingAs($this->user, 'api')

View File

@ -74,7 +74,7 @@ class SettingControllerTest extends FeatureTestCase
*/
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);
$response = $this->actingAs($this->user, 'api')
@ -92,7 +92,7 @@ class SettingControllerTest extends FeatureTestCase
*/
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);
$response = $this->actingAs($this->user, 'api')
@ -153,7 +153,7 @@ class SettingControllerTest extends FeatureTestCase
*/
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);
$response = $this->actingAs($this->user, 'api')
@ -187,7 +187,7 @@ class SettingControllerTest extends FeatureTestCase
*/
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);
$response = $this->actingAs($this->user, 'api')
@ -224,7 +224,7 @@ class SettingControllerTest extends FeatureTestCase
*/
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);
$response = $this->actingAs($this->user, 'api')

View File

@ -438,7 +438,7 @@ class TwoFAccountControllerTest extends FeatureTestCase
public function test_store_assigns_created_account_when_default_group_is_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);
$response = $this->actingAs($this->user, 'api')
@ -456,7 +456,7 @@ class TwoFAccountControllerTest extends FeatureTestCase
*/
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
$settingService->set('defaultGroup', -1);
@ -478,7 +478,7 @@ class TwoFAccountControllerTest extends FeatureTestCase
*/
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
$settingService->set('defaultGroup', 0);
@ -498,7 +498,7 @@ class TwoFAccountControllerTest extends FeatureTestCase
*/
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
$settingService->set('defaultGroup', 1000);
@ -786,7 +786,7 @@ class TwoFAccountControllerTest extends FeatureTestCase
*/
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);
$twofaccount = factory(TwoFAccount::class)->create();

View File

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

View File

@ -167,7 +167,7 @@ class LoginTest extends FeatureTestCase
public function test_user_logout_after_inactivity_returns_unauthorized()
{
// Set the autolock period to 1 minute
$settingService = resolve('App\Services\SettingServiceInterface');
$settingService = resolve('App\Services\SettingService');
$settingService->set('kickUserAfter', 1);
$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;
@ -59,7 +59,7 @@ class GroupServiceTest extends FeatureTestCase
parent::setUp();
$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->name = 'MyGroupOne';

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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