2018-10-18 07:04:36 +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-18 07:04:36 +02:00
|
|
|
|
|
|
|
namespace App\Libraries;
|
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
use App\Models\ClientContact;
|
2019-07-12 07:03:30 +02:00
|
|
|
use App\Models\Company;
|
2019-03-27 23:21:28 +01:00
|
|
|
use App\Models\CompanyToken;
|
2018-10-18 07:04:36 +02:00
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MultiDB
|
2019-12-08 11:28:52 +01:00
|
|
|
*
|
|
|
|
* Caution!
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* When we perform scans across databases,
|
|
|
|
* we need to remember that if we don't
|
2019-12-08 11:28:52 +01:00
|
|
|
* return a DB 'HIT' the DB connection will
|
|
|
|
* be set to the last DB in the chain,
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-08 11:28:52 +01:00
|
|
|
* So for these cases, we need to reset the
|
|
|
|
* DB connection to the default connection.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-08 11:28:52 +01:00
|
|
|
* Even that may be problematic, and we
|
|
|
|
* may need to know the current DB connection
|
|
|
|
* so that we can fall back gracefully.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2018-10-18 07:04:36 +02:00
|
|
|
* @package App\Libraries
|
|
|
|
*/
|
|
|
|
class MultiDB
|
|
|
|
{
|
2018-10-24 12:24:09 +02:00
|
|
|
const DB_PREFIX = 'db-ninja-';
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
public static $dbs = ['db-ninja-01', 'db-ninja-02'];
|
2018-10-21 00:26:21 +02:00
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
/**
|
|
|
|
* @param $email
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-10-21 00:26:21 +02:00
|
|
|
|
2018-11-27 08:24:26 +01:00
|
|
|
public static function getDbs() : array
|
2018-10-21 00:26:21 +02:00
|
|
|
{
|
|
|
|
return self::$dbs;
|
|
|
|
}
|
|
|
|
|
2019-12-10 21:53:41 +01:00
|
|
|
public static function checkDomainAvailable($subdomain) : bool
|
2019-07-12 07:03:30 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2019-12-10 21:53:41 +01:00
|
|
|
return Company::whereSubdomain($subdomain)->get()->count() == 0;
|
2019-07-12 07:03:30 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
//multi-db active
|
|
|
|
foreach (self::$dbs as $db) {
|
2020-01-02 00:09:34 +01:00
|
|
|
|
|
|
|
if (Company::on($db)->whereSubdomain($subdomain)->get()->count() >=1) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return false;
|
2019-07-12 07:03:30 +02:00
|
|
|
}
|
2020-01-02 00:09:34 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-12 07:03:30 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
self::setDefaultDatabase();
|
2020-01-02 00:09:34 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return true;
|
2019-07-12 07:03:30 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
public static function checkUserEmailExists($email) : bool
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2018-10-18 07:04:36 +02:00
|
|
|
return User::where(['email' => $email])->get()->count() >= 1 ?? false; // true >= 1 emails found / false -> == emails found
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
//multi-db active
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if (User::on($db)->where(['email' => $email])->get()->count() >=1) { // if user already exists, validation will fail
|
|
|
|
return true;
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
self::setDefaultDatabase();
|
|
|
|
return false;
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
/**
|
|
|
|
* A user and company must co exists on the same database.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-08 11:28:52 +01:00
|
|
|
* This function will check that if a user exists on the system,
|
|
|
|
* the company is also located on the same database.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* If no user is found, then we also return true as this must be
|
2019-12-08 11:28:52 +01:00
|
|
|
* a new user request.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-08 11:28:52 +01:00
|
|
|
* @param string $email The user email
|
|
|
|
* @param stirng $company_key The company key
|
|
|
|
* @return bool True|False
|
|
|
|
*/
|
|
|
|
public static function checkUserAndCompanyCoExist($email, $company_key) :bool
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if (User::on($db)->where(['email' => $email])->get()->count() >=1) { // if user already exists, validation will fail
|
|
|
|
if (Company::on($db)->where(['company_key' => $company_key])->get()->count() >=1) {
|
2019-12-08 11:28:52 +01:00
|
|
|
return true;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-12-08 11:28:52 +01:00
|
|
|
self::setDefaultDatabase();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDefaultDatabase();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
/**
|
|
|
|
* @param array $data
|
2018-12-02 11:42:06 +01:00
|
|
|
* @return App\Models\User | bool
|
2018-10-18 07:04:36 +02:00
|
|
|
*/
|
2018-11-27 08:24:26 +01:00
|
|
|
public static function hasUser(array $data) : ?User
|
2018-10-18 07:04:36 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2018-10-18 07:04:36 +02:00
|
|
|
return User::where($data)->first();
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
self::setDB($db);
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$user = User::where($data)->first();
|
2018-10-21 00:26:21 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($user) {
|
|
|
|
return $user;
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
self::setDefaultDatabase();
|
|
|
|
return null;
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
public static function contactFindAndSetDb($token) :bool
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($ct = ClientContact::on($db)->whereRaw("BINARY `token`= ?", [$token])->first()) {
|
2019-07-08 02:08:57 +02:00
|
|
|
self::setDb($ct->company->db);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
|
|
|
|
self::setDefaultDatabase();
|
2019-07-08 02:08:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
public static function userFindAndSetDb($email) : bool
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
//multi-db active
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if (User::on($db)->where(['email' => $email])->get()->count() >=1) { // if user already exists, validation will fail
|
|
|
|
return true;
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return false;
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:21:28 +01:00
|
|
|
public static function findAndSetDb($token) :bool
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($ct = CompanyToken::on($db)->whereRaw("BINARY `token`= ?", [$token])->first()) {
|
2019-03-27 23:21:28 +01:00
|
|
|
self::setDb($ct->company->db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2019-12-10 21:53:41 +01:00
|
|
|
public static function findAndSetDbByDomain($subdomain) :bool
|
2019-07-12 07:03:30 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
//\Log::error("searching for {$domain}");
|
2019-09-26 01:47:33 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($company = Company::on($db)->whereSubdomain($subdomain)->first()) {
|
2019-07-12 07:03:30 +02:00
|
|
|
self::setDb($company->db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
|
|
|
|
self::setDefaultDatabase();
|
2019-07-12 07:03:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
public static function findAndSetDbByInvitation($entity, $invitation_key)
|
|
|
|
{
|
2019-11-21 09:38:57 +01:00
|
|
|
$class = 'App\Models\\'.ucfirst($entity).'Invitation';
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($invite = $class::on($db)->whereRaw("BINARY `key`= ?", [$invitation_key])->first()) {
|
2019-11-16 04:12:29 +01:00
|
|
|
self::setDb($db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
|
|
|
|
self::setDefaultDatabase();
|
2019-11-16 04:12:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
/**
|
|
|
|
* @param $database
|
|
|
|
*/
|
2018-12-02 11:42:06 +01:00
|
|
|
public static function setDB(string $database) : void
|
2020-02-24 11:15:30 +01:00
|
|
|
{
|
2018-10-19 05:45:55 +02:00
|
|
|
/* This will set the database connection for the request */
|
2018-10-18 07:04:36 +02:00
|
|
|
config(['database.default' => $database]);
|
|
|
|
}
|
|
|
|
|
2019-12-04 03:27:28 +01:00
|
|
|
public static function setDefaultDatabase()
|
|
|
|
{
|
|
|
|
config(['database.default' => config('ninja.db.default')]);
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|