Added mail settings for SMTP. Configuration values that are the same as the defaults are no longer saved to the database (and are purged if changed to the default.) Started adding a "test mail config" settings function

This commit is contained in:
Andy Heathershaw 2016-09-06 12:23:14 +01:00
parent 6875b0dd22
commit c7a56d1753
14 changed files with 294 additions and 28 deletions

View File

@ -51,11 +51,17 @@ class ConfigHelper
public function defaults()
{
$currentAppName = $this->get('app_name', false);
return array(
'allow_self_registration' => true,
'app_name' => trans('global.app_name'),
'date_format' => $this->allowedDateFormats()[0],
'require_email_verification' => true
'require_email_verification' => true,
'sender_address' => sprintf('hostmaster@%s', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost')),
'sender_name' => (is_null($currentAppName) ? trans('global.app_name') : $currentAppName),
'smtp_server' => 'localhost',
'smtp_port' => 25
);
}
@ -63,7 +69,13 @@ class ConfigHelper
{
$config = Configuration::where('key', $key)->first();
return (is_null($config) ? $this->defaults()[$key] : $config->value);
if (is_null($config))
{
$defaults = $this->defaults();
return ($defaultIfUnset && isset($defaults[$key]) ? $defaults[$key] : null);
}
return $config->value;
}
public function getAll()

View File

@ -32,6 +32,11 @@ class ThemeHelper
}
public function render($viewPath, array $viewData = array())
{
return view($this->viewName($viewPath), $viewData);
}
public function viewName($viewPath)
{
$themeName = $this->getThemeName();
@ -46,7 +51,7 @@ class ThemeHelper
$themeName = ThemeHelper::DEFAULT_THEME;
}
return view(sprintf('themes.%s.%s', $themeName, $viewPath), $viewData);
return sprintf('themes.%s.%s', $themeName, $viewPath);
}
private function getRealFilePath($themeName, $viewPath)

View File

@ -32,26 +32,46 @@ class DefaultController extends Controller
{
$checkboxKeys = [
'allow_self_registration',
'require_email_verification'
'require_email_verification',
];
$updateKeys = [
'app_name',
'date_format',
'sender_address',
'sender_name',
'theme'
];
$defaults = UserConfig::defaults();
foreach ($updateKeys as $key)
{
$config = UserConfig::getOrCreateModel($key);
$config->value = $request->request->get($key);
$config->save();
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
{
$config->delete();
}
else
{
$config->save();
}
}
foreach ($checkboxKeys as $key)
{
$config = UserConfig::getOrCreateModel($key);
$config->value = ($request->request->get($key) == 'on' ? 1 : 0);
$config->save();
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
{
$config->delete();
}
else
{
$config->save();
}
}
$request->session()->flash('success', trans('admin.settings_saved_message'));

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Auth;
use App\Facade\Theme;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
@ -38,6 +39,16 @@ class LoginController extends Controller
$this->middleware('guest', ['except' => 'logout']);
}
protected function credentials(Request $request)
{
$result = $request->only($this->username(), 'password');
// Only allow activated users to login
$result['is_activated'] = true;
return $result;
}
/**
* Show the application's login form.
*

View File

@ -4,7 +4,11 @@ namespace App\Http\Controllers\Auth;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\MiscHelper;
use App\Mail\UserActivationRequired;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
@ -64,21 +68,50 @@ class RegisterController extends Controller
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$activationData = [
'is_activated' => true
];
if (UserConfig::get('require_email_verification'))
{
$activationData['is_activated'] = false;
$activationData['activation_code'] = MiscHelper::randomString();
}
return User::create(array_merge(
[
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'is_admin' => false
],
$activationData
));
}
public function register(Request $request)
{
if (UserConfig::get('allow_self_registration') == 1)
if (!UserConfig::get('allow_self_registration'))
{
return parent::register($request);
return redirect(route('home'));
}
return redirect(route('home'));
$this->validator($request->all())->validate();
/** @var User $user */
$user = $this->create($request->all());
if ($user->is_activated)
{
$this->guard()->login($user);
}
else
{
// Send activation e-mail
Mail::to($user)->send(new UserActivationRequired($user));
}
return redirect($this->redirectPath());
}
/**
@ -88,11 +121,11 @@ class RegisterController extends Controller
*/
public function showRegistrationForm()
{
if (UserConfig::get('allow_self_registration') == 1)
if (!UserConfig::get('allow_self_registration'))
{
return Theme::render('auth.register');
return redirect(route('home'));
}
return redirect(route('home'));
return Theme::render('auth.register');
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMailConfig extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from(UserConfig::get('sender_address'), UserConfig::get('sender_name'))
->subject(trans('email.test_email_subject', ['app_name' => UserConfig::get('app_name')]))
->view(Theme::viewName('email.test_email'));
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Mail;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserActivationRequired extends Mailable
{
use Queueable, SerializesModels;
private $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->from(UserConfig::get('sender_address'), UserConfig::get('sender_name'))
->subject(trans('email.activation_required_subject', ['app_name' => UserConfig::get('app_name')]))
->view(Theme::viewName('email.user_activation_required'))
->with([
'user' => $this->user
]);
}
}

