Completed
Push — feature/OPTIONS_4_sf28_update ( 50ec82...389dad )
by
unknown
08:36
created

OwnContextVoter::supportsAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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')
22
     */
23
    protected function getSupportedClasses()
24
    {
25
        return array(
26
            'GravitonDyn\AccountBundle\Document\Account',
27
            'GravitonDyn\CustomerBundle\Document\Customer',
28
        );
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')
35
     */
36
    protected function getSupportedAttributes()
37
    {
38
        return array(
39
            'VIEW',
40
            'CREATE',
41
            'EDIT',
42
            'DELETE',
43
        );
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.
56
     *
57
     * @return bool
58
     */
59
    protected function isGranted($attribute, $object, $user = null)
60
    {
61
        if (null === $user || !($user instanceof \Graviton\SecurityBundle\Entities\SecurityContract)) {
0 ignored issues
show
Bug introduced by
The class Graviton\SecurityBundle\Entities\SecurityContract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
62
            return false;
63
        }
64
65
        /** @var \GravitonDyn\ContractBundle\Document\Contract $contract */
66
        $contract = $user->getContract();
67
68
        return $this->grantByAccount($contract, $object)
69
            || $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
    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
        if ($object instanceof \GravitonDyn\AccountBundle\Document\Account) {
0 ignored issues
show
Bug introduced by
The class GravitonDyn\AccountBundle\Document\Account does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
84
            return $contract->getAccount()->contains($object);
85
        }
86
87
        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
    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
        if ($object instanceof \GravitonDyn\CustomerBundle\Document\Customer) {
0 ignored issues
show
Bug introduced by
The class GravitonDyn\CustomerBundle\Document\Customer does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
101
            return $contract->getCustomer() == $object;
102
        }
103
104
        return false;
105
    }
106
}
107