akaunting/app/Jobs/Auth/UpdateUser.php

122 lines
4.0 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
2021-08-15 10:11:54 +03:00
use App\Events\Auth\UserUpdated;
use App\Events\Auth\UserUpdating;
2022-06-01 10:15:55 +03:00
use App\Interfaces\Job\ShouldUpdate;
2019-11-16 10:21:14 +03:00
use App\Models\Auth\User;
2022-06-01 10:15:55 +03:00
use App\Models\Common\Company;
use Illuminate\Support\Facades\Artisan;
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
class UpdateUser extends Job implements ShouldUpdate
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
{
$this->authorize();
// Do not reset password if not entered/changed
if (empty($this->request['password'])) {
2022-06-01 10:15:55 +03:00
unset($this->request['current_password']);
2019-11-16 10:21:14 +03:00
unset($this->request['password']);
unset($this->request['password_confirmation']);
}
2021-09-06 11:53:57 +03:00
event(new UserUpdating($this->model, $this->request));
2021-08-15 10:11:54 +03:00
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-09-06 11:53:57 +03:00
$this->model->update($this->request->input());
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Upload picture
if ($this->request->file('picture')) {
$this->deleteMediaModel($this->model, 'picture', $this->request);
2020-06-26 13:40:19 +03:00
$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');
} elseif (! $this->request->file('picture') && $this->model->picture) {
$this->deleteMediaModel($this->model, 'picture', $this->request);
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('roles')) {
2021-09-06 11:53:57 +03:00
$this->model->roles()->sync($this->request->get('roles'));
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('companies')) {
2021-05-15 10:00:14 +03:00
if (app()->runningInConsole() || request()->isInstall()) {
2022-06-01 10:15:55 +03:00
$sync = $this->model->companies()->sync($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()) {
2022-06-01 10:15:55 +03:00
$sync = $this->model->companies()->sync($companies->toArray());
2021-05-15 10:00:14 +03:00
}
2021-05-14 18:29:24 +03:00
}
2020-06-26 13:40:19 +03:00
}
2021-04-29 11:43:52 +03:00
2021-09-06 11:53:57 +03:00
if ($this->model->contact) {
$this->model->contact->update($this->request->input());
2021-04-29 11:43:52 +03:00
}
2022-06-01 10:15:55 +03:00
if (isset($sync) && !empty($sync['attached'])) {
foreach ($sync['attached'] as $id) {
$company = Company::find($id);
Artisan::call('user:seed', [
'user' => $this->model->id,
'company' => $company->id,
]);
}
}
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 UserUpdated($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
}
/**
* Determine if this action is applicable.
*/
2021-09-06 11:53:57 +03:00
public function authorize(): void
2019-11-16 10:21:14 +03:00
{
// Can't disable yourself
2021-09-06 11:53:57 +03:00
if (($this->request->get('enabled', 1) == 0) && ($this->model->id == user()->id)) {
2019-11-16 10:21:14 +03:00
$message = trans('auth.error.self_disable');
throw new \Exception($message);
}
// Can't unassigned company, The company must be assigned at least one user.
if ($this->request->has('companies')) {
$companies = (array) $this->request->get('companies', []);
$user_companies = $this->model->companies()->pluck('id')->toArray();
$company_diff = array_diff($user_companies, $companies);
if ($company_diff) {
$errors = [];
foreach ($company_diff as $company_id) {
$company = Company::withCount('users')->find($company_id);
if ($company->users_count < 2) {
$errors[] = trans('auth.error.unassigned', ['company' => $company->name]);
}
}
if ($errors) {
$message = implode('\n', $errors);
throw new \Exception($message);
}
}
}
2019-11-16 10:21:14 +03:00
}
}