Commit 34b6fae0962cf15e83da89b37defeb7426cd86ee
1 parent
0c5cf28a
Exists in
master
and in
3 other branches
code MaterielsController/isAuthorized() plus clean avec un switch
($action)
Showing
1 changed file
with
150 additions
and
105 deletions
Show diff stats
src/Controller/MaterielsController.php
@@ -63,124 +63,169 @@ class MaterielsController extends AppController { | @@ -63,124 +63,169 @@ class MaterielsController extends AppController { | ||
63 | $this->role = $role; | 63 | $this->role = $role; |
64 | $action = $this->request->params['action']; | 64 | $action = $this->request->params['action']; |
65 | 65 | ||
66 | - // INDEX, VIEW, ADD, FIND | ||
67 | - // ACL: Accessibles à tous ( cf parent::isAuthorized() ) | ||
68 | - | ||
69 | - // EDIT | ||
70 | - if ($action == 'edit') { | ||
71 | - $id = (int) $this->request->params['pass'][0]; | ||
72 | - | ||
73 | - // (EP) TODO: il me semble qu'on devrait ajouter ici | ||
74 | - // || $this->isToBeArchived($id) | ||
75 | - // ainsi qu'une autre règle pour isArchived($id) | ||
76 | - // ??? | 66 | + switch ($action) { |
77 | 67 | ||
78 | - /* (EP) ACL: | ||
79 | - * | ||
80 | - * - adminplus et superadmin sont autorisés par défaut dans tous les cas | ||
81 | - * | ||
82 | - * SSi materiel CREATED ou VALIDATED, autoriser : | ||
83 | - * - user : si createur de la fiche ou owner du materiel | ||
84 | - * - resp : si responsable du groupe thematique ou metier de ce materiel | ||
85 | - * - admin et + : toujours | ||
86 | - * | ||
87 | - */ | ||
88 | - if ($this->isCreated($id) || $this->isValidated($id)) { | ||
89 | - | ||
90 | - if ($role == 'Utilisateur' && $this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0])) { | ||
91 | - return true; | ||
92 | - } else if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
93 | - return true; | ||
94 | - } else if ($this->userHasRole('Administration')) { | ||
95 | - return true; | ||
96 | - } | ||
97 | - } | ||
98 | - if ($this->userHasRole('Administration Plus')) { | ||
99 | - return true; | ||
100 | - } | ||
101 | - } | 68 | + // INDEX, VIEW, ADD, FIND |
69 | + // ACL: Accessibles à tous ( cf parent::isAuthorized() ) | ||
70 | + | ||
71 | + // EDIT | ||
72 | + //if ($action == 'edit') { | ||
73 | + case 'edit': | ||
74 | + $id = (int) $this->request->params['pass'][0]; | ||
75 | + | ||
76 | + // (EP) TODO: il me semble qu'on devrait ajouter ici | ||
77 | + // || $this->isToBeArchived($id) | ||
78 | + // ainsi qu'une autre règle pour isArchived($id) | ||
79 | + // ??? | ||
80 | + | ||
81 | + /* (EP) ACL: | ||
82 | + * | ||
83 | + * - adminplus et superadmin sont autorisés par défaut dans tous les cas | ||
84 | + * | ||
85 | + * SSi materiel CREATED ou VALIDATED, autoriser : | ||
86 | + * - user : si createur de la fiche ou owner du materiel | ||
87 | + * - resp : si responsable du groupe thematique ou metier de ce materiel | ||
88 | + * - admin et + : toujours | ||
89 | + * | ||
90 | + */ | ||
91 | + if ($this->isCreated($id) || $this->isValidated($id)) { | ||
102 | 92 | ||
103 | - // DELETE | ||
104 | - if ($action == 'delete') { | ||
105 | - $id = (int) $this->request->params['pass'][0]; | ||
106 | - | ||
107 | - /* (EP) ACL: | ||
108 | - * SSi materiel CREATED, autoriser : | ||
109 | - * - user : si createur de la fiche ou owner du materiel | ||
110 | - * - resp : si responsable du groupe thematique ou metier de ce materiel | ||
111 | - * - admin et + : toujours | ||
112 | - */ | ||
113 | - if ($this->isCreated($id)) { | ||
114 | - if ($role == 'Utilisateur' && $this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0])) { | ||
115 | - return true; | ||
116 | - } else if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
117 | - return true; | ||
118 | - } else if ($this->userHasRole('Administration')) { | ||
119 | - return true; | 93 | + /* |
94 | + if ($role == 'Utilisateur' && $this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0])) { | ||
95 | + return true; | ||
96 | + } else if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
97 | + return true; | ||
98 | + } else if ($this->userHasRole('Administration')) { | ||
99 | + return true; | ||
100 | + } | ||
101 | + */ | ||
102 | + switch ($role) { | ||
103 | + case 'Utilisateur': | ||
104 | + return ($this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0])); | ||
105 | + break; | ||
106 | + case 'Responsable': | ||
107 | + return ($this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])); | ||
108 | + break; | ||
109 | + // All other roles : Admin and more | ||
110 | + default: | ||
111 | + return true; | ||
112 | + break; | ||
113 | + } | ||
120 | } | 114 | } |
121 | - } | ||
122 | - } | ||
123 | - | ||
124 | - if ($action == 'statusValidated') { | ||
125 | - $id = (int) $this->request->params['pass'][0]; | ||
126 | - if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
127 | - if ($this->isCreated($id)) { | 115 | + if ($this->userHasRole('Administration Plus')) { |
128 | return true; | 116 | return true; |
129 | } | 117 | } |
130 | - } else if ($this->userHasRole('Administration')) { | 118 | + break; |
119 | + | ||
120 | + | ||
121 | + | ||
122 | + // DELETE | ||
123 | + case 'delete': | ||
124 | + $id = (int) $this->request->params['pass'][0]; | ||
125 | + | ||
126 | + /* (EP) ACL: | ||
127 | + * SSi materiel CREATED, autoriser : | ||
128 | + * - user : si createur de la fiche ou owner du materiel | ||
129 | + * - resp : si responsable du groupe thematique ou metier de ce materiel | ||
130 | + * - admin et + : toujours | ||
131 | + */ | ||
131 | if ($this->isCreated($id)) { | 132 | if ($this->isCreated($id)) { |
132 | - return true; | 133 | + /* |
134 | + if ($role == 'Utilisateur' && $this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0])) { | ||
135 | + return true; | ||
136 | + } else if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
137 | + return true; | ||
138 | + } else if ($this->userHasRole('Administration')) { | ||
139 | + return true; | ||
140 | + } | ||
141 | + */ | ||
142 | + switch ($role) { | ||
143 | + case 'Utilisateur': | ||
144 | + return ($this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0])); | ||
145 | + break; | ||
146 | + case 'Responsable': | ||
147 | + return ($this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])); | ||
148 | + break; | ||
149 | + // All other roles : Admin and more | ||
150 | + default: | ||
151 | + return true; | ||
152 | + break; | ||
153 | + } | ||
133 | } | 154 | } |
134 | - } | ||
135 | - } | 155 | + break; |
136 | 156 | ||
137 | - if ($action == 'statusToBeArchived') { | ||
138 | - $id = (int) $this->request->params['pass'][0]; | ||
139 | - if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
140 | - if ($this->isValidated($id)) { | ||
141 | - return true; | ||
142 | - } | ||
143 | - } else if ($this->userHasRole('Administration')) { | ||
144 | - if ($this->isValidated($id)) { | ||
145 | - return true; | 157 | + case 'statusValidated': |
158 | + $id = (int) $this->request->params['pass'][0]; | ||
159 | + if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
160 | + if ($this->isCreated($id)) { | ||
161 | + return true; | ||
162 | + } | ||
163 | + } else if ($this->userHasRole('Administration')) { | ||
164 | + if ($this->isCreated($id)) { | ||
165 | + return true; | ||
166 | + } | ||
146 | } | 167 | } |
147 | - } | ||
148 | - } | ||
149 | - | ||
150 | - if ($action == 'statusArchived') { | ||
151 | - $id = (int) $this->request->params['pass'][0]; | ||
152 | - if ($this->userHasRole('Administration')) { | ||
153 | - if ($this->isToBeArchived($id)) { | ||
154 | - if ($role == 'Super Administrateur') { | ||
155 | - return false; | ||
156 | - } else { | 168 | + break; |
169 | + | ||
170 | + case 'statusToBeArchived': | ||
171 | + $id = (int) $this->request->params['pass'][0]; | ||
172 | + if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) { | ||
173 | + if ($this->isValidated($id)) { | ||
174 | + return true; | ||
175 | + } | ||
176 | + } else if ($this->userHasRole('Administration')) { | ||
177 | + if ($this->isValidated($id)) { | ||
157 | return true; | 178 | return true; |
158 | } | 179 | } |
159 | } | 180 | } |
160 | - } | ||
161 | - } | ||
162 | - | ||
163 | - if ($action == 'export') { | ||
164 | - if ($this->userHasRole('Responsable')) { | ||
165 | - return true; | ||
166 | - } | ||
167 | - } | ||
168 | - | ||
169 | - if (in_array($action, [ | ||
170 | - 'execActions', | ||
171 | - 'setLabelIsPlaced', | ||
172 | - 'setLabelIsPlacedOrNotPlaced', | ||
173 | - 'setLabelIsNotPlaced', | ||
174 | - 'printLabelRuban' | ||
175 | - ])) { | ||
176 | - if ($this->userHasRole('Administration')) { | ||
177 | - return true; | ||
178 | - } | ||
179 | - } | 181 | + break; |
182 | + | ||
183 | + case 'statusArchived': | ||
184 | + $id = (int) $this->request->params['pass'][0]; | ||
185 | + if ($this->userHasRole('Administration')) { | ||
186 | + if ($this->isToBeArchived($id)) { | ||
187 | + if ($role == 'Super Administrateur') { | ||
188 | + return false; | ||
189 | + } else { | ||
190 | + return true; | ||
191 | + } | ||
192 | + } | ||
193 | + } | ||
194 | + break; | ||
195 | + | ||
196 | + case 'export': | ||
197 | + if ($this->userHasRole('Responsable')) { | ||
198 | + return true; | ||
199 | + } | ||
200 | + break; | ||
201 | + | ||
202 | + /* | ||
203 | + if (in_array($action, [ | ||
204 | + 'execActions', | ||
205 | + 'setLabelIsPlaced', | ||
206 | + 'setLabelIsPlacedOrNotPlaced', | ||
207 | + 'setLabelIsNotPlaced', | ||
208 | + 'printLabelRuban' | ||
209 | + ])) { | ||
210 | + */ | ||
211 | + case 'execActions': | ||
212 | + case 'setLabelIsPlaced': | ||
213 | + case 'setLabelIsPlacedOrNotPlaced': | ||
214 | + case 'setLabelIsNotPlaced': | ||
215 | + case 'printLabelRuban': | ||
216 | + if ($this->userHasRole('Administration')) { | ||
217 | + return true; | ||
218 | + } | ||
219 | + break; | ||
220 | + | ||
221 | + // Autorisations par defaut: | ||
222 | + default: | ||
223 | + return parent::isAuthorized($user); | ||
224 | + ; | ||
225 | + break; | ||
226 | + } // end of switch case | ||
227 | + } | ||
180 | 228 | ||
181 | - // Autorisations par defaut: | ||
182 | - return parent::isAuthorized($user); | ||
183 | - } | ||
184 | 229 | ||
185 | public function isOwnedBy($id, $nomCreateur) { | 230 | public function isOwnedBy($id, $nomCreateur) { |
186 | return ($this->Materiels->exists([ | 231 | return ($this->Materiels->exists([ |