PermissionService::canViewNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace AppBundle\Service;
3
4
use AppBundle\Entity\Node;
5
use AppBundle\Security\NodeVoter;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
7
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8
9
class PermissionService
10
{
11
    /** @var AuthorizationChecker */
12
    private $authorizationChecker;
13
14
    public function __construct(AuthorizationChecker $authorizationChecker)
15
    {
16
        $this->authorizationChecker = $authorizationChecker;
17
    }
18
19
    public function canEditNode(Node $node)
20
    {
21
        $this->denyAccessUnlessGranted(NodeVoter::EDIT, $node, 'you don\'t have permissions to edit this datanode');
22
    }
23
24
    public function canViewNode(Node $node)
25
    {
26
        $this->denyAccessUnlessGranted(NodeVoter::VIEW, $node, 'you don\'t have permissions to view this datanode');
27
    }
28
29
    protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
30
    {
31
        if (!$this->authorizationChecker->isGranted($attributes, $object)) {
32
            $exception = new AccessDeniedException($message);
33
            $exception->setAttributes($attributes);
34
            $exception->setSubject($object);
35
36
            throw $exception;
37
        }
38
    }
39
}
40