Exemple #1
0
 function parametresAction()
 {
     $moi = Zend_Registry::get('user');
     $this->view->user = $user = $this->_helper->Membre($moi);
     $this->view->individu = $individu = $user->findParentIndividus();
     $this->assert($moi, $user, 'parametres', "Vous n'avez pas le droit de modifier les paramètres de cet utilisateur.");
     $this->metas(array('DC.Title' => "Éditer l'utilisateur " . $user->username));
     $autoedit = $moi->id == $user->id;
     $db = Zend_Registry::get('db');
     /* Migration de l'identifiant */
     if ($autoedit && $user->username != $individu->adelec) {
         $this->view->migrate = $m = new Wtk_Form_Model('migrate');
         $m->addConstraintRequired($m->addString('motdepasse', 'Mot de passe'));
         $m->addNewSubmission('migrer', 'Migrer');
         if ($m->validate()) {
             $db->beginTransaction();
             try {
                 if (!$user->testPassword($m->get('motdepasse'))) {
                     throw new Wtk_Form_Model_Exception('Mot de passe erroné', $m->getInstance('motdepasse'));
                 }
                 $user->username = $individu->adelec;
                 $user->setPassword($m->get('motdepasse'));
                 $user->save();
                 $this->logger->info("Migration du compte", $this->_helper->Url('fiche', 'individus', null, array('individu' => $individu->slug)));
                 $db->commit();
                 $auth = Zend_Auth::getInstance();
                 $id = $auth->getIdentity();
                 $id['username'] = $user->username;
                 $auth->getStorage()->write($id);
             } catch (Wtk_Form_Model_Exception $e) {
                 $db->rollBack();
                 $m->errors[] = $e;
             } catch (Exception $e) {
                 $db->rollBack();
                 throw $e;
             }
             $this->redirectSimple('fiche', 'individus', null, array('individu' => $individu->slug), true);
         }
     }
     /* Changement d'adélec */
     if ($autoedit) {
         $this->view->adelec = $m = new Wtk_Form_Model('adelec');
         $i = $m->addString('adelec', 'Adelec', $individu->adelec);
         $m->addConstraintRequired($i);
         $m->addConstraintEMail($i);
         $m->addConstraintRequired($m->addString('motdepasse', 'Mot de passe'));
         $m->addNewSubmission('enregistrer', 'Enregistrer');
         if ($m->validate()) {
             $db->beginTransaction();
             try {
                 if (!$user->testPassword($m->get('motdepasse'))) {
                     throw new Wtk_Form_Model_Exception('Mot de passe erroné', $m->getInstance('motdepasse'));
                 }
                 if ($user->username == $individu->adelec) {
                     $user->username = $m->get('adelec');
                     $user->setPassword($m->get('motdepasse'));
                     $user->save();
                     $auth = Zend_Auth::getInstance();
                     $id = $auth->getIdentity();
                     $id['username'] = $user->username;
                     $auth->getStorage()->write($id);
                 }
                 $individu->adelec = $m->get('adelec');
                 $individu->save();
                 $this->logger->info("Changement d'adélec", $this->_helper->Url('fiche', 'individus', null, array('individu' => $individu->slug)));
                 $db->commit();
             } catch (Wtk_Form_Model_Exception $e) {
                 $db->rollBack();
                 $m->errors[] = $e;
             } catch (Exception $e) {
                 $db->rollBack();
                 throw $e;
             }
             $this->redirectSimple('fiche', 'individus', null, array('individu' => $individu->slug), true);
         }
     }
     /* Changement de mot de passe */
     $this->view->change = $m = new Wtk_Form_Model('chpass');
     $g = $m->addGroup('mdp', "Change le mot de passe");
     if (!$this->assert(null) || $autoedit) {
         $m->addConstraintRequired($g->addString('ancien', 'Ancien'));
     }
     $m->addConstraintRequired($g->addString('nouveau', 'Nouveau'));
     $m->addConstraintRequired($g->addString('confirmation', "Confirmation"));
     $m->addNewSubmission('valider', 'Valider');
     if ($m->validate()) {
         $db->beginTransaction();
         try {
             $mdp = $m->get('mdp');
             if (array_key_exists('ancien', $mdp)) {
                 if (!$user->testPassword($mdp['ancien'])) {
                     throw new Wtk_Form_Model_Exception("Ancien mot de passe erroné.", $m->getInstance('mdp/ancien'));
                 }
             }
             if ($mdp['nouveau'] != $mdp['confirmation']) {
                 throw new Wtk_Form_Model_Exception("Le mot de passe de confirmation n'est pas identique " . "au nouveau.");
             }
             $user->setPassword($mdp['nouveau']);
             $user->save();
             $this->logger->info("Mot de passe changé", $this->_helper->Url('fiche', 'individus', null, array('individu' => $individu->slug)));
             $db->commit();
         } catch (Wtk_Form_Model_Exception $e) {
             $db->rollBack();
             $m->errors[] = $e;
         } catch (Exception $e) {
             $db->rollBack();
             throw $e;
         }
         $this->redirectSimple('fiche', 'individus', null, array('individu' => $individu->slug), true);
     }
     /* Notifications */
     $this->view->notifications = $m = new Wtk_Form_Model('notifications');
     $m->addBool('send_mail', "Recevoir des notifications par mail", $user->send_mail);
     $m->addNewSubmission('valider', 'Valider');
     if ($m->validate()) {
         $db->beginTransaction();
         try {
             $user->send_mail = (bool) $m->get('send_mail');
             $user->save();
             $db->commit();
             if ($user->send_mail) {
                 $msg = "Notifications activées";
             } else {
                 $msg = "Notifications désactivées";
             }
             $this->logger->info($msg, $this->_helper->Url('fiche', 'individus', null, array('individu' => $individu->slug)));
         } catch (Exception $e) {
             $db->rollBack();
             throw $e;
         }
         $this->redirectSimple('fiche', 'individus', null, array('individu' => $individu->slug), true);
     }
     /* Promotion à l'administration */
     if ($this->assert($moi, $user, 'admin') && !$autoedit) {
         $this->view->admin = $m = new Wtk_Form_Model('admin');
         $m->addBool('admin', "Accorder tous les privilèges sur le site à " . $user->findParentIndividus()->getFullName(), $user->admin);
         $m->addNewSubmission('valider', 'Valider');
         if ($m->validate()) {
             $db->beginTransaction();
             try {
                 $user->admin = $m->get('admin');
                 $user->save();
                 $db->commit();
                 $msg = $user->admin ? "Privilèges accordés" : "Privilèges refusés";
                 $this->logger->warn($msg, $this->_helper->Url('fiche', 'individus', null, array('individu' => $individu->slug)));
             } catch (Exception $e) {
                 $db->rollBack();
                 throw $e;
             }
             $this->redirectSimple('fiche', 'individus', null, array('individu' => $individu->slug), true);
         }
     }
 }