Skip to content

Commit 14feef3

Browse files
committed
Updated user interfaces for LDAP and added email from LDAP
1 parent 1c8c9e6 commit 14feef3

File tree

14 files changed

+106
-21
lines changed

14 files changed

+106
-21
lines changed

app/Http/Controllers/Auth/AuthController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,22 @@ public function postRegister(Request $request)
118118
*/
119119
protected function authenticated(Request $request, Authenticatable $user)
120120
{
121+
if(!$user->exists && $user->email === null && !$request->has('email')) {
122+
$request->flash();
123+
session()->flash('request-email', true);
124+
return redirect('/login');
125+
}
126+
127+
if(!$user->exists && $user->email === null && $request->has('email')) {
128+
$user->email = $request->get('email');
129+
}
130+
121131
if(!$user->exists) {
122132
$user->save();
123133
$this->userRepo->attachDefaultRole($user);
124134
auth()->login($user);
125135
}
136+
126137
return redirect()->intended($this->redirectPath());
127138
}
128139

@@ -183,7 +194,7 @@ protected function registerUser(array $userData, $socialAccount = false)
183194
}
184195

185196
/**
186-
* Show the page to tell the user to check thier email
197+
* Show the page to tell the user to check their email
187198
* and confirm their address.
188199
*/
189200
public function getRegisterConfirmation()
@@ -243,7 +254,7 @@ public function resendConfirmation(Request $request)
243254
]);
244255
$user = $this->userRepo->getByEmail($request->get('email'));
245256
$this->emailConfirmationService->sendConfirmation($user);
246-
\Session::flash('success', 'Confirmation email resent, Please check your inbox.');
257+
session()->flash('success', 'Confirmation email resent, Please check your inbox.');
247258
return redirect('/register/confirm');
248259
}
249260

app/Http/Controllers/UserController.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function index()
4646
public function create()
4747
{
4848
$this->checkPermission('user-create');
49-
return view('users/create');
49+
$authMethod = config('auth.method');
50+
return view('users/create', ['authMethod' => $authMethod]);
5051
}
5152

