2016-01-10 03:23:35 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Providers;
|
|
|
|
|
|
|
|
|
2016-01-12 06:41:05 +08:00
|
|
|
use BookStack\Role;
|
|
|
|
use BookStack\Services\LdapService;
|
2016-01-10 03:23:35 +08:00
|
|
|
use BookStack\User;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
|
|
use Illuminate\Contracts\Auth\UserProvider;
|
|
|
|
|
|
|
|
class LdapUserProvider implements UserProvider
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
2016-01-12 06:41:05 +08:00
|
|
|
/**
|
|
|
|
* @var LdapService
|
|
|
|
*/
|
|
|
|
protected $ldapService;
|
|
|
|
|
2016-01-10 03:23:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* LdapUserProvider constructor.
|
2016-01-12 06:41:05 +08:00
|
|
|
* @param $model
|
|
|
|
* @param LdapService $ldapService
|
2016-01-10 03:23:35 +08:00
|
|
|
*/
|
2016-01-12 06:41:05 +08:00
|
|
|
public function __construct($model, LdapService $ldapService)
|
2016-01-10 03:23:35 +08:00
|
|
|
{
|
|
|
|
$this->model = $model;
|
2016-01-12 06:41:05 +08:00
|
|
|
$this->ldapService = $ldapService;
|
2016-01-10 03:23:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of the model.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function createModel()
|
|
|
|
{
|
2016-01-12 06:41:05 +08:00
|
|
|
$class = '\\' . ltrim($this->model, '\\');
|
2016-01-10 03:23:35 +08:00
|
|
|
return new $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a user by their unique identifier.
|
|
|
|
*
|
|
|
|
* @param mixed $identifier
|
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function retrieveById($identifier)
|
|
|
|
{
|
|
|
|
return $this->createModel()->newQuery()->find($identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a user by their unique identifier and "remember me" token.
|
|
|
|
*
|
|
|
|
* @param mixed $identifier
|
2016-01-12 06:41:05 +08:00
|
|
|
* @param string $token
|
2016-01-10 03:23:35 +08:00
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function retrieveByToken($identifier, $token)
|
|
|
|
{
|
|
|
|
$model = $this->createModel();
|
|
|
|
|
|
|
|
return $model->newQuery()
|
|
|
|
->where($model->getAuthIdentifierName(), $identifier)
|
|
|
|
->where($model->getRememberTokenName(), $token)
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the "remember me" token for the given user in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param string $token
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateRememberToken(Authenticatable $user, $token)
|
|
|
|
{
|
2016-01-16 07:21:47 +08:00
|
|
|
if ($user->exists) {
|
|
|
|
$user->setRememberToken($token);
|
|
|
|
$user->save();
|
|
|
|
}
|
2016-01-10 03:23:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a user by the given credentials.
|
|
|
|
*
|
|
|
|
* @param array $credentials
|
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function retrieveByCredentials(array $credentials)
|
|
|
|
{
|
|
|
|
// Get user via LDAP
|
2016-01-12 06:41:05 +08:00
|
|
|
$userDetails = $this->ldapService->getUserDetails($credentials['username']);
|
|
|
|
if ($userDetails === null) return null;
|
2016-01-10 03:23:35 +08:00
|
|
|
|
|
|
|
// Search current user base by looking up a uid
|
2016-01-12 06:41:05 +08:00
|
|
|
$model = $this->createModel();
|
|
|
|
$currentUser = $model->newQuery()
|
|
|
|
->where('external_auth_id', $userDetails['uid'])
|
|
|
|
->first();
|
2016-01-10 03:23:35 +08:00
|
|
|
|
2016-01-12 06:41:05 +08:00
|
|
|
if ($currentUser !== null) return $currentUser;
|
2016-01-10 03:23:35 +08:00
|
|
|
|
2016-01-12 06:41:05 +08:00
|
|
|
$model->name = $userDetails['name'];
|
|
|
|
$model->external_auth_id = $userDetails['uid'];
|
2016-01-14 06:22:30 +08:00
|
|
|
$model->email = $userDetails['email'];
|
2016-04-03 19:16:54 +08:00
|
|
|
$model->email_confirmed = false;
|
2016-01-12 06:41:05 +08:00
|
|
|
return $model;
|
2016-01-10 03:23:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a user against the given credentials.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param array $credentials
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateCredentials(Authenticatable $user, array $credentials)
|
|
|
|
{
|
2016-01-12 06:41:05 +08:00
|
|
|
return $this->ldapService->validateUserCredentials($user, $credentials['username'], $credentials['password']);
|
2016-01-10 03:23:35 +08:00
|
|
|
}
|
|
|
|
}
|