|
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\User; |
|
24
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
25
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
|
26
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
27
|
|
|
|
|
28
|
|
|
class EducationalTutorVoter extends Voter |
|
29
|
|
|
{ |
|
30
|
|
|
const VISIT_TRACK = 'USER_VISIT_TRACK'; |
|
31
|
|
|
const MANAGE = 'USER_MANAGE'; |
|
32
|
|
|
|
|
33
|
|
|
private $decisionManager; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(AccessDecisionManagerInterface $decisionManager) { |
|
36
|
|
|
$this->decisionManager = $decisionManager; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
protected function supports($attribute, $subject) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
if (!$subject instanceof User) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!in_array($attribute, [self::MANAGE, self::VISIT_TRACK], true)) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$subject instanceof User) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var User $user */ |
|
65
|
|
|
$user = $token->getUser(); |
|
66
|
|
|
|
|
67
|
|
|
// si el usuario no ha entrado, denegar |
|
68
|
|
|
if (!$user instanceof User) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$user = $token->getUser(); |
|
73
|
|
|
|
|
74
|
|
|
// si no es tutor docente, denegar |
|
75
|
|
|
if ($subject->getEducationalTutorAgreements()->isEmpty()) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// los administradores globales siempre tienen permiso |
|
80
|
|
|
if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// los jefes de departamento de los grupos a los que pertenece un tutor laboral |
|
85
|
|
|
// tienen permisos de seguimiento |
|
86
|
|
|
if ($subject->getEducationalTutorAgreements()->first()->getStudent()->getStudentGroup()->getTraining()->getDepartment()->getHead() === $user) { |
|
87
|
|
|
return $attribute != self::MANAGE; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// el propio tutor puede seguir sus visitas |
|
91
|
|
|
if ($subject == $user) { |
|
92
|
|
|
return $attribute === self::VISIT_TRACK; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// denegar en cualquier otro caso |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
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.