/**
  * Checks whether the maximum inactivity time has been exceeded.
  *
  * Returns TRUE in case the maximum inactivity time has not been exceeded.
  * Otherwise returns FALSE.
  *
  * @see \Ableron\Core\Session\Validators\ValidatorInterface::isValid()
  */
 public function isValid(SessionEntity $session)
 {
     // get setting key for the timeout to use
     $settingKeySessionInactivityTimeout = Application::getRequestHandler()->isFrontendRequest() ? 'session.frontend.inactivityTimeoutValidator.timeout' : 'session.backend.inactivityTimeoutValidator.timeout';
     // return whether session has timed out
     return $session->getLastActivityTime() > DateUtil::getCurrentUtcDateTime()->sub(Application::getConfig()->get($settingKeySessionInactivityTimeout));
 }
 /**
  * @see \Ableron\Core\Session\SessionHandlerInterface::runGarbageCollector()
  */
 public function runGarbageCollector(DateInterval $inactivityTimeout)
 {
     //TODO tblasche: Implement correctly und use it
     $query = Application::getPersistenceManager()->getEntityManager()->createQuery('DELETE Core:SessionEntity s WHERE s.lastActivityTime < ?1');
     $query->setParameter(1, DateUtil::getCurrentUtcDateTime()->sub($inactivityTimeout)->format('Y-m-d H:i:s'));
     $query->execute();
 }
 /**
  * @see \Ableron\Lib\Event\EventHandlerInterface::handle()
  */
 public function handle(EventInterface $event)
 {
     /** @var \Ableron\Modules\Core\Events\LoginValidatingDataEvent $event */
     if (($loginAttemptsEntity = BruteForceProtectionService::getLoginAttemptsByUsername($event->getUsername())) !== null) {
         if ($loginAttemptsEntity->getFailedAttemptsCount() >= 5 && $loginAttemptsEntity->getLastAttemptTime()->add(new \DateInterval('PT2M')) >= DateUtil::getCurrentUtcDateTime()) {
             throw new FormParameterException(array(), 'bruteForceProtector.backend.message.possibleBruteForceDetected');
         }
     }
 }
 /**
  * Updates a session.
  *
  * Sets the last activity time to "now".
  * Updates the validation data.
  *
  * @param \Ableron\Modules\Core\Model\Entities\SessionEntity $session The session to update
  * @return void
  */
 protected function update(SessionEntity $session)
 {
     // update last activity time
     $session->setLastActivityTime(DateUtil::getCurrentUtcDateTime());
     // update validation data
     foreach ($this->getValidators() as $validator) {
         $validator->setValidationData($session);
     }
 }
示例#5
0
 /**
  * Builds the log file entry for the given log message.
  *
  * TODO: Use localized time in local log files
  *
  * @param string $logLevel Log level of the message to log
  * @param string $message The message to log
  * @param \Exception|null $exception Exception belonging to the log entry
  * @return string
  */
 private function buildLogEntry(string $logLevel, string $message, Exception $exception = null)
 {
     // build base log entry
     $logEntry = sprintf('%s %s %s', DateUtil::getCurrentUtcDateTime()->format('Y-m-d H:i:s'), StringUtil::toUpperCase($logLevel), $message);
     // add exception information if present
     if ($exception !== null) {
         $logEntry .= StringUtil::CHAR_LINE_FEED . sprintf('Exception: %s in file %s on line %s', $exception->getMessage(), $exception->getFile(), $exception->getLine()) . StringUtil::CHAR_LINE_FEED . ExceptionUtil::getFullTrace($exception);
     }
     // return log entry
     return $logEntry;
 }
