2018-10-18 07:04:36 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
namespace App\Libraries;
|
|
|
|
|
2021-10-02 23:31:21 +02:00
|
|
|
use App\Models\Account;
|
2020-09-30 00:42:55 +02:00
|
|
|
use App\Models\Client;
|
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;
|
2021-07-01 23:57:55 +02:00
|
|
|
use App\Models\Document;
|
2018-10-18 07:04:36 +02:00
|
|
|
use App\Models\User;
|
2022-10-25 03:42:05 +02:00
|
|
|
use App\Models\VendorContact;
|
2021-04-27 01:34:35 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-10-19 12:59:58 +02:00
|
|
|
use Illuminate\Support\Str;
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* 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.
|
2018-10-18 07:04:36 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return array
|
2018-10-18 07:04:36 +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
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2023-04-30 10:09:08 +02:00
|
|
|
return Company::whereSubdomain($subdomain)->count() == 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2019-07-12 07:03:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-20 06:13:54 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-07-13 03:07:10 +02:00
|
|
|
if (Company::on($db)->whereSubdomain($subdomain)->exists()) {
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return false;
|
2019-07-12 07:03:30 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-12 07:03:30 +02:00
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
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
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
|
|
|
return User::where(['email' => $email])->withTrashed()->exists();
|
|
|
|
} // true >= 1 emails found / false -> == emails found
|
|
|
|
|
|
|
|
$current_db = config('database.default');
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-09-06 00:21:22 +02:00
|
|
|
if (User::on($db)->where(['email' => $email])->withTrashed()->exists()) { // if user already exists, validation will fail
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
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
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
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
|
2023-04-30 10:09:08 +02:00
|
|
|
* @param string $company_key The company key
|
2019-12-08 11:28:52 +01:00
|
|
|
* @return bool True|False
|
|
|
|
*/
|
|
|
|
public static function checkUserAndCompanyCoExist($email, $company_key) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (User::on($db)->where(['email' => $email])->withTrashed()->exists()) {
|
2021-05-26 03:32:01 +02:00
|
|
|
if (Company::on($db)->where(['company_key' => $company_key])->exists()) {
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
return true;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
/**
|
|
|
|
* @param array $data
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return User|null
|
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
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2020-03-25 00:20:42 +01:00
|
|
|
return User::where($data)->withTrashed()->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-05-13 08:01:12 +02:00
|
|
|
self::setDB($db);
|
2021-05-24 06:52:08 +02:00
|
|
|
if ($user = User::where($data)->withTrashed()->first()) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return $user;
|
2021-05-24 06:52:08 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return null;
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
|
2021-02-15 00:39:40 +01:00
|
|
|
/**
|
2023-04-30 10:09:08 +02:00
|
|
|
* @param string $email
|
|
|
|
* @return ClientContact|null
|
2021-02-15 00:39:40 +01:00
|
|
|
*/
|
2021-05-13 14:41:32 +02:00
|
|
|
public static function hasContact(string $email) : ?ClientContact
|
2021-02-15 00:39:40 +01:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2021-05-13 14:41:32 +02:00
|
|
|
return ClientContact::where('email', $email)->withTrashed()->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-02-15 00:39:40 +01:00
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
2021-05-13 14:41:32 +02:00
|
|
|
$user = ClientContact::on($db)->where('email', $email)->withTrashed()->first();
|
2021-02-15 00:39:40 +01:00
|
|
|
|
|
|
|
if ($user) {
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-02-15 00:39:40 +01:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDB($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-02-15 00:39:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-23 01:06:30 +02:00
|
|
|
/**
|
2023-04-30 10:09:08 +02:00
|
|
|
* @param array $search
|
|
|
|
* @return ClientContact|null
|
2021-10-23 01:06:30 +02:00
|
|
|
*/
|
|
|
|
public static function findContact(array $search) : ?ClientContact
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2021-10-23 01:06:30 +02:00
|
|
|
return ClientContact::where($search)->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-23 01:06:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-10-23 01:06:30 +02:00
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
$user = ClientContact::on($db)->where($search)->first();
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 01:06:30 +02:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-23 01:06:30 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
public static function contactFindAndSetDb($token) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2022-02-26 08:48:22 +01:00
|
|
|
if (ClientContact::on($db)->where('token', $token)->exists()) {
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDB($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
public static function userFindAndSetDb($email) : bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2021-02-15 00:39:40 +01:00
|
|
|
//multi-db active
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (User::on($db)->where('email', $email)->withTrashed()->exists()) {
|
2021-05-13 11:13:51 +02:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return true;
|
2021-05-13 11:13:51 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDB($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return false;
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
|
|
|
|
2021-07-01 23:23:25 +02:00
|
|
|
public static function documentFindAndSetDb($hash) : bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-07-01 23:23:25 +02:00
|
|
|
|
|
|
|
//multi-db active
|
|
|
|
foreach (self::$dbs as $db) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Document::on($db)->where('hash', $hash)->exists()) {
|
2021-07-01 23:23:25 +02:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-07-01 23:23:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-07-01 23:23:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:21:28 +01:00
|
|
|
public static function findAndSetDb($token) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2022-02-26 08:48:22 +01:00
|
|
|
if (CompanyToken::on($db)->where('token', $token)->exists()) {
|
2021-10-02 23:31:21 +02:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-03-27 23:21:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 06:52:08 +02:00
|
|
|
|
|
|
|
self::setDB($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-03-27 23:21:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
public static function findAndSetDbByCompanyKey($company_key) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-10-02 23:31:21 +02:00
|
|
|
if (Company::on($db)->where('company_key', $company_key)->exists()) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-02 23:31:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:07:08 +02:00
|
|
|
public static function findAndSetDbByCompanyId($company_id) :?Company
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2022-04-21 04:07:08 +02:00
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($company = Company::on($db)->where('id', $company_id)->first()) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-04-21 04:07:08 +02:00
|
|
|
return $company;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
|
|
|
|
2023-05-03 06:05:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function findAndSetDbByShopifyName($shopify_name) :?Company
|
|
|
|
{
|
|
|
|
$current_db = config('database.default');
|
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($company = Company::on($db)->with('tokens')->where('shopify_name', $shopify_name)->first()) {
|
|
|
|
self::setDb($db);
|
|
|
|
|
|
|
|
return $company;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
|
|
|
|
|
|
|
return null;
|
2022-04-21 04:07:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-02 23:31:21 +02:00
|
|
|
public static function findAndSetDbByAccountKey($account_key) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-10-02 23:31:21 +02:00
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if (Account::on($db)->where('key', $account_key)->exists()) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 06:52:08 +02:00
|
|
|
|
|
|
|
self::setDB($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:54 +02:00
|
|
|
public static function findAndSetDbByInappTransactionId($transaction_id) :bool
|
|
|
|
{
|
|
|
|
$current_db = config('database.default');
|
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if (Account::on($db)->where('inapp_transaction_id', $transaction_id)->exists()) {
|
|
|
|
self::setDb($db);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-23 03:45:07 +02:00
|
|
|
public static function findAndSetDbByContactKey($contact_key) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2020-09-23 03:45:07 +02:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-10-02 23:31:21 +02:00
|
|
|
if (ClientContact::on($db)->where('contact_key', $contact_key)->exists()) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-09-23 03:45:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 06:52:08 +02:00
|
|
|
|
|
|
|
self::setDB($current_db);
|
2020-09-23 03:45:07 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
return false;
|
2020-09-23 03:45:07 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 03:42:05 +02:00
|
|
|
public static function findAndSetDbByVendorContactKey($contact_key) :bool
|
|
|
|
{
|
|
|
|
$current_db = config('database.default');
|
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if (VendorContact::on($db)->where('contact_key', $contact_key)->exists()) {
|
|
|
|
self::setDb($db);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 00:42:55 +02:00
|
|
|
public static function findAndSetDbByClientHash($client_hash) :bool
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2020-09-30 00:42:55 +02:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-10-02 23:31:21 +02:00
|
|
|
if (Client::on($db)->where('client_hash', $client_hash)->exists()) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-09-30 00:42:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 06:52:08 +02:00
|
|
|
|
|
|
|
self::setDB($current_db);
|
2020-09-30 00:42:55 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
return false;
|
2020-09-30 00:42:55 +02:00
|
|
|
}
|
2020-09-23 03:45:07 +02:00
|
|
|
|
2022-04-21 04:07:08 +02:00
|
|
|
public static function findAndSetDbByClientId($client_id) :?Client
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2022-04-21 04:07:08 +02:00
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
if ($client = Client::on($db)->where('id', $client_id)->first()) {
|
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-04-21 04:07:08 +02:00
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDB($current_db);
|
|
|
|
|
2023-05-03 06:05:29 +02:00
|
|
|
return null;
|
2022-04-21 04:07:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 01:59:12 +02:00
|
|
|
public static function findAndSetDbByDomain($query_array)
|
2019-07-12 07:03:30 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
|
|
|
return Company::where($query_array)->first();
|
|
|
|
}
|
2021-04-12 14:15:34 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2021-05-24 06:52:08 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-05-15 05:31:41 +02:00
|
|
|
if ($company = Company::on($db)->where($query_array)->first()) {
|
2021-10-02 23:31:21 +02:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-27 01:14:21 +02:00
|
|
|
return $company;
|
2019-07-12 07:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDB($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2020-10-19 12:59:58 +02:00
|
|
|
$class = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach (self::$dbs as $db) {
|
2021-10-14 07:25:09 +02:00
|
|
|
if ($invite = $class::on($db)->where('key', $invitation_key)->exists()) {
|
2019-11-16 04:12:29 +01:00
|
|
|
self::setDb($db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:28:52 +01:00
|
|
|
|
2021-05-24 06:52:08 +02:00
|
|
|
self::setDB($current_db);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-27 03:22:11 +02:00
|
|
|
/**
|
2023-04-30 10:09:08 +02:00
|
|
|
* @param string $phone
|
|
|
|
* @return bool
|
2022-07-27 03:22:11 +02:00
|
|
|
*/
|
2022-07-27 12:03:46 +02:00
|
|
|
public static function hasPhoneNumber(string $phone) : bool
|
2022-07-27 03:22:11 +02:00
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2022-07-27 03:22:11 +02:00
|
|
|
return Account::where('account_sms_verification_number', $phone)->where('account_sms_verified', true)->exists();
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-27 03:22:11 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$current_db = config('database.default');
|
2022-07-27 03:22:11 +02:00
|
|
|
|
|
|
|
foreach (self::$dbs as $db) {
|
|
|
|
self::setDB($db);
|
|
|
|
if ($exists = Account::where('account_sms_verification_number', $phone)->where('account_sms_verified', true)->exists()) {
|
|
|
|
self::setDb($current_db);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::setDb($current_db);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-30 10:09:08 +02:00
|
|
|
public static function randomSubdomainGenerator(): string
|
2021-05-24 06:24:16 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$current_db = config('database.default');
|
|
|
|
|
|
|
|
do {
|
|
|
|
$length = 8;
|
|
|
|
$string = '';
|
|
|
|
$vowels = ['a', 'e', 'i', 'o', 'u'];
|
|
|
|
$consonants = [
|
|
|
|
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
|
|
|
|
'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z',
|
|
|
|
];
|
|
|
|
|
|
|
|
$max = $length / 2;
|
|
|
|
for ($i = 1; $i <= $max; $i++) {
|
|
|
|
$string .= $consonants[rand(0, 19)];
|
|
|
|
$string .= $vowels[rand(0, 4)];
|
2021-05-24 06:24:16 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} while (! self::checkDomainAvailable($string));
|
2021-05-24 06:24:16 +02:00
|
|
|
|
|
|
|
self::setDb($current_db);
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
/**
|
|
|
|
* @param $database
|
2023-04-30 10:09:08 +02:00
|
|
|
* @return void
|
2018-10-18 07:04:36 +02:00
|
|
|
*/
|
2018-12-02 11:42:06 +01:00
|
|
|
public static function setDB(string $database) : void
|
2020-03-21 06:37: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
|
|
|
}
|