<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Security\Voter;
use App\Entity\JIT\Account;
use App\Entity\Security\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
/**
* Description of AccountVoter
*
* @author NOUTCHEU Blaise
*/
class AccountVoter extends Voter {
// these strings are just invented: you can use anything
const LIST = 'ROLE_JIT_ACCOUNT_INDEX';
const ADD = 'ROLE_JIT_ACCOUNT_ADD';
const VIEW = 'ROLE_JIT_ACCOUNT_VIEW';
const CLONE = 'ROLE_JIT_ACCOUNT_CLONE';
const EDIT = 'ROLE_JIT_ACCOUNT_EDIT';
const DELETE = 'ROLE_JIT_ACCOUNT_DELETE';
const DEBIT = 'ROLE_JIT_ACCOUNT_DEBIT';
const CREDIT = 'ROLE_JIT_ACCOUNT_CREDIT';
const LISTT = 'ROLE_JIT_TRANSACTION_INDEX';
const VIEWT = 'ROLE_JIT_TRANSACTION_VIEW';
const VIEWM = 'ROLE_JIT_MOVEMENT_VIEW';
private $security;
public function __construct(Security $security) {
$this->security = $security;
}
protected function supports(string $attribute, $subject) {
// only a teacher can add account on his subject
if (in_array($attribute, [
self::LIST,
self::ADD,
self::VIEW,
self::CLONE,
self::EDIT,
self::DELETE,
self::DEBIT,
self::CREDIT,
self::LISTT,
self::VIEWT,
self::VIEWM,
])) {
return true;
}
if (!in_array($attribute, [
// self::VIEW,
])) {
return false;
}
// only vote on `Account` objects
if (!$subject instanceof Account) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token) {
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// ROLE_JIT_ACCOUNT_MANAGE can do anything on account! The power!
if ($this->security->isGranted('ROLE_MANAGER')) {
return true;
}
switch ($attribute) {
case self::ADD:
return $this->canAdd($user);
case self::LIST:
return true;
case self::VIEW:
return $this->canView($subject, $user);
case self::CLONE:
return $this->canClone($subject, $user);
case self::EDIT:
return $this->canEdit($subject, $user);
case self::DELETE:
return $this->canDelete($subject, $user);
case self::DEBIT:
return $this->canDebit($subject, $user);
case self::CREDIT:
return $this->canCredit($subject, $user);
case self::LISTT:
return $this->canListT($user);
case self::VIEWT:
return $this->canViewT($subject, $user);
case self::VIEWM:
return $this->canViewM($subject, $user);
}
throw new LogicException('This code should not be reached!');
}
private function canAdd(User $user): bool {
return true;
}
private function canView(Account $account, User $user) {
//Vérifier si c'est lié à une accountne (ie account null)
// if($account == null){
// return false;
// }
// vérifier que c'est une account de l'utilisateur connecté
if ($user->getAccounts()->contains($account)) {
return true;
}
// vérifier que c'est un employé de l'utilisateur connecté
foreach ($user->getMyEmployees() as $person){
if ($person->getAccounts()->contains($account)) {
return true;
}
}
// // vérifier que c'est un employé de l'utilisateur connecté
// if ($user->getMyEmployees()->contains($account)) {
// return true;
// }
//
// // vérifier que c'est un propriétaire de l'utilisateur connecté
// if ($user->getMyAOwners()->contains($account)) {
// return true;
// }
return false;
}
private function canClone(Account $account, User $user) {
return $this->canAdd($user) && $this->canView($account, $user);
}
private function canEdit(Account $account, User $user) {
//Uniquement le chef d'agence peut modifier une accountne
// if ($user->getMyAgencies()->contains($account->getAgency())) {
// return true;
// }
return false;
}
private function canDelete(Account $account, User $user) {
//Uniquement le chef d'agence peut modifier une accountne
// if ($user->getMyAgencies()->contains($account->getAgency())) {
// return true;
// }
return false;
}
private function canDebit(Account $account, User $user) {
// Tout le monde peut débiter ses comptes
if ($user->getAccounts()->contains($account) && $account->getBalance() >= Account::MIN_AMT) {
return true;
}
return false;
}
private function canCredit(Account $account, User $user) {
return true;
}
private function canListT(User $user) {
return true;
}
private function canViewT(\App\Entity\JIT\Transaction $transaction, User $user) {
// if ($this->security->isGranted('ROLE_COMPTABLE_ADM')) {
// return true;
// }
foreach ($transaction->getMovements() as $movement) {
if ($this->canViewM($movement, $user)) {
return true;
}
}
return false;
}
private function canViewM(\App\Entity\JIT\Movement $movement, User $user) {
// if ($this->security->isGranted('ROLE_COMPTABLE_ADM')) {
// return true;
// }
if ($movement->getAccount() != null) {
return $this->canView($movement->getAccount(), $user);
}
return false;
}
}