mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
commit
391302daaf
@ -12,7 +12,9 @@
|
|||||||
namespace App\Factory;
|
namespace App\Factory;
|
||||||
|
|
||||||
use App\DataMapper\CompanySettings;
|
use App\DataMapper\CompanySettings;
|
||||||
|
use App\Libraries\MultiDB;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
|
use App\Utils\Ninja;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
|
|
||||||
class CompanyFactory
|
class CompanyFactory
|
||||||
@ -33,7 +35,12 @@ class CompanyFactory
|
|||||||
$company->db = config('database.default');
|
$company->db = config('database.default');
|
||||||
//$company->custom_fields = (object) ['invoice1' => '1', 'invoice2' => '2', 'client1'=>'3'];
|
//$company->custom_fields = (object) ['invoice1' => '1', 'invoice2' => '2', 'client1'=>'3'];
|
||||||
$company->custom_fields = (object) [];
|
$company->custom_fields = (object) [];
|
||||||
|
|
||||||
|
if(Ninja::isHosted())
|
||||||
|
$company->subdomain = MultiDB::randomSubdomainGenerator();
|
||||||
|
else
|
||||||
$company->subdomain = '';
|
$company->subdomain = '';
|
||||||
|
|
||||||
$company->enabled_modules = config('ninja.enabled_modules'); //32767;//8191; //4095
|
$company->enabled_modules = config('ninja.enabled_modules'); //32767;//8191; //4095
|
||||||
$company->default_password_timeout = 1800000;
|
$company->default_password_timeout = 1800000;
|
||||||
|
|
||||||
|
@ -60,6 +60,12 @@ class UniqueUserRule implements Rule
|
|||||||
*/
|
*/
|
||||||
private function checkIfEmailExists($email) : bool
|
private function checkIfEmailExists($email) : bool
|
||||||
{
|
{
|
||||||
return MultiDB::checkUserEmailExists($email);
|
$current_db = config('database.default');
|
||||||
|
|
||||||
|
$result = MultiDB::checkUserEmailExists($email);
|
||||||
|
|
||||||
|
MultiDB::setDb($current_db);
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,14 @@ class ValidUserForCompany implements Rule
|
|||||||
*/
|
*/
|
||||||
public function passes($attribute, $value)
|
public function passes($attribute, $value)
|
||||||
{
|
{
|
||||||
return MultiDB::checkUserAndCompanyCoExist($value, auth()->user()->company()->company_key, auth()->user()->company()->id);
|
$current_db = config('database.default');
|
||||||
|
|
||||||
|
$result = MultiDB::checkUserAndCompanyCoExist($value, auth()->user()->company()->company_key, auth()->user()->company()->id);
|
||||||
|
|
||||||
|
|
||||||
|
MultiDB::setDb($current_db);
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -293,6 +293,9 @@ class CSVImport implements ShouldQueue {
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/* Make sure we don't apply any payments to invoices with a Zero Amount*/
|
||||||
|
if($invoice->amount > 0)
|
||||||
|
{
|
||||||
$payment_repository->save(
|
$payment_repository->save(
|
||||||
$payment_data,
|
$payment_data,
|
||||||
PaymentFactory::create( $this->company->id, $invoice->user_id, $invoice->client_id )
|
PaymentFactory::create( $this->company->id, $invoice->user_id, $invoice->client_id )
|
||||||
@ -300,6 +303,7 @@ class CSVImport implements ShouldQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->actionInvoiceStatus( $invoice, $invoice_data, $invoice_repository );
|
$this->actionInvoiceStatus( $invoice, $invoice_data, $invoice_repository );
|
||||||
}
|
}
|
||||||
|
@ -298,7 +298,7 @@ class Import implements ShouldQueue
|
|||||||
$data = $this->transformCompanyData($data);
|
$data = $this->transformCompanyData($data);
|
||||||
|
|
||||||
if(Ninja::isHosted())
|
if(Ninja::isHosted())
|
||||||
$data['subdomain'] = $this->randomSubdomainGenerator();
|
$data['subdomain'] = MultiDB::randomSubdomainGenerator();
|
||||||
|
|
||||||
$rules = (new UpdateCompanyRequest())->rules();
|
$rules = (new UpdateCompanyRequest())->rules();
|
||||||
|
|
||||||
@ -1721,28 +1721,7 @@ class Import implements ShouldQueue
|
|||||||
MultiDB::setDb($current_db);
|
MultiDB::setDb($current_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function randomSubdomainGenerator()
|
|
||||||
{
|
|
||||||
do {
|
|
||||||
$length = 8;
|
|
||||||
$string = '';
|
|
||||||
$vowels = array("a","e","i","o","u");
|
|
||||||
$consonants = array(
|
|
||||||
'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)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while(!MultiDB::checkDomainAvailable($string));
|
|
||||||
|
|
||||||
return $string;
|
|
||||||
}
|
|
||||||
/* In V4 we use negative invoices (credits) and add then into the client balance. In V5, these sit off ledger and are applied later.
|
/* In V4 we use negative invoices (credits) and add then into the client balance. In V5, these sit off ledger and are applied later.
|
||||||
This next section will check for credit balances and reduce the client balance so that the V5 balances are correct
|
This next section will check for credit balances and reduce the client balance so that the V5 balances are correct
|
||||||
*/
|
*/
|
||||||
|
@ -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,20 +293,48 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function randomSubdomainGenerator()
|
||||||
|
{
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
|
do {
|
||||||
|
$length = 8;
|
||||||
|
$string = '';
|
||||||
|
$vowels = array("a","e","i","o","u");
|
||||||
|
$consonants = array(
|
||||||
|
'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)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(!self::checkDomainAvailable($string));
|
||||||
|
|
||||||
|
self::setDb($current_db);
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $database
|
* @param $database
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user