68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Utilities;
|
|
|
|
use App\Models\Auth\User;
|
|
|
|
class AgreementSigningPermissions
|
|
{
|
|
public const SETTING_KEY = 'system.agreement_signing_user_ids';
|
|
|
|
public static function grantedUserIds(): array
|
|
{
|
|
$ids = setting(self::SETTING_KEY, []);
|
|
if (is_string($ids)) {
|
|
$ids = json_decode($ids, true) ?: [];
|
|
}
|
|
|
|
return array_values(array_unique(array_map('intval', is_array($ids) ? $ids : [])));
|
|
}
|
|
|
|
public static function setGranted(User $target, bool $granted): void
|
|
{
|
|
$ids = array_values(array_diff(self::grantedUserIds(), [$target->id]));
|
|
if ($granted) {
|
|
$ids[] = (int) $target->id;
|
|
}
|
|
setting()->set(self::SETTING_KEY, json_encode($ids));
|
|
setting()->save();
|
|
}
|
|
|
|
public static function canOperate(?User $actor = null): bool
|
|
{
|
|
$actor ??= user();
|
|
if (! $actor || ! $actor->enabled) {
|
|
return false;
|
|
}
|
|
|
|
$role = SystemAccess::roleName($actor);
|
|
$granted = in_array($role, [SystemAccess::SUPER_ADMIN, SystemAccess::ADMIN], true)
|
|
|| ($role === SystemAccess::USER && in_array((int) $actor->id, self::grantedUserIds(), true));
|
|
if (! $granted || ! $actor->companies()->where('companies.id', company_id())->exists()) {
|
|
return false;
|
|
}
|
|
|
|
$section = collect(SidebarPermissions::navigationFor($actor))->firstWhere('slug', 'courier-payables');
|
|
|
|
return collect($section['pages'] ?? [])
|
|
->flatMap(fn (array $page): array => [$page, ...($page['children'] ?? [])])
|
|
->contains('slug', 'agreement-signing');
|
|
}
|
|
|
|
public static function canAdminister(?User $actor = null): bool
|
|
{
|
|
$actor ??= user();
|
|
|
|
return $actor && SystemAccess::canOpenSettings($actor) && self::canOperate($actor);
|
|
}
|
|
|
|
public static function canDelete(?User $actor = null): bool
|
|
{
|
|
$actor ??= user();
|
|
|
|
return $actor
|
|
&& in_array(trim((string) $actor->name), ['李斯', '人参', '苏木'], true)
|
|
&& self::canOperate($actor);
|
|
}
|
|
}
|