/** * Edits the password of the currenly logged in user. * @param array $formParams * @return array $response */ public function change(array $formParams = array()) { // get the id of the user from the request $userId = Daiquiri_Auth::getInstance()->getCurrentId(); // create the form object $form = new Auth_Form_ChangePassword(); // valiadate the form if POST if (!empty($formParams)) { if ($form->isValid($formParams)) { // get the form values $values = $form->getValues(); // get the user credentials $user = $this->getResource()->fetchRow($userId); // check if the old password is valid $result = Daiquiri_Auth::getInstance()->authenticateUser($user['username'], $values['old_password']); if ($result) { // update the user and redirect $this->getResource()->updatePassword($userId, $values['new_password']); // log the event Daiquiri_Log::getInstance()->notice('password changed by user'); // send a notification mail if (Daiquiri_Config::getInstance()->auth->notification->changePassword) { $this->getModelHelper('mail')->send('auth.changePassword', array('to' => Daiquiri_Config::getInstance()->auth->notification->mail->toArray(), 'id' => $user['id'], 'username' => $user['username'], 'firstname' => $user['details']['firstname'], 'lastname' => $user['details']['lastname'])); } return array('status' => 'ok'); } else { return $this->getModelHelper('CRUD')->validationErrorResponse($form, 'Wrong (old) password provided'); } } else { return $this->getModelHelper('CRUD')->validationErrorResponse($form); } } return array('form' => $form, 'status' => 'form'); }
/** * Updates the credentials of the currently logged in user. * @param array $formParams * @return array $response */ public function update(array $formParams = array()) { // get id $id = Daiquiri_Auth::getInstance()->getCurrentId(); // get user $user = $this->getResource()->fetchRow($id); // get user detail keys model $detailKeyModel = new Auth_Model_DetailKeys(); $detailKeys = $detailKeyModel->getResource()->fetchRows(); // create the form object $form = new Auth_Form_Account(array('user' => $this->getResource()->fetchRow($id), 'detailKeys' => $detailKeys, 'changeUsername' => Daiquiri_Config::getInstance()->auth->changeUsername, 'changeEmail' => Daiquiri_Config::getInstance()->auth->changeEmail)); // valiadate the form if POST if (!empty($formParams)) { if ($form->isValid($formParams)) { // get the form values $values = $form->getValues(); // process the details $changed = false; $values['details'] = array(); foreach ($detailKeys as $detailKey) { if (is_array($values[$detailKey['key']])) { $values['details'][$detailKey['key']] = Zend_Json::encode($values[$detailKey['key']]); } else { if ($values[$detailKey['key']] === null) { $values['details'][$detailKey['key']] = Zend_Json::encode(array()); } else { $values['details'][$detailKey['key']] = $values[$detailKey['key']]; } } unset($values[$detailKey['key']]); if ($values['details'][$detailKey['key']] != $user['details'][$detailKey['key']]) { $changed = true; } } if (Daiquiri_Config::getInstance()->auth->changeUsername && $values['username'] != $user['username']) { $changed = true; } if (Daiquiri_Config::getInstance()->auth->changeEmail && $values['email'] != $user['email']) { $changed = true; } if ($changed) { // update the user $this->getResource()->updateRow($id, $values); // log the event Daiquiri_Log::getInstance()->notice('account updated by user'); // send a notification if (Daiquiri_Config::getInstance()->core->notification->updateUser) { $newUser = $this->getResource()->fetchRow($id); $this->getModelHelper('notification')->updateUser($user, $newUser); } } return array('status' => 'ok'); } else { return $this->getModelHelper('CRUD')->validationErrorResponse($form); } } return array('form' => $form, 'status' => 'form'); }
/** * Sets the status of a given user from 'disabled' to 'active'. * @param int $userId id of the user * @param array $formParams * @return array $response */ public function reenable($userId, array $formParams = array()) { // create the form object $form = new Daiquiri_Form_Confirm(array('submit' => 'Reenable user')); // valiadate the form if POST if (!empty($formParams)) { if ($form->isValid($formParams)) { // get the user credentials $user = $this->getResource()->fetchRow($userId); // update the use if ($user['status'] === 'active') { $form->setDescription('User status is already "active"'); return $this->getModelHelper('CRUD')->validationErrorResponse($form); } else { // get the new status id $statusId = Daiquiri_Auth::getInstance()->getStatusId('active'); // activate user in database $this->getResource()->updateRow($userId, array('status_id' => $statusId)); // send a notification mail if (Daiquiri_Config::getInstance()->auth->notification->updateUser) { $user = $this->getResource()->fetchRow($userId); $this->getModelHelper('mail')->send('auth.updateUser', array('to' => Daiquiri_Config::getInstance()->auth->notification->mail->toArray(), 'id' => $user['id'], 'username' => $user['username'], 'firstname' => $user['details']['firstname'], 'lastname' => $user['details']['lastname'])); } // log the event and return Daiquiri_Log::getInstance()->notice("user '{$user['username']}' reenabled"); return array('status' => 'ok'); } } else { return $this->getModelHelper('CRUD')->validationErrorResponse($form); } } return array('form' => $form, 'status' => 'form'); }
/** * Deletes an existing user. * @param int $id id of the user * @param array $formParams * @return array $response */ public function delete($id, array $formParams = array()) { // create the form object $form = new Daiquiri_Form_Danger(array('submit' => 'Delete user')); // valiadate the form if POST if (!empty($formParams)) { if ($form->isValid($formParams)) { // get the form values $values = $form->getValues(); // delete the user and redirect $this->getResource()->deleteRow($id); // invalidate the session of the user $resource = new Auth_Model_Resource_Sessions(); foreach ($resource->fetchAuthSessionsByUserId($id) as $session) { $resource->deleteRow($session); } // log the event and return Daiquiri_Log::getInstance()->notice("user deleted by admin (user_id: {$id})"); return array('status' => 'ok'); } else { return $this->getModelHelper('CRUD')->validationErrorResponse($form); } } return array('form' => $form, 'status' => 'form'); }