X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=blobdiff_plain;f=application%2Fship-simu%2Fmain%2Flogin%2Fclass_ShipSimuUserLogin.php;h=1e094cb86c45a44ebf7c33f724f99fdd4edf01e2;hp=2ffc777ad77035bb144781a6fcfcfcc2df4bfa0c;hb=4c35e8280767c1e33832097060e39a32b80ffd01;hpb=65d72098d3ec6a0ef7782668264fc6f33f9ebeb6 diff --git a/application/ship-simu/main/login/class_ShipSimuUserLogin.php b/application/ship-simu/main/login/class_ShipSimuUserLogin.php index 2ffc777..1e094cb 100644 --- a/application/ship-simu/main/login/class_ShipSimuUserLogin.php +++ b/application/ship-simu/main/login/class_ShipSimuUserLogin.php @@ -22,6 +22,11 @@ * along with this program. If not, see . */ class ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser { + /** + * The hashed password + */ + private $hashedPassword = ""; + /** * Protected constructor * @@ -61,10 +66,110 @@ class ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser { * in a boolean attribute which is then readable by a matching getter. * * @param $requestInstance An instance of a Requestable class + * @param $responseInstance An instance of a Responseable class + * @return void + * @throws UserAuthMethodException If wether username nor email login + * was detected + * @throws MissingMethodException If a method was not found in the + * User class + * @throws UserPasswordMismatchException If the supplied password did not + * match with the stored password + * @todo We need to add something here which will make more than one + * @todo guest logins, users who are online but based on the same + * @todo user account. + */ + public function doLogin (Requestable $requestInstance, Responseable $responseInstance) { + // By default no method is selected + $method = null; + $data = ""; + + // Detect login method (username or email) and try to get a userinstance + if (!is_null($requestInstance->getRequestElement('username'))) { + // Username found! + $method = "createUserByUsername"; + $data = $requestInstance->getRequestElement('username'); + } elseif (!is_null($requestInstance->getRequestElement('email'))) { + // Email found! + $method = "createUserByEmail"; + $data = $requestInstance->getRequestElement('email'); + } + + // Is a method detected? + if (is_null($method)) { + // Then abort here + throw new UserAuthMethodException($this, self::EXCEPTION_MISSING_METHOD); + } elseif (!method_exists($this->getConfigInstance()->readConfig('user_class'), $method)) { + // The method is invalid! + throw new MissingMethodException(array($this, $method), self::EXCEPTION_MISSING_METHOD); + } + + // Get a instance of the registry + $userInstance = Registry::getRegistry()->getInstance('user'); + + // Is there an instance? + if (is_null($userInstance)) { + // Get a user instance + $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), $method), array($data)); + + // Remember this new instance in registry + Registry::getRegistry()->addInstance($userInstance); + } // END - if + + // Is the password correct? + if (!$userInstance->ifPasswordHashMatches($requestInstance)) { + // Mismatching password + throw new UserPasswordMismatchException(array($this, $userInstance), User::EXCEPTION_USER_PASS_MISMATCH); + } // END - if + + // ToDo place + + // Now do the real login. This can be cookie- or session-based login + // which depends on the admins setting then on the user's taste. + // 1) Get a login helper instance + $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance)); + + // 2) Execute the login. This will now login... + $helperInstance->executeLogin($responseInstance); + } + + /** + * Determines wether the login was fine. This is done by checking if the 'login' instance is in registry + * + * @return $loginDone Wether the login was fine or not + */ + public function ifLoginWasSuccessfull () { + // Is the registry key there? + $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable); + + // Return the result + return $loginDone; + } + + /** + * Encrypt the given request key or throw an exception if the key was not + * found in the request + * + * @param $requestKey Key in request class * @return void */ - public function doLogin (Requestable $requestInstance) { - $this->partialStub(); + public function encryptPassword ($requestKey) { + // Check if the password is found in the request + if ($this->getRequestInstance()->isRequestElementSet($requestKey)) { + // So encrypt the password and store it for later usage in + // the request: + + // Get the plain password + $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey); + + // Get user instance + $userInstance = Registry::getRegistry()->getInstance('user'); + + // Get a crypto helper and hash the password + $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashPassword($plainPassword, $userInstance->getPasswordHash()); + + // Store the hash back in the request + $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword); + } // END - if } }