2019-03-18 19:05:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Entity\UserSystem\User;
|
2019-03-18 19:05:41 +01:00
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Voter that votes on Part entities.
|
|
|
|
*
|
|
|
|
* See parts permissions for valid operations.
|
|
|
|
*/
|
2019-03-19 17:17:04 +01:00
|
|
|
class PartVoter extends ExtendedVoter
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
2019-03-20 23:24:20 +01:00
|
|
|
public const READ = 'read';
|
2019-03-18 19:05:41 +01:00
|
|
|
|
|
|
|
protected function supports($attribute, $subject)
|
|
|
|
{
|
|
|
|
// replace with your own logic
|
|
|
|
// https://symfony.com/doc/current/security/voters.html
|
|
|
|
//return ($subject instanceof Part || in_array($subject, ['PERM_parts', 'PERM_parts_name']));
|
|
|
|
|
2019-03-20 23:16:07 +01:00
|
|
|
if ($subject instanceof Part) {
|
2019-03-19 19:00:39 +01:00
|
|
|
//Check if a sub permission should be checked -> $attribute has format name.edit
|
2019-03-20 23:16:07 +01:00
|
|
|
if (false !== strpos($attribute, '.')) {
|
2019-03-19 19:00:39 +01:00
|
|
|
[$perm, $op] = explode('.', $attribute);
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-19 19:00:39 +01:00
|
|
|
return in_array($op, $this->resolver->listOperationsForPermission('parts_'.$perm), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return in_array($attribute, $this->resolver->listOperationsForPermission('parts'), false);
|
2019-03-18 19:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:17:04 +01:00
|
|
|
protected function voteOnUser($attribute, $subject, User $user): bool
|
|
|
|
{
|
2019-03-20 23:16:07 +01:00
|
|
|
if ($subject instanceof Part) {
|
2019-03-19 19:00:39 +01:00
|
|
|
//Check for sub permissions
|
2019-03-20 23:16:07 +01:00
|
|
|
if (false !== strpos($attribute, '.')) {
|
2019-03-19 19:00:39 +01:00
|
|
|
[$perm, $op] = explode('.', $attribute);
|
2019-03-20 23:16:07 +01:00
|
|
|
|
|
|
|
return $this->resolver->inherit($user, 'parts_'.$perm, $op) ?? false;
|
2019-03-19 19:00:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
//Null concealing operator means, that no
|
|
|
|
return $this->resolver->inherit($user, 'parts', $attribute) ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Deny access by default.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|