示例#1
0
 /**
  * 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);
     }
 }
示例#2
0
 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;
 }