From 2c3fb557d6098301f6549fae69ccb1f19d8572b8 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 5 Sep 2015 17:42:05 +0100 Subject: [PATCH] Started social registration --- app/Http/Controllers/Auth/AuthController.php | 14 +- app/Http/Controllers/Controller.php | 8 +- app/Http/Controllers/UserController.php | 2 + app/Http/Middleware/Authenticate.php | 3 +- app/Http/routes.php | 1 + app/Role.php | 14 ++ app/Services/SettingService.php | 11 +- app/Services/SocialAuthService.php | 32 +-- resources/assets/sass/_forms.scss | 5 + resources/assets/sass/_grid.scss | 5 +- resources/views/auth/login.blade.php | 61 +++--- resources/views/auth/register.blade.php | 50 +++++ .../views/emails/email-confirmation.blade.php | 176 +++++++++++++++++ resources/views/emails/password.blade.php | 187 +----------------- resources/views/form/role-select.blade.php | 2 +- resources/views/public.blade.php | 17 ++ resources/views/settings/index.blade.php | 52 ++++- 17 files changed, 390 insertions(+), 250 deletions(-) create mode 100644 resources/views/auth/register.blade.php create mode 100644 resources/views/emails/email-confirmation.blade.php diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index a65710cde..f290aeabb 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -37,7 +37,7 @@ class AuthController extends Controller */ public function __construct(SocialAuthService $socialAuthService) { - $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]); + $this->middleware('guest', ['only' => ['getLogin', 'postLogin', 'getRegister']]); $this->socialAuthService = $socialAuthService; } @@ -71,6 +71,17 @@ class AuthController extends Controller ]); } + /** + * Show the application registration form. + * + * @return \Illuminate\Http\Response + */ + public function getRegister() + { + $socialDrivers = $this->socialAuthService->getActiveDrivers(); + return view('auth.register', ['socialDrivers' => $socialDrivers]); + } + /** * Show the application login form. * @@ -84,7 +95,6 @@ class AuthController extends Controller } $socialDrivers = $this->socialAuthService->getActiveDrivers(); - return view('auth.login', ['socialDrivers' => $socialDrivers]); } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 80c4c5526..13859fed4 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -31,12 +31,12 @@ abstract class Controller extends BaseController { // Get a user instance for the current user $user = auth()->user(); - if (!$user) { - $user = User::getDefault(); - } + if (!$user) $user = User::getDefault(); + // Share variables with views view()->share('signedIn', auth()->check()); view()->share('currentUser', $user); + // Share variables with controllers $this->currentUser = $user; $this->signedIn = auth()->check(); @@ -53,7 +53,7 @@ abstract class Controller extends BaseController if (!$this->currentUser || !$this->currentUser->can($permissionName)) { Session::flash('error', trans('errors.permission')); throw new HttpResponseException( - redirect()->back() + redirect('/') ); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 306641e71..f6a8d13e3 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -152,6 +152,8 @@ class UserController extends Controller return $this->currentUser->id == $id; }); $user = $this->user->findOrFail($id); + // Delete social accounts + $user->socialAccounts()->delete(); $user->delete(); return redirect('/users'); } diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index 58b25ee3f..f0b2f7eda 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -34,8 +34,7 @@ class Authenticate */ public function handle($request, Closure $next) { - $sitePublic = Setting::get('app-public', false) === 'true'; - if ($this->auth->guest() && !$sitePublic) { + if ($this->auth->guest() && !Setting::get('app-public')) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { diff --git a/app/Http/routes.php b/app/Http/routes.php index 97908ff48..be7ac8736 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -87,6 +87,7 @@ Route::get('/login/service/{socialDriver}/detach', 'Auth\AuthController@detachSo Route::get('/login', 'Auth\AuthController@getLogin'); Route::post('/login', 'Auth\AuthController@postLogin'); Route::get('/logout', 'Auth\AuthController@getLogout'); +Route::get('/register', 'Auth\AuthController@getRegister'); // Password reset link request routes... Route::get('/password/email', 'Auth\PasswordController@getEmail'); diff --git a/app/Role.php b/app/Role.php index dd955863e..d35b349a8 100644 --- a/app/Role.php +++ b/app/Role.php @@ -6,6 +6,12 @@ use Illuminate\Database\Eloquent\Model; class Role extends Model { + /** + * Sets the default role name for newly registed users. + * @var string + */ + protected static $default = 'viewer'; + /** * The roles that belong to the role. */ @@ -31,4 +37,12 @@ class Role extends Model $this->permissions()->attach($permission->id); } + /** + * Get an instance of the default role. + * @return Role + */ + public static function getDefault() + { + return static::where('name', '=', static::$default)->first(); + } } diff --git a/app/Services/SettingService.php b/app/Services/SettingService.php index 46c802a05..b7215f524 100644 --- a/app/Services/SettingService.php +++ b/app/Services/SettingService.php @@ -33,7 +33,16 @@ class SettingService public function get($key, $default = false) { $setting = $this->getSettingObjectByKey($key); - return $setting === null ? $default : $setting->value; + $value = $setting === null ? null : $setting->value; + + // Change string booleans to actual booleans + if($value === 'true') $value = true; + if($value === 'false') $value = false; + + // Set to default if empty + if($value === '') $value = $default; + + return $value === null ? $default : $value; } /** diff --git a/app/Services/SocialAuthService.php b/app/Services/SocialAuthService.php index fda39819d..f76a339b4 100644 --- a/app/Services/SocialAuthService.php +++ b/app/Services/SocialAuthService.php @@ -63,8 +63,8 @@ class SocialAuthService $isLoggedIn = auth()->check(); $currentUser = auth()->user(); - // When a user is not logged in but a matching SocialAccount exists, - // Log the user found on the SocialAccount into the application. + // When a user is not logged in and a matching SocialAccount exists, + // Simply log the user into the application. if (!$isLoggedIn && $socialAccount !== null) { return $this->logUserIn($socialAccount->user); } @@ -87,30 +87,16 @@ class SocialAuthService // When a user is logged in, A social account exists but the users do not match. // Change the user that the social account is assigned to. if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id != $currentUser->id) { - $socialAccount->user_id = $currentUser->id; - $socialAccount->save(); - \Session::flash('success', 'This ' . title_case($socialDriver) . ' account is now attached to your profile.'); + \Session::flash('success', 'This ' . title_case($socialDriver) . ' account is already used buy another user.'); + return redirect($currentUser->getEditUrl()); } - if ($user === null) { - throw new SocialSignInException('A system user with the email ' . $socialUser->getEmail() . - ' was not found and this ' . $socialDriver . ' account is not linked to any users.', '/login'); + // Otherwise let the user know this social account is not used by anyone. + $message = 'This ' . $socialDriver . ' account is not linked to any users. Please attach it in your profile settings'; + if(\Setting::get('registration-enabled')) { + $message .= 'or, If you do not yet have an account, You can register an account using the ' . $socialDriver . ' option'; } - return $this->authenticateUserWithNewSocialAccount($user, $socialUser, $socialUser); - } - - /** - * Logs a user in and creates a new social account entry for future usage. - * @param User $user - * @param string $socialDriver - * @param \Laravel\Socialite\Contracts\User $socialUser - * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector - */ - private function authenticateUserWithNewSocialAccount($user, $socialDriver, $socialUser) - { - $this->fillSocialAccount($socialDriver, $socialUser); - $user->socialAccounts()->save($this->socialAccount); - return $this->logUserIn($user); + throw new SocialSignInException($message . '.', '/login'); } private function logUserIn($user) diff --git a/resources/assets/sass/_forms.scss b/resources/assets/sass/_forms.scss index 59ce23da9..825793e48 100644 --- a/resources/assets/sass/_forms.scss +++ b/resources/assets/sass/_forms.scss @@ -29,6 +29,7 @@ label { font-weight: 500; color: #666; padding-bottom: 2px; + margin-bottom: 0.2em; } label.radio, label.checkbox { @@ -38,6 +39,10 @@ label.radio, label.checkbox { } } +label + p.small { + margin-bottom: 0.8em; +} + input[type="text"], input[type="number"], input[type="email"], input[type="search"], input[type="url"], input[type="password"], select, textarea { @extend .input-base; } diff --git a/resources/assets/sass/_grid.scss b/resources/assets/sass/_grid.scss index 76ee7c591..4532cca6f 100644 --- a/resources/assets/sass/_grid.scss +++ b/resources/assets/sass/_grid.scss @@ -42,9 +42,12 @@ div[class^="col-"] img { } .center-box { - margin: 15vh auto 0 auto; + margin: $-xl auto 0 auto; padding: $-m $-xxl $-xl*2 $-xxl; max-width: 346px; + display: inline-block; + text-align: left; + vertical-align: top; &.login { background-color: #EEE; box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.1); diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index ee5164cd7..2bbf859d6 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -1,38 +1,47 @@ @extends('public') +@section('header-buttons') + @if(Setting::get('registration-enabled')) + Sign up + @endif +@stop + @section('content') -
-

Log In

+
+
+

Log In

-
- {!! csrf_field() !!} + + {!! csrf_field() !!} -
- - @include('form/text', ['name' => 'email']) -
+
+ + @include('form/text', ['name' => 'email']) +
-
- - @include('form/password', ['name' => 'password']) - Forgot Password? -
+
+ + @include('form/password', ['name' => 'password']) + Forgot Password? +
-
- -
-
- @if(count($socialDrivers) > 0) -
-

Social Login

- @if(isset($socialDrivers['google'])) - +
+ +
+ + + @if(count($socialDrivers) > 0) +
+

Social Login

+ @if(isset($socialDrivers['google'])) + + @endif + @if(isset($socialDrivers['github'])) + + @endif @endif - @if(isset($socialDrivers['github'])) - - @endif - @endif +
@stop \ No newline at end of file diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php new file mode 100644 index 000000000..48f8ebaa8 --- /dev/null +++ b/resources/views/auth/register.blade.php @@ -0,0 +1,50 @@ +@extends('public') + +@section('header-buttons') + Sign in +@stop + +@section('content') + +
+
+

Register

+ +
+ {!! csrf_field() !!} + +
+ + @include('form/text', ['name' => 'name']) +
+ +
+ + @include('form/text', ['name' => 'email']) +
+ +
+ + @include('form/password', ['name' => 'password']) +
+ +
+ +
+
+ + @if(count($socialDrivers) > 0) +
+

Social Registration

+ @if(isset($socialDrivers['google'])) + + @endif + @if(isset($socialDrivers['github'])) + + @endif + @endif +
+
+ + +@stop diff --git a/resources/views/emails/email-confirmation.blade.php b/resources/views/emails/email-confirmation.blade.php new file mode 100644 index 000000000..f2c9710a1 --- /dev/null +++ b/resources/views/emails/email-confirmation.blade.php @@ -0,0 +1,176 @@ + + + + + + + Confirm Your Email At {{ Setting::get('app-name')}} + + + + + + + + + + + +
+ +
+ + + + +
+

Email Confirmation

+

Thank's for joining {{ Setting::get('app-name')}}.
+ Please confirm your email address by clicking the button below.

+ + + + +
+

Confirm Email

+
+
+
+ +
+ + + + diff --git a/resources/views/emails/password.blade.php b/resources/views/emails/password.blade.php index 6d4827272..95fe012eb 100644 --- a/resources/views/emails/password.blade.php +++ b/resources/views/emails/password.blade.php @@ -1,186 +1 @@ - - - - - - Password Reset - - - - - - - - - - - - -
- - -
- - - - -
-

Password Reset

-

A password reset was requested for this email address on the application found at {{url('/')}}. If you did not request a password change please ignore this email.

- - - - -
-

Click here to reset your password

-
-
-
- - -
- - - - - \ No newline at end of file + Password Reset From {{ Setting::get('app-name')}}

Password Reset

A password reset was requested for this email address on {{ Setting::get('app-name')}}. If you did not request a password change please ignore this email.

Click here to reset your password

\ No newline at end of file diff --git a/resources/views/form/role-select.blade.php b/resources/views/form/role-select.blade.php index 159487d4c..036ba7847 100644 --- a/resources/views/form/role-select.blade.php +++ b/resources/views/form/role-select.blade.php @@ -5,7 +5,7 @@ @if($errors->has($name)) class="neg" @endif @if(isset($model) || old($name)) @if(old($name) && old($name) === $option->id) selected @elseif(isset($model) && $model->role->id === $option->id) selected @endif @endif > - {{ $option->$displayKey }} + {{ $option->display_name }} @endforeach diff --git a/resources/views/public.blade.php b/resources/views/public.blade.php index b11971104..eaff2c2d8 100644 --- a/resources/views/public.blade.php +++ b/resources/views/public.blade.php @@ -26,6 +26,23 @@
@endif + +
@yield('content')
diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index 72d62f733..cc6c6a230 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -10,17 +10,61 @@
{!! csrf_field() !!} + +

App Settings

- +
- - + +
+ +
+ +

Registration Settings

+
+
+
+ + + +
+
+ + +
+
+ +

If domain restriction is used then email confirmation will be required and the below value will be ignored.

+ + +
+
+
+
+ +

Enter a comma separated list of email domains you would like to restrict registration to. Users will be sent an email to confirm their address before being allowed to interact with the application. +
Note that users will be able to change their email addresses after successful registration.

+ +
+
+
+ +
+
- +