mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-04-11 11:08:44 +02:00
Set Options with fallback values and better boolean handling
This commit is contained in:
parent
a545716798
commit
76ebf847df
57
app/Classes/Options.php
Normal file
57
app/Classes/Options.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Classes;
|
||||||
|
|
||||||
|
class Options
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a collection of options to apply
|
||||||
|
*
|
||||||
|
* @return Options collection
|
||||||
|
*/
|
||||||
|
public static function get()
|
||||||
|
{
|
||||||
|
// Get a collection of user saved options
|
||||||
|
$userOptions = \Illuminate\Support\Facades\DB::table('options')->pluck('value', 'key');
|
||||||
|
|
||||||
|
// We replace patterned string that represent booleans with real booleans
|
||||||
|
$userOptions->transform(function ($item, $key) {
|
||||||
|
if( $item === '{{}}' ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if( $item === '{{1}}' ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return $item;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Merge options from App configuration. It ensures we have a complete options collection with
|
||||||
|
// fallback values for every options
|
||||||
|
$options = collect(config('app.options'))->merge($userOptions);
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set user options
|
||||||
|
*
|
||||||
|
* @param array All options to store
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function store($userOptions)
|
||||||
|
{
|
||||||
|
foreach($userOptions as $opt => $val) {
|
||||||
|
|
||||||
|
// We replace boolean values by a patterned string in order to retrieve
|
||||||
|
// them later (as the Laravel Options package do not support var type)
|
||||||
|
// Not a beatufilly solution but, hey, it works ^_^
|
||||||
|
option([$opt => is_bool($val) ? '{{' . $val . '}}' : $val]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Settings;
|
namespace App\Http\Controllers\Settings;
|
||||||
|
|
||||||
|
use App\Classes\Options;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
|
||||||
class OptionController extends Controller
|
class OptionController extends Controller
|
||||||
@ -17,7 +17,7 @@ class OptionController extends Controller
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
// Fetch all setting values
|
// Fetch all setting values
|
||||||
$settings = DB::table('options')->get();
|
$settings = Options::get();
|
||||||
|
|
||||||
return response()->json(['settings' => $settings], 200);
|
return response()->json(['settings' => $settings], 200);
|
||||||
}
|
}
|
||||||
@ -29,12 +29,9 @@ public function index()
|
|||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
// Store all setting values
|
// Store all options
|
||||||
foreach($request->all() as $opt => $val) {
|
Options::store($request->all());
|
||||||
option([$opt => $val]);
|
|
||||||
$settings[$opt] = option($opt);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => $settings], 200);
|
return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => Options::get()], 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Classes\Options;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class SinglePageController extends Controller
|
class SinglePageController extends Controller
|
||||||
@ -13,8 +14,6 @@ class SinglePageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$appSettings = \Illuminate\Support\Facades\DB::table('options')->pluck('value', 'key')->toJson();
|
return view('landing')->with('appSettings', Options::get()->toJson());
|
||||||
|
|
||||||
return view('landing')->with('appSettings', $appSettings);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,18 @@
|
|||||||
|
|
||||||
'version' => '1.0.0',
|
'version' => '1.0.0',
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application fallback for user options
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'options' => [
|
||||||
|
'isDemoApp' => false,
|
||||||
|
'showTokenAsDot' => false,
|
||||||
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Application Environment
|
| Application Environment
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
displayedOtp() {
|
displayedOtp() {
|
||||||
return Boolean(Number(appSettings.showTokenAsDot)) ? this.otp.replace(/[0-9]/g, '●') : this.otp
|
return appSettings.showTokenAsDot ? this.otp.replace(/[0-9]/g, '●') : this.otp
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -23,8 +23,8 @@
|
|||||||
fail: '',
|
fail: '',
|
||||||
form: new Form({
|
form: new Form({
|
||||||
lang: this.$root.$i18n.locale,
|
lang: this.$root.$i18n.locale,
|
||||||
showTokenAsDot: Boolean(Number(appSettings.showTokenAsDot)),
|
showTokenAsDot: appSettings.showTokenAsDot,
|
||||||
isDemoApp: Boolean(Number(appSettings.isDemoApp)),
|
isDemoApp: appSettings.isDemoApp,
|
||||||
}),
|
}),
|
||||||
options: [
|
options: [
|
||||||
{ text: this.$t('languages.en'), value: 'en' },
|
{ text: this.$t('languages.en'), value: 'en' },
|
||||||
|
@ -60,16 +60,8 @@ public function testSettingsIndexListing()
|
|||||||
->assertStatus(200)
|
->assertStatus(200)
|
||||||
->assertJson([
|
->assertJson([
|
||||||
'settings' => [
|
'settings' => [
|
||||||
[
|
'setting_1' => 'value_1',
|
||||||
'id' => '1',
|
'setting_2' => 'value_2'
|
||||||
'key' => 'setting_1',
|
|
||||||
'value' => 'value_1'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'id' => '2',
|
|
||||||
'key' => 'setting_2',
|
|
||||||
'value' => 'value_2'
|
|
||||||
]
|
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user