self::PROFILE_USER, 'Responsable' => self::PROFILE_RESPONSABLE, 'Administration' => self::PROFILE_ADMIN, 'Administration Plus' => self::PROFILE_ADMINPLUS, 'Super Administrateur' => self::PROFILE_SUPERADMIN ]; // Current role (profile) of the user private $CURRENT_USER = null; //private $CURRENT_ROLE = null; public static function getRoleLevel($role) { //return $this->allProfiles[$role]; //debug("role is" .$role); return self::PROFILES[$role]; } public function getActionPassed() { // BETTER: //return $this->request->getAttribute('params')['action']; return $this->request->getParam('action'); } public function getIdPassed() { //return (int) $this->request->getAttribute('params')['pass'][0]; return (int) $this->request->getParam('pass.0'); } /** * 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' ] ]); // On charge la configuration $this->confLabinvent = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first(); } /** * Autorisations fréquentes utilisées par bcp de isAuthorized() de controlleurs * Ca évite de répéter 10 fois la meme chose !!! */ public function isAuthorizedCommons() { $action = $this->getActionPassed(); //$role = $this->getUserRole($user); // Seul Administration (et +) peut ajouter, supprimer ou modifier un organisme if( in_array($action,['add','delete','edit'])) { if ($this->USER_IS_ADMIN_AT_LEAST()) return true; // Les autres n'y ont pas accès return false; } // By default return parent::isAuthorized($user); } /** * @param $user * @return boolean * * isAuthorized is located in the Auth component * Check whether a LOGGED in user has a set of permissions to perform a given action * Give authorization in general * * 2) Autorisation APRES connexion * (Avant, c'est beforeFilter() qui s'en occupe) */ public function isAuthorized($user) { /* // ATTENTION, normalement, on devrait tester si role est défini..., mais c'est sans doute pas utile // cf https://book.cakephp.org/3.0/fr/tutorials-and-examples/blog-auth-example/auth.html if (isset($user['role']) && $user['role'] === 'admin') { return true; } */ $configuration = $this->confLabinvent; $role = $this->getUserRole($user); /* $role = TableRegistry::get('Users')->find() ->where(['username' => $user[$configuration->authentificationType_ldap][0]]) ->first()['role']; */ $this->myDebug("role is ".$role); //BETTER: //$action = $this->request->getAttribute('params')['action']; //$action = $this->request->getParam('action'); $action = $this->getActionPassed(); // error_log($action); // ACL : Actions accessibles à tous les roles (profils) if (in_array($action, ['index', 'view', 'add', 'find', 'creer', 'getNextDate', 'getDateGarantie'])) return true; // ACL : Super-Admin peut accéder à toutes les actions if ($role == 'Super Administrateur') return true; // ACL : Par défaut refuser return false; } public function getUser($user=null) { return $this->getTablePriviledgedUserFromCurrentSessionUser($user); } public function getTablePriviledgedUserFromCurrentSessionUser($user=null) { if (! $this->CURRENT_USER) { $configuration = $this->confLabinvent; $username = $user ? $user[$configuration->authentificationType_ldap][0] : $this->LdapAuth->user($configuration->authentificationType_ldap)[0]; $priviledgedUser = TableRegistry::get('Users') ->find() ->where(['username' => $username]) //->where(['username' => $this->LdapAuth->user('cn')[0]]) ->first(); //if (! $priviledgedUser) $priviledgedUser = "Unpriviledged User (not in table utilisateurs)"; $this->CURRENT_USER = $priviledgedUser; } return $this->CURRENT_USER; } public function getUserRole($user=null) { return $this->getPriviledgedUserRole($user); } public function getPriviledgedUserRole($user=null) { $priviledgedUser = $this->getTablePriviledgedUserFromCurrentSessionUser($user); // default role is "Utilisateur" (for people who are not in the table utilisateurs) if (! $priviledgedUser) return 'Utilisateur'; return $priviledgedUser['role']; /* if (! $this->CURRENT_ROLE) { $configuration = $this->confLabinvent; $user = TableRegistry::get('Users')->find() ->where(['username' => $this->LdapAuth->user($configuration->authentificationType_ldap)[0]]) //->where(['username' => $this->LdapAuth->user('cn')[0]]) ->first(); $this->CURRENT_ROLE = $user['role']; } return $this->CURRENT_ROLE; */ } private function userHasRole($expectedRole, $ORMORE=false) { $role = $this->getUserRole(); if (! $ORMORE) return ($role == $expectedRole); return ($this->getRoleLevel($role) >= $this->getRoleLevel($expectedRole)); /* //$hasRole = false; switch ($expectedRole) { case 'Super Administrateur' : return (in_array($role, ['Super Administrateur'])); break; case 'Administration Plus' : return (in_array($role, ['Administration Plus', 'Super Administrateur'])); break; case 'Administration' : return (in_array($role, ['Administration', 'Administration Plus', 'Super Administrateur' ])); break; case 'Responsable' : return (in_array($role, ['Responsable', 'Administration', 'Administration Plus', 'Super Administrateur'])); break; case 'Utilisateur' : return (in_array($role, ['Utilisateur', 'Responsable', 'Administration', 'Administration Plus', 'Super Administrateur'])); break; } return $false; */ } private function userHasRoleAtLeast($expectedRole) { return $this->userHasRole($expectedRole, true); } public function USER_IS_ADMIN_AT_LEAST() { return $this->userHasRoleAtLeast('Administration'); } public function USER_IS_RESP_AT_LEAST() { return $this->userHasRoleAtLeast('Responsable'); } public function USER_IS_SUPERADMIN() { return $this->userHasRole('Super Administrateur'); } public function USER_IS_ADMIN() { return $this->userHasRole('Administration'); } public function USER_IS_RESP() { return $this->userHasRole('Responsable'); } public function USER_IS_USER() { return $this->userHasRole('Utilisateur'); } /** * {@inheritdoc} * * @see \Cake\Controller\Controller::beforeFilter() * * 1) Autorisations SANS (ou AVANT) connexion * 2) Ensuite, c'est isAuthorized qui gère * */ 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) !!! //parent::beforeFilter($event); /* EXEMPLES d'utilisation: // to allow all access to all actions: if (isset($this->Auth)) { $this->Auth->allow('*'); } // Allow access to index & view actions: if (isset($this->Auth)) { $this->Auth->allowedActions = array('index', 'view'); } */ $configuration = $this->confLabinvent; 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é à accéder à cette zone."); } public function afterFilter(Event $event) { if (in_array($this->request->getAttribute('params')['action'], ['edit', 'add'])) $this->request->session()->write("retourForm1", true); else if ($this->request->getAttribute('params')['action'] != 'creer') $this->request->session()->write("retourForm1", false); } /** * Before render callback. * * @param \Cake\Event\Event * $event The beforeRender event * @return void */ public function beforeRender(Event $event) { $this->set('PROFILE_USER', self::PROFILE_USER); $this->set('PROFILE_ADMIN', self::PROFILE_ADMIN); $this->set('PROFILE_RESPONSABLE', self::PROFILE_RESPONSABLE); $this->set('PROFILE_ADMINPLUS', self::PROFILE_ADMINPLUS); $this->set('PROFILE_SUPERADMIN', self::PROFILE_SUPERADMIN); //$this->set('allProfiles', $this->allProfiles); $this->set('allProfiles', self::PROFILES); 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('sn')[0] . ' ' . $this->LdapAuth->user('givenname')[0]); $configuration = $this->confLabinvent; $this->set('configuration', $configuration); $this->request->session()->write("authType", $configuration->authentificationType_ldap); //TODO: mettre ca au propre, le cas ou l'utilisateur a un role UTILISATEUR doit etre traité AVANT, en amont, et pas ici, bien trop tard $user = $this->getUser(); $role = $this->getUserRole(); /* $user = TableRegistry::get('Users')->find() ->where(['username' => $this->LdapAuth->user($configuration->authentificationType_ldap)[0]]) ->first(); $role = $user['role']; if ($role == null) $role = 'Utilisateur'; */ $this->set('role', $role); //$profile = $this->allProfiles["$role"]; $profile = self::PROFILES["$role"]; $this->set('profile', $profile); $USER_IS_UTILISATEUR = ($profile == self::PROFILE_USER); $USER_IS_RESPONSABLE = ($profile == self::PROFILE_RESPONSABLE); $USER_IS_ADMIN = ($profile == self::PROFILE_ADMIN); $USER_IS_ADMINPLUS = ($profile == self::PROFILE_ADMINPLUS); $USER_IS_SUPERADMIN = ($profile == self::PROFILE_SUPERADMIN); $USER_IS_RESPONSABLE_OR_MORE = ($profile >= self::PROFILE_RESPONSABLE); $USER_IS_ADMIN_OR_MORE = ($profile >= self::PROFILE_ADMIN); $USER_IS_ADMINPLUS_OR_MORE = ($profile >= self::PROFILE_ADMINPLUS); $this->set('USER_IS_UTILISATEUR', $USER_IS_UTILISATEUR); $this->set('USER_IS_RESPONSABLE', $USER_IS_RESPONSABLE); $this->set('USER_IS_ADMIN', $USER_IS_ADMIN); $this->set('USER_IS_ADMINPLUS', $USER_IS_ADMINPLUS); $this->set('USER_IS_SUPERADMIN', $USER_IS_SUPERADMIN); $this->set('USER_IS_RESPONSABLE_OR_MORE', $USER_IS_RESPONSABLE_OR_MORE); $this->set('USER_IS_ADMIN_OR_MORE', $USER_IS_ADMIN_OR_MORE); $this->set('USER_IS_ADMINPLUS_OR_MORE', $USER_IS_ADMINPLUS_OR_MORE); $this->set('userConnected', $user); $this->set('idGmNa', TableRegistry::get('GroupesMetiers')->find()->where(['nom =' => 'N/A'])->first()['id']); $this->set('idGtNa', TableRegistry::get('GroupesThematiques')->find()->where(['nom =' => 'N/A'])->first()['id']); $displayElement = function ($nom, $valeur, $params = "") { $balise = ($params != "") ? '