akaunting/app/Jobs/Auth/UpdateUser.php

82 lines
1.9 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;
class UpdateUser extends Job
{
protected $user;
protected $request;
/**
* Create a new job instance.
*
* @param $user
* @param $request
*/
public function __construct($user, $request)
{
$this->user = $user;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return User
*/
public function handle()
{
$this->authorize();
// Do not reset password if not entered/changed
if (empty($this->request['password'])) {
unset($this->request['password']);
unset($this->request['password_confirmation']);
}
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->user->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')) {
$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('roles')) {
$this->user->roles()->sync($this->request->get('roles'));
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
if ($this->request->has('companies')) {
$this->user->companies()->sync($this->request->get('companies'));
}
2021-04-29 11:43:52 +03:00
if ($this->user->contact) {
$this->user->contact->update($this->request->input());
}
2020-06-26 13:40:19 +03:00
});
2019-11-16 10:21:14 +03:00
return $this->user;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
// Can't disable yourself
if (($this->request->get('enabled', 1) == 0) && ($this->user->id == user()->id)) {
$message = trans('auth.error.self_disable');
throw new \Exception($message);
}
}
}