Completed
Push — feature/EVO-4597-rabbitmq-hand... ( 6e503b...616297 )
by
unknown
52:22 queued 46:35
created

OwnContextVoter::getSupportedAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Voter deciding, if the provided object is
4
 */
5
namespace Graviton\SecurityBundle\Voter;
6
7
use GravitonDyn\ContractBundle\Document\Contract;
8
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class OwnContextVoter extends AbstractVoter
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Securi...ion\Voter\AbstractVoter has been deprecated with message: since version 2.8, to be removed in 3.0. Upgrade to Symfony\Component\Security\Core\Authorization\Voter\Voter instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
17
{
18
    /**
19
     * Return an array of supported classes. This will be called by supportsClass
20
     *
21
     * @return array an array of supported classes, i.e. array('Acme\DemoBundle\Model\Product')
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
22
     */
23 1
    protected function getSupportedClasses()
24
    {
25
        return array(
26 1
            'GravitonDyn\AccountBundle\Document\Account',
27 1
            'GravitonDyn\CustomerBundle\Document\Customer',
28 1
        );
29
    }
30
31
    /**
32
     * Return an array of supported attributes. This will be called by supportsAttribute
33
     *
34
     * @return array an array of supported attributes, i.e. array('CREATE', 'READ')
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
35
     */
36 1
    protected function getSupportedAttributes()
37
    {
38
        return array(
39 1
            'VIEW',
40 1
            'CREATE',
41 1
            'EDIT',
42 1
            'DELETE',
43 1
        );
44
    }
45
46
    /**
47
     * Perform a single access check operation on a given attribute, object and (optionally) user
48
     * It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass
49
     * $user can be one of the following:
50
     *   a UserInterface object (fully authenticated user)
51
     *   a string               (anonymously authenticated user)
52
     *
53
     * @param string               $attribute The attribute to be checked against.
54
     * @param object               $object    The object the access shall be granted for.
55
     * @param UserInterface|string $user      The user asking for permission.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user not be UserInterface|string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
     *
57
     * @return bool
58
     */
59 3
    protected function isGranted($attribute, $object, $user = null)
60
    {
61 3
        if (null === $user || !($user instanceof \Graviton\SecurityBundle\Entities\SecurityContract)) {
62 2
            return false;
63
        }
64
65
        /** @var \GravitonDyn\ContractBundle\Document\Contract $contract */
66 1
        $contract = $user->getContract();
67
68 1
        return $this->grantByAccount($contract, $object)
69 1
            || $this->grantByCustomer($contract, $object);
70
    }
71
72
73
    /**
74
     * Determines, if the given object is of type Account and if it in the set of accounts related to the contract.
75
     *
76
     * @param Contract $contract The current contract identified by provided the access token.
77
     * @param mixed    $object   The object to be handled
78
     *
79
     * @return bool
80
     */
81 3
    protected function grantByAccount(Contract $contract, $object)
0 ignored issues
show
Coding Style introduced by
function grantByAccount() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
82
    {
83 3
        if ($object instanceof \GravitonDyn\AccountBundle\Document\Account) {
84 1
            return $contract->getAccount()->contains($object);
85
        }
86
87 2
        return false;
88
    }
89
90
    /**
91
     * Determines, if the given object is of type Customer and if it is related to the contract.
92
     *
93
     * @param Contract $contract The current contract identified by provided the access token.
94
     * @param mixed    $object   The object to be handled
95
     *
96
     * @return bool
97
     */
98 3
    protected function grantByCustomer(Contract $contract, $object)
0 ignored issues
show
Coding Style introduced by
function grantByCustomer() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
99
    {
100 3
        if ($object instanceof \GravitonDyn\CustomerBundle\Document\Customer) {
101 1
            return $contract->getCustomer() == $object;
102
        }
103
104 2
        return false;
105
    }
106
}
107