invoiceninja/app/Jobs/User/CreateUser.php

90 lines
2.4 KiB
PHP
Raw Permalink Normal View History

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