Dan Brown

Started social registration

...@@ -37,7 +37,7 @@ class AuthController extends Controller ...@@ -37,7 +37,7 @@ class AuthController extends Controller
37 */ 37 */
38 public function __construct(SocialAuthService $socialAuthService) 38 public function __construct(SocialAuthService $socialAuthService)
39 { 39 {
40 - $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]); 40 + $this->middleware('guest', ['only' => ['getLogin', 'postLogin', 'getRegister']]);
41 $this->socialAuthService = $socialAuthService; 41 $this->socialAuthService = $socialAuthService;
42 } 42 }
43 43
...@@ -72,6 +72,17 @@ class AuthController extends Controller ...@@ -72,6 +72,17 @@ class AuthController extends Controller
72 } 72 }
73 73
74 /** 74 /**
75 + * Show the application registration form.
76 + *
77 + * @return \Illuminate\Http\Response
78 + */
79 + public function getRegister()
80 + {
81 + $socialDrivers = $this->socialAuthService->getActiveDrivers();
82 + return view('auth.register', ['socialDrivers' => $socialDrivers]);
83 + }
84 +
85 + /**
75 * Show the application login form. 86 * Show the application login form.
76 * 87 *
77 * @return \Illuminate\Http\Response 88 * @return \Illuminate\Http\Response
...@@ -84,7 +95,6 @@ class AuthController extends Controller ...@@ -84,7 +95,6 @@ class AuthController extends Controller
84 } 95 }
85 96
86 $socialDrivers = $this->socialAuthService->getActiveDrivers(); 97 $socialDrivers = $this->socialAuthService->getActiveDrivers();
87 -
88 return view('auth.login', ['socialDrivers' => $socialDrivers]); 98 return view('auth.login', ['socialDrivers' => $socialDrivers]);
89 } 99 }
90 100
......
...@@ -31,12 +31,12 @@ abstract class Controller extends BaseController ...@@ -31,12 +31,12 @@ abstract class Controller extends BaseController
31 { 31 {
32 // Get a user instance for the current user 32 // Get a user instance for the current user
33 $user = auth()->user(); 33 $user = auth()->user();
34 - if (!$user) { 34 + if (!$user) $user = User::getDefault();
35 - $user = User::getDefault(); 35 +
36 - }
37 // Share variables with views 36 // Share variables with views
38 view()->share('signedIn', auth()->check()); 37 view()->share('signedIn', auth()->check());
39 view()->share('currentUser', $user); 38 view()->share('currentUser', $user);
39 +
40 // Share variables with controllers 40 // Share variables with controllers
41 $this->currentUser = $user; 41 $this->currentUser = $user;
42 $this->signedIn = auth()->check(); 42 $this->signedIn = auth()->check();
...@@ -53,7 +53,7 @@ abstract class Controller extends BaseController ...@@ -53,7 +53,7 @@ abstract class Controller extends BaseController
53 if (!$this->currentUser || !$this->currentUser->can($permissionName)) { 53 if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
54 Session::flash('error', trans('errors.permission')); 54 Session::flash('error', trans('errors.permission'));
55 throw new HttpResponseException( 55 throw new HttpResponseException(
56 - redirect()->back() 56 + redirect('/')
57 ); 57 );
58 } 58 }
59 59
......
...@@ -152,6 +152,8 @@ class UserController extends Controller ...@@ -152,6 +152,8 @@ class UserController extends Controller
152 return $this->currentUser->id == $id; 152 return $this->currentUser->id == $id;
153 }); 153 });
154 $user = $this->user->findOrFail($id); 154 $user = $this->user->findOrFail($id);
155 + // Delete social accounts
156 + $user->socialAccounts()->delete();
155 $user->delete(); 157 $user->delete();
156 return redirect('/users'); 158 return redirect('/users');
157 } 159 }
......
...@@ -34,8 +34,7 @@ class Authenticate ...@@ -34,8 +34,7 @@ class Authenticate
34 */ 34 */
35 public function handle($request, Closure $next) 35 public function handle($request, Closure $next)
36 { 36 {
37 - $sitePublic = Setting::get('app-public', false) === 'true'; 37 + if ($this->auth->guest() && !Setting::get('app-public')) {
38 - if ($this->auth->guest() && !$sitePublic) {
39 if ($request->ajax()) { 38 if ($request->ajax()) {
40 return response('Unauthorized.', 401); 39 return response('Unauthorized.', 401);
41 } else { 40 } else {
......
...@@ -87,6 +87,7 @@ Route::get('/login/service/{socialDriver}/detach', 'Auth\AuthController@detachSo ...@@ -87,6 +87,7 @@ Route::get('/login/service/{socialDriver}/detach', 'Auth\AuthController@detachSo
87 Route::get('/login', 'Auth\AuthController@getLogin'); 87 Route::get('/login', 'Auth\AuthController@getLogin');
88 Route::post('/login', 'Auth\AuthController@postLogin'); 88 Route::post('/login', 'Auth\AuthController@postLogin');
89 Route::get('/logout', 'Auth\AuthController@getLogout'); 89 Route::get('/logout', 'Auth\AuthController@getLogout');
90 +Route::get('/register', 'Auth\AuthController@getRegister');
90 91
91 // Password reset link request routes... 92 // Password reset link request routes...
92 Route::get('/password/email', 'Auth\PasswordController@getEmail'); 93 Route::get('/password/email', 'Auth\PasswordController@getEmail');
......
...@@ -7,6 +7,12 @@ use Illuminate\Database\Eloquent\Model; ...@@ -7,6 +7,12 @@ use Illuminate\Database\Eloquent\Model;
7 class Role extends Model 7 class Role extends Model
8 { 8 {
9 /** 9 /**
10 + * Sets the default role name for newly registed users.
11 + * @var string
12 + */
13 + protected static $default = 'viewer';
14 +
15 + /**
10 * The roles that belong to the role. 16 * The roles that belong to the role.
11 */ 17 */
12 public function users() 18 public function users()
...@@ -31,4 +37,12 @@ class Role extends Model ...@@ -31,4 +37,12 @@ class Role extends Model
31 $this->permissions()->attach($permission->id); 37 $this->permissions()->attach($permission->id);
32 } 38 }
33 39
40 + /**
41 + * Get an instance of the default role.
42 + * @return Role
43 + */
44 + public static function getDefault()
45 + {
46 + return static::where('name', '=', static::$default)->first();
47 + }
34 } 48 }
......
...@@ -33,7 +33,16 @@ class SettingService ...@@ -33,7 +33,16 @@ class SettingService
33 public function get($key, $default = false) 33 public function get($key, $default = false)
34 { 34 {
35 $setting = $this->getSettingObjectByKey($key); 35 $setting = $this->getSettingObjectByKey($key);
36 - return $setting === null ? $default : $setting->value; 36 + $value = $setting === null ? null : $setting->value;
37 +
38 + // Change string booleans to actual booleans
39 + if($value === 'true') $value = true;
40 + if($value === 'false') $value = false;
41 +
42 + // Set to default if empty
43 + if($value === '') $value = $default;
44 +
45 + return $value === null ? $default : $value;
37 } 46 }
38 47
39 /** 48 /**
......
...@@ -63,8 +63,8 @@ class SocialAuthService ...@@ -63,8 +63,8 @@ class SocialAuthService
63 $isLoggedIn = auth()->check(); 63 $isLoggedIn = auth()->check();
64 $currentUser = auth()->user(); 64 $currentUser = auth()->user();
65 65
66 - // When a user is not logged in but a matching SocialAccount exists, 66 + // When a user is not logged in and a matching SocialAccount exists,
67 - // Log the user found on the SocialAccount into the application. 67 + // Simply log the user into the application.
68 if (!$isLoggedIn && $socialAccount !== null) { 68 if (!$isLoggedIn && $socialAccount !== null) {
69 return $this->logUserIn($socialAccount->user); 69 return $this->logUserIn($socialAccount->user);
70 } 70 }
...@@ -87,30 +87,16 @@ class SocialAuthService ...@@ -87,30 +87,16 @@ class SocialAuthService
87 // When a user is logged in, A social account exists but the users do not match. 87 // When a user is logged in, A social account exists but the users do not match.
88 // Change the user that the social account is assigned to. 88 // Change the user that the social account is assigned to.
89 if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id != $currentUser->id) { 89 if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id != $currentUser->id) {
90 - $socialAccount->user_id = $currentUser->id; 90 + \Session::flash('success', 'This ' . title_case($socialDriver) . ' account is already used buy another user.');
91 - $socialAccount->save(); 91 + return redirect($currentUser->getEditUrl());
92 - \Session::flash('success', 'This ' . title_case($socialDriver) . ' account is now attached to your profile.');
93 } 92 }
94 93
95 - if ($user === null) { 94 + // Otherwise let the user know this social account is not used by anyone.
96 - throw new SocialSignInException('A system user with the email ' . $socialUser->getEmail() . 95 + $message = 'This ' . $socialDriver . ' account is not linked to any users. Please attach it in your profile settings';
97 - ' was not found and this ' . $socialDriver . ' account is not linked to any users.', '/login'); 96 + if(\Setting::get('registration-enabled')) {
98 - } 97 + $message .= 'or, If you do not yet have an account, You can register an account using the ' . $socialDriver . ' option';
99 - return $this->authenticateUserWithNewSocialAccount($user, $socialUser, $socialUser);
100 } 98 }
101 - 99 + throw new SocialSignInException($message . '.', '/login');
102 - /**
103 - * Logs a user in and creates a new social account entry for future usage.
104 - * @param User $user
105 - * @param string $socialDriver
106 - * @param \Laravel\Socialite\Contracts\User $socialUser
107 - * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
108 - */
109 - private function authenticateUserWithNewSocialAccount($user, $socialDriver, $socialUser)
110 - {
111 - $this->fillSocialAccount($socialDriver, $socialUser);
112 - $user->socialAccounts()->save($this->socialAccount);
113 - return $this->logUserIn($user);
114 } 100 }
115 101
116 private function logUserIn($user) 102 private function logUserIn($user)
......
...@@ -29,6 +29,7 @@ label { ...@@ -29,6 +29,7 @@ label {
29 font-weight: 500; 29 font-weight: 500;
30 color: #666; 30 color: #666;
31 padding-bottom: 2px; 31 padding-bottom: 2px;
32 + margin-bottom: 0.2em;
32 } 33 }
33 34
34 label.radio, label.checkbox { 35 label.radio, label.checkbox {
...@@ -38,6 +39,10 @@ label.radio, label.checkbox { ...@@ -38,6 +39,10 @@ label.radio, label.checkbox {
38 } 39 }
39 } 40 }
40 41
42 +label + p.small {
43 + margin-bottom: 0.8em;
44 +}
45 +
41 input[type="text"], input[type="number"], input[type="email"], input[type="search"], input[type="url"], input[type="password"], select, textarea { 46 input[type="text"], input[type="number"], input[type="email"], input[type="search"], input[type="url"], input[type="password"], select, textarea {
42 @extend .input-base; 47 @extend .input-base;
43 } 48 }
......
...@@ -42,9 +42,12 @@ div[class^="col-"] img { ...@@ -42,9 +42,12 @@ div[class^="col-"] img {
42 } 42 }
43 43
44 .center-box { 44 .center-box {
45 - margin: 15vh auto 0 auto; 45 + margin: $-xl auto 0 auto;
46 padding: $-m $-xxl $-xl*2 $-xxl; 46 padding: $-m $-xxl $-xl*2 $-xxl;
47 max-width: 346px; 47 max-width: 346px;
48 + display: inline-block;
49 + text-align: left;
50 + vertical-align: top;
48 &.login { 51 &.login {
49 background-color: #EEE; 52 background-color: #EEE;
50 box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.1); 53 box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.1);
......
1 @extends('public') 1 @extends('public')
2 2
3 +@section('header-buttons')
4 + @if(Setting::get('registration-enabled'))
5 + <a href="/register"><i class="zmdi zmdi-account-add"></i>Sign up</a>
6 + @endif
7 +@stop
8 +
3 @section('content') 9 @section('content')
4 10
11 + <div class="text-center">
5 <div class="center-box"> 12 <div class="center-box">
6 <h1>Log In</h1> 13 <h1>Log In</h1>
7 14
...@@ -23,6 +30,7 @@ ...@@ -23,6 +30,7 @@
23 <button class="button block pos">Sign In</button> 30 <button class="button block pos">Sign In</button>
24 </div> 31 </div>
25 </form> 32 </form>
33 +
26 @if(count($socialDrivers) > 0) 34 @if(count($socialDrivers) > 0)
27 <hr class="margin-top"> 35 <hr class="margin-top">
28 <h3 class="text-muted">Social Login</h3> 36 <h3 class="text-muted">Social Login</h3>
...@@ -34,5 +42,6 @@ ...@@ -34,5 +42,6 @@
34 @endif 42 @endif
35 @endif 43 @endif
36 </div> 44 </div>
45 + </div>
37 46
38 @stop 47 @stop
...\ No newline at end of file ...\ No newline at end of file
......
1 +@extends('public')
2 +
3 +@section('header-buttons')
4 + <a href="/login"><i class="zmdi zmdi-sign-in"></i>Sign in</a>
5 +@stop
6 +
7 +@section('content')
8 +
9 + <div class="text-center">
10 + <div class="center-box">
11 + <h1>Register</h1>
12 +
13 + <form action="/login" method="POST">
14 + {!! csrf_field() !!}
15 +
16 + <div class="form-group">
17 + <label for="email">Name</label>
18 + @include('form/text', ['name' => 'name'])
19 + </div>
20 +
21 + <div class="form-group">
22 + <label for="email">Email</label>
23 + @include('form/text', ['name' => 'email'])
24 + </div>
25 +
26 + <div class="form-group">
27 + <label for="password">Password</label>
28 + @include('form/password', ['name' => 'password'])
29 + </div>
30 +
31 + <div class="from-group">
32 + <button class="button block pos">Sign In</button>
33 + </div>
34 + </form>
35 +
36 + @if(count($socialDrivers) > 0)
37 + <hr class="margin-top">
38 + <h3 class="text-muted">Social Registration</h3>
39 + @if(isset($socialDrivers['google']))
40 + <a href="/register/service/google" style="color: #DC4E41;"><i class="zmdi zmdi-google-plus-box zmdi-hc-4x"></i></a>
41 + @endif
42 + @if(isset($socialDrivers['github']))
43 + <a href="/register/service/github" style="color:#444;"><i class="zmdi zmdi-github zmdi-hc-4x"></i></a>
44 + @endif
45 + @endif
46 + </div>
47 + </div>
48 +
49 +
50 +@stop
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
5 @if($errors->has($name)) class="neg" @endif 5 @if($errors->has($name)) class="neg" @endif
6 @if(isset($model) || old($name)) @if(old($name) && old($name) === $option->id) selected @elseif(isset($model) && $model->role->id === $option->id) selected @endif @endif 6 @if(isset($model) || old($name)) @if(old($name) && old($name) === $option->id) selected @elseif(isset($model) && $model->role->id === $option->id) selected @endif @endif
7 > 7 >
8 - {{ $option->$displayKey }} 8 + {{ $option->display_name }}
9 </option> 9 </option>
10 @endforeach 10 @endforeach
11 </select> 11 </select>
......
...@@ -26,6 +26,23 @@ ...@@ -26,6 +26,23 @@
26 </div> 26 </div>
27 @endif 27 @endif
28 28
29 +<header id="header">
30 + <div class="container">
31 + <div class="row">
32 + <div class="col-md-6">
33 + <a href="/" class="logo">{{ Setting::get('app-name', 'BookStack') }}</a>
34 + </div>
35 + <div class="col-md-6">
36 + <div class="float right">
37 + <div class="links text-center">
38 + @yield('header-buttons')
39 + </div>
40 + </div>
41 + </div>
42 + </div>
43 + </div>
44 +</header>
45 +
29 <section class="container"> 46 <section class="container">
30 @yield('content') 47 @yield('content')
31 </section> 48 </section>
......
...@@ -10,17 +10,61 @@ ...@@ -10,17 +10,61 @@
10 10
11 <form action="/settings" method="POST"> 11 <form action="/settings" method="POST">
12 {!! csrf_field() !!} 12 {!! csrf_field() !!}
13 +
14 + <h3>App Settings</h3>
13 <div class="form-group"> 15 <div class="form-group">
14 - <label for="setting-app-name">Application Name</label> 16 + <label for="setting-app-name">Application name</label>
15 <input type="text" value="{{ Setting::get('app-name', 'BookStack') }}" name="setting-app-name" id="setting-app-name"> 17 <input type="text" value="{{ Setting::get('app-name', 'BookStack') }}" name="setting-app-name" id="setting-app-name">
16 </div> 18 </div>
17 <div class="form-group"> 19 <div class="form-group">
18 <label for="setting-app-public">Allow public viewing?</label> 20 <label for="setting-app-public">Allow public viewing?</label>
19 - <label><input type="radio" name="setting-app-public" @if(Setting::get('app-public') == 'true') checked @endif value="true"> Yes</label> 21 + <label><input type="radio" name="setting-app-public" @if(Setting::get('app-public')) checked @endif value="true"> Yes</label>
20 - <label><input type="radio" name="setting-app-public" @if(Setting::get('app-public') == 'false') checked @endif value="false"> No</label> 22 + <label><input type="radio" name="setting-app-public" @if(!Setting::get('app-public')) checked @endif value="false"> No</label>
23 + </div>
24 +
25 + <hr class="margin-top">
26 +
27 + <h3>Registration Settings</h3>
28 + <div class="row">
29 + <div class="col-md-6">
30 + <div class="form-group">
31 + <label for="setting-registration-enabled">Allow registration?</label>
32 + <label><input type="radio" name="setting-registration-enabled" @if(Setting::get('registration-enabled')) checked @endif value="true"> Yes</label>
33 + <label><input type="radio" name="setting-registration-enabled" @if(!Setting::get('registration-enabled')) checked @endif value="false"> No</label>
34 + </div>
35 + <div class="form-group">
36 + <label for="setting-registration-role">Default user role after registration</label>
37 + <select id="setting-registration-role" name="setting-registration-role" @if($errors->has('setting-registration-role')) class="neg" @endif>
38 + @foreach(\Oxbow\Role::all() as $role)
39 + <option value="{{$role->id}}"
40 + @if(\Setting::get('registration-role', \Oxbow\Role::getDefault()->id) == $role->id) selected @endif
41 + >
42 + {{ $role->display_name }}
43 + </option>
44 + @endforeach
45 + </select>
46 + </div>
47 + <div class="form-group">
48 + <label for="setting-registration-confirmation">Require Email Confirmation?</label>
49 + <p class="small">If domain restriction is used then email confirmation will be required and the below value will be ignored.</p>
50 + <label><input type="radio" name="setting-registration-confirmation" @if(Setting::get('registration-confirmation')) checked @endif value="true"> Yes</label>
51 + <label><input type="radio" name="setting-registration-confirmation" @if(!Setting::get('registration-confirmation')) checked @endif value="false"> No</label>
52 + </div>
53 + </div>
54 + <div class="col-md-6">
55 + <div class="form-group">
56 + <label for="setting-registration-restrict">Restrict registration to domain</label>
57 + <p class="small">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.
58 + <br> Note that users will be able to change their email addresses after successful registration.</p>
59 + <input type="text" id="setting-registration-restrict" name="setting-registration-restrict" placeholder="No restriction set" value="{{ Setting::get('registration-restrict', '') }}">
21 </div> 60 </div>
61 + </div>
62 + </div>
63 +
64 + <hr class="margin-top">
65 +
22 <div class="form-group"> 66 <div class="form-group">
23 - <button type="submit" class="button pos">Update Settings</button> 67 + <button type="submit" class="button pos">Save Settings</button>
24 </div> 68 </div>
25 </form> 69 </form>
26 70
......