示例#1
0
 /**
  * Tests UserLoginData->getLoginAttempts() && UserLoginData->setLoginAttempts() && UserLoginData->incLoginAttempts()
  */
 public function testGetSetIncLoginAttempts()
 {
     $this->assertNull($this->UserLoginData->getLoginAttempts());
     $this->UserLoginData->incLoginAttempts();
     $this->assertEquals(1, $this->UserLoginData->getLoginAttempts());
     $this->UserLoginData->incLoginAttempts();
     $this->assertEquals(2, $this->UserLoginData->getLoginAttempts());
     $incTimes = rand(5, 30);
     for ($i = 0; $i < $incTimes; $i++) {
         $this->UserLoginData->incLoginAttempts();
     }
     $this->assertEquals(2 + $incTimes, $this->UserLoginData->getLoginAttempts());
     $rand = rand(0, 200);
     $this->UserLoginData->setLoginAttempts($rand);
     $this->assertEquals($rand, $this->UserLoginData->getLoginAttempts());
 }
示例#2
0
 private static function userLogin(UserLoginData $loginData = null, $password, $partnerId = null, $validatePassword = true)
 {
     $requestedPartner = $partnerId;
     if (!$loginData) {
         throw new kUserException('', kUserException::LOGIN_DATA_NOT_FOUND);
     }
     // check if password is valid
     if ($validatePassword && !$loginData->isPasswordValid($password)) {
         if (time() < $loginData->getLoginBlockedUntil(null)) {
             throw new kUserException('', kUserException::LOGIN_BLOCKED);
         }
         if ($loginData->getLoginAttempts() + 1 >= $loginData->getMaxLoginAttempts()) {
             $loginData->setLoginBlockedUntil(time() + $loginData->getLoginBlockPeriod());
             $loginData->setLoginAttempts(0);
             $loginData->save();
             throw new kUserException('', kUserException::LOGIN_RETRIES_EXCEEDED);
         }
         $loginData->incLoginAttempts();
         $loginData->save();
         throw new kUserException('', kUserException::WRONG_PASSWORD);
     }
     if (time() < $loginData->getLoginBlockedUntil(null)) {
         throw new kUserException('', kUserException::LOGIN_BLOCKED);
     }
     $loginData->setLoginAttempts(0);
     $loginData->save();
     $passUpdatedAt = $loginData->getPasswordUpdatedAt(null);
     if ($passUpdatedAt && time() > $passUpdatedAt + $loginData->getPassReplaceFreq()) {
         throw new kUserException('', kUserException::PASSWORD_EXPIRED);
     }
     if (!$partnerId) {
         $partnerId = $loginData->getLastLoginPartnerId();
     }
     if (!$partnerId) {
         throw new kUserException('', kUserException::INVALID_PARTNER);
     }
     $partner = PartnerPeer::retrieveByPK($partnerId);
     $kuser = kuserPeer::getByLoginDataAndPartner($loginData->getId(), $partnerId);
     if (!$kuser || $kuser->getStatus() != KuserStatus::ACTIVE || !$partner || $partner->getStatus() != Partner::PARTNER_STATUS_ACTIVE) {
         // if a specific partner was requested - throw error
         if ($requestedPartner) {
             if ($partner && $partner->getStatus() != Partner::PARTNER_STATUS_ACTIVE) {
                 throw new kUserException('', kUserException::USER_IS_BLOCKED);
             } else {
                 if ($kuser && $kuser->getStatus() == KuserStatus::BLOCKED) {
                     throw new kUserException('', kUserException::USER_IS_BLOCKED);
                 } else {
                     throw new kUserException('', kUserException::USER_NOT_FOUND);
                 }
             }
         }
         // if kuser was found, keep status for following exception message
         $kuserStatus = $kuser ? $kuser->getStatus() : null;
         // if no specific partner was requested, but last logged in partner is not available, login to first found partner
         $kuser = null;
         $kuser = self::findFirstValidKuser($loginData->getId(), $partnerId);
         if (!$kuser) {
             if ($kuserStatus === KuserStatus::BLOCKED) {
                 throw new kUserException('', kUserException::USER_IS_BLOCKED);
             }
             throw new kUserException('', kUserException::USER_NOT_FOUND);
         }
     }
     if ($kuser->getIsAdmin() && !in_array($kuser->getPartnerId(), kConf::get('no_save_of_last_login_partner_for_partner_ids'))) {
         $loginData->setLastLoginPartnerId($kuser->getPartnerId());
     }
     $loginData->save();
     $kuser->setLastLoginTime(time());
     $kuser->save();
     return $kuser;
 }