/** * @test */ public function removeIpLockFromObjectStorageHoldingIpLocks() { $ipLock = new \Pixelink\Simplepoll\Domain\Model\IpLock(); $ipLocksObjectStorageMock = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', array('detach'), array(), '', FALSE); $ipLocksObjectStorageMock->expects($this->once())->method('detach')->with($this->equalTo($ipLock)); $this->inject($this->subject, 'ipLocks', $ipLocksObjectStorageMock); $this->subject->removeIpLock($ipLock); }
/** * check vote ok from ip * * checks if the current user is allowed to vote with his ip. also writes the ip to the lock list * and calls the garbage collector if the ipBlock is activated via typoscript. * * @param \Pixelink\Simplepoll\Domain\Model\SimplePoll $simplePoll * @return mixed TRUE if the user is allowed to vote, string with the error message if not */ protected function checkVoteOkFromIp(\Pixelink\Simplepoll\Domain\Model\SimplePoll $simplePoll) { $ipBlock = $this->settings['ipBlock']; if (!(strtolower($ipBlock) == 'true' || $ipBlock == '1')) { // IPs are not blocked, so the user can vote return TRUE; } // get the settings for multiple votes either global or local if ($this->settings['useTyposcriptSettings'] == 'true' || $this->settings['useTyposcriptSettings'] == '1') { $allowMultipleVote = $this->settings['allowMultipleVote']; } else { $allowMultipleVote = $simplePoll->getAllowMultipleVote(); } //call the garbage collector on the ip locks. $this->cleanupIpLocks($simplePoll); $userIp = $_SERVER['REMOTE_ADDR']; $lockedIp = $this->ipLockRepository->getIpInPoll($simplePoll, $userIp); if ($lockedIp) { // in here we know that the user already voted for this poll. // now we need to check if mutliple votes are allowed and if so, if enough time passed since the last vote. if ($allowMultipleVote) { //if the timestamp in the ip lock is not null, he is not allowed to vote again (yet) $lockedIpTimestamp = $lockedIp->getTimestamp(); if ($lockedIpTimestamp !== NULL) { return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('tx_simplepoll.voteNotNow', 'Simplepoll'); } else { // vote is allowed, so we simply remove the current lock and let it create again down below $simplePoll->removeIpLock($lockedIp); } } else { //voted before and not allowed to vote again, because no multiple votes return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('tx_simplepoll.voteOnlyOnce', 'Simplepoll'); } } // first or allowed vote from this IP, so add the user to the ip lock list. return true because he is allowed to vote $ipLock = $this->objectManager->get(\Pixelink\Simplepoll\Domain\Model\IpLock::class); $ipLock->setAddress($userIp); $ipLock->setTimestamp(new \DateTime()); $simplePoll->addIpLock($ipLock); $this->simplePollRepository->update($simplePoll); return TRUE; }
public function getIpInPoll(\Pixelink\Simplepoll\Domain\Model\SimplePoll $simplePoll, $userIp) { $query = $this->createQuery(); $query->matching($query->logicalAnd($query->equals('simplepoll', $simplePoll->getUid()), $query->equals('address', $userIp))); return $query->execute()->getFirst(); }