<?php
namespace Harmonizely\Service\SuperAdmin\Panel\AccessVoters;
use Harmonizely\Entity\SuperAdmin\UserActionLogEntity;
use Harmonizely\Types\SuperAdmin\UserRole;
use Harmonizely\Entity\SuperAdmin\UserEntity;
class UserActionLogVoter extends AbstractVoter
{
/**
* Resource name
*/
const RESOURCE_NAME = 'user_action_log';
/**
* View user action log
*/
const VIEW = 'user_action_log_view';
/**
* Return resource name
*
* @return string
*/
function getResourceName(): string
{
return self::RESOURCE_NAME;
}
/**
* Return allowed attributes for current user
*
* @return array|string[]
*/
function getResourceAttributes(): array
{
return [self::VIEW];
}
/**
* Return allowed attributes for current user
*
* @param UserEntity $user
* @return array|string[]
*/
function getAllowedAttributes(UserEntity $user): array
{
switch ($user->getRole()) {
case UserRole::ROLE_ADMIN:
case UserRole::ROLE_SYSTEM_USER:
$allowedAttributes = $this->getResourceAttributes();
break;
default:
$allowedAttributes = [];
}
return $allowedAttributes;
}
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
protected function supports($attribute, $subject): bool
{
if ($subject !== null && !($subject instanceof UserActionLogEntity)) {
return false;
}
if (!in_array($attribute, $this->getResourceAttributes())) {
return false;
}
if (!in_array($attribute, [self::VIEW]) && $subject === null) {
return false;
}
return true;
}
}