2018-10-24 05:50:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\User;
|
|
|
|
|
2019-02-17 12:07:58 +01:00
|
|
|
use App\DataMapper\DefaultSettings;
|
2018-10-29 04:16:17 +01:00
|
|
|
use App\Events\User\UserCreated;
|
2018-11-02 11:54:46 +01:00
|
|
|
use App\Models\CompanyUser;
|
2019-02-17 12:07:58 +01:00
|
|
|
use App\Models\User;
|
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;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-10-24 05:50:15 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-27 08:24:26 +01:00
|
|
|
public function handle() : ?User
|
2018-10-24 05:50:15 +02:00
|
|
|
{
|
2019-04-19 03:59:07 +02:00
|
|
|
$x = mt_rand(1,10000);//todo
|
2019-04-18 07:01:40 +02:00
|
|
|
|
2019-04-19 03:59:07 +02:00
|
|
|
$email = 'turbo124+'. $x .'@gmail.com'; //todo
|
2018-10-24 05:50:15 +02:00
|
|
|
|
|
|
|
$user = new User();
|
|
|
|
$user->account_id = $this->account->id;
|
2018-12-02 11:42:06 +01:00
|
|
|
$user->password = bcrypt($this->request['password']);
|
2018-10-24 05:50:15 +02:00
|
|
|
$user->accepted_terms_version = config('ninja.terms_version');
|
2018-10-26 06:53:29 +02:00
|
|
|
$user->confirmation_code = $this->createDbHash(config('database.default'));
|
2018-12-02 11:42:06 +01:00
|
|
|
$user->fill($this->request);
|
2019-04-18 07:01:40 +02:00
|
|
|
$user->email = $email;//todo need to remove this in production
|
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,
|
|
|
|
'permissions' => json_encode([]),
|
2019-02-17 12:07:58 +01:00
|
|
|
'settings' => json_encode(DefaultSettings::userSettings()),
|
2018-11-02 12:57:59 +01:00
|
|
|
]);
|
2018-10-24 12:24:09 +02:00
|
|
|
|
2019-04-18 07:01:40 +02:00
|
|
|
event(new UserCreated($user,$this->company));
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2019-03-02 22:44:08 +01:00
|
|
|
|
2018-10-24 05:50:15 +02:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|