EntryVoter::supports()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 12
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Security;
22
23
use AppBundle\Entity\Documentation\Entry;
24
use AppBundle\Entity\User;
25
use AppBundle\Service\UserExtensionService;
26
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
27
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
28
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
29
30
class EntryVoter extends Voter
31
{
32
    const MANAGE = 'ENTRY_MANAGE';
33
    const ACCESS = 'ENTRY_ACCESS';
34
    const APPROVE = 'ENTRY_APPROVE';
35
    const REVIEW = 'ENTRY_REVIEW';
36
    const REQUEST_CHANGES = 'ENTRY_REQUEST_CHANGES';
37
38
    private $extensionService;
39
    private $accessDecisionManager;
40
41
    public function __construct(UserExtensionService $extensionService, AccessDecisionManagerInterface $accessDecisionManager) {
42
        $this->extensionService = $extensionService;
43
        $this->accessDecisionManager = $accessDecisionManager;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 View Code Duplication
    protected function supports($attribute, $subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (!$subject instanceof Entry) {
52
            return false;
53
        }
54
55
        if (!in_array($attribute, [self::MANAGE, self::ACCESS, self::APPROVE, self::REVIEW, self::REQUEST_CHANGES], true)) {
56
            return false;
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
66
    {
67
        if (!$subject instanceof Entry) {
68
            return false;
69
        }
70
71
        // los administradores globales siempre tienen permiso
72
        if ($this->extensionService->isUserGlobalAdministrator()) {
73
            return true;
74
        }
75
76
        /** @var User $user */
77
        $user = $token->getUser();
78
79
        if (!$user instanceof User) {
80
            // si el usuario no ha entrado, denegar
81
            return false;
82
        }
83
84
        $organization = $this->extensionService->getCurrentOrganization();
85
86
        // si la carpeta no pertence a la organización actual, denegar
87
        if ($organization !== $subject->getFolder()->getOrganization()) {
88
            return true;
89
        }
90
91
        // si es administrador de la organización, permitir siempre
92
        if ($this->extensionService->isUserLocalAdministrator()) {
93
            return true;
94
        }
95
96
        $table = [
97
            self::ACCESS => FolderVoter::ACCESS,
98
            self::APPROVE => FolderVoter::APPROVE,
99
            self::REVIEW => FolderVoter::REVIEW,
100
            self::REQUEST_CHANGES => FolderVoter::REQUEST_CHANGES,
101
        ];
102
103
        // todos los permisos salvo el de gestión se admiten si se tiene el mismo permiso para la carpeta
104
        if (isset($table[$attribute])) {
105
            return $this->accessDecisionManager->decide($token, [$table[$attribute]], $subject->getFolder());
106
        }
107
108
        // se permite la gestión si es el creador del documento original y no tiene revisión activa
109
        // o si es el creador de la revisión activa
110
        if (self::MANAGE === $attribute) {
111
            return (!$subject->getCurrentVersion() && $subject->getCreatedBy() === $user) ||
112
                ($subject->getCurrentVersion() && $subject->getCurrentVersion()->getCreatedBy() === $user);
113
        }
114
115
        // denegar en otro caso
116
        return false;
117
    }
118
}
119