2020-01-07 17:15:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldUpdate;
|
2020-01-07 17:15:00 +03:00
|
|
|
use App\Models\Common\Dashboard;
|
|
|
|
use App\Traits\Users;
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class UpdateDashboard extends Job implements ShouldUpdate
|
2020-01-07 17:15:00 +03:00
|
|
|
{
|
|
|
|
use Users;
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): Dashboard
|
2020-01-07 17:15:00 +03:00
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->update($this->request->all());
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('users')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->users()->sync($this->request->get('users'));
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
|
|
|
});
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this action is applicable.
|
|
|
|
*/
|
2021-09-06 11:53:57 +03:00
|
|
|
public function authorize(): void
|
2020-01-07 17:15:00 +03:00
|
|
|
{
|
2020-01-11 22:57:19 +03:00
|
|
|
// Can't disable last dashboard for any shared user
|
|
|
|
if ($this->request->has('enabled') && !$this->request->get('enabled')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
foreach ($this->model->users as $user) {
|
2020-01-11 22:57:19 +03:00
|
|
|
if ($user->dashboards()->enabled()->count() > 1) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-01-11 22:57:19 +03:00
|
|
|
$message = trans('dashboards.error.disable_last');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-01-11 22:57:19 +03:00
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('users')) {
|
|
|
|
$user = user();
|
|
|
|
|
|
|
|
if (!in_array($user->id, (array) $this->request->get('users')) && ($user->dashboards()->enabled()->count() == 1)) {
|
|
|
|
$message = trans('dashboards.error.delete_last');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user can access dashboard
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->isNotUserDashboard($this->model->id)) {
|
2020-01-07 17:15:00 +03:00
|
|
|
$message = trans('dashboards.error.not_user_dashboard');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|