/**
  * Method to register an User Entity.
  * @param User $user The User Entity which will be registered.
  * @return bool The state if the User Entity could be registered successfully.
  * @since 0.0.1-dev
  */
 public function register(User $user)
 {
     //get the User Entity with the hashed password information.
     $hashingService = new HashingService();
     $user = $hashingService->hash($user);
     //save the User Entity and return the state.
     return UserMapper::build()->save($user);
 }
Beispiel #2
0
 /**
  * Method to test the update method.
  * @since 1.0.0
  * @test
  */
 public function testUpdate()
 {
     //the User Entity which will be updated on database.
     $user = new User();
     $user->id = 2;
     $user->birthday = '1996-08-17';
     $user->email = '*****@*****.**';
     $user->firstname = 'Ida Q.';
     $user->gender = 'F';
     $user->lastname = 'Garnes';
     $user->password = '******';
     $user->salt = 'd02991c2a8e062a6d419883b6e3a1e58c4090029d1078b58c89c0f096177c379f0552bce705aaf47d7263a70cbf3e527c3662965d62d7cdf34df0053702c50ad';
     $user->username = '******';
     //the UserMapper to update the User Entity on database.
     $userMapper = new UserMapper($this->getConnection()->getConnection());
     $userMapper->update($user);
     //get the actual and expected table.
     $queryTable = $this->getConnection()->createQueryTable('user', 'SELECT * FROM user');
     $expectedDataSet = __DIR__ . '/DataSets/User/user-update.xml';
     $expectedTable = $this->createXMLDataSet($expectedDataSet)->getTable('user');
     //check whether the tables are equal.
     $this->assertTablesEqual($expectedTable, $queryTable);
     //another Entity than User Entity is not valid on the UserMapper.
     $this->assertFalse($userMapper->update(new Clan()));
 }
Beispiel #3
0
 /**
  * The save action of the User.
  * @return bool The state if the User was successfully saved.
  * @since 0.0.1-dev
  */
 public function save()
 {
     //get the session.
     $this->needSession();
     //get the information from post.
     $user = new User();
     $user->loadFromPOST('user_');
     //check if the birthday is valid.
     if ((new IsValidBirthday())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The birthday is not valid!', 'user_birthday', LogLevel::ERROR);
         return false;
     }
     //check if the email is valid.
     if ((new IsValidEmail())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The email is not valid!', 'user_email', LogLevel::ERROR);
         return false;
     }
     //check if the firstname is valid.
     if ((new IsValidFirstname())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The firstname is not valid!', 'user_firstname', LogLevel::ERROR);
         return false;
     }
     //check if the gender is valid.
     if ((new IsValidGender())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The gender is not valid!', 'user_gender', LogLevel::ERROR);
         return false;
     }
     //check if the lastname is valid.
     if ((new IsValidLastname())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The lastname is not valid!', 'user_lastname', LogLevel::ERROR);
         return false;
     }
     //check if the username is valid.
     if ((new IsValidUsername())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The username is not valid!', 'user_username', LogLevel::ERROR);
         return false;
     }
     //check if a password is given.
     if ($user->password !== '' || $user->id < 1) {
         //check if the password is valid.
         if ((new IsValidPassword())->isSatisfiedBy($user) === false) {
             $this->jsonOutput('The password is not valid!', 'user_password', LogLevel::ERROR);
             return false;
         } else {
             $hashingService = new HashingService();
             $user = $hashingService->hash($user);
         }
     }
     //check if the password should be changed.
     if ($user->password === '') {
         $userDB = UserRepository::build()->findByID($user->id);
         //check if the User Entity was found.
         if (count($userDB) === 1) {
             $userDB = $userDB[0];
             //check if the ID is the same.
             if ($user->id == $userDB->id) {
                 $user->password = $userDB->password;
                 $user->salt = $userDB->salt;
             } else {
                 $this->jsonOutput('The User could not be saved!', '', LogLevel::ERROR);
                 return false;
             }
         }
     }
     //save the User on the database.
     if (UserMapper::build()->save($user)) {
         $this->jsonOutput('The User was saved successfully!', '', LogLevel::INFO, URL . 'user');
         return true;
     } else {
         $this->jsonOutput('The User could not be saved!', '', LogLevel::ERROR);
         return false;
     }
 }
Beispiel #4
0
 /**
  * Method to build a new object of UserRepository.
  * @return UserRepository The created object of UserRepository.
  * @since 0.0.1-dev
  */
 public static function build()
 {
     return new self(UserMapper::build());
 }
Beispiel #5
0
 /**
  * Method to test if the method update() works.
  * @since 0.0.1-dev
  * @test
  */
 public function testSaveUpdate()
 {
     //The User which will be updated on database.
     $user = new User();
     $user->id = 2;
     $user->birthday = '1996-08-17';
     $user->email = '*****@*****.**';
     $user->firstname = 'Ida Q.';
     $user->gender = 'F';
     $user->lastname = 'Garnes';
     $user->password = '******';
     $user->username = '******';
     //The UserMapper to update the User on database.
     $userMapper = new UserMapper($this->pdo);
     $userMapper->save($user);
     //Get the actual and expected table.
     $queryTable = $this->getConnection()->createQueryTable('user', 'SELECT * FROM user');
     $expectedDataSet = __DIR__ . '/DataSets/User/user-save-update.xml';
     $expectedTable = $this->createXMLDataSet($expectedDataSet)->getTable('user');
     //Check if the tables are equal.
     $this->assertTablesEqual($expectedTable, $queryTable);
 }