예제 #1
0
 function testAddDeleteAccount()
 {
     $expected = new UserAccount('new_acc', 'pass', 'ADMIN');
     $this->db->addAccount($expected);
     $result = $this->db->queryUser('new_acc');
     $this->db->deleteUser('new_acc');
     // clean up after ourselves
     $this->assertIdentical($expected, $result);
 }
예제 #2
0
 /**
  * Checks if valid registration details have been submitted.
  * @throws Exception If an error occurs with registration.
  */
 public function checkValidRegistration()
 {
     if (empty($_POST['username']) && empty($_POST['password']) && empty($_POST['password2'])) {
         /* hasn't submitted any information */
         return;
     }
     $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
     $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
     $password2 = filter_var($_POST['password2'], FILTER_SANITIZE_STRING);
     $this->model->setUsername($username);
     if ($username === false) {
         $this->model->setError('Invalid username.');
     } else {
         if (strlen($username) < 3) {
             $this->model->setError('Username requires 3 characters.');
         } else {
             if (strlen($username) > 50) {
                 $this->model->setError('Username exceeds 50 characters.');
             } else {
                 if ($password === false) {
                     $this->model->setError('Invalid password.');
                 } else {
                     if (strlen($password) < 3) {
                         $this->model->setError('Password requires 3 characters.');
                     } else {
                         if (strlen($password) > 50) {
                             $this->model->setError('Password exceeds 50 characters.');
                         } else {
                             if ($password !== $password2) {
                                 $this->model->setError('Passwords do not match.');
                             } else {
                                 $database = new DatabaseWrapper();
                                 $database->connect(DB_NAME);
                                 try {
                                     /* if the queryUser method executes without any errors then it means an account does exist */
                                     $database->queryUser($username);
                                     throw new AlreadyRegisteredException();
                                 } catch (AlreadyRegisteredException $e) {
                                     throw $e;
                                     // throw the error upwards for it to be handled correctly
                                 } catch (Exception $e) {
                                     /* error thrown as the statement failed to find an account */
                                 }
                                 $database->addAccount(new UserAccount($username, password_hash($password, PASSWORD_BCRYPT), 'USER'));
                                 $this->model->registered();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
예제 #3
0
 /**
  * Checks for valid login details.
  * @throws Exception If there was an error with the login procedure.
  */
 public function checkValidLogin()
 {
     if (empty($_POST['username']) && empty($_POST['password'])) {
         /* hasn't submitted any information */
         return;
     }
     $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
     $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
     $database = new DatabaseWrapper();
     $database->connect(DB_NAME);
     $account = null;
     try {
         $account = $database->queryUser($username);
     } catch (Exception $e) {
         throw new LoginDetailsException();
     }
     $passwordHash = $account->getPasswordHash();
     if (!password_verify($password, $passwordHash)) {
         throw new LoginDetailsException();
     }
     $_SESSION['username'] = $account->getUsername();
     $_SESSION['rank'] = $account->getRank();
     $this->model->loggedIn();
 }