118 lines
2.7 KiB
PHP
118 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\Explanation;
|
|
use App\Models\User;
|
|
use App\Policies\Concerns\ScopedByDepartment;
|
|
|
|
class ExplanationPolicy
|
|
{
|
|
use ScopedByDepartment;
|
|
|
|
public function viewAny(User $user): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
return $this->canRead($user);
|
|
}
|
|
|
|
public function view(User $user, Explanation $explanation): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->hasFullAccess($user) || $user->hasRole('supervisor')) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->userIsViewer($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($user->hasRole('department_manager')) {
|
|
return $this->userManagesDepartment($user, $explanation->employee?->department_id);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function create(User $user): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->hasFullAccess($user) || $user->hasRole('supervisor')) {
|
|
return true;
|
|
}
|
|
|
|
return $user->hasRole('department_manager');
|
|
}
|
|
|
|
public function update(User $user, Explanation $explanation): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->hasFullAccess($user) || $user->hasRole('supervisor')) {
|
|
return true;
|
|
}
|
|
|
|
if ($user->hasRole('department_manager')) {
|
|
return $this->userManagesDepartment($user, $explanation->employee?->department_id);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function delete(User $user, Explanation $explanation): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->hasFullAccess($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($user->hasRole('department_manager')) {
|
|
return $this->userManagesDepartment($user, $explanation->employee?->department_id);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function restore(User $user, Explanation $explanation): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->hasFullAccess($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($user->hasRole('department_manager')) {
|
|
return $this->userManagesDepartment($user, $explanation->employee?->department_id);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function forceDelete(User $user, Explanation $explanation): bool
|
|
{
|
|
if ($this->isSuperAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
return $this->canForceDelete($user);
|
|
}
|
|
}
|