51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Applications\Application;
|
|
use App\Models\Objects\NirObject;
|
|
use App\Models\Pages\Page;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class PermissionsService {
|
|
private Model $model;
|
|
private ?User $user;
|
|
|
|
private array $rules = [
|
|
NirObject::class => 'nirObject',
|
|
Application::class => 'application',
|
|
Page::class => 'page'
|
|
];
|
|
|
|
public function __construct(Model $model, ?User $user = null) {
|
|
$this->model = $model;
|
|
$this->user = $user ?? Auth::user();
|
|
}
|
|
|
|
public function get(): array {
|
|
$result = [];
|
|
if ($this->user) {
|
|
$rule = $this->rules[get_class($this->model)] ?? null;
|
|
$func = "{$rule}Permissions";
|
|
$result = method_exists($this, $func) ? $this->$func() : [];
|
|
if ($this->user->isAdmin) $result['anything'] = true;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
public function nirObjectPermissions(): array {
|
|
return ['edit' => $this->model->owner_id === $this->user->id];
|
|
}
|
|
|
|
public function applicationPermissions(): array {
|
|
return ['edit' => $this->model->submitter_id === $this->user->id, 'reply' => $this->model->isExpert($this->user), 'manage' => $this->user->isApplicationsManager];
|
|
}
|
|
|
|
public function pagePermissions(): array {
|
|
return ['edit' => $this->model->isEditable($this->user)];
|
|
}
|
|
|
|
} |