2018-10-08 11:38:45 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-08 11:38:45 +02:00
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2018-10-21 00:26:21 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2019-04-19 04:58:40 +02:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
|
2018-10-08 11:38:45 +02:00
|
|
|
use Illuminate\Contracts\Auth\UserProvider;
|
|
|
|
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
|
2019-04-19 04:58:40 +02:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Str;
|
2018-10-08 11:38:45 +02:00
|
|
|
|
|
|
|
class MultiDatabaseUserProvider implements UserProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The hasher implementation.
|
|
|
|
*
|
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
|
|
|
*/
|
|
|
|
protected $hasher;
|
|
|
|
|
|
|
|
/**
|
2018-10-15 11:06:57 +02:00
|
|
|
* The Eloquent user model.
|
2018-10-08 11:38:45 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-10-15 11:06:57 +02:00
|
|
|
protected $model;
|
2018-10-08 11:38:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database user provider.
|
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
|
|
|
* @param string $model
|
2018-10-08 11:38:45 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-10-15 11:06:57 +02:00
|
|
|
public function __construct(HasherContract $hasher, $model)
|
2018-10-08 11:38:45 +02:00
|
|
|
{
|
2018-10-15 11:06:57 +02:00
|
|
|
$this->model = $model;
|
2018-10-08 11:38:45 +02:00
|
|
|
$this->hasher = $hasher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a user by their unique identifier.
|
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param mixed $identifier
|
2018-10-08 11:38:45 +02:00
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function retrieveById($identifier)
|
|
|
|
{
|
|
|
|
$this->setDefaultDatabase($identifier);
|
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
$model = $this->createModel();
|
2018-10-08 11:38:45 +02:00
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
return $model->newQuery()
|
|
|
|
->where($model->getAuthIdentifierName(), $identifier)
|
|
|
|
->first();
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a user by their unique identifier and "remember me" token.
|
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param mixed $identifier
|
|
|
|
* @param string $token
|
2018-10-08 11:38:45 +02:00
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function retrieveByToken($identifier, $token)
|
|
|
|
{
|
2018-10-15 11:06:57 +02:00
|
|
|
$this->setDefaultDatabase($identifier, $token);
|
|
|
|
|
|
|
|
$model = $this->createModel();
|
|
|
|
|
|
|
|
$model = $model->where($model->getAuthIdentifierName(), $identifier)->first();
|
|
|
|
|
|
|
|
if (! $model) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-08 11:38:45 +02:00
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
$rememberToken = $model->getRememberToken();
|
2018-10-08 11:38:45 +02:00
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
return $rememberToken && hash_equals($rememberToken, $token) ? $model : null;
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the "remember me" token for the given user in storage.
|
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable|\Illuminate\Database\Eloquent\Model $user
|
|
|
|
* @param string $token
|
2018-10-08 11:38:45 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateRememberToken(UserContract $user, $token)
|
|
|
|
{
|
2018-10-15 11:06:57 +02:00
|
|
|
$user->setRememberToken($token);
|
|
|
|
|
|
|
|
$timestamps = $user->timestamps;
|
|
|
|
|
|
|
|
$user->timestamps = false;
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$user->timestamps = $timestamps;
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a user by the given credentials.
|
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param array $credentials
|
2018-10-08 11:38:45 +02:00
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function retrieveByCredentials(array $credentials)
|
|
|
|
{
|
2018-10-15 11:06:57 +02:00
|
|
|
if (empty($credentials) ||
|
|
|
|
(count($credentials) === 1 &&
|
|
|
|
array_key_exists('password', $credentials))) {
|
|
|
|
return;
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
$this->setDefaultDatabase(false, $credentials['email'], false);
|
|
|
|
|
|
|
|
// First we will add each credential element to the query as a where clause.
|
|
|
|
// Then we can execute the query and, if we found a user, return it in a
|
|
|
|
// Eloquent User "model" that will be utilized by the Guard instances.
|
|
|
|
$query = $this->createModel()->newQuery();
|
2018-10-08 11:38:45 +02:00
|
|
|
|
|
|
|
foreach ($credentials as $key => $value) {
|
2018-10-15 11:06:57 +02:00
|
|
|
if (Str::contains($key, 'password')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($value) || $value instanceof Arrayable) {
|
|
|
|
$query->whereIn($key, $value);
|
|
|
|
} else {
|
2018-10-08 11:38:45 +02:00
|
|
|
$query->where($key, $value);
|
|
|
|
}
|
|
|
|
}
|
2019-07-10 05:10:18 +02:00
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
return $query->first();
|
|
|
|
}
|
2018-10-08 11:38:45 +02:00
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
/**
|
|
|
|
* Validate a user against the given credentials.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param array $credentials
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateCredentials(UserContract $user, array $credentials)
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2019-04-19 04:58:40 +02:00
|
|
|
$plain = $credentials['password'];
|
2019-07-10 05:10:18 +02:00
|
|
|
|
2018-10-15 11:06:57 +02:00
|
|
|
return $this->hasher->check($plain, $user->getAuthPassword());
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 11:06:57 +02:00
|
|
|
* Create a new instance of the model.
|
2018-10-08 11:38:45 +02:00
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function createModel()
|
|
|
|
{
|
|
|
|
$class = '\\'.ltrim($this->model, '\\');
|
|
|
|
|
|
|
|
return new $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the hasher implementation.
|
2018-10-08 11:38:45 +02:00
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @return \Illuminate\Contracts\Hashing\Hasher
|
2018-10-08 11:38:45 +02:00
|
|
|
*/
|
2018-10-15 11:06:57 +02:00
|
|
|
public function getHasher()
|
2018-10-08 11:38:45 +02:00
|
|
|
{
|
2018-10-15 11:06:57 +02:00
|
|
|
return $this->hasher;
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 11:06:57 +02:00
|
|
|
* Sets the hasher implementation.
|
2018-10-08 11:38:45 +02:00
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setHasher(HasherContract $hasher)
|
|
|
|
{
|
|
|
|
$this->hasher = $hasher;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the name of the Eloquent user model.
|
2018-10-08 11:38:45 +02:00
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @return string
|
2018-10-08 11:38:45 +02:00
|
|
|
*/
|
2018-10-15 11:06:57 +02:00
|
|
|
public function getModel()
|
2018-10-08 11:38:45 +02:00
|
|
|
{
|
2018-10-15 11:06:57 +02:00
|
|
|
return $this->model;
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 11:06:57 +02:00
|
|
|
* Sets the name of the Eloquent user model.
|
2018-10-08 11:38:45 +02:00
|
|
|
*
|
2018-10-15 11:06:57 +02:00
|
|
|
* @param string $model
|
|
|
|
* @return $this
|
2018-10-08 11:38:45 +02:00
|
|
|
*/
|
2018-10-15 11:06:57 +02:00
|
|
|
public function setModel($model)
|
|
|
|
{
|
|
|
|
$this->model = $model;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-07-10 05:10:18 +02:00
|
|
|
/**
|
|
|
|
* Sets correct database by variable
|
|
|
|
*/
|
2018-10-15 11:06:57 +02:00
|
|
|
private function setDefaultDatabase($id = false, $email = false, $token = false) : void
|
2018-10-08 11:38:45 +02:00
|
|
|
{
|
2018-10-21 00:26:21 +02:00
|
|
|
foreach (MultiDB::getDbs() as $database) {
|
2019-07-10 05:10:18 +02:00
|
|
|
$this->setDB($database);
|
|
|
|
|
2019-07-08 07:16:38 +02:00
|
|
|
/** Make sure we hook into the correct guard class */
|
2019-07-08 02:08:57 +02:00
|
|
|
$query = $this->conn->table((new $this->model)->getTable());
|
2018-10-08 11:38:45 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($id) {
|
2018-10-08 11:38:45 +02:00
|
|
|
$query->where('id', '=', $id);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2018-10-08 11:38:45 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($email) {
|
2018-10-15 11:06:57 +02:00
|
|
|
$query->where('email', '=', $email);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2018-10-08 11:38:45 +02:00
|
|
|
|
|
|
|
$user = $query->get();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (count($user) >= 1) {
|
2018-10-08 11:38:45 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-18 08:11:37 +02:00
|
|
|
|
|
|
|
$query = $this->conn->table('company_tokens');
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($token) {
|
2019-04-18 08:11:37 +02:00
|
|
|
$query->whereRaw("BINARY `token`= ?", $token);
|
|
|
|
|
|
|
|
$token = $query->get();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (count($token) >= 1) {
|
2019-04-18 08:11:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 11:38:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 05:10:18 +02:00
|
|
|
/**
|
|
|
|
* Sets the database at runtime
|
|
|
|
*/
|
2018-10-08 11:38:45 +02:00
|
|
|
private function setDB($database)
|
|
|
|
{
|
|
|
|
/** Get the database name we want to switch to*/
|
|
|
|
$db_name = config('database.connections.'.$database.'.database');
|
|
|
|
|
|
|
|
/* This will set the default configuration for the request / session?*/
|
|
|
|
config(['database.default' => $database]);
|
|
|
|
|
|
|
|
/* Set the connection to complete the user authentication */
|
|
|
|
$this->conn = app('db')->connection(config('database.connections.database.'.$database));
|
|
|
|
}
|
|
|
|
}
|