OrganizationVoter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 13 3
C voteOnAttribute() 0 39 11
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\Membership;
24
use AppBundle\Entity\Organization;
25
use AppBundle\Entity\User;
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 OrganizationVoter extends Voter
31
{
32
    const MANAGE = 'ORGANIZATION_MANAGE';
33
    const ACCESS = 'ORGANIZATION_ACCESS';
34
35
    private $decisionManager;
36
37
    public function __construct(AccessDecisionManagerInterface $decisionManager) {
38
        $this->decisionManager = $decisionManager;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function supports($attribute, $subject)
45
    {
46
47
        if (!$subject instanceof Organization) {
48
            return false;
49
        }
50
51
        if (!in_array($attribute, [self::MANAGE, self::ACCESS], true)) {
52
            return false;
53
        }
54
55
        return true;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
62
    {
63
        if (!$subject instanceof Organization) {
64
            return false;
65
        }
66
67
        // los administradores globales siempre tienen permiso
68
        if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) {
69
            return true;
70
        }
71
72
        /** @var User $user */
73
        $user = $token->getUser();
74
75
        if (!$user instanceof User) {
76
            // si el usuario no ha entrado, denegar
77
            return false;
78
        }
79
80
        // Si es administrador de la organización, permitir siempre
81
        if ($user->getManagedOrganizations()->contains($subject)) {
82
            return true;
83
        }
84
85
        // Si es permiso de acceso, comprobar que pertenece actualmente a la organización
86
        if ($attribute === self::ACCESS) {
87
88
            $date = new \DateTime();
89
            /** @var Membership $membership */
90
            foreach ($user->getMemberships() as $membership) {
91
                if ($membership->getOrganization() == $subject && $membership->getValidFrom() <= $date && ($membership->getValidUntil() === null || $membership->getValidUntil() >= $date)) {
92
                    return true;
93
                }
94
            }
95
        }
96
97
        // denegamos en cualquier otro caso
98
        return false;
99
    }
100
}
101