/** * 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; } }
/** * 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; } }
/** * Method to test the loadFromObject method. * @since 1.0.0 * @test */ public function testLoadFromObject() { //create an User Entity. $user = new User(); //the object without prefix to load the User Entity. $object = new \stdClass(); $object->id = 1; $object->birthday = 'Birthday'; $object->email = 'Email'; $object->firstname = 'Firstname'; $object->gender = 'Gender'; $object->lastname = 'Lastname'; $object->password = '******'; $object->salt = 'Salt'; $object->username = '******'; //load the object without prefix to the User Entity. $user->loadFromObject($object); //check whether the values are valid. $this->assertEquals(1, $user->id); $this->assertEquals('Birthday', $user->birthday); $this->assertEquals('Email', $user->email); $this->assertEquals('Firstname', $user->firstname); $this->assertEquals('Gender', $user->gender); $this->assertEquals('Lastname', $user->lastname); $this->assertEquals('Password', $user->password); $this->assertEquals('Salt', $user->salt); $this->assertEquals('Username', $user->username); //the object with prefix to load the User Entity. $object_prefix = new \stdClass(); $object_prefix->test_id = 2; $object_prefix->test_birthday = 'TestBirthday'; $object_prefix->test_email = 'TestEmail'; $object_prefix->test_firstname = 'TestFirstname'; $object_prefix->test_gender = 'TestGender'; $object_prefix->test_lastname = 'TestLastname'; $object_prefix->test_password = '******'; $object_prefix->test_salt = 'TestSalt'; $object_prefix->test_username = '******'; //load the object with prefix to the User Entity. $user->loadFromObject($object_prefix, 'test_'); //check whether the values are valid. $this->assertEquals(2, $user->id); $this->assertEquals('TestBirthday', $user->birthday); $this->assertEquals('TestEmail', $user->email); $this->assertEquals('TestFirstname', $user->firstname); $this->assertEquals('TestGender', $user->gender); $this->assertEquals('TestLastname', $user->lastname); $this->assertEquals('TestPassword', $user->password); $this->assertEquals('TestSalt', $user->salt); $this->assertEquals('TestUsername', $user->username); }
/** * 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; } }