Passed
Push — master ( 465233...3e7150 )
by Jan
03:11
created

StructureVoter::instanceToPermissionName()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 1
dl 0
loc 21
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Security\Voter;
33
34
35
use App\Entity\AttachmentType;
36
use App\Entity\Category;
37
use App\Entity\Device;
38
use App\Entity\Footprint;
39
use App\Entity\Manufacturer;
40
use App\Entity\Storelocation;
41
use App\Entity\Supplier;
42
use App\Entity\User;
43
44
class StructureVoter extends ExtendedVoter
45
{
46
47
    /**
48
     * Determines if the attribute and subject are supported by this voter.
49
     *
50
     * @param string $attribute An attribute
51
     * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
52
     *
53
     * @return bool True if the attribute and subject are supported, false otherwise
54
     */
55
    protected function supports($attribute, $subject)
56
    {
57
        $permission_name = $this->instanceToPermissionName($subject);
58
        //If permission name is null, then the subject is not supported
59
        return ($permission_name !== null) && $this->resolver->isValidOperation($permission_name, $attribute);
60
61
    }
62
63
    /**
64
     * Maps a instance type to the permission name.
65
     * @param $subject mixed The subject for which the permission name should be generated.
66
     * @return string|null The name of the permission for the subject's type or null, if the subject is not supported.
67
     */
68
    protected function instanceToPermissionName($subject) : ?string
69
    {
70
        $class_name = get_class($subject);
71
        switch ($class_name) {
72
            case AttachmentType::class:
73
                return 'attachment_types';
74
            case Category::class:
75
                return 'categories';
76
            case Device::class:
77
                return 'devices';
78
            case Footprint::class:
79
                return 'footprints';
80
            case Manufacturer::class:
81
                return 'manufacturers';
82
            case Storelocation::class:
83
                return 'storelocations';
84
            case Supplier::class:
85
                return 'suppliers';
86
        }
87
        //When the class is not supported by this class return null
88
        return null;
89
    }
90
91
    /**
92
     * Similar to voteOnAttribute, but checking for the anonymous user is already done.
93
     * The current user (or the anonymous user) is passed by $user.
94
     *
95
     * @param $attribute
96
     * @param $subject
97
     * @param User $user
98
     *
99
     * @return bool
100
     */
101
    protected function voteOnUser($attribute, $subject, User $user): bool
102
    {
103
        $permission_name = $this->instanceToPermissionName($subject);
104
        //Just resolve the permission
105
        return $this->resolver->inherit($user, $permission_name, $attribute) ?? false;
0 ignored issues
show
Bug introduced by
It seems like $permission_name can also be of type null; however, parameter $permission of App\Services\PermissionResolver::inherit() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
        return $this->resolver->inherit($user, /** @scrutinizer ignore-type */ $permission_name, $attribute) ?? false;
Loading history...
106
    }
107
108
109
}