|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2016: 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\Agreement; |
|
24
|
|
|
use AppBundle\Entity\User; |
|
25
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
26
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
|
27
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
28
|
|
|
|
|
29
|
|
|
class UserVoter extends Voter |
|
30
|
|
|
{ |
|
31
|
|
|
const TRACK = 'USER_TRACK'; |
|
32
|
|
|
const MANAGE = 'USER_MANAGE'; |
|
33
|
|
|
|
|
34
|
|
|
private $decisionManager; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(AccessDecisionManagerInterface $decisionManager) { |
|
37
|
|
|
$this->decisionManager = $decisionManager; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
protected function supports($attribute, $subject) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
if (!$subject instanceof User) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!in_array($attribute, [self::MANAGE, self::TRACK], true)) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function voteOnTrack(User $subject, TokenInterface $token) |
|
57
|
|
|
{ |
|
58
|
|
|
$user = $token->getUser(); |
|
59
|
|
|
|
|
60
|
|
|
// si no es estudiante, denegar |
|
61
|
|
|
if ($subject->getStudentGroup() === null) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// los administradores globales siempre tienen permiso |
|
66
|
|
|
if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) { |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// los jefes de departamento de los grupos a los que pertenece un estudiante |
|
71
|
|
|
// tienen permisos de seguimiento |
|
72
|
|
|
if ($subject->getStudentGroup()->getTraining()->getDepartment()->getHead() === $user) { |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// los tutores del grupo al que pertenece el estudiante tienen permisos de seguimiento |
|
77
|
|
|
if ($subject->getStudentGroup()->getTutors()->contains($user)) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// los tutores docentes de los acuerdos que tiene el estudiante tienen permisos de seguimiento |
|
82
|
|
|
if ($subject->getStudentAgreements()) { |
|
83
|
|
|
/** @var Agreement $agreement */ |
|
84
|
|
|
foreach ($subject->getStudentAgreements() as $agreement) { |
|
85
|
|
|
if ($agreement->getEducationalTutor() === $user || $agreement->getWorkTutor() === $user) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function voteOnManage(User $subject, TokenInterface $token) |
|
95
|
|
|
{ |
|
96
|
|
|
$user = $token->getUser(); |
|
97
|
|
|
|
|
98
|
|
|
// los administradores globales siempre tienen permiso |
|
99
|
|
|
if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) { |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// si no es estudiante, denegar |
|
104
|
|
|
if ($subject->getStudentGroup() === null) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// los jefes de departamento de los grupos a los que pertenece un estudiante |
|
109
|
|
|
// tienen permisos de seguimiento |
|
110
|
|
|
if ($subject->getStudentGroup()->getTraining()->getDepartment()->getHead() === $user) { |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// los tutores del grupo al que pertenece el estudiante tienen permisos de seguimiento |
|
115
|
|
|
if ($subject->getStudentGroup()->getTutors()->contains($user)) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!$subject instanceof User) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** @var User $user */ |
|
132
|
|
|
$user = $token->getUser(); |
|
133
|
|
|
|
|
134
|
|
|
// si el usuario no ha entrado, denegar |
|
135
|
|
|
if (!$user instanceof User) { |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
switch($attribute) { |
|
140
|
|
|
case self::TRACK: |
|
141
|
|
|
return $this->voteOnTrack($subject, $token); |
|
142
|
|
|
case self::MANAGE: |
|
143
|
|
|
return $this->voteOnManage($subject, $token); |
|
144
|
|
|
default: |
|
145
|
|
|
// denegamos en cualquier otro caso |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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.