|
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 AgreementVoter extends Voter |
|
30
|
|
|
{ |
|
31
|
|
|
const MANAGE = 'AGREEMENT_MANAGE'; |
|
32
|
|
|
const ACCESS = 'AGREEMENT_ACCESS'; |
|
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
|
|
|
|
|
46
|
|
|
if (!$subject instanceof Agreement) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (!in_array($attribute, [self::MANAGE, self::ACCESS], true)) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$subject instanceof Agreement) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
// los administradores globales siempre tienen permiso |
|
66
|
|
|
if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) { |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** @var User $user */ |
|
71
|
|
|
$user = $token->getUser(); |
|
72
|
|
|
|
|
73
|
|
|
if (!$user instanceof User) { |
|
74
|
|
|
// si el usuario no ha entrado, denegar |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Si es el jefe de departamento, permitir |
|
79
|
|
|
if ($user->getDirects()->contains($subject->getTraining()->getDepartment())) { |
|
|
|
|
|
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Si es el propio estudiante, tutor de grupo, tutor docente o tutor loboral, sólo acceso de lectura |
|
84
|
|
|
if ($subject->getStudent() == $user |
|
85
|
|
|
|| $user->getTutorizedGroups()->contains($subject) |
|
86
|
|
|
|| $subject->getEducationalTutor() == $user |
|
87
|
|
|
|| $subject->getWorkTutor() == $user) { |
|
88
|
|
|
|
|
89
|
|
|
return $attribute == self::ACCESS; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// denegamos en cualquier otro caso |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.