2018-10-24 05:50:15 +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-24 05:50:15 +02:00
|
|
|
|
|
|
|
namespace App\Jobs\User;
|
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2019-04-19 11:09:55 +02:00
|
|
|
use App\Events\User\UserWasCreated;
|
2019-02-17 12:07:58 +01:00
|
|
|
use App\Models\User;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2018-10-26 06:53:29 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2018-10-24 05:50:15 +02:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
|
|
|
class CreateUser
|
|
|
|
{
|
2018-10-26 06:53:29 +02:00
|
|
|
use MakesHash;
|
2018-10-24 05:50:15 +02:00
|
|
|
use Dispatchable;
|
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
protected $account;
|
2018-10-24 12:24:09 +02:00
|
|
|
|
|
|
|
protected $company;
|
|
|
|
|
2019-07-09 02:01:29 +02:00
|
|
|
protected $company_owner;
|
|
|
|
|
2018-10-24 05:50:15 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param array $request
|
|
|
|
* @param $account
|
|
|
|
* @param $company
|
|
|
|
* @param bool $company_owner
|
2018-10-24 05:50:15 +02:00
|
|
|
*/
|
2019-04-18 13:57:22 +02:00
|
|
|
public function __construct(array $request, $account, $company, $company_owner = false)
|
2018-10-24 05:50:15 +02:00
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
$this->account = $account;
|
2018-10-24 12:24:09 +02:00
|
|
|
$this->company = $company;
|
2019-04-18 13:57:22 +02:00
|
|
|
$this->company_owner = $company_owner;
|
2018-10-24 05:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return User|null
|
2018-10-24 05:50:15 +02:00
|
|
|
*/
|
2018-11-27 08:24:26 +01:00
|
|
|
public function handle() : ?User
|
2018-10-24 05:50:15 +02:00
|
|
|
{
|
|
|
|
$user = new User();
|
2020-03-24 10:15:30 +01:00
|
|
|
$user->account_id = $this->account->id;
|
2021-07-17 09:38:59 +02:00
|
|
|
$user->password = $this->request['password'] ? bcrypt($this->request['password']) : '';
|
2018-10-24 05:50:15 +02:00
|
|
|
$user->accepted_terms_version = config('ninja.terms_version');
|
2021-11-06 01:46:12 +01:00
|
|
|
$user->confirmation_code = $this->createDbHash($this->company->db);
|
2018-12-02 11:42:06 +01:00
|
|
|
$user->fill($this->request);
|
2020-09-06 11:38:10 +02:00
|
|
|
$user->email = $this->request['email']; //todo need to remove this in production
|
2019-10-04 13:54:03 +02:00
|
|
|
$user->last_login = now();
|
2019-11-04 01:22:59 +01:00
|
|
|
$user->ip = request()->ip();
|
2021-03-08 05:20:02 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isSelfHost()) {
|
2021-03-08 05:20:02 +01:00
|
|
|
$user->email_verified_at = now();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-03-08 05:20:02 +01:00
|
|
|
|
2018-10-24 05:50:15 +02:00
|
|
|
$user->save();
|
|
|
|
|
2018-11-02 12:57:59 +01:00
|
|
|
$user->companies()->attach($this->company->id, [
|
|
|
|
'account_id' => $this->account->id,
|
2019-04-18 13:57:22 +02:00
|
|
|
'is_owner' => $this->company_owner,
|
2018-11-02 12:57:59 +01:00
|
|
|
'is_admin' => 1,
|
2019-03-02 22:44:08 +01:00
|
|
|
'is_locked' => 0,
|
2019-11-21 09:38:57 +01:00
|
|
|
'permissions' => '',
|
2020-03-09 10:38:15 +01:00
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
2020-02-26 04:26:07 +01:00
|
|
|
'settings' => null,
|
2018-11-02 12:57:59 +01:00
|
|
|
]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! Ninja::isSelfHost()) {
|
2021-04-13 07:04:53 +02:00
|
|
|
event(new UserWasCreated($user, $user, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2021-03-10 10:15:24 +01:00
|
|
|
}
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2018-10-24 05:50:15 +02:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|