public function testCheck() { $this->assertTrue(Security::check("hello", "5d41402abc4b2a76b9719d911017c592", Security::HASHTYPE_MD5)); $this->assertTrue(Security::check("hi", "49f68a5c8493ec2c0bf489821c21fc3b", Security::HASHTYPE_MD5)); $this->assertTrue(Security::check("hello", '$2a$08$uHDGnFAtkAbBdH/iRt.jQOViR6bd2g3wwn6IS7MyvlMHoMvvBXDyi', Security::HASHTYPE_PHPASS)); $this->assertFalse(Security::check("hello2", "5d41402abc4b2a76b9719d911017c592", Security::HASHTYPE_MD5)); $this->assertFalse(Security::check("hello2", "hello2", Security::HASHTYPE_MD5)); $this->assertFalse(Security::check("hello2", "5d41402abc4b2a76b9719d911017c592", Security::HASHTYPE_PHPASS)); }
/** * Tries to log user in with supplied credentials. * @param string $name username * @param string $pass password * @return bool true if login was successful */ public function login($name, $pass) { if ($this->data != null) { $this->logout(); } /// Username is case-insensitive. $name = strtolower($name); $users = Repositories::getRepository(Repositories::User)->findBy(['name' => $name]); if (!empty($users)) { /** * @var $user \User */ $user = $users[0]; if ($user->getActivationCode() !== '') { // Non-empty activation code means the account is not yet activated. return false; } $authenticationSuccess = Security::check($pass, $user->getPass(), $user->getEncryptionType()); if ($authenticationSuccess) { $this->data = array('id' => $user->getId(), 'name' => $user->getName(), 'privileges' => $user->getType()->getPrivileges(), 'realName' => $user->getRealName(), 'email' => $user->getEmail(), 'lastAccess' => $user->getLastAccess()->format("Y-m-d H:i:s"), 'applicationVersion' => implode('.', Config::get('version')), User::sendEmailOnAssignmentAvailableStudent => $user->getSendEmailOnNewAssignment() ? 1 : 0, User::sendEmailOnSubmissionConfirmedTutor => $user->getSendEmailOnNewSubmission() ? 1 : 0, User::sendEmailOnSubmissionRatedStudent => $user->getSendEmailOnSubmissionRated() ? 1 : 0); $this->refresh(); $user->setLastAccess(new \DateTime()); Repositories::persistAndFlush($user); $this->entity = $user; return true; } else { return false; } } return false; }