<?php
namespace Harmonizely\Service\SuperAdmin\Panel\AccessVoters;
use Harmonizely\Model\UserInterface;
use Harmonizely\Types\SuperAdmin\UserRole;
use Harmonizely\Entity\SuperAdmin\UserEntity;
class CustomerDeletionReasonVoter extends AbstractVoter
{
/**
* Resource name
*/
const RESOURCE_NAME = 'customer_deletion_reason';
/**
* View customers deletion reason
*/
const VIEW = 'customer_deletion_reason_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:
return $this->getResourceAttributes();
default:
return [];
}
}
/**
* 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 UserInterface)) {
return false;
}
if (!in_array($attribute, $this->getResourceAttributes())) {
return false;
}
if (!in_array($attribute, [self::VIEW]) && $subject === null) {
return false;
}
return true;
}
}