Skip to content

Commit

Permalink
[FEATURE] Respect authorization in activity protocol, conversion funn…
Browse files Browse the repository at this point in the history
…el and fingerprint profiles
  • Loading branch information
einpraegsam committed Feb 28, 2024
1 parent 49185ef commit 6a7a114
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 29 deletions.
16 changes: 5 additions & 11 deletions Classes/Controller/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);
namespace In2code\Lux\Controller;

use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception as ExceptionDbal;
use In2code\Lux\Domain\Factory\VisitorFactory;
use In2code\Lux\Domain\Model\Visitor;
Expand All @@ -23,7 +24,6 @@
use In2code\Lux\Exception\ConfigurationException;
use In2code\Lux\Exception\EmailValidationException;
use In2code\Lux\Exception\FakeException;
use In2code\Lux\Exception\FileNotFoundException;
use In2code\Lux\Utility\BackendUtility;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -79,6 +79,7 @@ public function initializeDispatchRequestAction(): void
* @param array $arguments
* @return ResponseInterface
* @noinspection PhpUnused
* @throws InvalidConfigurationTypeException
*/
public function dispatchRequestAction(
string $dispatchAction,
Expand Down Expand Up @@ -378,10 +379,6 @@ protected function callAdditionalTrackers(Visitor $visitor, array $arguments): v
$luxletterTracker->trackFromLuxletterLink();
}

/**
* @param Visitor $visitor
* @return array
*/
protected function afterAction(Visitor $visitor): array
{
/** @var AfterTrackingEvent $event */
Expand All @@ -391,10 +388,6 @@ protected function afterAction(Visitor $visitor): array
return $event->getResults();
}