View File

@ -40,6 +40,9 @@ class AppServiceProvider extends ServiceProvider
$this->addThemeInfoToView();
$this->addAlbumsToView();
}
// Set the default mail configuration as per user's requirements
$this->updateMailConfig();
}
/**
@ -72,4 +75,17 @@ class AppServiceProvider extends ServiceProvider
// Also add a theme_url key
View::share('theme_url', sprintf('themes/%s', Theme::current()));
}
private function updateMailConfig()
{
/** @var \Swift_Mailer $swiftMailer */
$swiftMailer = $this->app->mailer->getSwiftMailer();
/** @var \Swift_SmtpTransport $transport */
$transport = $swiftMailer->getTransport();
$transport->setHost(UserConfig::get('smtp_server'));
$transport->setPort(intval(UserConfig::get('smtp_port')));
$transport->setUsername(UserConfig::get('smtp_username'));
$transport->setPassword(UserConfig::get('smtp_password'));
}
}

View File

@ -15,7 +15,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'is_admin'
'name', 'email', 'password', 'is_admin', 'is_activated', 'activation_token'
];
/**
@ -24,6 +24,6 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'password', 'remember_token', 'activation_token'
];
}

View File

@ -1,5 +1,11 @@
<?php
/*
* Andy Heathershaw, 06/09/2016
* All of the mailer settings are configured by the user through the application's UI.
* Our AppServiceProvider overrides any SMTP settings provided here, in the private method updateMailConfig().
*/
return [
/*
@ -16,7 +22,7 @@ return [
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
'driver' => 'smtp',
/*
|--------------------------------------------------------------------------
@ -29,7 +35,7 @@ return [
|
*/
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'host' => '',
/*
|--------------------------------------------------------------------------
@ -42,7 +48,7 @@ return [
|
*/
'port' => env('MAIL_PORT', 587),
'port' => 0,
/*
|--------------------------------------------------------------------------
@ -56,8 +62,8 @@ return [
*/
'from' => [
'address' => 'hello@example.com',
'name' => 'Example',
'address' => '',
'name' => '',
],
/*
@ -71,7 +77,7 @@ return [
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'encryption' => '',
/*
|--------------------------------------------------------------------------
@ -84,7 +90,7 @@ return [
|
*/
'username' => env('MAIL_USERNAME'),
'username' => '',
/*
|--------------------------------------------------------------------------
@ -97,7 +103,7 @@ return [
|
*/
'password' => env('MAIL_PASSWORD'),
'password' => '',
/*
|--------------------------------------------------------------------------
@ -110,6 +116,6 @@ return [
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
'sendmail' => '',
];

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddUserActivationColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table)
{
$table->boolean('is_activated');
$table->string('activation_token')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table)
{
$table->dropColumn('is_activated');
$table->dropColumn('activation_token');
});
}
}

View File

@ -0,0 +1,5 @@
<?php
return [
'activation_required_subject' => 'Activate your :app_name account',
'test_email_subject' => 'Test e-mail from :app_name'
];

View File

@ -14,6 +14,7 @@
{{-- Nav tabs --}}
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#general-tab" aria-controls="general-tab" role="tab" data-toggle="tab"><i class="fa fa-fw fa-info-circle"></i> General</a></li>
<li role="presentation"><a href="#email-tab" aria-controls="email-tab" role="tab" data-toggle="tab"><i class="fa fa-fw fa-envelope"></i> E-mail</a></li>
<li role="presentation"><a href="#security-tab" aria-controls="security-tab" role="tab" data-toggle="tab"><i class="fa fa-fw fa-lock"></i> Security</a></li>
</ul>
@ -44,6 +45,42 @@
</div>
</div>
<div role="tabpanel" class="tab-pane" id="email-tab">
<div class="form-group">
{!! Form::label('sender_name', 'Sender name:', ['class' => 'control-label']) !!}
{!! Form::text('sender_name', old('sender_name'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('sender_address', 'Sender address:', ['class' => 'control-label']) !!}
{!! Form::text('sender_address', old('sender_address'), ['class' => 'form-control']) !!}
</div>
<hr/>
<p style="margin-bottom: 15px;">Configure your SMTP server using the settings below.</p>
<div class="form-group">
{!! Form::label('smtp_server', 'Hostname:', ['class' => 'control-label']) !!}
{!! Form::text('smtp_server', old('smtp_server'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('smtp_port', 'Port:', ['class' => 'control-label']) !!}
{!! Form::text('smtp_port', old('smtp_port'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('smtp_username', 'Username:', ['class' => 'control-label']) !!}
{!! Form::text('smtp_username', old('smtp_username'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('smtp_password', 'Password:', ['class' => 'control-label']) !!}
{!! Form::text('smtp_password', old('smtp_password'), ['class' => 'form-control']) !!}
</div>
</div>
<div role="tabpanel" class="tab-pane" id="security-tab">
<div class="checkbox">
<label>

View File

@ -0,0 +1,6 @@
Hi {{ $user->name }},
Thanks for registering.
Regards,
{{ UserConfig::get('app_name') }}