示例#6
0
 /**
  * Initializes the entity.
  *
  * @param \Ableron\Modules\Core\Model\Entities\UserEntity $user The user belonging to the session
  */
 public function __construct(UserEntity $user = null)
 {
     $this->id = StringUtil::getRandomString(24);
     $this->setCreationTime(DateUtil::getCurrentUtcDateTime());
     $this->setLastActivityTime($this->getCreationTime());
     $this->data = array();
     // TODO
     if ($user !== null) {
         $this->setUser($user);
     }
 }
 /**
  * Tests whether get() works as expected.
  *
  * @return void
  */
 public function testGet()
 {
     $storage = $this->getNewStorageInstance();
     $this->assertNull($storage->get('xyz'));
     $storageItem = new BasicStorageItem('k');
     $storage->set($storageItem);
     $this->assertNotNull($storage->get('k'));
     $this->assertSame('k', $storage->get('k')->getKey());
     $this->assertNull($storage->get('k')->getValue());
     $this->assertNull($storage->get('k')->getExpirationTime());
     $storageItem = new BasicStorageItem('k', 42, new DateInterval('PT5M'));
     $storage->set($storageItem);
     $this->assertNotNull($storage->get('k'));
     $this->assertSame('k', $storage->get('k')->getKey());
     $this->assertSame(42, $storage->get('k')->getValue());
     $this->assertNotNull($storage->get('k')->getExpirationTime());
     $this->assertTrue($storage->get('k')->getExpirationTime() > DateUtil::getCurrentUtcDateTime());
 }
 /**
  * Tests whether expiresAfter() works as expected.
  *
  * @return void
  */
 public function testExpiresAfter()
 {
     $storageItem = new BasicStorageItem('key');
     $this->assertNull($storageItem->getExpirationTime());
     $lifetimePeriod = new DateInterval('PT5M');
     $storageItem->expiresAfter($lifetimePeriod);
     $this->assertNotNull($storageItem->getExpirationTime());
     $this->assertTrue($storageItem->getExpirationTime() > DateUtil::getCurrentUtcDateTime());
     $storageItem->expiresAfter(null);
     $this->assertNull($storageItem->getExpirationTime());
 }
示例#9
0
 /**
  * Tests whether getUtcTimeZone() works as expected.
  *
  * @return void
  */
 public function gtestGetUtcTimeZone()
 {
     $this->assertSame('UTC', DateUtil::getUtcTimeZone()->getName());
 }
 /**
  * Adds a failed attempt.
  *
  * @return void
  */
 public function addFailedAttempt()
 {
     // increase failed attempts count
     $this->failedAttemptsCount++;
     // update time of last failed attempt
     $this->lastAttemptTime = DateUtil::getCurrentUtcDateTime();
 }
示例#11
0
 /**
  * @see \Ableron\Lib\Storage\StorageItemInterface::isExpired()
  */
 public function isExpired()
 {
     return $this->getExpirationTime() !== null && $this->getExpirationTime() < DateUtil::getCurrentUtcDateTime();
 }
示例#12
0
 /**
  * Initializes the entity.
  */
 public function __construct()
 {
     $this->setOccurrenceTime(DateUtil::getCurrentUtcDateTime());
     $this->setRequestMethod(Application::getRequestHandler()->getRequest()->getMethod());
     $this->setRequestUri(Application::getRequestHandler()->getRequest()->getUri());
 }
示例#13
0
 /**
  * @see \Psr\Cache\CacheItemInterface::expiresAfter()
  */
 public function expiresAfter($time)
 {
     if (is_numeric($time)) {
         $this->expiresAt(DateUtil::getCurrentUtcDateTime()->add(DateInterval::createFromDateString('+' . $time . ' seconds')));
     } else {
         $this->expiresAt($time === null ? null : DateUtil::getCurrentUtcDateTime()->add($time));
     }
     return $this;
 }
示例#14
0
 /**
  * Tests whether save() works as expected.
  *
  * @return void
  */
 public function testSave()
 {
     $cache = $this->getNewCacheInstance();
     $expirationTime = DateUtil::getCurrentUtcDateTime()->add(new DateInterval('PT15M'));
     $cacheItemToSave = new BasicCacheItem('foobar', 'foo');
     $cacheItemToSave->expiresAt($expirationTime);
     $cache->save($cacheItemToSave);
     $cacheItem = $cache->getItem('foobar');
     $this->assertNotNull($cacheItem);
     $this->assertSame('foobar', $cacheItem->getKey());
     $this->assertTrue($cacheItem->isHit());
     $this->assertSame('foo', $cacheItem->get());
     $this->assertEquals($expirationTime, $cacheItem->getExpirationTime());
 }