Compare commits

..

2 Commits

Author SHA1 Message Date
philipp lang c6af58ab97 Remove Setting store actions
continuous-integration/drone/push Build is passing Details
2024-08-01 13:33:28 +02:00
philipp lang bac1eeca00 Remove Middleware 2024-08-01 12:20:08 +02:00
19 changed files with 166 additions and 249 deletions

View File

@ -1,46 +0,0 @@
<?php
namespace App\Form\Actions;
use App\Form\FormSettings;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SettingStoreAction
{
use AsAction;
/**
* @param array<string, mixed> $input
*/
public function handle(array $input): void
{
$settings = app(FormSettings::class);
$settings->fill([
'registerUrl' => $input['register_url'],
'clearCacheUrl' => $input['clear_cache_url'],
]);
$settings->save();
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'register_url' => 'present|string',
'clear_cache_url' => 'present|string',
];
}
public function asController(ActionRequest $request): RedirectResponse
{
$this->handle($request->validated());
return redirect()->back();
}
}

View File

@ -5,6 +5,7 @@ namespace App\Form;
use App\Form\Actions\SettingStoreAction; use App\Form\Actions\SettingStoreAction;
use App\Setting\Contracts\Storeable; use App\Setting\Contracts\Storeable;
use App\Setting\LocalSettings; use App\Setting\LocalSettings;
use Lorisleiva\Actions\ActionRequest;
class FormSettings extends LocalSettings implements Storeable class FormSettings extends LocalSettings implements Storeable
{ {
@ -21,9 +22,19 @@ class FormSettings extends LocalSettings implements Storeable
return 'Formulare'; return 'Formulare';
} }
public static function storeAction(): string /**
* @inheritdoc
*/
public function rules(): array
{
return [
'registerUrl' => 'present|string',
'clearCacheUrl' => 'present|string',
];
}
public function beforeSave(ActionRequest $request): void
{ {
return SettingStoreAction::class;
} }
/** /**
@ -34,8 +45,8 @@ class FormSettings extends LocalSettings implements Storeable
return [ return [
'data' => [ 'data' => [
'data' => [ 'data' => [
'register_url' => $this->registerUrl, 'registerUrl' => $this->registerUrl,
'clear_cache_url' => $this->clearCacheUrl, 'clearCacheUrl' => $this->clearCacheUrl,
] ]
] ]
]; ];

View File

@ -4,6 +4,7 @@ namespace App\Invoice;
use App\Setting\Contracts\Storeable; use App\Setting\Contracts\Storeable;
use App\Setting\LocalSettings; use App\Setting\LocalSettings;
use Lorisleiva\Actions\ActionRequest;
class InvoiceSettings extends LocalSettings implements Storeable class InvoiceSettings extends LocalSettings implements Storeable
{ {
@ -51,14 +52,33 @@ class InvoiceSettings extends LocalSettings implements Storeable
'zip' => $this->zip, 'zip' => $this->zip,
'iban' => $this->iban, 'iban' => $this->iban,
'bic' => $this->bic, 'bic' => $this->bic,
'remember_weeks' => $this->rememberWeeks, 'rememberWeeks' => $this->rememberWeeks,
] ]
]; ];
} }
public static function storeAction(): string /**
* @inheritdoc
*/
public function rules(): array
{
return [
'from_long' => '',
'from' => '',
'mobile' => '',
'email' => '',
'website' => '',
'address' => '',
'place' => '',
'zip' => '',
'iban' => '',
'bic' => '',
'rememberWeeks' => '',
];
}
public function beforeSave(ActionRequest $request): void
{ {
return SettingSaveAction::class;
} }
public static function title(): string public static function title(): string

View File

@ -1,43 +0,0 @@
<?php
namespace App\Invoice;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SettingSaveAction
{
use AsAction;
/**
* @param array<string, string> $input
*/
public function handle(array $input): void
{
$settings = app(InvoiceSettings::class);
$settings->fill([
'from_long' => $input['from_long'] ?? '',
'from' => $input['from'] ?? '',
'mobile' => $input['mobile'] ?? '',
'email' => $input['email'] ?? '',
'website' => $input['website'] ?? '',
'address' => $input['address'] ?? '',
'place' => $input['place'] ?? '',
'zip' => $input['zip'] ?? '',
'iban' => $input['iban'] ?? '',
'bic' => $input['bic'] ?? '',
'rememberWeeks' => $input['remember_weeks'] ?? 1,
]);
$settings->save();
}
public function asController(ActionRequest $request): RedirectResponse
{
$this->handle($request->all());
return redirect()->back();
}
}

View File