5253
/**
@@ -94,10 +95,12 @@ public function edit($id, SocialAuthService $socialAuthService)
9495
return $this->currentUser->id == $id;
9596
});
9697

98+
$authMethod = config('auth.method');
99+
97100
$user = $this->user->findOrFail($id);
98101
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
99102
$this->setPageTitle('User Profile');
100-
return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers]);
103+
return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod]);
101104
}
102105

103106
/**
@@ -124,17 +127,24 @@ public function update(Request $request, $id)
124127
]);
125128

126129
$user = $this->user->findOrFail($id);
127-
$user->fill($request->except('password'));
130+
$user->fill($request->all());
128131

132+
// Role updates
129133
if ($this->currentUser->can('user-update') && $request->has('role')) {
130134
$user->attachRoleId($request->get('role'));
131135
}
132136

137+
// Password updates
133138
if ($request->has('password') && $request->get('password') != '') {
134139
$password = $request->get('password');
135140
$user->password = bcrypt($password);
136141
}
137142

143+
// External auth id updates
144+
if ($this->currentUser->can('user-update') && $request->has('external_auth_id')) {
145+
$user->external_auth_id = $request->get('external_auth_id');
146+
}
147+
138148
$user->save();
139149
return redirect('/users');
140150
}

app/Providers/LdapUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function retrieveByToken($identifier, $token)
8787
public function updateRememberToken(Authenticatable $user, $token)
8888
{
8989
$user->setRememberToken($token);
90-
9190
$user->save();
9291
}
9392

@@ -113,6 +112,7 @@ public function retrieveByCredentials(array $credentials)
113112

114113
$model->name = $userDetails['name'];
115114
$model->external_auth_id = $userDetails['uid'];
115+
$model->email = $userDetails['email'];
116116
return $model;
117117
}
118118

app/Repos/UserRepo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function isOnlyAdmin(User $user)
8888
*/
8989
public function create(array $data)
9090
{
91-
return $this->user->create([
91+
return $this->user->forceCreate([
9292
'name' => $data['name'],
9393
'email' => $data['email'],
9494
'password' => bcrypt($data['password'])

app/Services/LdapService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ public function getUserDetails($userName)
2323
// Find user
2424
$userFilter = $this->buildFilter(config('services.ldap.user_filter'), ['user' => $userName]);
2525
$baseDn = config('services.ldap.base_dn');
26-
$ldapSearch = ldap_search($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn']);
26+
$ldapSearch = ldap_search($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', 'mail']);
2727
$users = ldap_get_entries($ldapConnection, $ldapSearch);
2828
if ($users['count'] === 0) return null;
2929

3030
$user = $users[0];
3131
return [
3232
'uid' => $user['uid'][0],
3333
'name' => $user['cn'][0],
34-
'dn' => $user['dn']
34+
'dn' => $user['dn'],
35+
'email' => (isset($user['mail'])) ? $user['mail'][0] : null
3536
];
3637
}
3738

app/Services/SettingService.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(Setting $setting, Cache $cache)
3838
*/
3939
public function get($key, $default = false)
4040
{
41-
$value = $this->getValueFromStore($key, $default);
41+
$value = $this->getValueFromStore($key, $default);
4242
return $this->formatValue($value, $default);
4343
}
4444

@@ -50,13 +50,17 @@ public function get($key, $default = false)
5050
*/
5151
protected function getValueFromStore($key, $default)
5252
{
53+
$overrideValue = $this->getOverrideValue($key);
54+
if ($overrideValue !== null) return $overrideValue;
55+
5356
$cacheKey = $this->cachePrefix . $key;
5457
if ($this->cache->has($cacheKey)) {
5558
return $this->cache->get($cacheKey);
5659
}
5760

5861
$settingObject = $this->getSettingObjectByKey($key);
59-
if($settingObject !== null) {
62+
63+
if ($settingObject !== null) {
6064
$value = $settingObject->value;
6165
$this->cache->forever($cacheKey, $value);
6266
return $value;
@@ -65,6 +69,10 @@ protected function getValueFromStore($key, $default)
6569
return $default;
6670
}
6771

72+
/**
73+
* Clear an item from the cache completely.
74+
* @param $key
75+
*/
6876
protected function clearFromCache($key)
6977
{
7078
$cacheKey = $this->cachePrefix . $key;
@@ -136,9 +144,23 @@ public function remove($key)
136144
* @param $key
137145
* @return mixed
138146
*/
139-
private function getSettingObjectByKey($key)
147+
protected function getSettingObjectByKey($key)
140148
{
141149
return $this->setting->where('setting_key', '=', $key)->first();
142150
}
143151

152+
153+
/**
154+
* Returns an override value for a setting based on certain app conditions.
155+
* Used where certain configuration options overrule others.
156+
* Returns null if no override value is available.
157+
* @param $key
158+
* @return bool|null
159+
*/
160+
protected function getOverrideValue($key)
161+
{
162+
if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;
163+
return null;
164+
}
165+
144166
}

app/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
2424
*
2525
* @var array
2626
*/
27-
protected $fillable = ['name', 'email', 'password', 'image_id'];
27+
protected $fillable = ['name', 'email', 'image_id'];
2828

2929
/**
3030
* The attributes excluded from the model's JSON form.
@@ -68,7 +68,7 @@ public function getRoleAttribute()
6868
}
6969

7070
/**
71-
* Loads the user's permissions from thier role.
71+
* Loads the user's permissions from their role.
7272
*/
7373
private function loadPermissions()
7474
{

config/auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
'providers' => [
7171
'users' => [
72-
'driver' => env('AUTH_METHOD', 'eloquent'),
72+
'driver' => env('AUTH_METHOD', 'standard') === 'standard' ? 'eloquent' : env('AUTH_METHOD'),
7373
'model' => BookStack\User::class,
7474
],
7575

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<env name="QUEUE_DRIVER" value="sync"/>
2727
<env name="DB_CONNECTION" value="mysql_testing"/>
2828
<env name="MAIL_PRETEND" value="true"/>
29+
<env name="AUTH_METHOD" value="standard"/>
2930
<env name="DISABLE_EXTERNAL_SERVICES" value="false"/>
3031
</php>
3132
</phpunit>

resources/views/auth/forms/login/ldap.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
@include('form/text', ['name' => 'username', 'tabindex' => 1])
44
</div>
55

6+
@if(session('request-email', false) === true)
7+
<div class="form-group">
8+
<label for="email">Email</label>
9+
@include('form/text', ['name' => 'email', 'tabindex' => 1])
10+
<span class="text-neg">
11+
Please enter an email to use for this account.
12+
</span>
13+
</div>
14+
@endif
15+
616
<div class="form-group">
717
<label for="password">Password</label>
818
@include('form/password', ['name' => 'password', 'tabindex' => 2])

0 commit comments

Comments
 (0)