feat: Added Mail Settings & Fixed ReCaptcha bug on DB Change (reload config correctly)

This commit is contained in:
IceToast 2022-01-21 17:20:23 +01:00
parent 9193e08180
commit 99dff634b0
6 changed files with 236 additions and 56 deletions

View file

@ -5,9 +5,9 @@ namespace App\Classes\Settings;
use App\Models\Settings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Validator;
class Misc
{
public function __construct()
@ -15,8 +15,6 @@ class Misc
return;
}
public function updateSettings(Request $request)
{
$validator = Validator::make($request->all(), [
@ -31,6 +29,14 @@ class Misc
'recaptcha-site-key' => 'nullable|string',
'recaptcha-secret-key' => 'nullable|string',
'enable-recaptcha' => 'nullable|string',
'mailservice' => 'nullable|string',
'mailhost' => 'nullable|string',
'mailport' => 'nullable|string',
'mailusername' => 'nullable|string',
'mailpassword' => 'nullable|string',
'mailencryption' => 'nullable|string',
'mailfromadress' => 'nullable|string',
'mailfromname' => 'nullable|string',
]);
if ($validator->fails()) {
@ -55,12 +61,17 @@ class Misc
"SETTINGS::RECAPTCHA:SITE_KEY" => "recaptcha-site-key",
"SETTINGS::RECAPTCHA:SECRET_KEY" => "recaptcha-secret-key",
"SETTINGS::RECAPTCHA:ENABLED" => "enable-recaptcha",
"SETTINGS::MAIL:MAILER" => "mailservice",
"SETTINGS::MAIL:HOST" => "mailhost",
"SETTINGS::MAIL:PORT" => "mailport",
"SETTINGS::MAIL:USERNAME" => "mailusername",
"SETTINGS::MAIL:PASSWORD" => "mailpassword",
"SETTINGS::MAIL:ENCRYPTION" => "mailencryption",
"SETTINGS::MAIL:FROM_ADDRESS" => "mailfromadress",
"SETTINGS::MAIL:FROM_NAME" => "mailfromname",
];
Config::set('services.discord.client_id', $request->get("discord-client-id"));
Config::set('services.discord.client_secret', $request->get("discord-client-secret"));
foreach ($values as $key => $value) {
$param = $request->get($value);

View file

@ -16,9 +16,9 @@ class ProfileController extends Controller
{
return view('profile.index')->with([
'user' => Auth::user(),
'credits_reward_after_verify_discord' => Settings::getValueByKey('SETTINGS::USER:CREDITS_REWARD_AFTER_VERIFY_DISCORD'),
'force_email_verification' => Settings::getValueByKey('SETTINGS::USER:FORCE_EMAIL_VERIFICATION'),
'force_discord_verification' => Settings::getValueByKey('SETTINGS::USER:FORCE_DISCORD_VERIFICATION'),
'credits_reward_after_verify_discord' => config('SETTINGS::USER:CREDITS_REWARD_AFTER_VERIFY_DISCORD'),
'force_email_verification' => config('SETTINGS::USER:FORCE_EMAIL_VERIFICATION'),
'force_discord_verification' => config('SETTINGS::USER:FORCE_DISCORD_VERIFICATION'),
]);
}
@ -34,15 +34,15 @@ class ProfileController extends Controller
$user = User::findOrFail($id);
//update password if necessary
if (!is_null($request->input('new_password'))){
if (!is_null($request->input('new_password'))) {
//validate password request
$request->validate([
'current_password' => [
'required' ,
'required',
function ($attribute, $value, $fail) use ($user) {
if (!Hash::check($value, $user->password)) {
$fail('The '.$attribute.' is invalid.');
$fail('The ' . $attribute . ' is invalid.');
}
},
],
@ -58,13 +58,13 @@ class ProfileController extends Controller
//validate request
$request->validate([
'name' => 'required|min:4|max:30|alpha_num|unique:users,name,'.$id.',id',
'email' => 'required|email|max:64|unique:users,email,'.$id.',id',
'name' => 'required|min:4|max:30|alpha_num|unique:users,name,' . $id . ',id',
'email' => 'required|email|max:64|unique:users,email,' . $id . ',id',
'avatar' => 'nullable'
]);
//update avatar
if(!is_null($request->input('avatar'))){
if (!is_null($request->input('avatar'))) {
$avatar = json_decode($request->input('avatar'));
if ($avatar->input->size > 3000000) abort(500);
@ -83,6 +83,6 @@ class ProfileController extends Controller
'email' => $request->input('email'),
]);
return redirect()->route('profile.index')->with('success' , __('Profile updated'));
return redirect()->route('profile.index')->with('success', __('Profile updated'));
}
}

View file

@ -4,7 +4,7 @@ namespace App\Providers;
use App\Models\Settings;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
@ -51,20 +51,58 @@ class AppServiceProvider extends ServiceProvider
return $ok;
});
if (!App::runningInConsole()) {
// Set Discord-API Config
config(['services.discord.client_id' => Settings::getValueByKey('SETTINGS::DISCORD:CLIENT_ID')]);
config(['services.discord.client_secret' => Settings::getValueByKey('SETTINGS::DISCORD:CLIENT_SECRET')]);
// Set Recaptcha API Config
config(['recaptcha.api_site_key' => Settings::getValueByKey('SETTINGS::RECAPTCHA:SITE_KEY')]);
config(['recaptcha.api_secret_key' => Settings::getValueByKey('SETTINGS::RECAPTCHA:SECRET_KEY')]);
// Set all configs from database
$settings = Settings::all();
foreach ($settings as $setting) {
config([$setting->key => $setting->value]);
}
// TODO: Check if Installer Lockfile exists instead of "running in console"
$settings = Settings::all();
// Set all configs from database
foreach ($settings as $setting) {
config([$setting->key => $setting->value]);
}
// Set Mail Config
//only update config if mail settings have changed in DB
if (
config('mail.default') != config('SETTINGS:MAIL:MAILER') ||
config('mail.mailers.smtp.host') != config('SETTINGS:MAIL:HOST') ||
config('mail.mailers.smtp.port') != config('SETTINGS:MAIL:PORT') ||
config('mail.mailers.smtp.username') != config('SETTINGS:MAIL:USERNAME') ||
config('mail.mailers.smtp.password') != config('SETTINGS:MAIL:PASSWORD') ||
config('mail.mailers.smtp.encryption') != config('SETTINGS:MAIL:ENCRYPTION') ||
config('mail.from.address') != config('SETTINGS:MAIL:FROM_ADDRESS') ||
config('mail.from.name') != config('SETTINGS:MAIL:FROM_NAME')
) {
config(['mail.default' => config('SETTINGS::MAIL:MAILER')]);
config(['mail.mailers.smtp' => [
'transport' => 'smtp',
'host' => config('SETTINGS::MAIL:HOST'),
'port' => config('SETTINGS::MAIL:PORT'),
'encryption' => config('SETTINGS::MAIL:ENCRYPTION'),
'username' => config('SETTINGS::MAIL:USERNAME'),
'password' => config('SETTINGS::MAIL:PASSWORD'),
'timeout' => null,
'auth_mode' => null,
]]);
config(['mail.from' => ['address' => config('SETTINGS::MAIL:FROM_ADDRESS'), 'name' => config('SETTINGS::MAIL:FROM_NAME')]]);
Artisan::call('queue:restart');
}
// Set Recaptcha API Config
//only update config if recaptcha settings have changed in DB
if (
config('recaptcha.api_site_key') != config('SETTINGS::RECAPTCHA:SITE_KEY') ||
config('recaptcha.api_secret_key') != config('SETTINGS::RECAPTCHA:SECRET_KEY')
) {
config(['recaptcha.api_site_key' => config('SETTINGS::RECAPTCHA:SITE_KEY')]);
config(['recaptcha.api_secret_key' => config('SETTINGS::RECAPTCHA:SECRET_KEY')]);
Artisan::call('config:clear');
Artisan::call('cache:clear');
}
// Set Discord-API Config
config(['services.discord.client_id' => config('SETTINGS::DISCORD:CLIENT_ID')]);
config(['services.discord.client_secret' => config('SETTINGS::DISCORD:CLIENT_SECRET')]);
}
}

View file

@ -45,31 +45,22 @@ return [
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
// 'ses' => [
// 'transport' => 'ses',
// ],
//
// 'mailgun' => [
// 'transport' => 'mailgun',
// ],
//
// 'postmark' => [
// 'transport' => 'postmark',
// ],
//
// 'sendmail' => [
// 'transport' => 'sendmail',
// 'path' => '/usr/sbin/sendmail -bs',
// ],
],
/*

View file

@ -405,6 +405,64 @@ class SettingsSeeder extends Seeder
], [
'value' => 'true',
'type' => 'boolean',
'description' => 'Enables or disables the ReCaptcha feature on the registration/login page'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:MAILER',
], [
'value' => 'smtp',
'type' => 'string',
'description' => 'Selected Mailer (smtp, mailgun, sendgrid, mailtrap)'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:HOST',
], [
'value' => 'localhost',
'type' => 'string',
'description' => 'Mailer Host Adress'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:PORT',
], [
'value' => '1025',
'type' => 'string',
'description' => 'Mailer Server Port'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:USERNAME',
], [
'value' => '',
'type' => 'string',
'description' => 'Mailer Username'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:PASSWORD',
], [
'value' => '',
'type' => 'string',
'description' => 'Mailer Password'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:ENCRYPTION',
], [
'value' => 'tls',
'type' => 'string',
'description' => 'Mailer Encryption (tls, ssl)'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:FROM_ADDRESS',
], [
'value' => '',
'type' => 'string',
'description' => 'Mailer From Address'
]);
Settings::firstOrCreate([
'key' => 'SETTINGS::MAIL:FROM_NAME',
], [
'value' => env('APP_NAME', 'Controlpanel'),
'type' => 'string',
'description' => 'Mailer From Name'
]);
}
}

View file

@ -5,6 +5,86 @@
@method('PATCH')
<div class="row">
{{-- E-Mail --}}
<div class="col-md-3 px-3">
<div class="row mb-2">
<div class="col text-center">
<h1>E-Mail</h1>
</div>
</div>
<div class="custom-control mb-3 p-0">
<label for="mailservice">{{ __('Mail Service') }}:
<i data-toggle="popover" data-trigger="hover"
data-content="{{ __('The Mailer to send e-mails with') }}" class="fas fa-info-circle"></i>
</label>
<select id="mailservice" style="width:100%" class="custom-select" name="mailservice" required
autocomplete="off" @error('mailservice') is-invalid @enderror>
@foreach (array_keys(config('mail.mailers')) as $mailer)
<option value="{{ $mailer }}" @if (config('SETTINGS::MAIL:MAILER') == $mailer) selected
@endif>{{ __($mailer) }}</option>
@endforeach
</select>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailhost">{{ __('Mail Host') }}:</label>
<input x-model="mailhost" id="mailhost" name="mailhost" type="text"
value="{{ config('SETTINGS::MAIL:HOST') }}"
class="form-control @error('mailhost') is-invalid @enderror">
</div>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailport">{{ __('Mail Port') }}:</label>
<input x-model="mailhost" id="mailport" name="mailport" type="text"
value="{{ config('SETTINGS::MAIL:PORT') }}"
class="form-control @error('mailport') is-invalid @enderror">
</div>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailusername">{{ __('Mail Username') }}:</label>
<input x-model="mailusername" id="mailusername" name="mailusername" type="text"
value="{{ config('SETTINGS::MAIL:USERNAME') }}"
class="form-control @error('mailusername') is-invalid @enderror">
</div>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailpassword">{{ __('Mail Password') }}:</label>
<input x-model="mailpassword" id="mailpassword" name="mailpassword" type="text"
value="{{ config('SETTINGS::MAIL:PASSWORD') }}"
class="form-control @error('mailpassword') is-invalid @enderror">
</div>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailencryption">{{ __('Mail Encryption') }}:</label>
<input x-model="mailencryption" id="mailencryption" name="mailencryption" type="text"
value="{{ config('SETTINGS::MAIL:ENCRYPTION') }}"
class="form-control @error('mailencryption') is-invalid @enderror">
</div>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailfromadress">{{ __('Mail From Adress') }}:</label>
<input x-model="mailfromadress" id="mailfromadress" name="mailfromadress" type="text"
value="{{ config('SETTINGS::MAIL:FROM_ADDRESS') }}"
class="form-control @error('mailfromadress') is-invalid @enderror">
</div>
</div>
<div class="form-group mb-3">
<div class="custom-control p-0">
<label for="mailfromname">{{ __('Mail From Name') }}:</label>
<input x-model="mailfromname" id="mailfromname" name="mailfromname" type="text"
value="{{ config('SETTINGS::MAIL:FROM_NAME') }}"
class="form-control @error('mailfromname') is-invalid @enderror">
</div>
</div>
</div>
<!-- DISCORD -->
<div class="col-md-3 px-3">
<div class="row mb-2">
@ -104,6 +184,8 @@
</div>
</div>
</div>
</div>
<div class="row">
<button class="btn btn-primary mt-3 ml-3">{{ __('Submit') }}</button>