mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-30 07:45:36 +02:00
Added a simple Voter for checking, if a user is allowed, to view/edit/create a part.
This commit is contained in:
parent
ab3f5db174
commit
01e1f27b68
10 changed files with 750 additions and 4 deletions
62
src/Security/Voter/PartVoter.php
Normal file
62
src/Security/Voter/PartVoter.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace App\Security\Voter;
|
||||
|
||||
use App\Configuration\PermissionsConfiguration;
|
||||
use App\Entity\Part;
|
||||
use App\Entity\User;
|
||||
use App\Services\PermissionResolver;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
|
||||
/**
|
||||
* A Voter that votes on Part entities.
|
||||
*
|
||||
* See parts permissions for valid operations.
|
||||
*
|
||||
* @package App\Security\Voter
|
||||
*/
|
||||
class PartVoter extends Voter
|
||||
{
|
||||
const READ = "read";
|
||||
|
||||
protected $resolver;
|
||||
|
||||
public function __construct(PermissionResolver $resolver)
|
||||
{
|
||||
$this->resolver = $resolver;
|
||||
}
|
||||
|
||||
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']));
|
||||
|
||||
if ($subject instanceof Part)
|
||||
{
|
||||
return in_array($attribute, $this->resolver->listOperationsForPermission('parts'), false);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
|
||||
{
|
||||
$user = $token->getUser();
|
||||
// if the user is anonymous, do not grant access
|
||||
if (!$user instanceof User) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if($subject instanceof Part) {
|
||||
//Null concealing operator means, that no
|
||||
return $this->resolver->inherit($user, 'parts', $attribute) ?? false;
|
||||
}
|
||||
|
||||
//Deny access by default.
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue