<?php
/**
* Created by Logics Tecnologia e Serviços LTDA.
* @author: Romeu Godoi <romeu@logics.com.br>
* Date: 04/12/19
* Time: 11:56
* @copyright Copyright (C) 2019 LogicSITE. Todos os Direitos Reservados.
* LogicSITE. Este software é de propriedade exclusiva da LOGICS TEC. E SERV. LTDA
* e seu uso só pode ser dado por usuários licenciados por escrito.
* O uso indevido desta plataforma, ou parte dela estará sujeita a penalidades
* previstas em lei, conforme legislação pertinente.
*/
namespace App\Security;
use App\Application\Sonata\UserBundle\Entity\User;
use App\Entity\Cliente;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
/**
* Class UserCRUDVoter
* @package App\Security
*/
class ClienteCRUDVoter extends Voter
{
const LIST = 'ROLE_ADMIN_CLIENTE_EDIT';
const VIEW = 'ROLE_ADMIN_CLIENTE_VIEW';
const CREATE = 'ROLE_ADMIN_CLIENTE_CREATE';
const EDIT = 'ROLE_SONATA_USER_ADMIN_USER_EDIT';
const DELETE = 'ROLE_ADMIN_CLIENTE_DELETE';
const ALL = 'ROLE_ADMIN_CLIENTE_ALL';
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* 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)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::LIST, self::VIEW, self::CREATE, self::EDIT, self::DELETE, self::ALL])) {
return false;
}
// only vote on Cliente objects inside this voter
if (!$subject instanceof Cliente) {
return false;
}
return true;
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$userLogged = $token->getUser();
if (!$userLogged instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// Se for um super admin do sistema retorna verdadeiro
if ($userLogged->isSuperAdmin()) {
return true;
}
// you know $subject is a User object, thanks to supports
/** @var User $obj */
$obj = $subject;
switch ($attribute) {
case self::LIST:
return $this->canList($obj, $userLogged);
case self::VIEW:
return $this->canView($obj, $userLogged);
case self::CREATE:
return $this->canCreate($obj, $userLogged);
case self::EDIT:
return $this->canEdit($obj, $userLogged);
case self::DELETE:
return $this->canDelete($obj, $userLogged);
case self::ALL:
return $this->canAll($obj, $userLogged);
}
throw new \LogicException('This code should not be reached!');
}
private function canList(Cliente $obj, User $userLogged)
{
// if they can edit, they can view
if ($this->canEdit($obj, $userLogged)) {
return true;
}
return false;
}
private function canCreate(Cliente $obj, User $userLogged)
{
return true;
}
private function canView(Cliente $obj, User $userLogged)
{
return true;
}
private function canEdit(Cliente $obj, User $userLogged)
{
/** @var User $userLogged */
$userLogged = $this->security->getUser();
$franquia = $userLogged ? $userLogged->getFranquia() : null;
return $obj->getFranquia() == $franquia;
}
private function canDelete(Cliente $obj, User $userLogged)
{
// if they can edit, they can view
if ($this->canEdit($obj, $userLogged)) {
return true;
}
return false;
}
private function canAll(Cliente $obj, User $userLogged)
{
return $this->canDelete($obj, $userLogged);
}
}