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\UserDeleted;
|
|
|
|
use App\Events\Auth\UserDeleting;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldDelete;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class DeleteUser extends Job implements ShouldDelete
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): bool
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
event(new UserDeleting($this->model));
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2022-06-29 10:48:57 +03:00
|
|
|
$this->deleteRelationships($this->model, ['invitation']);
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->delete();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->flushCache();
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
event(new UserDeleted($this->model));
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 delete yourself
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->model->id == user()->id) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('auth.error.self_delete');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|