2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Auth;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Events\Auth\UserCreated;
|
|
|
|
use App\Events\Auth\UserCreating;
|
2021-09-07 10:33:34 +03:00
|
|
|
use App\Interfaces\Job\HasOwner;
|
|
|
|
use App\Interfaces\Job\HasSource;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldCreate;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Auth\User;
|
2021-09-06 11:53:57 +03:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-07 10:33:34 +03:00
|
|
|
class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): User
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-08-15 10:11:54 +03:00
|
|
|
event(new UserCreating($this->request));
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2022-07-28 17:31:34 +03:00
|
|
|
if (empty($this->request->get('password', false))) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->request->merge(['password' => Str::random(40)]);
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model = User::create($this->request->input());
|
2020-06-26 13:40:19 +03:00
|
|
|
|
|
|
|
// Upload picture
|
|
|
|
if ($this->request->file('picture')) {
|
|
|
|
$media = $this->getMedia($this->request->file('picture'), 'users');
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->attachMedia($media, 'picture');
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('dashboards')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->dashboards()->attach($this->request->get('dashboards'));
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('permissions')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->permissions()->attach($this->request->get('permissions'));
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('roles')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->roles()->attach($this->request->get('roles'));
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('companies')) {
|
2021-05-15 10:00:14 +03:00
|
|
|
if (app()->runningInConsole() || request()->isInstall()) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->companies()->attach($this->request->get('companies'));
|
2021-05-15 10:00:14 +03:00
|
|
|
} else {
|
2021-05-14 20:43:17 +03:00
|
|
|
$user = user();
|
|
|
|
|
|
|
|
$companies = $user->withoutEvents(function () use ($user) {
|
|
|
|
return $user->companies()->whereIn('id', $this->request->get('companies'))->pluck('id');
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($companies->isNotEmpty()) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->companies()->attach($companies->toArray());
|
2021-05-14 20:43:17 +03:00
|
|
|
}
|
2021-05-14 18:29:24 +03:00
|
|
|
}
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
if (empty($this->model->companies)) {
|
2020-06-26 13:40:19 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
foreach ($this->model->companies as $company) {
|
2020-05-02 16:12:17 +03:00
|
|
|
Artisan::call('user:seed', [
|
2021-09-06 11:53:57 +03:00
|
|
|
'user' => $this->model->id,
|
2020-05-02 16:12:17 +03:00
|
|
|
'company' => $company->id,
|
|
|
|
]);
|
2022-06-28 21:45:14 +03:00
|
|
|
}
|
2022-06-01 10:15:55 +03:00
|
|
|
|
2022-07-28 17:31:34 +03:00
|
|
|
if ($this->shouldSendInvitation()) {
|
2022-06-28 21:45:14 +03:00
|
|
|
$this->dispatch(new CreateInvitation($this->model));
|
2020-05-02 16:12:17 +03:00
|
|
|
}
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
event(new UserCreated($this->model, $this->request));
|
2021-08-15 10:11:54 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
2022-07-28 17:31:34 +03:00
|
|
|
|
|
|
|
protected function shouldSendInvitation()
|
|
|
|
{
|
2022-07-28 20:44:25 +03:00
|
|
|
if (app()->runningUnitTests()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:31:34 +03:00
|
|
|
if (app()->runningInConsole()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request()->isInstall()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|