mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Reset connection when performing simple DB checks
This commit is contained in:
parent
3c99c65033
commit
347f10caa6
@ -52,37 +52,38 @@ class MultiDB
|
|||||||
|
|
||||||
public static function checkDomainAvailable($subdomain) : bool
|
public static function checkDomainAvailable($subdomain) : bool
|
||||||
{
|
{
|
||||||
if (! config('ninja.db.multi_db_enabled')) {
|
if (! config('ninja.db.multi_db_enabled'))
|
||||||
return Company::whereSubdomain($subdomain)->get()->count() == 0;
|
return Company::whereSubdomain($subdomain)->get()->count() == 0;
|
||||||
}
|
|
||||||
|
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
//multi-db active
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if (Company::on($db)->whereSubdomain($subdomain)->get()->count() >= 1) {
|
if (Company::on($db)->whereSubdomain($subdomain)->get()->count() >= 1) {
|
||||||
|
self::setDb($current_db);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//self::setDefaultDatabase();
|
self::setDb($current_db);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function checkUserEmailExists($email) : bool
|
public static function checkUserEmailExists($email) : bool
|
||||||
{
|
{
|
||||||
if (! config('ninja.db.multi_db_enabled')) {
|
if (! config('ninja.db.multi_db_enabled'))
|
||||||
return User::where(['email' => $email])->get()->count() >= 1 ?? false; // true >= 1 emails found / false -> == emails found
|
return User::where(['email' => $email])->get()->count() >= 1 ?? false; // true >= 1 emails found / false -> == emails found
|
||||||
}
|
|
||||||
|
|
||||||
//multi-db active
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail
|
if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail
|
||||||
|
self::setDb($current_db);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDb($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -102,19 +103,21 @@ class MultiDB
|
|||||||
*/
|
*/
|
||||||
public static function checkUserAndCompanyCoExist($email, $company_key) :bool
|
public static function checkUserAndCompanyCoExist($email, $company_key) :bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail
|
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) {
|
if (Company::on($db)->where(['company_key' => $company_key])->get()->count() >= 1) {
|
||||||
|
self::setDb($current_db);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
self::setDefaultDatabase();
|
self::setDb($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDb($current_db);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -125,20 +128,21 @@ class MultiDB
|
|||||||
*/
|
*/
|
||||||
public static function hasUser(array $data) : ?User
|
public static function hasUser(array $data) : ?User
|
||||||
{
|
{
|
||||||
if (! config('ninja.db.multi_db_enabled')) {
|
if (! config('ninja.db.multi_db_enabled'))
|
||||||
return User::where($data)->withTrashed()->first();
|
return User::where($data)->withTrashed()->first();
|
||||||
}
|
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
|
|
||||||
self::setDB($db);
|
self::setDB($db);
|
||||||
|
if ($user = User::where($data)->withTrashed()->first()) {
|
||||||
if ($user = User::where($data)->withTrashed()->first())
|
|
||||||
return $user;
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDb($current_db);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -149,107 +153,119 @@ class MultiDB
|
|||||||
*/
|
*/
|
||||||
public static function hasContact(string $email) : ?ClientContact
|
public static function hasContact(string $email) : ?ClientContact
|
||||||
{
|
{
|
||||||
if (! config('ninja.db.multi_db_enabled')) {
|
if (! config('ninja.db.multi_db_enabled'))
|
||||||
return ClientContact::where('email', $email)->withTrashed()->first();
|
return ClientContact::where('email', $email)->withTrashed()->first();
|
||||||
}
|
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
|
|
||||||
$user = ClientContact::on($db)->where('email', $email)->withTrashed()->first();
|
$user = ClientContact::on($db)->where('email', $email)->withTrashed()->first();
|
||||||
|
|
||||||
if ($user) {
|
if ($user) {
|
||||||
self::setDB($db);
|
self::setDb($db);
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDB($current_db);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function contactFindAndSetDb($token) :bool
|
public static function contactFindAndSetDb($token) :bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($ct = ClientContact::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) {
|
if ($ct = ClientContact::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) {
|
||||||
self::setDb($ct->company->db);
|
self::setDb($db);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function userFindAndSetDb($email) : bool
|
public static function userFindAndSetDb($email) : bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
//multi-db active
|
//multi-db active
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
|
|
||||||
if (User::on($db)->where('email', $email)->count() >= 1){
|
if (User::on($db)->where('email', $email)->count() >= 1){
|
||||||
nlog("setting db {$db}");
|
|
||||||
self::setDb($db);
|
self::setDb($db);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDB($current_db);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function findAndSetDb($token) :bool
|
public static function findAndSetDb($token) :bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($ct = CompanyToken::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) {
|
if ($ct = CompanyToken::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) {
|
||||||
self::setDb($ct->company->db);
|
self::setDb($ct->company->db);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self::setDefaultDatabase();
|
|
||||||
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function findAndSetDbByCompanyKey($company_key) :bool
|
public static function findAndSetDbByCompanyKey($company_key) :bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($company = Company::on($db)->where('company_key', $company_key)->first()) {
|
if ($company = Company::on($db)->where('company_key', $company_key)->first()) {
|
||||||
self::setDb($company->db);
|
self::setDb($company->db);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self::setDefaultDatabase();
|
|
||||||
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function findAndSetDbByContactKey($contact_key) :bool
|
public static function findAndSetDbByContactKey($contact_key) :bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($client_contact = ClientContact::on($db)->where('contact_key', $contact_key)->first()) {
|
if ($client_contact = ClientContact::on($db)->where('contact_key', $contact_key)->first()) {
|
||||||
self::setDb($client_contact->company->db);
|
self::setDb($client_contact->company->db);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self::setDefaultDatabase();
|
|
||||||
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function findAndSetDbByClientHash($client_hash) :bool
|
public static function findAndSetDbByClientHash($client_hash) :bool
|
||||||
{
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($client = Client::on($db)->where('client_hash', $client_hash)->first()) {
|
if ($client = Client::on($db)->where('client_hash', $client_hash)->first()) {
|
||||||
self::setDb($client->company->db);
|
self::setDb($client->company->db);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self::setDefaultDatabase();
|
|
||||||
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -260,6 +276,8 @@ class MultiDB
|
|||||||
if (! config('ninja.db.multi_db_enabled'))
|
if (! config('ninja.db.multi_db_enabled'))
|
||||||
return (Company::where($query_array)->exists() === true);
|
return (Company::where($query_array)->exists() === true);
|
||||||
|
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($company = Company::on($db)->where($query_array)->first()) {
|
if ($company = Company::on($db)->where($query_array)->first()) {
|
||||||
self::setDb($company->db);
|
self::setDb($company->db);
|
||||||
@ -267,7 +285,7 @@ class MultiDB
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -275,16 +293,16 @@ class MultiDB
|
|||||||
public static function findAndSetDbByInvitation($entity, $invitation_key)
|
public static function findAndSetDbByInvitation($entity, $invitation_key)
|
||||||
{
|
{
|
||||||
$class = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
|
$class = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
foreach (self::$dbs as $db) {
|
foreach (self::$dbs as $db) {
|
||||||
if ($invite = $class::on($db)->whereRaw('BINARY `key`= ?', [$invitation_key])->first()) {
|
if ($invite = $class::on($db)->whereRaw('BINARY `key`= ?', [$invitation_key])->first()) {
|
||||||
self::setDb($db);
|
self::setDb($db);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::setDefaultDatabase();
|
self::setDB($current_db);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user