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'];
// Super-Admin peuvent accéder à chaque action
if (isset($role) && $role === 'Super Administrateur') {
return true;
}
//
if ($this->request->action === 'display') {
return true;
}
// Par défaut refuser
return false;
}
/**
* {@inheritDoc}
* @see \Cake\Controller\Controller::beforeFilter()
*/
public function beforeFilter(Event $event)
{
$this->LdapAuth->allow(['display', 'add']);
$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);
$displayElement = function ($nom, $valeur) {
if ($valeur != "")
echo '
'.$nom.' | '.$valeur.' |
';
};
$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;
}
}
}