1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Account/CreateAccount.php

164 lines
4.9 KiB
PHP
Raw Normal View History

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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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
*/
namespace App\Jobs\Account;
2020-10-07 05:56:35 +02:00
use App\DataMapper\Analytics\AccountCreated as AnalyticsAccountCreated;
2022-01-23 00:28:13 +01:00
use App\DataMapper\Analytics\AccountPlatform;
use App\Events\Account\AccountCreated;
use App\Jobs\Company\CreateCompany;
use App\Jobs\Company\CreateCompanyPaymentTerms;
2020-10-20 01:01:59 +02:00
use App\Jobs\Company\CreateCompanyTaskStatuses;
use App\Jobs\Company\CreateCompanyToken;
2021-02-14 12:36:36 +01:00
use App\Jobs\Mail\NinjaMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\User\CreateUser;
2020-11-11 21:42:20 +01:00
use App\Jobs\Util\VersionCheck;
2021-02-14 12:36:36 +01:00
use App\Mail\Admin\AccountCreatedObject;
use App\Mail\Admin\VerifyUserObject;
use App\Models\Account;
2021-05-26 01:14:41 +02:00
use App\Models\Timezone;
use App\Notifications\Ninja\NewAccountCreated;
use App\Utils\Ninja;
2021-07-05 00:18:57 +02:00
use App\Utils\Traits\User\LoginCache;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2021-05-26 01:14:41 +02:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;
2020-10-07 05:56:35 +02:00
use Turbo124\Beacon\Facades\LightLogs;
2021-09-12 12:35:56 +02:00
use Illuminate\Support\Facades\App;
class CreateAccount
{
use Dispatchable;
2021-07-05 00:18:57 +02:00
use LoginCache;
protected $request;
2021-05-26 02:37:59 +02:00
protected $client_ip;
public function __construct(array $sp660339, $client_ip)
{
$this->request = $sp660339;
2021-05-26 02:37:59 +02:00
$this->client_ip = $client_ip;
}
public function handle()
{
if (config('ninja.environment') == 'selfhost' && Account::all()->count() == 0) {
return $this->create();
} elseif (config('ninja.environment') == 'selfhost' && Account::all()->count() > 1) {
return response()->json(['message' => Ninja::selfHostedMessage()], 400);
} elseif (! Ninja::boot()) {
return response()->json(['message' => Ninja::parse()], 401);
}
return $this->create();
}
private function create()
{
2021-03-13 07:45:41 +01:00
Account::reguard();
$sp794f3f = new Account();
$sp794f3f->fill($this->request);
2021-08-08 13:50:13 +02:00
if(array_key_exists('rc', $this->request))
$sp794f3f->referral_code = $this->request['rc'];
if (! $sp794f3f->key) {
$sp794f3f->key = Str::random(32);
}
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-02-23 07:39:36 +01:00
// $sp794f3f->trial_started = now();
// $sp794f3f->trial_plan = 'pro';
}
2021-07-21 00:07:19 +02:00
$sp794f3f->save();
$sp035a66 = CreateCompany::dispatchNow($this->request, $sp794f3f);
$sp035a66->load('account');
$sp794f3f->default_company_id = $sp035a66->id;
$sp794f3f->save();
$spaa9f78 = CreateUser::dispatchNow($this->request, $sp794f3f, $sp035a66, true);
CreateCompanyPaymentTerms::dispatchNow($sp035a66, $spaa9f78);
2020-10-20 01:01:59 +02:00
CreateCompanyTaskStatuses::dispatchNow($sp035a66, $spaa9f78);
if ($spaa9f78) {
auth()->login($spaa9f78, false);
}
$spaa9f78->setCompany($sp035a66);
2021-07-05 00:18:57 +02:00
$this->setLoginCache($spaa9f78);
$spafe62e = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT');
$sp2d97e8 = CreateCompanyToken::dispatchNow($sp035a66, $spaa9f78, $spafe62e);
if ($spaa9f78) {
2020-07-08 14:02:16 +02:00
event(new AccountCreated($spaa9f78, $sp035a66, Ninja::eventVars()));
}
$spaa9f78->fresh();
2021-09-12 12:35:56 +02:00
if(Ninja::isHosted()){
2021-11-05 10:43:25 +01:00
2021-09-12 12:35:56 +02:00
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($sp035a66->settings));
$nmo = new NinjaMailerObject;
$nmo->mailable = new \Modules\Admin\Mail\Welcome($sp035a66->owner());
$nmo->company = $sp035a66;
$nmo->settings = $sp035a66->settings;
$nmo->to_user = $sp035a66->owner();
NinjaMailerJob::dispatch($nmo);
2021-06-14 10:06:34 +02:00
\Modules\Admin\Jobs\Account\NinjaUser::dispatch([], $sp035a66);
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-01-23 00:28:13 +01:00
2022-01-30 00:46:39 +01:00
$ip = '';
if(request()->hasHeader('Cf-Connecting-Ip'))
$ip = request()->header('Cf-Connecting-Ip');
elseif(request()->hasHeader('X-Forwarded-For'))
$ip = request()->header('Cf-Connecting-Ip');
else
$ip = request()->ip();
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))
->queue();
2021-09-12 12:35:56 +02:00
return $sp794f3f;
}
2021-05-26 01:14:41 +02:00
}
2021-05-26 01:14:41 +02:00