Esempio n. 1
0
 /**
  * The login action of the Login.
  * @since 0.0.1-dev
  */
 public function login()
 {
     //get the user from the login form.
     $user = new User();
     $user->loadFromPOST('login_');
     //check if the username is valid.
     if ((new IsValidUsername())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The username is not valid!', 'login_username', LogLevel::ERROR);
         return false;
     }
     //check if the password is valid.
     if ((new IsValidPassword())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The password is not valid!', 'login_password', LogLevel::ERROR);
         return false;
     }
     //check if the ID is trusted.
     if (PROJECT_HONEYPOT_KEY !== '') {
         if (filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
             if ((new ProjectHoneypot(PROJECT_HONEYPOT_KEY))->check($_SERVER['REMOTE_ADDR'])) {
                 $this->jsonOutput('The IP you are using is not trusted!', '', LogLevel::ERROR);
                 return false;
             }
         }
     }
     //try to login the User.
     if ((new AuthenticationService())->login($user)) {
         $this->jsonOutput('The User could be logged in!', '', LogLevel::INFO, URL . 'dashboard');
         return true;
     } else {
         $this->jsonOutput('The User could not be logged in!', '', LogLevel::ERROR);
         return false;
     }
 }
Esempio n. 2
0
 /**
  * The register action of the Register.
  * @since 0.0.1-dev
  */
 public function register()
 {
     //load the user from register form.
     $user = new User();
     $user->loadFromPOST('register_');
     //check if the username is valid.
     if ((new IsValidUsername())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The username is not valid!', 'register_username', LogLevel::ERROR);
         return false;
     }
     //check if the email is valid.
     if ((new IsValidEmail())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The email is not valid!', 'register_email', LogLevel::ERROR);
         return false;
     }
     //check if the password is valid.
     if ((new IsValidPassword())->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The password is not valid!', 'register_password', LogLevel::ERROR);
         return false;
     }
     //check if the user is unique.
     if ((new IsUnique(UserRepository::build()))->isSatisfiedBy($user) === false) {
         $this->jsonOutput('The User already exists!', '', LogLevel::ERROR);
         return false;
     }
     //register the User with the AuthenticationService.
     if ((new AuthenticationService())->register($user)) {
         $this->jsonOutput('The User was successfully registered!', '', LogLevel::INFO, URL . 'login');
         return true;
     } else {
         $this->jsonOutput('The User could not be registered!', '', LogLevel::ERROR);
         return false;
     }
 }
Esempio n. 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;
     }
 }