akaunting/app/Jobs/Auth/CreateUser.php

73 lines
1.8 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Models\Auth\User;
use Artisan;
class CreateUser extends Job
{
2020-06-26 13:40:19 +03:00
protected $user;
2019-11-16 10:21:14 +03:00
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Permission
*/
public function handle()
{
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->user = User::create($this->request->input());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'users');
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
$this->user->attachMedia($media, 'picture');
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
if ($this->request->has('dashboards')) {
$this->user->dashboards()->attach($this->request->get('dashboards'));
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
if ($this->request->has('permissions')) {
$this->user->permissions()->attach($this->request->get('permissions'));
}
2020-01-07 17:15:00 +03:00
2020-06-26 13:40:19 +03:00
if ($this->request->has('roles')) {
$this->user->roles()->attach($this->request->get('roles'));
}
2020-01-07 17:15:00 +03:00
2020-06-26 13:40:19 +03:00
if ($this->request->has('companies')) {
$this->user->companies()->attach($this->request->get('companies'));
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
if (empty($this->user->companies)) {
return;
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
foreach ($this->user->companies as $company) {
Artisan::call('user:seed', [
2020-06-26 13:40:19 +03:00
'user' => $this->user->id,
'company' => $company->id,
]);
}
2020-06-26 13:40:19 +03:00
});
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
return $this->user;
2019-11-16 10:21:14 +03:00
}
}