/**
* @param Throwable $exception
* @return array
*/
protected function getError(Throwable $exception): array
{
$this->eventDispatcher->dispatch(
Expand Down Expand Up @@ -422,12 +415,13 @@ protected function getError(Throwable $exception): array
* @param bool $tempVisitor
* @return Visitor
* @throws ConfigurationException
* @throws ExceptionDbal
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws IllegalObjectTypeException
* @throws UnknownObjectException
* @throws FileNotFoundException
* @throws InvalidConfigurationTypeException
* @throws UnknownObjectException
* @throws Exception
*/
protected function getVisitor(string $identificator, bool $tempVisitor = false): Visitor
{
Expand Down
28 changes: 18 additions & 10 deletions Classes/Domain/Factory/VisitorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
declare(strict_types=1);
namespace In2code\Lux\Domain\Factory;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception as ExceptionDbalDriver;
use Doctrine\DBAL\Exception as ExceptionDbal;
use In2code\Lux\Domain\Factory\Ipinformation\Handler;
use In2code\Lux\Domain\Model\Fingerprint;
use In2code\Lux\Domain\Model\Visitor;
use In2code\Lux\Domain\Repository\VisitorRepository;
use In2code\Lux\Domain\Service\SiteService;
use In2code\Lux\Domain\Service\VisitorMergeService;
use In2code\Lux\Events\Log\LogVisitorEvent;
use In2code\Lux\Events\StopAnyProcessBeforePersistenceEvent;
use In2code\Lux\Events\VisitorFactoryAfterCreateNewEvent;
use In2code\Lux\Events\VisitorFactoryBeforeCreateNewEvent;
use In2code\Lux\Exception\ConfigurationException;
use In2code\Lux\Exception\FileNotFoundException;
use In2code\Lux\Exception\FingerprintMustNotBeEmptyException;
use In2code\Lux\Exception\Validation\IdentificatorFormatException;
use In2code\Lux\Utility\ConfigurationUtility;
use In2code\Lux\Utility\CookieUtility;
use In2code\Lux\Utility\FrontendUtility;
use In2code\Lux\Utility\IpUtility;
use In2code\Lux\Utility\StringUtility;
use Psr\EventDispatcher\EventDispatcherInterface;
Expand All @@ -42,14 +44,18 @@ class VisitorFactory
* @param string $identificator
* @param bool $tempVisitor If there is no fingerprint (doNotTrack) but we even want to generate a visitor object
* @throws FingerprintMustNotBeEmptyException
* @throws IdentificatorFormatException
*/
public function __construct(string $identificator, bool $tempVisitor = false)
{
$this->checkIdentificator($identificator);
if ($identificator === '' && $tempVisitor === true) {
$identificator = StringUtility::getRandomString(32, false);
}
$this->fingerprint = GeneralUtility::makeInstance(Fingerprint::class)->setValue($identificator);
$this->fingerprint = GeneralUtility::makeInstance(Fingerprint::class)
->setValue($identificator)
->setSite(GeneralUtility::makeInstance(SiteService::class)
->getSiteIdentifierFromPageIdentifier(FrontendUtility::getCurrentPageIdentifier()));
$this->visitorRepository = GeneralUtility::makeInstance(VisitorRepository::class);
$this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
$this->eventDispatcher->dispatch(
Expand All @@ -60,13 +66,13 @@ public function __construct(string $identificator, bool $tempVisitor = false)
/**
* @return Visitor
* @throws ConfigurationException
* @throws DBALException
* @throws ExceptionDbal
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws FileNotFoundException
* @throws IllegalObjectTypeException
* @throws InvalidConfigurationTypeException
* @throws UnknownObjectException
* @throws ExceptionDbalDriver
*/
public function getVisitor(): Visitor
{
Expand All @@ -90,9 +96,11 @@ public function getVisitor(): Visitor
* respected, to not lose visitors when changing lux from 6.x to 7.x
*
* @return Visitor|null
* @throws ConfigurationException
* @throws IllegalObjectTypeException
* @throws UnknownObjectException
* @throws DBALException
* @throws ExceptionDbalDriver
* @throws ExceptionDbal
*/
protected function getVisitorFromDatabaseByFingerprint(): ?Visitor
{
Expand Down Expand Up @@ -133,13 +141,12 @@ protected function getVisitorFromDatabaseByLegacyCookie(): ?Visitor
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws IllegalObjectTypeException
* @throws FileNotFoundException
* @throws InvalidConfigurationTypeException
*/
protected function createNewVisitor(): Visitor
{
$visitor = GeneralUtility::makeInstance(Visitor::class);
$visitor->addFingerprint($this->fingerprint);
$visitor = GeneralUtility::makeInstance(Visitor::class)
->addFingerprint($this->fingerprint);
$this->enrichNewVisitorWithIpInformation($visitor);
$visitor->resetCompanyAutomatic(); // must be after enrichNewVisitorWithIpInformation()
/** @var LogVisitorEvent $event */
Expand All @@ -150,10 +157,11 @@ protected function createNewVisitor(): Visitor
/**
* @param Visitor $visitor
* @return void
* @throws ConfigurationException
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws IllegalObjectTypeException
* @throws ConfigurationException
* @throws InvalidConfigurationTypeException
*/
protected function enrichNewVisitorWithIpInformation(Visitor $visitor)
{
Expand Down
28 changes: 28 additions & 0 deletions Classes/Domain/Model/Fingerprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
declare(strict_types=1);
namespace In2code\Lux\Domain\Model;

use In2code\Lux\Domain\Service\SiteService;
use In2code\Lux\Exception\FingerprintMustNotBeEmptyException;
use In2code\Lux\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WhichBrowser\Parser;

Expand All @@ -17,6 +19,7 @@ class Fingerprint extends AbstractModel
protected string $value = '';
protected string $domain = '';
protected string $userAgent = '';
protected string $site = '';
protected int $type = 0;

public function __construct(string $domain = '', string $userAgent = '')
Expand Down Expand Up @@ -122,6 +125,17 @@ public function setUserAgent(string $userAgent): self
return $this;
}

public function getSite(): string
{
return $this->site;
}

public function setSite(string $site): self
{
$this->site = $site;
return $this;
}

public function getType(): int
{
return $this->type;
Expand All @@ -146,4 +160,18 @@ public function getTypeString(): string
}
return 'Unknown';
}

/**
* Check if this record can be viewed by current editor
*
* @return bool
*/
public function canBeRead(): bool
{
if (BackendUtility::isAdministrator() || $this->site === '') {
return true;
}
$sites = GeneralUtility::makeInstance(SiteService::class)->getAllowedSites();
return array_key_exists($this->getSite(), $sites);
}
}
16 changes: 16 additions & 0 deletions Classes/Domain/Model/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use In2code\Lux\Domain\Repository\LinklistenerRepository;
use In2code\Lux\Domain\Repository\SearchRepository;
use In2code\Lux\Domain\Repository\UtmRepository;
use In2code\Lux\Domain\Service\SiteService;
use In2code\Lux\Utility\BackendUtility;
use In2code\Luxenterprise\Domain\Model\AbTestingPage;
use In2code\Luxenterprise\Domain\Repository\AbTestingPageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -201,4 +203,18 @@ public static function getIdentifiedStatus(): array
Log::STATUS_IDENTIFIED_EMAIL4LINK,
];
}

/**
* Check if this record can be viewed by current editor
*
* @return bool
*/
public function canBeRead(): bool
{
if (BackendUtility::isAdministrator() || $this->site === '') {
return true;
}
$sites = GeneralUtility::makeInstance(SiteService::class)->getAllowedSites();
return array_key_exists($this->getSite(), $sites);
}
}
15 changes: 15 additions & 0 deletions Classes/Domain/Model/Pagevisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use In2code\Lux\Domain\Repository\NewsvisitRepository;
use In2code\Lux\Domain\Service\Referrer\Readable;
use In2code\Lux\Domain\Service\SiteService;
use In2code\Lux\Utility\BackendUtility;
use In2code\Lux\Utility\FrontendUtility;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
Expand Down Expand Up @@ -194,4 +195,18 @@ public function getNewsvisit(): ?Newsvisit
$newsvisitRepository = GeneralUtility::makeInstance(NewsvisitRepository::class);
return $newsvisitRepository->findByPagevisit($this);
}

/**
* Check if this record can be viewed by current editor
*
* @return bool
*/
public function canBeRead(): bool
{
if (BackendUtility::isAdministrator() || $this->site === '') {
return true;
}
$sites = GeneralUtility::makeInstance(SiteService::class)->getAllowedSites();
return array_key_exists($this->getSite(), $sites);
}
}
45 changes: 38 additions & 7 deletions Classes/Domain/Model/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,18 @@ public function increaseCategoryscoringByCategory(int $value, Category $category
$this->setCategoryscoringByCategory($newScoring, $category);
}

public function getFingerprints(): ?ObjectStorage
public function getFingerprints(): array
{
return $this->fingerprints instanceof LazyLoadingProxy
$fingerprints = $this->fingerprints instanceof LazyLoadingProxy
? $this->fingerprints->_loadRealInstance()
: $this->fingerprints;
$fpArray = $fingerprints->toArray();
foreach ($fpArray as $key => $fingerprint) {
if ($fingerprint->canBeRead() === false) {
unset($fpArray[$key]);
}
}
return $fpArray;
}

/**
Expand All @@ -257,7 +264,7 @@ public function getFingerprints(): ?ObjectStorage
*/
public function getFingerprintsSorted(): array
{
$fingerprints = $this->getFingerprints()->toArray();
$fingerprints = $this->getFingerprints();
return array_reverse($fingerprints);
}

Expand Down Expand Up @@ -432,6 +439,18 @@ public function setVisits(int $visits): self
return $this;
}

