* improved dev fixtures with more diversity, more data, better testcases, user avatars #42 * added customer stats to dashboard, fixed column length for 3 widgets #42 * fixed empty alias - display empty message for new user #42 * unified-ui for "new" toolbar icon #42 * use kimai2_ as database prefix #42 * added customer stats to dashboard, fixed column length for 3 widgets #42 * fix long project/customer names in "currently active" navbar flyout" #42 * added services config for dev environment #42 * added command to run unit tests #42 * added command to run integration tests #42 * added command to run code sniffer #42 * added phpcs and phpunit to composer #42 * created tests directory #42 * fixed code sniffer warnings #42 * added function to switch result type from Pagerfanta to QueryBuilder #42 * dramatically reduced database calls by using custom joined query #42 * added command to install kimai dependencies #42 * added dev:reset command #42
113 lines
2.5 KiB
PHP
113 lines
2.5 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Kimai package.
|
|
*
|
|
* (c) Kevin Papst <kevin@kevinpapst.de>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace TimesheetBundle\Voter;
|
|
|
|
use AppBundle\Entity\User;
|
|
use AppBundle\Voter\AbstractVoter;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use TimesheetBundle\Entity\Customer;
|
|
|
|
/**
|
|
* A voter to check permissions on Customers.
|
|
*
|
|
* @author Kevin Papst <kevin@kevinpapst.de>
|
|
*/
|
|
class CustomerVoter extends AbstractVoter
|
|
{
|
|
const VIEW = 'view';
|
|
const EDIT = 'edit';
|
|
const DELETE = 'delete';
|
|
|
|
/**
|
|
* @param string $attribute
|
|
* @param mixed $subject
|
|
* @return bool
|
|
*/
|
|
protected function supports($attribute, $subject)
|
|
{
|
|
if (!in_array($attribute, array(self::VIEW, self::EDIT, self::DELETE))) {
|
|
return false;
|
|
}
|
|
|
|
if (!$subject instanceof Customer) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param string $attribute
|
|
* @param Customer $subject
|
|
* @param TokenInterface $token
|
|
* @return bool
|
|
*/
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
|
|
{
|
|
$user = $token->getUser();
|
|
|
|
if (!$user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
switch ($attribute) {
|
|
case self::VIEW:
|
|
return $this->canView($subject, $user, $token);
|
|
case self::EDIT:
|
|
return $this->canEdit($subject, $user, $token);
|
|
case self::DELETE:
|
|
return $this->canDelete($token);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param Customer $customer
|
|
* @param User $user
|
|
* @param TokenInterface $token
|
|
* @return bool
|
|
*/
|
|
protected function canView(Customer $customer, User $user, TokenInterface $token)
|
|
{
|
|
if ($this->canEdit($customer, $user, $token)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param Customer $customer
|
|
* @param User $user
|
|
* @param TokenInterface $token
|
|
* @return bool
|
|
*/
|
|
protected function canEdit(Customer $customer, User $user, TokenInterface $token)
|
|
{
|
|
if ($this->canDelete($token)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param TokenInterface $token
|
|
* @return bool
|
|
*/
|
|
protected function canDelete(TokenInterface $token)
|
|
{
|
|
return $this->hasRole('ROLE_ADMIN', $token);
|
|
}
|
|
}
|