extranet/application/controllers/UserController.php
2012-12-26 10:36:47 +00:00

435 lines
12 KiB
PHP

<?php
class UserController extends Zend_Controller_Action
{
/**
* Récupére les valeurs du profil depuis le webservice
* @param unknown_type $login
* @param unknown_type $password
*/
protected function updateProfil($login, $password)
{
Zend_Registry::get('firebug')->info('updateProfil');
$ws = new WsScores();
$InfosLogin = $ws->getInfosLogin($login, $_SERVER['REMOTE_ADDR']);
$identity = new stdClass;
$identity->username = $login;
$identity->password = $password;
$identity->email = $InfosLogin->result->email;
$identity->profil = $InfosLogin->result->profil;
$identity->pref = $InfosLogin->result->pref;
$identity->droits = $InfosLogin->result->droits;
$identity->droitsClients = $InfosLogin->result->droitsClients;
$identity->nom = $InfosLogin->result->nom;
$identity->prenom = $InfosLogin->result->prenom;
$identity->tel = $InfosLogin->result->tel;
$identity->fax = $InfosLogin->result->fax;
$identity->mobile = $InfosLogin->result->mobile;
$identity->id = $InfosLogin->result->id;
$identity->idClient = $InfosLogin->result->idClient;
$identity->reference = $InfosLogin->result->reference;
$identity->nbReponses = $InfosLogin->result->nbReponses;
$identity->typeScore = $InfosLogin->result->typeScore;
$identity->timeout = (!empty($InfosLogin->result->timeout)) ?
$InfosLogin->result->timeout : 1800;
$identity->time = time() + $identity->timeout;
$identity->modeEdition = false;
$identity->acceptationCGU = $InfosLogin->result->acceptationCGU;;
return $identity;
}
public function init()
{
require_once 'Scores/WsScores.php';
$this->view->headLink()->appendStylesheet('/themes/default/styles/user.css', 'all');
}
/**
* Affiche le fomulaire d'edition des paramètres utilisateur
*/
public function indexAction()
{
$user = new Scores_Utilisateur();
if (!$user->checkPerm('MONPROFIL')){
$this->_forward('perms', 'error');
}
$this->view->headLink()->appendStylesheet('/themes/default/styles/form.css', 'all');
$this->view->headScript()->appendFile('/themes/default/scripts/user.js', 'text/javascript');
$request = $this->getRequest();
$messages = '';
$isProfilUpdated = false;
$isPasswordUpdated = false;
$updateResult = false;
$ws = new WsScores();
$login = $request->getParam('login', '');
$op = $request->getParam('op');
if ($request->isPost()) {
$options = $request->getParam('frmOptions', '');
$action = $options['action'];
if ($login=='') $login = $options['login'];
//Enregistrement des données new & update
if (in_array($action, array('new','update'))) {
if ($options['changepwd']!=1) {
$options['password'] = '';
}
if ( in_array($options['profil'], array('Administrateur', 'SuperAdministrateur'))
&& !in_array('monprofil', $options['droits']) ) {
$options['droits'][] = 'monprofil';
}
if( !isset($options['profil']) ) {
$options['profil'] = 'Utilisateur';
}
$reponse = $ws->setInfosLogin($login, $action, $options);
$isProfilUpdated = true;
$message = 'Erreur lors de la mise à jour du compte !';
if ($reponse){
$updateResult = true;
$message = 'Compte mis à jour.';
}
}
}
//Liste des catégories des accès
$reponse = $ws->getCategory();
$wscategory = $reponse->item;
$this->view->assign('wscategory', $wscategory);
//Liste de tous les droits
$listeDroits = $ws->getListeDroits();
$droitsLib = array();
foreach($listeDroits->item as $droit) {
$droitsLib[strtoupper($droit->code)] = $droit->desc;
}
$this->view->assign('droitsLib', $droitsLib);
//Liste de toutes les préférences
$listePrefs = $ws->getListePrefs();
$prefsLib = array();
foreach($listePrefs->item as $pref) {
$prefsLib[strtoupper($pref->code)] = $pref->desc;
}
$this->view->assign('prefsLib', $prefsLib);
//Récupération des informations de l'identité
$auth = Zend_Auth::getInstance();
$identity = $auth->getIdentity();
if ($identity->idClient == $options['idClient'] && $identity->username == $login) {
//Modification lors du changement de mot de passe
if ($options['changepwd']==1 && $updateResult) {
$identity->password = md5($login.'|'.$options['password']);
$auth->getStorage()->write($identity);
}
//Mise à jour du profil
if ($isProfilUpdated && $updateResult) {
$identity = $this->updateProfil($identity->username, $identity->password);
$auth->getStorage()->write($identity);
}
//Gestion mode edition en SESSION
if ($action=='update') {
$modeEdition = $request->getParam('modeEdition', false);
if ( $modeEdition ) {
$identity->modeEdition = true;
$auth->getStorage()->write($identity);
}
}
}
if ( $isProfilUpdated || $isPasswordUpdated ) {
$this->view->assign('message', $message);
}
$isAdmin = false;
if ( $identity->profil == 'Administrateur'
|| $identity->profil == 'SuperAdministrateur' ) {
$isAdmin = true;
}
$this->view->assign('isAdmin', $isAdmin);
$isSuperAdmin = false;
if ($identity->profil == 'SuperAdministrateur') {
$isSuperAdmin = true;
}
$this->view->assign('isSuperAdmin', $isSuperAdmin);
if ($op=='new')
{
$idClient = $request->getParam('idClient', '');
if ($idClient == '') {
$idClient = $identity->idClient;
}
$reponse = $ws->getNextLogin($idClient);
$options->idClient = $idClient;
if ($identity->idClient!=1 && $identity->profil!='SuperAdministrateur') {
$options->profil = 'Utilisateur';
}
$this->view->assign('options', $options);
$this->view->assign('loginNew', $reponse->result->racine);
$this->view->assign('droitsClients', explode(' ', strtolower($reponse->result->droitsClients)));
$this->view->assign('action', 'new');
$this->view->assign('pref', array());
}
elseif (!empty($op) || $op!='new')
{
if ( !empty($login) && $identity->username != $login ) {
Zend_Registry::get('firebug')->info('getInfosLogin');
$reponse = $ws->getInfosLogin($login, $_SERVER['REMOTE_ADDR']);
$this->view->assign('options', $reponse->result);
$this->view->assign('loginVu', $reponse->result->login);
$this->view->assign('droits', explode(' ', strtolower($reponse->result->droits)));
$this->view->assign('droitsClients', explode(' ', strtolower($reponse->result->droitsClients)));
} else {
$this->view->assign('options', $identity);
$this->view->assign('loginVu', $identity->username);
$this->view->assign('droits', explode(' ', strtolower($identity->droits)));
$this->view->assign('droitsClients', explode(' ', strtolower($identity->droitsClients)));
}
$this->view->assign('loginNew', '');
$this->view->assign('action', 'update');
$this->view->assign('pref', explode(' ',$identity->pref));
}
}
/**
* Téléchargement de la consommation au format CSV
*/
public function consoAction()
{
$this->view->headScript()->appendFile('/themes/default/scripts/conso.js', 'text/javascript');
$user = new Scores_Utilisateur();
$request = $this->getRequest();
$idClient = $request->getParam('idClient', $user->getIdClient());
$login = $request->getParam('login', '');
$this->view->assign('idClient', $idClient);
$this->view->assign('login', $login);
$this->view->assign('profil', $user->getProfil());
}
/**
* Renvoi vers le formulaire utilisateur avec les paramètres de la requete
*/
public function editAction()
{
$params = $this->getRequest()->getParams();
$this->_forward('index', 'user', null, $params);
}
/**
* Suppression d'un utilisateur
*/
public function deleteAction()
{
$request = $this->getRequest();
$login = $request->getParam('login');
$action = 'delete';
$ws = new WsScores();
$ws->setInfosLogin($login, $action);
//Redirect
$this->_forward('liste');
}
/**
* Activation d'un utilisateur
*/
public function enableAction()
{
$request = $this->getRequest();
$login = $request->getParam('login');
$action = 'enable';
$ws = new WsScores();
$ws->setInfosLogin($login, $action);
//Redirect
$this->_forward('liste');
}
/**
* Désactivation d'un utilisateur
*/
public function disableAction()
{
$request = $this->getRequest();
$login = $request->getParam('login');
$action = 'disable';
$ws = new WsScores();
$ws->setInfosLogin($login, $action);
//Redirect
$this->_forward('liste');
}
/**
* Méthode AJAX pour modifier le password d'un utilisateur
*/
public function changepwdAction()
{
//Redirect
}
/**
* Affiche la liste des utiliateurs
*/
public function listeAction()
{
$user = new Scores_Utilisateur();
$request = $this->getRequest();
$idClient = $request->getParam('idClient', $user->getIdClient());
if (!$user->isSuperAdmin() && !$user->isAdmin()) {
$this->renderScript('error/perms.phtml');
}
if ($user->isAdmin()){
$idClient = $user->getIdClient();
}
$ws = new WsScores();
$infos = $ws->getListeUtilisateurs($user->getLogin(), $idClient);
$utilisateurs = $infos->result->item;
$this->view->assign('utilisateurs', $utilisateurs);
$this->view->assign('idClient', $idClient);
}
/**
* Gestion de l'authentification
*/
public function loginAction()
{
$this->view->inlineScript()
->appendFile('/libs/jquery/jquery.js')
->appendFile('/libs/jquery/jquery.infieldlabel.min.js');
//@todo : gestion des affichages particuliers pour les clients
$this->view->headTitle()->append('Connexion');
$form = new Form_Login();
$this->view->form = $form;
$request = $this->getRequest();
if ($request->isPost()) {
$formData = $request->getPost ();
if ($form->isValid($formData)) {
$login = $form->getValue('login');
$pass = $form->getValue('pass');
$auth = Zend_Auth::getInstance();
$authAdapter = new Scores_AuthAdapter($login, md5($login.'|'.$pass));
$result = $auth->authenticate($authAdapter);
if (!$result->isValid()){
$this->view->message = '';
Zend_Registry::get('firebug')->info($result);
foreach ($result->getMessages() as $message) {
$this->view->message.= $message."<br/>";
}
} else {
$url = '';
if (Zend_Session::namespaceIsset('login')){
$session = new Zend_Session_Namespace('login');
if (isset($session->url)) {
$url = $session->url;
}
}
if (!empty($url) && $url!='/user/login' && $url!='/user/logout' && $url!='/localauth'){
$this->_redirect($url);
}
$this->_redirect('/');
}
}
}
$this->_helper->layout()->disableLayout();
}
/**
* Gestion de la déconnexion
*/
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$message = $request->getParam('message');
$this->view->assign('message', $message);
$ajax = $request->getParam('ajax', 0);
$this->view->assign('ajax', $ajax);
$refresh = 5;
$url = 'http://'.$_SERVER['SERVER_NAME'].$this->view->url(array(
'controller' => 'user',
'action' => 'login',
), null, true);
$this->view->assign('url', $url);
if (!$ajax) {
$this->view->assign('refresh', $refresh);
$this->view->headMeta()->appendHttpEquiv('refresh', $refresh.'; url='.$url);
}
}
/**
* Mettre à jour le mode edition en session sans refresh de la page
*/
public function editionsessionAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$mode = $request->getParam('mode', 'false');
$auth = Zend_Auth::getInstance();
$identity = $auth->getIdentity();
if ($identity->idClient == 1) {
if ($mode == 'false') {
$identity->modeEdition = false;
echo 0;
} else {
$identity->modeEdition = true;
echo 1;
}
$auth->getStorage()->write($identity);
} else {
echo 0;
}
}
/**
* Changer le theme de l'utilisateur
*/
public function changethemeAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$nom = $request->getParam('nom', 'default');
$auth = Zend_Auth::getInstance();
$identity = $auth->getIdentity();
$identity->theme = $nom;
$auth->getStorage()->write($identity);
//Rediriger vers l'écran de recherche
$this->_redirect('/');
}
}