public function getPagevisitsAuthorized(): array
{
$pagevisits = $this->pagevisits->toArray();
foreach ($pagevisits as $key => $pagevisit) {
/** @var Pagevisit $pagevisits */
if ($pagevisit->canBeRead() === false) {
unset($pagevisits[$key]);
}
}
return $pagevisits;
}

/**
* Get pagevisits of a visitor and sort it descending (last visit at first)
*
Expand All @@ -440,7 +459,7 @@ public function setVisits(int $visits): self
*/
public function getPagevisits(): array
{
$pagevisits = $this->pagevisits;
$pagevisits = $this->getPagevisitsAuthorized();
$pagevisitsArray = [];
/** @var Pagevisit $pagevisit */
foreach ($pagevisits as $pagevisit) {
Expand All @@ -452,7 +471,7 @@ public function getPagevisits(): array

public function getPagevisitsOfGivenPageIdentifier(int $pageIdentifier): array
{
$pagevisits = $this->pagevisits;
$pagevisits = $this->getPagevisitsAuthorized();
$pagevisitsArray = [];
/** @var Pagevisit $pagevisit */
foreach ($pagevisits as $pagevisit) {
Expand Down Expand Up @@ -538,7 +557,7 @@ public function getLastPagevisit(): ?Pagevisit
*/
public function getNumberOfUniquePagevisits(): int
{
$pagevisits = $this->pagevisits;
$pagevisits = $this->getPagevisitsAuthorized();
$number = 1;
if (count($pagevisits) > 1) {
/** @var DateTime $lastVisit **/
Expand Down Expand Up @@ -577,7 +596,7 @@ public function addNewsvisit(Newsvisit $newsvisits): self

public function removeNewsvisit(Newsvisit $newsvisits): self
{
$this->pagevisits->detach($newsvisits);
$this->newsvisits->detach($newsvisits);
return $this;
}

Expand Down Expand Up @@ -762,10 +781,22 @@ public function getLastDownload(): ?Download
public function getLogs(): array
{
$logs = $this->logs->toArray();
$logs = $this->filterLogsByAuthentication($logs);
krsort($logs);
return $logs;
}

protected function filterLogsByAuthentication(array $logs): array
{
foreach ($logs as $key => $log) {
/** @var Log $log */
if ($log->canBeRead() === false) {
unset($logs[$key]);
}
}
return $logs;
}

public function getLatestLog(): ?Log
{
$logs = $this->getLogs();
Expand Down
Loading

0 comments on commit 6a7a114

Please sign in to comment.