/** * Save account information. * @return <type> * * TODO move logic into model or form */ public function saveAction() { $login = Zend_Auth::getInstance()->getIdentity(); $config = $this->getConfig(); $logger = $this->getLogger(); if (!empty($login) && $this->getRequest()->isPost()) { $accountForm = new Account_Form_Account(); $account = new Opus_Account(null, null, $login); $accountForm->populateFromModel($account); $postData = $this->getRequest()->getPost(); $isPasswordChanged = true; if (empty($postData['password'])) { // modify to pass default validation // TODO think about better solution $postData[Account_Form_Account::ELEMENT_PASSWORD] = 'notchanged'; $postData[Account_Form_Account::ELEMENT_CONFIRM_PASSWORD] = 'notchanged'; $isPasswordChanged = false; } // check if username was provided and if it may be changed if (!isset($postData['username']) || isset($config->account->editPasswordOnly) && $config->account->editPasswordOnly || isset($config->account->changeLogin) && !$config->account->changeLogin) { $postData['username'] = $login; } $postData['oldLogin'] = $login; if ($accountForm->isValid($postData)) { $account = new Opus_Account(null, null, $login); $newLogin = $postData['username']; $password = $postData['password']; $firstname = $postData['firstname']; $lastname = $postData['lastname']; $email = $postData['email']; $isLoginChanged = false; if (isset($config->account->editPasswordOnly) && !$config->account->editPasswordOnly) { $account->setFirstName($firstname); $account->setLastName($lastname); $account->setEmail($email); $logger->debug('login = '******'new login = '******'admin') { $logger->debug('login changed'); $account->setLogin($newLogin); } } if ($isPasswordChanged) { $logger->debug('Password changed'); $account->setPassword($password); } $account->store(); if ($isLoginChanged || $isPasswordChanged) { Zend_Auth::getInstance()->clearIdentity(); } } else { $actionUrl = $this->view->url(array('action' => 'save')); $accountForm->setAction($actionUrl); return $this->renderForm($accountForm); } } $this->_helper->redirector('index'); }
/** * Updates account information. */ public function updateAction() { if ($this->getRequest()->isPost()) { $button = $this->getRequest()->getParam('cancel'); if (isset($button)) { $this->_helper->redirector('index'); return; } $id = $this->getRequest()->getParam('id'); $accountForm = new Admin_Form_Account($id); $postData = $this->getRequest()->getPost(); $passwordChanged = true; if (empty($postData['password'])) { // modify to pass default validation // TODO think about better solution (validation context?) $postData['password'] = '******'; $postData['confirmPassword'] = '******'; $passwordChanged = false; } $account = new Opus_Account($id); $postData['oldLogin'] = strtolower($account->getLogin()); if ($accountForm->isValid($postData)) { $account->setFirstName($postData['firstname']); $account->setLastName($postData['lastname']); $account->setEmail($postData['email']); $oldLogin = strtolower($account->getLogin()); // update login name $newLogin = $postData['username']; if ($newLogin !== $oldLogin) { $account->setLogin($newLogin); $loginChanged = true; } else { $loginChanged = false; } // update password if ($passwordChanged) { $password = $postData['password']; $account->setPassword($password); } // update roles $newRoles = Admin_Form_Account::parseSelectedRoles($postData); // TODO optimize code $hasAdministratorRole = false; foreach ($newRoles as $role) { if (strtolower($role->getDisplayName()) === 'administrator') { $hasAdministratorRole = true; break; } } $currentUser = Zend_Auth::getInstance()->getIdentity(); $isCurrentUser = $currentUser === $oldLogin ? true : false; if (!$hasAdministratorRole && $isCurrentUser) { $newRoles[] = Opus_UserRole::fetchByName('administrator'); } $account->setRole($newRoles); $account->store(); if ($isCurrentUser && ($loginChanged || $passwordChanged)) { Zend_Auth::getInstance()->clearIdentity(); } } else { $actionUrl = $this->view->url(array('action' => 'update', 'id' => $id)); $accountForm->setAction($actionUrl); $this->view->form = $accountForm; $this->view->title = 'admin_account_edit'; return $this->renderScript('account/edit.phtml'); } } $this->_helper->redirector('index'); }