Passed
Push — master ( 9d0dde...4d39d5 )
by Jan
08:02
created

ExtendedVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
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\User;
36
use App\Services\PermissionResolver;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
39
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
40
41
/**
42
 * The purpose of this class is, to use the anonymous user from DB in the case, that nobody is logged in.
43
 * @package App\Security\Voter
44
 */
45
abstract class ExtendedVoter extends Voter
46
{
47
    /**
48
     * @var PermissionResolver
49
     */
50
    protected $resolver;
51
52
    protected $entityManager;
53
54
    public function __construct(PermissionResolver $resolver, EntityManagerInterface $entityManager)
55
    {
56
        $this->resolver = $resolver;
57
        $this->entityManager = $entityManager;
58
    }
59
60
    final protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
61
    {
62
        $user = $token->getUser();
63
        // if the user is anonymous, we use the anonymous user.
64
        if (!$user instanceof User) {
65
            $user = $this->entityManager->find(User::class, User::ID_ANONYMOUS);
66
            if($user === null) {
67
                return false;
68
            }
69
        }
70
71
        return $this->voteOnUser($attribute, $subject, $user);
72
    }
73
74
    /**
75
     * Similar to voteOnAttribute, but checking for the anonymous user is already done.
76
     * The current user (or the anonymous user) is passed by $user.
77
     * @param $attribute
78
     * @param $subject
79
     * @param User $user
80
     * @return bool
81
     */
82
    abstract protected function voteOnUser($attribute, $subject, User $user) : bool;
83
}