<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\User;
use App\Entity\Admin;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AdminUserVoter extends Voter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const PASSWORD_CHANGE = 'password-change';
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::PASSWORD_CHANGE, self::DELETE, self::BLOCK, self::ACTIVE], true)) {
$supports = false;
}
if (!$subject instanceof User) {
$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 User $entity */
$entity = $subject;
switch ($attribute) {
case self::VIEW:
$hasVotePassed = $this->canView($entity);
break;
case self::EDIT:
case self::PASSWORD_CHANGE:
$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 User $entity
*
* @return bool
*/
private function canView(User $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 User $entity
*
* @return bool
*/
private function canEdit(User $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 User $entity
*
* @return bool
*/
private function canBlock(User $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 User $entity
*
* @return bool
*/
private function canDelete(User $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 User $entity
*
* @return bool
*/
private function canActivate(User $entity): bool
{
return true;
}
}