/**
  * 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
 /**
  * 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;
     }
 }