AppController.php
7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\Mailer\Email;
use Cake\Core\Configure;
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller
{
/**
* Initialization hook method.
*
* Use this method to add common initialization code like loading components.
*
* e.g. `$this->loadComponent('Security');`
*
* @return void
*/
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('LdapAuth', [
'authorize' => ['Controller'],
'loginRedirect' => [
'controller' => 'Pages',
'action' => 'home'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'home',
]
]);
}
/**
* @param $user
*
* Give authorization in general
*
* @return boolean
*/
public function isAuthorized($user)
{
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
$role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
$action = $this->request->params['action'];
//error_log($action);
// Super-Admin peut accéder à chaque action
if($role == 'Super Administrateur') return true;
//Pour tout le monde
if (in_array($action, ['index', 'find', 'view', 'creer', 'add'])) return true;
// Par défaut refuser
return false;
}
/**
* {@inheritDoc}
* @see \Cake\Controller\Controller::beforeFilter()
*/
public function beforeFilter(Event $event)
{
//!!! Ne jamais autoriser l'action 'login', sinon cela va créer des problèmes sur le fonctionnement normal de AuthComponent (cf doc) !!!
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
if($configuration->mode_install) {
$this->LdapAuth->allow(['display', 'add', 'edit', 'installOff']);
}
else {
$this->LdapAuth->allow(['display']);
}
$this->LdapAuth->config('authError', "Désolé, vous n'êtes pas autorisés à accéder à cette zone.");
}
/**
* Before render callback.
*
* @param \Cake\Event\Event $event The beforeRender event.
* @return void
*/
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
$this->set('username', $this->LdapAuth->user('givenname')[0].' '.$this->LdapAuth->user('sn')[0]);
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
$this->set('configuration', $configuration);
$this->request->session()->write("authType", $configuration->authentificationType_ldap);
$role = TableRegistry::get('Users')->find()->where(['username' => $this->LdapAuth->user($configuration->authentificationType_ldap)[0]])->first()['role'];
if($role == null) $role = 'Utilisateur';
$this->set('role', $role);
$displayElement = function ($nom, $valeur) {
if ($valeur != "")
echo '<tr><td><strong>'.$nom.' </strong></td><td>'.$valeur.'</td></tr>';
};
$this->set('displayElement', $displayElement);
}
// "le materiel", "le suivi"...
protected function getArticle() {
return "Le ";
}
/**
* Envoi d'un email (et log) à la gestion (et aux devs) pour prévenir qu'un matériel a été créé ou modifié
* (cf howto dans http://book.cakephp.org/2.0/fr/core-utility-libraries/email.html)
* @param string $subject
* @param string $message
*/
public function sendEmailToManagementWith($subject, $message) {
//Desactivation de l'envoi des mails pour le moment
if (false) {
//$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
//$to = $configuration->emailGuest2;
$to = "labinvent2@gmail.com";
$email = new Email();
$email->transport('gmail')
->to($to)
->subject("[LabInvent] ".$subject)
->send($message);
}
}
public function sendEmailToManagement($idObj = null) {
//Desactivation de l'envoi des mails pour le moment
if (false) {
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
$userAuth = $this->LdapAuth->user($configuration->authentificationType_ldap);
$controller = substr($this->request->params['controller'], 0, -1); // materiel
$action = $this->request->params['action']; // add or edit or delete or ...
$userName = $this->LdapAuth->user('givenname')[0].' '.$this->LdapAuth->user('sn')[0];
$userEmail = $this->LdapAuth->user('email');
$modelName = $this->modelClass; // 'Materiels'
$id = $idObj;
switch ($action) {
case 'add':
$actionFrench = ['Création', 'créé'];
break;
case 'edit':
$actionFrench = ['Modification', 'modifié'];
break;
case 'delete':
$actionFrench = ['Suppression', 'supprimé'];
break;
case 'statusValidated':
$actionFrench = ['Validation', 'validé'];
break;
case 'statusToBeArchived':
$actionFrench = ['Demande Archivage', 'demandé pour archivage'];
break;
case 'statusArchived':
$actionFrench = ['Archivage', 'archivé'];
break;
default:
$actionFrench = [$action, $action];
break;
}
$doneBy = $userName." (".$userEmail.", login=".$userAuth.", profil=".$this->LdapAuth->user('role').").";
$subject = $actionFrench[0]." d'un " .$controller;
if($id != null) {
$entityName = TableRegistry::get($modelName)->find('all')->where(['id =' => $id])->first();
if($modelName == 'Materiels') {
$entityName = $entityName['designation'];
}
else if ($modelName == 'Suivis' || $modelName == 'Emprunts') {
$entityName = $entityName['id'];
}
else {
$entityName = $entityName['nom'];
}
}
else {
$entityName = NULL;
}
$message = $this->getArticle().$controller." ".$entityName." (id=".$id.") a été ".$actionFrench[1]." par ".$doneBy;
$this->sendEmailToManagementWith($subject, $message);
}
}
static function isLabinventDebugMode() {
return TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first()->mode_debug;
}
function myDebug($arg, $stop=false) {
if ($this->isLabinventDebugMode()) {
Configure::write('debug', true);
debug($arg);
if ($stop) exit;
}
}
}