Example #1
0
 /**
  * User registration.
  *
  * @param string $email
  * @param string $username
  * @param string $password
  * @param string $password2 Password confirmation
  *
  * @return User
  *
  * @throws InvalidArgumentException
  * @throws GeneralException
  */
 public function register($email, $username, $password, $password2)
 {
     $email = mb_strtolower($email, "UTF-8");
     // checks email addresses & username and throw InvalidArgumentException on error
     $this->validator->checkEmail($email);
     $this->validator->checkUsername($username);
     // Check for password strength and throw InvalidArgumentException on error
     $this->validator->checkPassword($password);
     // Check passwords match and throw InvalidArgumentException on error
     $this->validator->checkPasswordConfirmation($password, $password2);
     // check email is unique
     if ($this->gateway->getByEmail($email)) {
         $this->log("debug", "Cannot register user: Email {$email} exists");
         throw new GeneralException("Има регистриран потребител с този email адрес");
     }
     // check username is unique
     if ($this->gateway->getByUsername($username)) {
         $this->log("debug", "Cannot register user: Username {$username} exists");
         throw new GeneralException("Има регистриран потребител с това потребителско име");
     }
     $user = new User(-1, $username, $email, UserInterface::STATE_INACTIVE, "");
     // crypt password
     $user->setPassword($password);
     $passwordHash = $user->getPassword();
     // insert in the DB and get new user's ID or some other data that will be returned
     if (!($user = $this->gateway->add($email, $username, UserInterface::STATE_INACTIVE, $passwordHash))) {
         $this->log("error", "Error while inserting user in the DB {$username}<{$email}>");
         throw new GeneralException("Грешка при създаване на акаунт");
     }
     $token = $this->tokenGen->generateToken($user->getId());
     $user->setToken($token);
     return $user;
 }
Example #2
0
 /**
  * @expectedException SugiPHP\Auth2\Exception\InvalidArgumentException
  */
 public function testWithStateTrhowsExceptionIfTheValueIsNotSet()
 {
     $user = new User(2, 'demo', '*****@*****.**', 1, 'passw0rd');
     $user->setState(false);
 }