/** * @param ChangePasswordInputModel $model * @return ServiceResponse */ public function changePassword(ChangePasswordInputModel $model) : ServiceResponse { $user = $this->dbContext->getUsersRepository()->filterByUsername(' = "' . HttpContext::getInstance()->getIdentity()->getUsername() . '"')->findOne(); if (!password_verify($model->getCurrentPassword(), $user->getPassword())) { return new ServiceResponse(1, 'Wrong current password.'); } $user->setPassword(password_hash($model->getNewPassword(), PASSWORD_DEFAULT)); $this->dbContext->saveChanges(); return new ServiceResponse(null, 'Password changed successfully.'); }
/** * @param ChangePasswordInputModel $model * @Validatetoken('token') * @return mixed * @throws \Exception */ public function changePassword(ChangePasswordInputModel $model) : View { if (!HttpContext::getInstance()->getIdentity()->isAuthorised()) { throw new \Exception('Unauthorised', 401); } if (!$model->isValid()) { return new View('account', 'changePassword', $model); } $service = new AccountService($this->dbContext); if (HttpContext::getInstance()->isPost()) { $result = $service->changepassword($model); if (!$result->hasError()) { $this->addInfoMessage($result->getMessage()); $this->redirect('home', 'index'); } else { $this->addErrorMessage($result->getMessage()); $this->redirect('account', 'register'); } } else { return new View('account', 'changePassword', new ChangePasswordInputModel()); } }