245 lines
5.6 KiB
PHP
245 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
/**
|
|
* Access Helper - Функции-хелперы для проверки прав доступа в представлениях
|
|
*
|
|
* Предоставляет простые функции для использования в Twig-шаблонах.
|
|
*/
|
|
|
|
/**
|
|
* Проверка права на действие
|
|
*
|
|
* @param string $action
|
|
* @param string $resource
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('can')) {
|
|
function can(string $action, string $resource): bool
|
|
{
|
|
$access = service('access');
|
|
return $access->can($action, $resource);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка права на просмотр
|
|
*
|
|
* @param string $resource
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('canView')) {
|
|
function canView(string $resource): bool
|
|
{
|
|
return can('view', $resource);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка права на создание
|
|
*
|
|
* @param string $resource
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('canCreate')) {
|
|
function canCreate(string $resource): bool
|
|
{
|
|
return can('create', $resource);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка права на редактирование
|
|
*
|
|
* @param string $resource
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('canEdit')) {
|
|
function canEdit(string $resource): bool
|
|
{
|
|
return can('edit', $resource);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка права на удаление
|
|
*
|
|
* @param string $resource
|
|
* @param bool $any
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('canDelete')) {
|
|
function canDelete(string $resource, bool $any = false): bool
|
|
{
|
|
return can('delete', $resource) || ($any && can('delete_any', $resource));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка роли пользователя
|
|
*
|
|
* @param string|array $roles
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('isRole')) {
|
|
function isRole($roles): bool
|
|
{
|
|
$access = service('access');
|
|
return $access->isRole($roles);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, является ли пользователь владельцем
|
|
*
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('isOwner')) {
|
|
function isOwner(): bool
|
|
{
|
|
return isRole(\App\Services\AccessService::ROLE_OWNER);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, является ли пользователем администратором
|
|
*
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('isAdmin')) {
|
|
function isAdmin(): bool
|
|
{
|
|
return isRole(\App\Services\AccessService::ROLE_ADMIN);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, является ли пользователем менеджером или выше
|
|
*
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('isManager')) {
|
|
function isManager(): bool
|
|
{
|
|
$access = service('access');
|
|
return $access->isManagerOrHigher();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, может ли управлять пользователями
|
|
*
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('canManageUsers')) {
|
|
function canManageUsers(): bool
|
|
{
|
|
return can('manage_users', 'users');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, может ли управлять модулями
|
|
*
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('canManageModules')) {
|
|
function canManageModules(): bool
|
|
{
|
|
return can('manage_modules', 'modules');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Получение текстового названия роли
|
|
*
|
|
* @param string $role
|
|
* @return string
|
|
*/
|
|
if (!function_exists('roleLabel')) {
|
|
function roleLabel(string $role): string
|
|
{
|
|
$access = service('access');
|
|
return $access->getRoleLabel($role);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Получение роли текущего пользователя
|
|
*
|
|
* @return string|null
|
|
*/
|
|
if (!function_exists('currentRole')) {
|
|
function currentRole(): ?string
|
|
{
|
|
$access = service('access');
|
|
return $access->getCurrentRole();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, авторизован ли пользователь в организации
|
|
*
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('isAuthenticatedInOrg')) {
|
|
function isAuthenticatedInOrg(): bool
|
|
{
|
|
$access = service('access');
|
|
return $access->isAuthenticated();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Получение списка доступных ролей для назначения
|
|
*
|
|
* @return array
|
|
*/
|
|
if (!function_exists('availableRolesForAssignment')) {
|
|
function availableRolesForAssignment(): array
|
|
{
|
|
$currentRole = currentRole();
|
|
if (!$currentRole) {
|
|
return [];
|
|
}
|
|
|
|
$access = service('access');
|
|
$roles = $access->getAvailableRolesForAssignment($currentRole);
|
|
|
|
$result = [];
|
|
foreach ($roles as $role) {
|
|
$result[$role] = roleLabel($role);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Проверка, показывать ли кнопку действия
|
|
* Скрывает кнопку для пользователей без прав
|
|
*
|
|
* @param string $action
|
|
* @param string $resource
|
|
* @param bool $showForOwnerAdmin
|
|
* @return bool
|
|
*/
|
|
if (!function_exists('showAction')) {
|
|
function showAction(string $action, string $resource, bool $showForOwnerAdmin = true): bool
|
|
{
|
|
// Если это просмотр - показываем всегда для авторизованных
|
|
if ($action === 'view') {
|
|
return isAuthenticatedInOrg();
|
|
}
|
|
|
|
// Если пользователь - владелец или админ, показываем всё
|
|
if ($showForOwnerAdmin && isManager()) {
|
|
return true;
|
|
}
|
|
|
|
// Иначе проверяем конкретное право
|
|
return can($action, $resource);
|
|
}
|
|
}
|