/** * Creates new account if username is not taken yet. * Tries to authenticate the user * if username is already taken. * @param string $username login of the user * @param string $password plain text password * @throws \Exception */ public function create($username, $password) { $account = $this->objectManager->getRepository('Application\\Entity\\Account')->findBy(array('username' => $username)); if ($account != null) { return $this->authenticate($username, $password); } try { try { $account = new Account(); $passwordHash = static::hashPassword($password); $email = new EMailAddress(); $person = new Person(); $credentials = new Credentials(); $credentials->setOwner($person); $credentials->setNameFirst($username); $person->addCredential($credentials); $email->setValue($username)->setOwner($person); $person->addEmailAddress($email); $account->setUsername($username)->setPassword($passwordHash); $account->setPerson($person); $this->objectManager->persist($email); $this->objectManager->persist($credentials); $this->objectManager->persist($person); $this->objectManager->persist($account); $this->objectManager->flush(); $success = true; } catch (\Exception $e) { throw new \Exception("Failed to write data to the database.", 500, $e); $success = false; } return $this->authenticate($username, $password); } catch (\Exception $e) { throw new \Exception("Failed to create an account due to unknown internal server error.", 500, $e); } }
/** * Takes $account that is user attempting to delete e-mail address to the database * and id of e-mail address to delete. * If the user appears to be the owner of the e-mail address, delete the e-mail address. * Do nothing overwise. * @param \Application\Entity\Account $account * @param string $emailAddressId */ public function destroy(\Application\Entity\Account $account, $emailAddressId) { $emailAddress = $this->retrieve($emailAddressId); if ($account->getPerson()->getEMailAddresses()->contains($emailAddress)) { $this->objectManager->remove($emailAddress); $this->objectManager->flush(); } }
/** * Takes $account that is user attempting to delete phone number to the database * and id of phone number to delete. * If the user appears to be the owner of the phone number, delete the phone number. * Do nothing overwise. * @param \Application\Entity\Account $account * @param string $phoneNumberId */ public function destroy(\Application\Entity\Account $account, $phoneNumberId) { $phoneNumber = $this->retrieve($phoneNumberId); if ($account->getPerson()->getPhoneNumbers()->contains($phoneNumber)) { $this->objectManager->remove($phoneNumber); $this->objectManager->flush(); } }
public function createAccount($data) { $em = $this->getEntityManager(); $accountEntity = new Account(); $accountEntity->setUsername($data['username'])->setPassword($data['password'])->setLastLoggedInDtTm(new \DateTime())->setCreateDtTm(new \DateTime()); $em->persist($accountEntity); $em->flush(); return $accountEntity; }
public function createAccountAction() { $resultModel = new JsonResultModel(); if ($this->getRequest()->isPost()) { try { $jsonData = $this->params()->fromPost('account'); $account = new Account(Json::decode($jsonData, Json::TYPE_ARRAY)); $account->setCreateTime(new \DateTime()); //@todo 这一行抛出异常被捕获处理后,会不会执行下一行??不会! $this->accountService->create($account); } catch (ValidationException $e) { $resultModel->setErrors($e->getValidationError()); return $resultModel; } catch (\Exception $e) { $resultModel->addErrors('error', $e->getMessage()); return $resultModel; } return $resultModel; } }