use BookStack\Auth\Access\LdapService;
use BookStack\Auth\Access\RegistrationService;
use BookStack\Auth\User;
-use BookStack\Auth\UserRepo;
+use BookStack\Exceptions\JsonDebugException;
use BookStack\Exceptions\LdapException;
-use BookStack\Exceptions\LoginAttemptException;
use BookStack\Exceptions\LoginAttemptEmailNeededException;
+use BookStack\Exceptions\LoginAttemptException;
use BookStack\Exceptions\UserRegistrationException;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Session\Session;
-use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class LdapSessionGuard extends ExternalBaseSessionGuard
{
-
- protected $ldapService;
+ protected LdapService $ldapService;
/**
* LdapSessionGuard constructor.
*/
- public function __construct($name,
+ public function __construct(
+ $name,
UserProvider $provider,
Session $session,
LdapService $ldapService,
RegistrationService $registrationService
- )
- {
+ ) {
$this->ldapService = $ldapService;
parent::__construct($name, $provider, $session, $registrationService);
}
* Validate a user's credentials.
*
* @param array $credentials
- * @return bool
+ *
* @throws LdapException
+ *
+ * @return bool
*/
public function validate(array $credentials = [])
{
if (isset($userDetails['uid'])) {
$this->lastAttempted = $this->provider->retrieveByCredentials([
- 'external_auth_id' => $userDetails['uid']
+ 'external_auth_id' => $userDetails['uid'],
]);
}
* Attempt to authenticate a user using the given credentials.
*
* @param array $credentials
- * @param bool $remember
- * @return bool
+ * @param bool $remember
+ *
+ * @throws LdapException*@throws \BookStack\Exceptions\JsonDebugException
* @throws LoginAttemptException
- * @throws LdapException
+ * @throws JsonDebugException
+ *
+ * @return bool
*/
public function attempt(array $credentials = [], $remember = false)
{
$user = null;
if (isset($userDetails['uid'])) {
$this->lastAttempted = $user = $this->provider->retrieveByCredentials([
- 'external_auth_id' => $userDetails['uid']
+ 'external_auth_id' => $userDetails['uid'],
]);
}
try {
$user = $this->createNewFromLdapAndCreds($userDetails, $credentials);
} catch (UserRegistrationException $exception) {
- throw new LoginAttemptException($exception->message);
+ throw new LoginAttemptException($exception->getMessage());
}
}
$this->ldapService->syncGroups($user, $username);
}
+ // Attach avatar if non-existent
+ if (!$user->avatar()->exists()) {
+ $this->ldapService->saveAndAttachAvatar($user, $userDetails);
+ }
+
$this->login($user, $remember);
+
return true;
}
/**
- * Create a new user from the given ldap credentials and login credentials
+ * Create a new user from the given ldap credentials and login credentials.
+ *
* @throws LoginAttemptEmailNeededException
* @throws LoginAttemptException
* @throws UserRegistrationException
}
$details = [
- 'name' => $ldapUserDetails['name'],
- 'email' => $ldapUserDetails['email'] ?: $credentials['email'],
+ 'name' => $ldapUserDetails['name'],
+ 'email' => $ldapUserDetails['email'] ?: $credentials['email'],
'external_auth_id' => $ldapUserDetails['uid'],
- 'password' => Str::random(32),
+ 'password' => Str::random(32),
];
- return $this->registrationService->registerUser($details, null, false);
- }
+ $user = $this->registrationService->registerUser($details, null, false);
+ $this->ldapService->saveAndAttachAvatar($user, $ldapUserDetails);
+ return $user;
+ }
}