Exemplo n.º 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);
     }
 }
Exemplo n.º 2
0
 public function create(Person $owner, $nameFirst, $nameLast)
 {
     $credentials = new Credentials();
     $credentials->setOwner($owner);
     $credentials->setNameFirst($nameFirst);
     $credentials->setNameLast($nameLast);
     $this->objectManager->persist($credentials);
     $this->objectManager->flush();
     return $credentials;
 }