@ -4,6 +4,8 @@ namespace App\Module;
use App\Setting\Contracts\Storeable; use App\Setting\Contracts\Storeable;
use App\Setting\LocalSettings; use App\Setting\LocalSettings;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
class ModuleSettings extends LocalSettings implements Storeable class ModuleSettings extends LocalSettings implements Storeable
{ {
@ -20,16 +22,26 @@ class ModuleSettings extends LocalSettings implements Storeable
return 'Module'; return 'Module';
} }
public static function storeAction(): string
{
return ModuleStoreAction::class;
}
public function hasModule(string $module): bool public function hasModule(string $module): bool
{ {
return in_array($module, $this->modules); return in_array($module, $this->modules);
} }
public function beforeSave(ActionRequest $request): void
{
}
/**
* @inheritdoc
*/
public function rules(): array
{
return [
'modules' => 'present|array',
'modules.*' => ['string', Rule::in(Module::values())],
];
}
/** /**
* @inheritdoc * @inheritdoc
*/ */

View File

@ -1,45 +0,0 @@
<?php
namespace App\Module;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class ModuleStoreAction
{
use AsAction;
/**
* @param array<string, mixed> $input
*/
public function handle(array $input): void
{
$settings = app(ModuleSettings::class);
$settings->fill([
'modules' => $input['modules'],
]);
$settings->save();
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'modules' => 'present|array',
'modules.*' => ['string', Rule::in(Module::values())],
];
}
public function asController(ActionRequest $request): RedirectResponse
{
$this->handle($request->validated());
return redirect()->back();
}
}

View File

@ -1,42 +0,0 @@
<?php
namespace App\Nami\Actions;
use App\Initialize\Actions\NamiLoginCheckAction;
use App\Setting\NamiSettings;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SettingSaveAction
{
use AsAction;
/**
* @param array<string, string> $input
*/
public function handle(array $input): void
{
$settings = app(NamiSettings::class);
$settings->fill([
'mglnr' => $input['mglnr'] ?? '',
'password' => $input['password'] ?? '',
'default_group_id' => $input['default_group_id'] ?? '',
]);
$settings->save();
}
public function asController(ActionRequest $request): RedirectResponse
{
NamiLoginCheckAction::run([
'mglnr' => $request->mglnr,
'password' => $request->password,
]);
$this->handle($request->all());
return redirect()->back();
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Setting\Actions;
use App\Setting\Contracts\Storeable;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class StoreAction
{
use AsAction;
/**
* @param array<string, mixed> $input
*/
public function handle(Storeable $settings, array $input): void
{
$settings->fill($input)->save();
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
/** @var Storeable */
$group = request()->route('settingGroup');
return $group->rules();
}
public function asController(ActionRequest $request, Storeable $settingGroup): RedirectResponse
{
$settingGroup->beforeSave($request);
$this->handle($settingGroup, $request->validated());
return redirect()->back();
}
}

View File

@ -3,6 +3,7 @@
namespace App\Setting\Actions; namespace App\Setting\Actions;
use App\Setting\LocalSettings; use App\Setting\LocalSettings;
use App\Setting\SettingFactory;
use Inertia\Inertia; use Inertia\Inertia;
use Inertia\Response; use Inertia\Response;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
@ -16,6 +17,9 @@ class ViewAction
session()->put('menu', 'setting'); session()->put('menu', 'setting');
session()->put('title', $settingGroup::title()); session()->put('title', $settingGroup::title());
return Inertia::render('setting/' . ucfirst($settingGroup::group()), $settingGroup->viewData()); return Inertia::render('setting/' . ucfirst($settingGroup::group()), [
...$settingGroup->viewData(),
'setting_menu' => app(SettingFactory::class)->getShare(),
]);
} }
} }

View File

@ -2,12 +2,26 @@
namespace App\Setting\Contracts; namespace App\Setting\Contracts;
use App\Setting\LocalSettings;
use Lorisleiva\Actions\ActionRequest;
use Spatie\LaravelSettings\Settings;
/**
* @mixin LocalSettings
*/
interface Storeable interface Storeable
{ {
/** public function url(): string;
* @return class-string
*/
public static function storeAction(): string;
public static function url(): string; /**
* @param array<string, mixed> $input
*/
public function fill(array $input): Settings;
/**
* @return array<string, mixed>
*/
public function rules(): array;
public function beforeSave(ActionRequest $request): void;
} }

View File

