akaunting/app/Jobs/Auth/CreateUser.php

101 lines
2.9 KiB
PHP
Raw Normal View History

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 () {
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 {
$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 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) {
Artisan::call('user:seed', [
2021-09-06 11:53:57 +03:00
'user' => $this->model->id,
'company' => $company->id,
]);
2022-06-28 21:45:14 +03:00
}
2022-06-01 10:15:55 +03:00
if ($this->shouldSendInvitation()) {
2022-06-28 21:45:14 +03:00
$this->dispatch(new CreateInvitation($this->model));
}
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
}
protected function shouldSendInvitation()
{
2022-07-28 20:44:25 +03:00
if (app()->runningUnitTests()) {
return true;
}
if (app()->runningInConsole()) {
return false;
}
if (request()->isInstall()) {
return false;
}
return true;
}
2019-11-16 10:21:14 +03:00
}