Completed
Push — feature/OPTIONS_4_sf28_update ( 830dee...692213 )
by
unknown
09:05
created

OwnContextVoter::isGranted()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.2
cc 4
eloc 6
nc 3
nop 3
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\Voter;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class OwnContextVoter extends Voter
18
{
19
    /**
20
     * Return an array of supported classes. This will be called by supportsClass
21
     *
22
     * @return array an array of supported classes, i.e. array('Acme\DemoBundle\Model\Product')
23
     */
24
    protected function getSupportedClasses()
25
    {
26
        return array(
27
            'GravitonDyn\AccountBundle\Document\Account',
28
            'GravitonDyn\CustomerBundle\Document\Customer',
29
        );
30
    }
31
32
    /**
33
     * Return an array of supported attributes. This will be called by supportsAttribute
34
     *
35
     * @return array an array of supported attributes, i.e. array('CREATE', 'READ')
36
     */
37
    protected function getSupportedAttributes()
38
    {
39
        return array(
40
            'VIEW',
41
            'CREATE',
42
            'EDIT',
43
            'DELETE',
44
        );
45
    }
46
47
    /**
48
     * Perform a single access check operation on a given attribute, object and (optionally) user
49
     * It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass
50
     * $user can be one of the following:
51
     *   a UserInterface object (fully authenticated user)
52
     *   a string               (anonymously authenticated user)
53
     *
54
     * @param string               $attribute The attribute to be checked against.
55
     * @param object               $object    The object the access shall be granted for.
56
     * @param UserInterface|string $user      The user asking for permission.
57
     *
58
     * @return bool
59
     */
60
    protected function isGranted($attribute, $object, $user = null)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        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...
63
            return false;
64
        }
65
66
        /** @var \GravitonDyn\ContractBundle\Document\Contract $contract */
67
        $contract = $user->getContract();
68
69
        return $this->grantByAccount($contract, $object)
70
            || $this->grantByCustomer($contract, $object);
71
    }
72
73
74
    /**
75
     * Determines, if the given object is of type Account and if it in the set of accounts related to the contract.
76
     *
77
     * @param Contract $contract The current contract identified by provided the access token.
78
     * @param mixed    $object   The object to be handled
79
     *
80
     * @return bool
81
     */
82
    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...
83
    {
84
        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...
85
            return $contract->getAccount()->contains($object);
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * Determines, if the given object is of type Customer and if it is related to the contract.
93
     * todo Remove depency on external classes not present in Graviton StandAlone.
94
     *
95
     * @param Contract $contract The current contract identified by provided the access token.
96
     * @param mixed    $object   The object to be handled
97
     *
98
     * @return bool
99
     */
100
    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...
101
    {
102
        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...
103
            return $contract->getCustomer() == $object;
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * todo Implement new voter attribute since symfony 2.8
111
     *
112
     * @param string         $attribute Object attribute
113
     * @param mixed          $subject   Subject to be supported
114
     * @param TokenInterface $token     Token
115
     *
116
     * @return bool
117
     */
118
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
119
    {
120
        return true;
121
    }
122
123
    /**
124
     * @param string $attribute To be supported
125
     * @return void
126
     * @throws \BadMethodCallException
127
     */
128
    public function supportsAttribute($attribute)
129
    {
130
        $msg = 'supportsAttribute method is deprecated since version 2.8, to be removed in 3.0';
131
        throw new \BadMethodCallException($msg);
132
    }
133
134
    /**
135
     * @param string $class to be supported
136
     * @return void
137
     * @throws \BadMethodCallException
138
     */
139
    public function supportsClass($class)
140
    {
141
        $msg = 'supportsClass method is deprecated since version 2.8, to be removed in 3.0';
142
        throw new \BadMethodCallException($msg);
143
    }
144
145
    /**
146
     * Determines if the attribute and subject are supported by this voter.
147
     * todo implement voters
148
     *
149
     * @param string $attribute An attribute
150
     * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
151
     *
152
     * @return bool True if the attribute and subject are supported, false otherwise
153
     */
154
    protected function supports($attribute, $subject)
155
    {
156
        return true;
157
    }
158
}
159