New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[!!!][TASK] Allow to disable siteHash check by setting query.allowedSites to * #929
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace ApacheSolrForTypo3\Solr\Domain\Site; | ||
|
||
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2017- Timo Hund <timo.hund@dkd.de> | ||
* All rights reserved | ||
* | ||
* This script is part of the TYPO3 project. The TYPO3 project is | ||
* free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The GNU General Public License can be found at | ||
* http://www.gnu.org/copyleft/gpl.html. | ||
* | ||
* This script is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* This copyright notice MUST APPEAR in all copies of the script! | ||
***************************************************************/ | ||
|
||
use ApacheSolrForTypo3\Solr\Site; | ||
|
||
/** | ||
* SiteHashService | ||
* | ||
* Responsible to provide sitehash related service methods. | ||
* | ||
* @author Timo Hund <timo.hund@dkd.de> | ||
*/ | ||
class SiteHashService | ||
{ | ||
|
||
/** | ||
* Resolves magic keywords in allowed sites configuration. | ||
* Supported keywords: | ||
* __solr_current_site - The domain of the site the query has been started from | ||
* __current_site - Same as __solr_current_site | ||
* __all - Adds all domains as allowed sites | ||
* * - Means all sites are allowed, same as no siteHash | ||
* | ||
* @param integer $pageId A page ID that is then resolved to the site it belongs to | ||
* @param string $allowedSitesConfiguration TypoScript setting for allowed sites | ||
* @return string List of allowed sites/domains, magic keywords resolved | ||
*/ | ||
public function getAllowedSitesForPageIdAndAllowedSitesConfiguration($pageId, $allowedSitesConfiguration) | ||
{ | ||
if ($allowedSitesConfiguration === '__all') { | ||
return $this->getDomainListOfAllSites(); | ||
} elseif ($allowedSitesConfiguration === '*') { | ||
return '*'; | ||
} else { | ||
return $this->getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the site hash for a domain | ||
* | ||
* @param string $domain Domain to calculate the site hash for. | ||
* @return string site hash for $domain | ||
*/ | ||
public function getSiteHashForDomain($domain) | ||
{ | ||
static $siteHashes = []; | ||
if (isset($siteHashes[$domain])) { | ||
return $siteHashes[$domain]; | ||
} | ||
|
||
$siteHashes[$domain] = sha1($domain . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr'); | ||
return $siteHashes[$domain]; | ||
} | ||
|
||
|
||
/** | ||
* Returns a comma separated list of all domains from all sites. | ||
* | ||
* @return string | ||
*/ | ||
protected function getDomainListOfAllSites() | ||
{ | ||
$sites = $this->getAvailableSites(); | ||
$domains = []; | ||
foreach ($sites as $site) { | ||
$domains[] = $site->getDomain(); | ||
} | ||
|
||
$allowedSites = implode(',', $domains); | ||
return $allowedSites; | ||
} | ||
|
||
/** | ||
* Retrieves the domain of the site that belongs to the passed pageId and replaces their markers __solr_current_site | ||
* and __current_site. | ||
* | ||
* @param integer $pageId | ||
* @param string $allowedSitesConfiguration | ||
* @return string | ||
*/ | ||
protected function getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration) | ||
{ | ||
$domainOfPage = $this->getSiteByPageId($pageId)->getDomain(); | ||
$allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration); | ||
return (string)$allowedSites; | ||
} | ||
|
||
/** | ||
* @return Site[] | ||
*/ | ||
protected function getAvailableSites() | ||
{ | ||
return Site::getAvailableSites(); | ||
} | ||
|
||
/** | ||
* @param $pageId | ||
* @return Site | ||
*/ | ||
protected function getSiteByPageId($pageId) | ||
{ | ||
return Site::getSiteByPageId($pageId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace ApacheSolrForTypo3\Solr\Domain\Variants; | ||
|
||
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2017- Timo Hund <timo.hund@dkd.de> | ||
* All rights reserved | ||
* | ||
* This script is part of the TYPO3 project. The TYPO3 project is | ||
* free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The GNU General Public License can be found at | ||
* http://www.gnu.org/copyleft/gpl.html. | ||
* | ||
* This script is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* This copyright notice MUST APPEAR in all copies of the script! | ||
***************************************************************/ | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* The variantId can be used to group documents by a variantId. This variantId is by default unique per system, | ||
* and has the following syntax: | ||
* | ||
* <SystemHash>/type/uid | ||
* | ||
* A file from one system will get the same variantId, which could be useful for de-duplication. | ||
* @author Timo Hund <timo.hund@dkd.de> | ||
*/ | ||
class IdBuilder | ||
{ | ||
|
||
/** | ||
* This method is used to build a variantId. | ||
* | ||
* By default the variantId is used | ||
* @param string $type | ||
* @param integer $uid | ||
* @return string | ||
*/ | ||
public function buildFromTypeAndUid($type, $uid) | ||
{ | ||
$systemHash = $this->getSystemHash(); | ||
$variantId = $systemHash . '/' . $type . '/' . $uid; | ||
|
||
$variantId = $this->applyHook($variantId, $systemHash, $type, $uid); | ||
return $variantId; | ||
} | ||
|
||
/** | ||
* Applies configured postProcessing hooks to build a custom variantId. | ||
* | ||
* @param string $variantId | ||
* @param string $systemHash | ||
* @param string $type | ||
* @param integer $uid | ||
* @return string | ||
*/ | ||
protected function applyHook($variantId, $systemHash, $type, $uid) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be better to use the new Signal concept in TYPO3? - I think hooks are only kept for compatibility There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. signals don't work for every scenario. If you want to change data, you still need hooks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not convinced - see https://usetypo3.com/signals-and-hooks-in-typo3.html and https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/core/Documentation/Changelog/master/Feature-79387-AddSignalToExcludeTablesFromReferenceIndex.rst - or have I missed something in this method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think signals and slots are more like "events" and the intension is normally a one way direction. It is possible to return data, but only by passing a variable by reference (what is not optimal i would say). I think with hooks you can directly return a value, what is needed in this case (Add btw. in the changes of the core also new hooks are added). |
||
{ | ||
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'])) { | ||
return $variantId; | ||
} | ||
|
||
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'] as $classReference) { | ||
$variantIdModifier = GeneralUtility::getUserObj($classReference); | ||
if ($variantIdModifier instanceof IdModifier) { | ||
$variantId = $variantIdModifier->modifyVariantId($variantId, $systemHash, $type, $uid); | ||
} | ||
} | ||
|
||
return $variantId; | ||
} | ||
|
||
/** | ||
* Returns a system unique hash. | ||
* | ||
* @return string | ||
*/ | ||
protected function getSystemHash() | ||
{ | ||
if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'])) { | ||
throw new \InvalidArgumentException("No sitename set in TYPO3_CONF_VARS|SYS|sitename"); | ||
} | ||
|
||
$siteName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']; | ||
$systemKey = 'tx_solr' . $siteName; | ||
return GeneralUtility::hmac($systemKey); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace ApacheSolrForTypo3\Solr\Domain\Variants; | ||
|
||
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2017- Timo Hund <timo.hund@dkd.de> | ||
* All rights reserved | ||
* | ||
* This script is part of the TYPO3 project. The TYPO3 project is | ||
* free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The GNU General Public License can be found at | ||
* http://www.gnu.org/copyleft/gpl.html. | ||
* | ||
* This script is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* This copyright notice MUST APPEAR in all copies of the script! | ||
***************************************************************/ | ||
|
||
|
||
/** | ||
* An implementation of this class can be used to modify the variantId. | ||
* | ||
* @author Timo Hund <timo.hund@dkd.de> | ||
*/ | ||
interface IdModifier | ||
{ | ||
|
||
/** | ||
* @param string $variantId | ||
* @param string $systemHash | ||
* @param string $type | ||
* @param integer $uid | ||
* @return string | ||
*/ | ||
public function modifyVariantId($variantId, $systemHash, $type, $uid); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just moved, the hash should be the same as before