@ -8,9 +8,9 @@ abstract class LocalSettings extends Settings
{ {
abstract public static function title(): string; abstract public static function title(): string;
public static function url(): string public function url(): string
{ {
return '/setting/' . static::group(); return route('setting.view', ['settingGroup' => $this->group()]);
} }
/** /**

View File

@ -3,8 +3,10 @@
namespace App\Setting; namespace App\Setting;
use App\Group; use App\Group;
use App\Initialize\Actions\NamiLoginCheckAction;
use App\Nami\Actions\SettingSaveAction; use App\Nami\Actions\SettingSaveAction;
use App\Setting\Contracts\Storeable; use App\Setting\Contracts\Storeable;
use Lorisleiva\Actions\ActionRequest;
use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Nami; use Zoomyboy\LaravelNami\Nami;
@ -29,16 +31,31 @@ class NamiSettings extends LocalSettings implements Storeable
return Nami::login($this->mglnr, $this->password); return Nami::login($this->mglnr, $this->password);
} }
/**
* @inheritdoc
*/
public function rules(): array
{
return [
'mglnr' => 'required',
'password' => 'required',
'default_group_id' => 'required',
];
}
public function beforeSave(ActionRequest $request): void
{
NamiLoginCheckAction::run([
'mglnr' => $request->mglnr,
'password' => $request->password,
]);
}
public function localGroup(): ?Group public function localGroup(): ?Group
{ {
return Group::firstWhere('nami_id', $this->default_group_id); return Group::firstWhere('nami_id', $this->default_group_id);
} }
public static function storeAction(): string
{
return SettingSaveAction::class;
}
public static function title(): string public static function title(): string
{ {
return 'NaMi-Login'; return 'NaMi-Login';

View File

@ -20,10 +20,6 @@ class SettingFactory
{ {
$this->settings[] = $setting; $this->settings[] = $setting;
if (new $setting instanceof Storeable) {
$this->registerStoreRoute(new $setting);
}
if (1 === count($this->settings)) { if (1 === count($this->settings)) {
app(Router::class)->redirect('/setting', '/setting/' . $setting::group()); app(Router::class)->redirect('/setting', '/setting/' . $setting::group());
} }
@ -35,8 +31,8 @@ class SettingFactory
public function getShare(): array public function getShare(): array
{ {
return collect($this->settings)->map(fn ($setting) => [ return collect($this->settings)->map(fn ($setting) => [
'url' => $setting::url(), 'url' => (new $setting)->url(),
'is_active' => '/' . request()->path() === $setting::url(), 'is_active' => url(request()->path()) === (new $setting)->url(),
'title' => $setting::title(), 'title' => $setting::title(),
]) ])
->toArray(); ->toArray();
@ -48,9 +44,4 @@ class SettingFactory
return app($settingClass); return app($settingClass);
} }
protected function registerStoreRoute(Storeable $setting): void
{
app(Router::class)->middleware(['web', 'auth:web'])->post($setting::url(), $setting::storeAction());
}
} }

View File

@ -1,26 +0,0 @@
<?php
namespace App\Setting;
use Closure;
use Illuminate\Http\Request;
use Inertia;
class SettingMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
Inertia::share([
'setting_menu' => app(SettingFactory::class)->getShare(),
]);
return $next($request);
}
}

View File

@ -8,6 +8,7 @@ use App\Invoice\InvoiceSettings;
use App\Mailgateway\MailgatewaySettings; use App\Mailgateway\MailgatewaySettings;
use App\Module\ModuleSettings; use App\Module\ModuleSettings;
use App\Prevention\PreventionSettings; use App\Prevention\PreventionSettings;
use App\Setting\Actions\StoreAction;
use App\Setting\Actions\ViewAction; use App\Setting\Actions\ViewAction;
use Illuminate\Routing\Router; use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -23,7 +24,9 @@ class SettingServiceProvider extends ServiceProvider
{ {
app()->singleton(SettingFactory::class, fn () => new SettingFactory()); app()->singleton(SettingFactory::class, fn () => new SettingFactory());
app(Router::class)->bind('settingGroup', fn ($param) => app(SettingFactory::class)->resolveGroupName($param)); app(Router::class)->bind('settingGroup', fn ($param) => app(SettingFactory::class)->resolveGroupName($param));
app(Router::class)->middleware(['web', 'auth:web', SettingMiddleware::class])->get('/setting/{settingGroup}', ViewAction::class); app(Router::class)->middleware(['web', 'auth:web'])->name('setting.view')->get('/setting/{settingGroup}', ViewAction::class);
app(Router::class)->middleware(['web', 'auth:web'])->name('setting.data')->get('/setting/{settingGroup}/data', ViewAction::class);
app(Router::class)->middleware(['web', 'auth:web'])->name('setting.store')->post('/setting/{settingGroup}', StoreAction::class);
} }
/** /**

View File

@ -17,7 +17,7 @@
<f-text id="website" v-model="inner.website" label="Webseite"></f-text> <f-text id="website" v-model="inner.website" label="Webseite"></f-text>
<f-text id="iban" v-model="inner.iban" label="IBAN"></f-text> <f-text id="iban" v-model="inner.iban" label="IBAN"></f-text>
<f-text id="bic" v-model="inner.bic" label="BIC"></f-text> <f-text id="bic" v-model="inner.bic" label="BIC"></f-text>
<f-text id="remember_weeks" v-model="inner.remember_weeks" type="number" label="Erinnerung alle X Wochen versenden"></f-text> <f-text id="remember_weeks" v-model="inner.rememberWeeks" type="number" label="Erinnerung alle X Wochen versenden"></f-text>
</form> </form>
</setting-layout> </setting-layout>
</page-layout> </page-layout>

View File

@ -9,8 +9,8 @@
<p class="text-sm">Hier kannst du Einstellungen für Anmeldeformulare setzen.</p> <p class="text-sm">Hier kannst du Einstellungen für Anmeldeformulare setzen.</p>
</div> </div>
<div class="grid grid-cols-2 gap-4"> <div class="grid grid-cols-2 gap-4">
<f-text id="register_url" v-model="inner.register_url" label="Formular-Link"></f-text> <f-text id="register_url" v-model="inner.registerUrl" label="Formular-Link"></f-text>
<f-text id="clear_cache_url" v-model="inner.clear_cache_url" label="Frontend-Cache-Url"></f-text> <f-text id="clear_cache_url" v-model="inner.clearCacheUrl" label="Frontend-Cache-Url"></f-text>
</div> </div>
</form> </form>
</setting-layout> </setting-layout>

View File

@ -11,10 +11,8 @@ use App\Membership\Actions\MassStoreAction;
use App\Membership\Actions\MembershipDestroyAction; use App\Membership\Actions\MembershipDestroyAction;
use App\Membership\Actions\MembershipStoreAction; use App\Membership\Actions\MembershipStoreAction;
use App\Subactivity; use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Tests\EndToEndTestCase; use Tests\EndToEndTestCase;
use Tests\TestCase;
use Throwable; use Throwable;
use Zoomyboy\LaravelNami\Fakes\MembershipFake; use Zoomyboy\LaravelNami\Fakes\MembershipFake;

View File

@ -10,6 +10,15 @@ class SettingTest extends TestCase
{ {
use DatabaseTransactions; use DatabaseTransactions;
public function testItDisplaysView(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
$this->get(route('setting.view', ['settingGroup' => 'bill']))
->assertOk()
->assertComponent('setting/Bill');
}
public function testDisplaySettings(): void public function testDisplaySettings(): void
{ {
$this->withoutExceptionHandling()->login()->loginNami(); $this->withoutExceptionHandling()->login()->loginNami();
@ -27,7 +36,7 @@ class SettingTest extends TestCase
'rememberWeeks' => 6 'rememberWeeks' => 6
])->save(); ])->save();
$this->get('/setting/bill') $this->get(route('setting.data', ['settingGroup' => 'bill']))
->assertOk() ->assertOk()
->assertComponent('setting/Bill') ->assertComponent('setting/Bill')
->assertInertiaPath('data.from_long', 'DPSG Stamm Muster') ->assertInertiaPath('data.from_long', 'DPSG Stamm Muster')
@ -40,16 +49,16 @@ class SettingTest extends TestCase
->assertInertiaPath('data.zip', '12345') ->assertInertiaPath('data.zip', '12345')
->assertInertiaPath('data.iban', 'DE05') ->assertInertiaPath('data.iban', 'DE05')
->assertInertiaPath('data.bic', 'SOLSDE') ->assertInertiaPath('data.bic', 'SOLSDE')
->assertInertiaPath('data.remember_weeks', 6); ->assertInertiaPath('data.rememberWeeks', 6);
} }
public function testItReturnsTabs(): void public function testItReturnsTabs(): void
{ {
$this->withoutExceptionHandling()->login()->loginNami(); $this->withoutExceptionHandling()->login()->loginNami();
$this->get('/setting/bill') $this->get(route('setting.view', ['settingGroup' => 'bill']))
->assertInertiaPath('setting_menu.1.title', 'Rechnung') ->assertInertiaPath('setting_menu.1.title', 'Rechnung')
->assertInertiaPath('setting_menu.1.url', '/setting/bill') ->assertInertiaPath('setting_menu.1.url', url('/setting/bill'))
->assertInertiaPath('setting_menu.1.is_active', true) ->assertInertiaPath('setting_menu.1.is_active', true)
->assertInertiaPath('setting_menu.0.is_active', false); ->assertInertiaPath('setting_menu.0.is_active', false);
} }
@ -69,7 +78,7 @@ class SettingTest extends TestCase
'zip' => '12345', 'zip' => '12345',
'iban' => 'DE05', 'iban' => 'DE05',
'bic' => 'SOLSDE', 'bic' => 'SOLSDE',
'remember_weeks' => 10 'rememberWeeks' => 10
]); ]);
$response->assertRedirect('/setting/bill'); $response->assertRedirect('/setting/bill');