<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Organization;
use App\Entity\Admin;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AdminOrganizationVoter extends Voter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const DELETE = 'delete';
public const BLOCK = 'block';
public const ACTIVE = 'active';
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
$supports = true;
if (!\in_array($attribute, [self::VIEW, self::EDIT, self::DELETE, self::BLOCK, self::ACTIVE], true)) {
$supports = false;
}
if (!$subject instanceof Organization) {
$supports = false;
}
return $supports;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
$hasVotePassed = false;
if (!$user instanceof Admin) {
return $hasVotePassed;
}
/** @var Organization $entity */
$entity = $subject;
switch ($attribute) {
case self::VIEW:
$hasVotePassed = $this->canView($entity);
break;
case self::EDIT:
$hasVotePassed = $this->canEdit($entity);
break;
case self::BLOCK:
$hasVotePassed = $this->canBlock($entity);
break;
case self::DELETE:
$hasVotePassed = $this->canDelete($entity);
break;
case self::ACTIVE:
$hasVotePassed = $this->canActivate($entity);
break;
}
return $hasVotePassed;
}
/**
* @param Organization $entity
*
* @return bool
*/
private function canView(Organization $entity): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param Organization $entity
*
* @return bool
*/
private function canEdit(Organization $entity): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param Organization $entity
*
* @return bool
*/
private function canBlock(Organization $entity): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param Organization $entity
*
* @return bool
*/
private function canDelete(Organization $entity): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param Organization $entity
*
* @return bool
*/
private function canActivate(Organization $entity): bool
{
return true;
}
}