2018-10-24 05:50:15 +02:00
|
|
|
<?php
|
2020-10-07 05:56:35 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-10-07 05:56:35 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-10-07 05:56:35 +02:00
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
namespace App\Jobs\Account;
|
|
|
|
|
2023-12-02 22:45:49 +01:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Models\Account;
|
|
|
|
use Illuminate\Support\Str;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Jobs\User\CreateUser;
|
2023-12-02 22:45:49 +01:00
|
|
|
use App\DataProviders\Domains;
|
2020-11-11 21:42:20 +01:00
|
|
|
use App\Jobs\Util\VersionCheck;
|
2023-12-02 22:45:49 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Company\CreateCompany;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2023-12-02 22:45:49 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
|
|
|
use App\Utils\Traits\User\LoginCache;
|
|
|
|
use App\Events\Account\AccountCreated;
|
2020-10-07 05:56:35 +02:00
|
|
|
use Turbo124\Beacon\Facades\LightLogs;
|
2023-12-02 22:45:49 +01:00
|
|
|
use App\Jobs\Company\CreateCompanyToken;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use App\DataMapper\Analytics\AccountPlatform;
|
|
|
|
use App\Jobs\Company\CreateCompanyPaymentTerms;
|
|
|
|
use App\Jobs\Company\CreateCompanyTaskStatuses;
|
|
|
|
use App\DataMapper\Analytics\AccountCreated as AnalyticsAccountCreated;
|
2020-03-21 06:37:30 +01:00
|
|
|
|
|
|
|
class CreateAccount
|
|
|
|
{
|
|
|
|
use Dispatchable;
|
2021-07-05 00:18:57 +02:00
|
|
|
use LoginCache;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $request;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-05-26 02:37:59 +02:00
|
|
|
protected $client_ip;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-12-02 22:26:29 +01:00
|
|
|
|
2021-05-26 02:37:59 +02:00
|
|
|
public function __construct(array $sp660339, $client_ip)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
|
|
|
$this->request = $sp660339;
|
2021-05-26 02:37:59 +02:00
|
|
|
$this->client_ip = $client_ip;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function handle()
|
|
|
|
{
|
2023-04-29 08:19:41 +02:00
|
|
|
if (config('ninja.environment') == 'selfhost' && Account::count() == 0) {
|
|
|
|
return $this->create();
|
|
|
|
} elseif (config('ninja.environment') == 'selfhost' && Account::count() > 1) {
|
|
|
|
return response()->json(['message' => Ninja::selfHostedMessage()], 400);
|
|
|
|
} elseif (! Ninja::boot()) {
|
|
|
|
return response()->json(['message' => Ninja::parse()], 401);
|
|
|
|
}
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-31 13:52:21 +02:00
|
|
|
return $this->create();
|
2020-03-30 08:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function create()
|
|
|
|
{
|
2021-03-13 07:45:41 +01:00
|
|
|
Account::reguard();
|
|
|
|
$sp794f3f = new Account();
|
|
|
|
$sp794f3f->fill($this->request);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('rc', $this->request)) {
|
2021-08-08 13:50:13 +02:00
|
|
|
$sp794f3f->referral_code = $this->request['rc'];
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $sp794f3f->key) {
|
2020-03-29 14:22:14 +02:00
|
|
|
$sp794f3f->key = Str::random(32);
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2022-01-28 06:30:40 +01:00
|
|
|
$sp794f3f->hosted_client_count = config('ninja.quotas.free.clients');
|
|
|
|
$sp794f3f->hosted_company_count = config('ninja.quotas.free.max_companies');
|
2022-08-05 01:54:05 +02:00
|
|
|
$sp794f3f->account_sms_verified = true;
|
2022-08-25 06:02:40 +02:00
|
|
|
|
2023-12-02 22:45:49 +01:00
|
|
|
if (in_array($this->getDomain($this->request['email']), Domains::getDomains())) {
|
2022-08-25 06:02:40 +02:00
|
|
|
$sp794f3f->account_sms_verified = false;
|
|
|
|
}
|
|
|
|
|
2021-07-14 04:22:37 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-07-21 00:07:19 +02:00
|
|
|
$sp794f3f->save();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$sp035a66 = (new CreateCompany($this->request, $sp794f3f))->handle();
|
2020-03-21 06:37:30 +01:00
|
|
|
$sp035a66->load('account');
|
|
|
|
$sp794f3f->default_company_id = $sp035a66->id;
|
|
|
|
$sp794f3f->save();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2022-06-24 13:15:14 +02:00
|
|
|
$spaa9f78 = (new CreateUser($this->request, $sp794f3f, $sp035a66, true))->handle();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2023-11-24 22:55:02 +01:00
|
|
|
$sp035a66->service()->localizeCompany($spaa9f78);
|
|
|
|
|
2022-07-31 23:30:04 +02:00
|
|
|
(new CreateCompanyPaymentTerms($sp035a66, $spaa9f78))->handle();
|
|
|
|
(new CreateCompanyTaskStatuses($sp035a66, $spaa9f78))->handle();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($spaa9f78) {
|
|
|
|
auth()->login($spaa9f78, false);
|
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78->setCompany($sp035a66);
|
2021-07-05 00:18:57 +02:00
|
|
|
$this->setLoginCache($spaa9f78);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spafe62e = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT');
|
2022-06-24 13:15:14 +02:00
|
|
|
$sp2d97e8 = (new CreateCompanyToken($sp035a66, $spaa9f78, $spafe62e))->handle();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($spaa9f78) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new AccountCreated($spaa9f78, $sp035a66, Ninja::eventVars()));
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78->fresh();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2021-09-12 12:35:56 +02:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($sp035a66->settings));
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$nmo = new NinjaMailerObject();
|
2021-09-12 12:35:56 +02:00
|
|
|
$nmo->mailable = new \Modules\Admin\Mail\Welcome($sp035a66->owner());
|
2022-06-21 11:57:17 +02:00
|
|
|
$nmo->company = $sp035a66;
|
2021-09-12 12:35:56 +02:00
|
|
|
$nmo->settings = $sp035a66->settings;
|
|
|
|
$nmo->to_user = $sp035a66->owner();
|
|
|
|
|
2022-07-28 02:58:13 +02:00
|
|
|
NinjaMailerJob::dispatch($nmo, true);
|
2021-09-12 12:35:56 +02:00
|
|
|
|
2023-03-13 07:31:30 +01:00
|
|
|
(new \Modules\Admin\Jobs\Account\NinjaUser([], $sp035a66))->handle();
|
2021-09-12 12:35:56 +02:00
|
|
|
}
|
2021-06-14 10:06:34 +02:00
|
|
|
|
|
|
|
VersionCheck::dispatch();
|
2020-11-11 21:42:20 +01:00
|
|
|
|
2020-10-07 05:56:35 +02:00
|
|
|
LightLogs::create(new AnalyticsAccountCreated())
|
|
|
|
->increment()
|
2021-12-11 10:49:29 +01:00
|
|
|
->queue();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-01-30 00:46:39 +01:00
|
|
|
$ip = '';
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if (request()->hasHeader('Cf-Connecting-Ip')) {
|
2022-01-30 00:46:39 +01:00
|
|
|
$ip = request()->header('Cf-Connecting-Ip');
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (request()->hasHeader('X-Forwarded-For')) {
|
2022-01-30 00:46:39 +01:00
|
|
|
$ip = request()->header('Cf-Connecting-Ip');
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2022-01-30 00:46:39 +01:00
|
|
|
$ip = request()->ip();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-01-30 00:46:39 +01:00
|
|
|
|
2022-01-23 00:28:13 +01:00
|
|
|
$platform = request()->has('platform') ? request()->input('platform') : 'www';
|
2020-10-07 05:56:35 +02:00
|
|
|
|
2022-01-23 00:28:13 +01:00
|
|
|
LightLogs::create(new AccountPlatform($platform, request()->server('HTTP_USER_AGENT'), $ip))
|
2022-06-21 11:57:17 +02:00
|
|
|
->queue();
|
2021-09-12 12:35:56 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $sp794f3f;
|
|
|
|
}
|
2022-08-25 06:02:40 +02:00
|
|
|
|
|
|
|
private function getDomain($email)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
2022-08-25 06:02:40 +02:00
|
|
|
// split on @ and return last value of array (the domain)
|
|
|
|
$domain = explode('@', $email);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-08-25 06:02:40 +02:00
|
|
|
$domain_name = end($domain);
|
|
|
|
|
|
|
|
return $domain_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'gmail.com';
|
|
|
|
}
|
2023-12-02 22:26:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|