<?php
namespace Harmonizely\Service\Panel\AccessVoters;
use Harmonizely\Model\UserInterface;
use Harmonizely\Types\IntegrationType;
class EnabledIntegrationVoter extends AbstractVoter
{
const RESOURCE_NAME = 'enabled_integration';
const ZAPIER = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_ZAPIER;
const ZOOM = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_ZOOM;
const WHEREBY = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_WHEREBY;
const FB_PIXEL = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_FB_PIXEL;
const KOPANO_MEET = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_KOPANO_MEET;
const GA = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_GA;
const SIMPLYPAY = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_SIMPLYPAY;
const SMTP = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_SMTP;
const SMS = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_SMS;
const FLEXIBLE_DURATION = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_FLEXIBLE_DURATION;
const MULTIPLE_MEETING = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_MULTIPLE_MEETING;
const MULTIPLE_EMAIL = self::RESOURCE_NAME . '_' . IntegrationType::TYPE_MULTIPLE_EMAIL;
const EVENT_TYPE_TAG = self::RESOURCE_NAME . '_' . IntegrationType::EVENT_TYPE_TAG;
const ROUTING_FORM = self::RESOURCE_NAME . '_' . IntegrationType::ROUTING_FORM;
/**
* 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::ZAPIER,
self::ZOOM,
self::WHEREBY,
self::FB_PIXEL,
self::KOPANO_MEET,
self::GA,
self::SIMPLYPAY,
self::SMTP,
self::SMS,
self::FLEXIBLE_DURATION,
self::MULTIPLE_MEETING,
self::MULTIPLE_EMAIL,
self::EVENT_TYPE_TAG,
self::ROUTING_FORM
];
}
/**
* Return allowed attributes for current user
*
* @param UserInterface $user
* @param $subject
* @return array|string[]
*/
function getAllowedAttributes(UserInterface $user, $subject): array
{
$allowedAttributes = [];
foreach ($user->getIntegrations() as $integration) {
if ($integration->isEnabled()) {
$allowedAttributes[] = self::RESOURCE_NAME . '_' . $integration->getType();
}
}
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 (!in_array($attribute, $this->getResourceAttributes())) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param UserInterface $user
* @return bool
*/
public static function staticVoteOnAttribute(string $attribute, UserInterface $user): bool
{
$instance = new self();
if (!in_array($attribute, $instance->getResourceAttributes())) {
return false;
}
return in_array($attribute, $instance->getAllowedAttributes($user, null));
}
}