<?php
namespace App\Security\Voter;
use App\Entity\DeviceGroup;
use App\Entity\User;
use App\Repository\DeviceGroupRepository;
use App\Utils\Commons;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class DeviceGroupVoter extends Voter
{
const DEVICE_GROUP_VIEW = 'DEVICE_GROUP_VIEW';
const DEVICE_GROUP_CREATE = 'DEVICE_GROUP_CREATE';
const DEVICE_GROUP_EDIT = 'DEVICE_GROUP_EDIT';
const DEVICE_GROUP_DELETE = 'DEVICE_GROUP_DELETE';
const DEVICE_GROUP_MULTI_DELETE = 'DEVICE_GROUP_MULTI_DELETE';
const DEVICE_GROUP_SHOW = 'DEVICE_GROUP_SHOW';
/**
* @var Security
*/
private $security;
/**
* @var Commons
*/
private $commons;
/**
* @var array
*/
private $permissions;
/**
* @var DeviceGroupRepository
*/
private $deviceGroupRepository;
/**
* DeviceGroupVoter constructor.
* @param Security $security
* @param Commons $commons
* @param DeviceGroupRepository $deviceGroupRepository
*/
public function __construct(Security $security, Commons $commons, DeviceGroupRepository $deviceGroupRepository)
{
$this->security = $security;
$this->commons = $commons;
$this->deviceGroupRepository = $deviceGroupRepository;
$this->permissions = [];
}
/**
* @param string $attribute
* @param mixed $subject
* @return bool
*/
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute,
[self::DEVICE_GROUP_VIEW, self::DEVICE_GROUP_MULTI_DELETE, self::DEVICE_GROUP_SHOW, self::DEVICE_GROUP_EDIT, self::DEVICE_GROUP_CREATE, self::DEVICE_GROUP_DELETE]
)) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
$this->permissions = $this->commons->getUserPermissions($user);
switch ($attribute) {
case self::DEVICE_GROUP_VIEW:
return $this->canView();
case self::DEVICE_GROUP_CREATE:
return $this->canCreate();
case self::DEVICE_GROUP_EDIT:
return ($this->canEdit() && $this->isOwner($user, $subject));
case self::DEVICE_GROUP_SHOW:
return ($this->canShow() && $this->isOwner($user, $subject));
case self::DEVICE_GROUP_DELETE:
return ($this->canDelete() && $this->isOwner($user, $subject));
case self::DEVICE_GROUP_MULTI_DELETE:
return $this->canMultiDelete();
}
throw new LogicException('Invalid attribute: ' . $attribute);
}
/**
* @param User|null $user
* @param $subject
* @return bool
*/
private function isOwner(User $user, $subject): bool
{
if (!is_object($subject)) {
$subject = $this->deviceGroupRepository->findOneBy(["id" => intval($subject)]);
}
if (!$subject instanceof DeviceGroup) {
return false;
}
return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
}
/** Return True if have View Permission else return false
* @return bool
*/
private function canView(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('deviceGroup', $this->permissions)) {
if (in_array('View', $this->permissions['deviceGroup'])) {
return true;
}
}
return false;
}
/** Return True if have Edit Permission else return false
* @return bool
*/
private function canEdit(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('deviceGroup', $this->permissions)) {
if (in_array('Edit', $this->permissions['deviceGroup'])) {
return true;
}
}
return false;
}
/** Return True if have Edit Permission else return false
* @return bool
*/
private function canShow(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('deviceGroup', $this->permissions)) {
if (in_array('Show', $this->permissions['deviceGroup'])) {
return true;
}
}
return false;
}
/** Return True if have Create Permission else return false
* @return bool
*/
private function canCreate(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('deviceGroup', $this->permissions)) {
if (in_array('Create', $this->permissions['deviceGroup'])) {
return true;
}
}
return false;
}
/** Return True if have Delete Permission else return false
* @return bool
*/
private function canDelete(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('deviceGroup', $this->permissions)) {
if (in_array('Delete', $this->permissions['deviceGroup'])) {
return true;
}
}
return false;
}
/**
* @return bool
*/
private function canMultiDelete(): bool
{
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if (array_key_exists('deviceGroup', $this->permissions)) {
if (in_array('Delete', $this->permissions['deviceGroup'])) {
return true;
}
}
return false;
}
}