feat: inisialisasi project kit v2
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Policies\UserPolicy;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Vite;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
use Laravel\Passport\Contracts\AuthorizationViewResponse;
|
||||
use Laravel\Passport\Http\Responses\SimpleViewResponse;
|
||||
use Laravel\Passport\Passport;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
Passport::ignoreRoutes();
|
||||
|
||||
$this->app->bind(\Illuminate\Contracts\Auth\StatefulGuard::class, function () {
|
||||
return \Illuminate\Support\Facades\Auth::guard('web');
|
||||
});
|
||||
|
||||
$this->app->bind(AuthorizationViewResponse::class, function () {
|
||||
return new SimpleViewResponse('passport::authorize');
|
||||
});
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
Vite::prefetch(concurrency: 3);
|
||||
|
||||
Gate::policy(User::class, UserPolicy::class);
|
||||
|
||||
// super-admin bypasses all gates
|
||||
Gate::before(function (User $user, string $ability) {
|
||||
if ($user->hasRole('super-admin')) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Dynamic password rules driven by system settings
|
||||
Password::defaults(function () {
|
||||
$settings = Cache::get('system_settings', []);
|
||||
|
||||
$rule = Password::min((int) ($settings['password_minimum_length'] ?? 8));
|
||||
|
||||
if (filter_var($settings['password_require_symbols'] ?? false, FILTER_VALIDATE_BOOLEAN)) {
|
||||
$rule->symbols();
|
||||
}
|
||||
if (filter_var($settings['password_require_numbers'] ?? false, FILTER_VALIDATE_BOOLEAN)) {
|
||||
$rule->numbers();
|
||||
}
|
||||
if (filter_var($settings['password_require_mixed_case'] ?? false, FILTER_VALIDATE_BOOLEAN)) {
|
||||
$rule->mixedCase();
|
||||
}
|
||||
|
||||
return $rule;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ConfigServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void {}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$settings = Cache::rememberForever('system_settings', function () {
|
||||
return Setting::all()->pluck('value', 'key')->toArray();
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
// DB not ready yet (e.g. first migration)
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($settings['app_name'])) {
|
||||
Config::set('app.name', $settings['app_name']);
|
||||
}
|
||||
|
||||
if (!empty($settings['mail_host'])) {
|
||||
Config::set([
|
||||
'mail.mailers.smtp.host' => $settings['mail_host'],
|
||||
'mail.mailers.smtp.port' => $settings['mail_port'] ?? '587',
|
||||
'mail.mailers.smtp.username' => $settings['mail_username'] ?? '',
|
||||
'mail.mailers.smtp.password' => $settings['mail_password'] ?? '',
|
||||
'mail.mailers.smtp.encryption' => $settings['mail_encryption'] ?? 'tls',
|
||||
'mail.from.address' => $settings['mail_from_address'] ?? 'hello@example.com',
|
||||
'mail.from.name' => $settings['mail_from_name'] ?? ($settings['app_name'] ?? config('app.name')),
|
||||
]);
|
||||
}
|
||||
|
||||
if (!empty($settings['oauth_google_client_id'])) {
|
||||
Config::set([
|
||||
'services.google.client_id' => $settings['oauth_google_client_id'],
|
||||
'services.google.client_secret' => $settings['oauth_google_client_secret'] ?? '',
|
||||
'services.google.redirect' => url('/auth/google/callback'),
|
||||
]);
|
||||
}
|
||||
|
||||
if (!empty($settings['oauth_github_client_id'])) {
|
||||
Config::set([
|
||||
'services.github.client_id' => $settings['oauth_github_client_id'],
|
||||
'services.github.client_secret' => $settings['oauth_github_client_secret'] ?? '',
|
||||
'services.github.redirect' => url('/auth/github/callback'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user