Skip to content

Commit

Permalink
Merge pull request #397 from Yoast/feature/global-configuration-basic…
Browse files Browse the repository at this point in the history
…-auth-disable-preview

[FEATURE] Added global configuration for basic auth and disabling preview
  • Loading branch information
haassie authored Dec 22, 2020
2 parents a6ee5a2 + fdbd3a6 commit 4e70d86
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 50 deletions.
27 changes: 1 addition & 26 deletions Classes/Backend/PageLayoutHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use TYPO3\CMS\Backend\Controller\PageLayoutController;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
Expand All @@ -30,8 +29,6 @@ class PageLayoutHeader
'languageKeyToLocaleMapping' => []
],
'menuActions' => [],
'previewDomain' => null,
'previewUrlTemplate' => '',
'viewSettings' => []
];

Expand Down Expand Up @@ -76,12 +73,8 @@ public function render()
{
$moduleData = BackendUtility::getModuleData(['language'], [], 'web_layout');
$pageId = (int)GeneralUtility::_GET('id');

if (!$this->showSnippetPreview()) {
return '';
}

$currentPage = $this->getCurrentPage($pageId, $moduleData);

if (!YoastUtility::snippetPreviewEnabled($pageId, $currentPage)) {
return '';
}
Expand Down Expand Up @@ -136,24 +129,6 @@ public function render()
return '';
}

/**
* Check to see if snippet preview has to be shown
*
* @return bool
*/
protected function showSnippetPreview(): bool
{
if (!$GLOBALS['BE_USER'] instanceof BackendUserAuthentication ||
!$GLOBALS['BE_USER']->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) {
return false;
}

if ((bool)$GLOBALS['BE_USER']->uc['hideYoastInPageModule']) {
return false;
}
return true;
}

/**
* @param $pageId
* @param $moduleData
Expand Down
2 changes: 0 additions & 2 deletions Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class ModuleController extends ActionController
'languageKeyToLocaleMapping' => []
],
'menuActions' => [],
'previewDomain' => null,
'previewUrlTemplate' => '',
'viewSettings' => []
];

Expand Down
27 changes: 26 additions & 1 deletion Classes/Service/PreviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public function getPreviewData($uriToCheck, $pageId)
*/
protected function getContentFromUrl($uriToCheck): string
{
$GLOBALS['TYPO3_CONF_VARS']['HTTP']['verify'] = false;
$backupSettings = $GLOBALS['TYPO3_CONF_VARS']['HTTP'];
$this->setHttpOptions();
$report = [];
$content = GeneralUtility::getUrl(
$uriToCheck,
Expand All @@ -78,6 +79,7 @@ protected function getContentFromUrl($uriToCheck): string
$report
);

$GLOBALS['TYPO3_CONF_VARS']['HTTP'] = $backupSettings;
if ((int)$report['error'] === 0) {
return $content;
}
Expand Down Expand Up @@ -192,4 +194,27 @@ protected function stripTagsContent($text, $tags = '', $invert = false)

return $text;
}

/**
* Set http options for the preview request
*/
protected function setHttpOptions()
{
$GLOBALS['TYPO3_CONF_VARS']['HTTP']['verify'] = false;
if (!isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth'])) {
return;
}

$basicAuth = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth'];
if (!is_array($basicAuth) || !isset($basicAuth['username'], $basicAuth['password'])) {
return;
}

if (!empty($basicAuth['username']) && !empty($basicAuth['password'])) {
$GLOBALS['TYPO3_CONF_VARS']['HTTP']['auth'] = [
$basicAuth['username'],
$basicAuth['password']
];
}
}
}
53 changes: 33 additions & 20 deletions Classes/Utility/YoastUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Package\Exception;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Class YoastUtility
Expand Down Expand Up @@ -69,10 +77,17 @@ public static function getAllowedDoktypes($configuration = null, $returnInString
*/
public static function snippetPreviewEnabled($pageId, array $pageRecord, $pageTs = null)
{
$showPreview = !$pageRecord['tx_yoastseo_hide_snippet_preview'];
if (!$GLOBALS['BE_USER'] instanceof BackendUserAuthentication ||
!$GLOBALS['BE_USER']->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) {
return false;
}

if ((bool)$GLOBALS['BE_USER']->uc['hideYoastInPageModule']) {
return false;
}

if ($pageTs === null) {
$pageTs = CMS\Backend\Utility\BackendUtility::getPagesTSconfig($pageId);
$pageTs = BackendUtility::getPagesTSconfig($pageId);
}

if (is_array($pageTs) &&
Expand All @@ -83,10 +98,10 @@ public static function snippetPreviewEnabled($pageId, array $pageRecord, $pageTs
array_key_exists('disableSnippetPreview', $pageTs['mod.']['web_SeoPlugin.']) &&
(int)$pageTs['mod.']['web_SeoPlugin.']['disableSnippetPreview'] === 1
) {
$showPreview = false;
return false;
}

return $showPreview;
return ($pageRecord['tx_yoastseo_hide_snippet_preview']) ? false : true;
}

/**
Expand All @@ -102,7 +117,7 @@ public static function getFocusKeywordOfPage($uid, $table = 'pages')
return '';
}

$record = CMS\Backend\Utility\BackendUtility::getRecord($table, $uid);
$record = BackendUtility::getRecord($table, $uid);
if (\is_array($record) && array_key_exists(self::COLUMN_NAME_FOCUSKEYWORD, $record)) {
$focusKeyword = $record[self::COLUMN_NAME_FOCUSKEYWORD];
}
Expand All @@ -117,7 +132,7 @@ public static function getFocusKeywordOfPage($uid, $table = 'pages')
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['get_focus_keyword'] as $_funcRef) {
if ($_funcRef) {
$tmp = new \stdClass();
CMS\Core\Utility\GeneralUtility::callUserFunction($_funcRef, $params, $tmp);
GeneralUtility::callUserFunction($_funcRef, $params, $tmp);
}
}
}
Expand All @@ -133,7 +148,7 @@ public static function getFocusKeywordOfPage($uid, $table = 'pages')
public static function getRelatedKeyphrases($parentTable, $parentId): array
{
$config = [];
$queryBuilder = CMS\Core\Utility\GeneralUtility::makeInstance(CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable('tx_yoast_seo_premium_focus_keywords');
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_yoast_seo_premium_focus_keywords');
if ($queryBuilder) {
$relatedKeyphrases = $queryBuilder->select('*')
->from('tx_yoast_seo_premium_focus_keywords')
Expand All @@ -160,7 +175,7 @@ public static function getRelatedKeyphrases($parentTable, $parentId): array
*/
public static function isPremiumInstalled()
{
return (bool)CMS\Core\Utility\ExtensionManagementUtility::isLoaded('yoast_seo_premium');
return (bool)ExtensionManagementUtility::isLoaded('yoast_seo_premium');
}

/**
Expand All @@ -187,23 +202,21 @@ public static function inProductionMode($configuration = null)

protected static function getTypoScriptConfiguration()
{
/** @var CMS\Extbase\Object\ObjectManager $objectManager */
$objectManager = CMS\Core\Utility\GeneralUtility::makeInstance(CMS\Extbase\Object\ObjectManager::class);
/** @var CMS\Extbase\Configuration\ConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(CMS\Extbase\Configuration\ConfigurationManager::class);
$configuration = $configurationManager->getConfiguration(
CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var ConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(ConfigurationManager::class);
return $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'yoastseo'
);

return $configuration;
}
/**
* @param string $utm_term
* @param string $utm_content
* @param string $utm_source
* @return string
* @throws CMS\Core\Package\Exception
* @throws Exception
*/
public static function getYoastLink($utm_term = 'Go premium', $utm_content = '', $utm_source = 'yoast-seo-for-typo3')
{
Expand All @@ -216,9 +229,9 @@ public static function getYoastLink($utm_term = 'Go premium', $utm_content = '',
'utm_campaign' => 'typo3-ad',
'php_version' => $php_version[1] ?: 'unknown',
'platform' => 'TYPO3',
'platform_version' => CMS\Core\Utility\VersionNumberUtility::getNumericTypo3Version(),
'platform_version' => VersionNumberUtility::getNumericTypo3Version(),
'software' => self::isPremiumInstalled() ? 'premium' : 'free',
'software_version' => CMS\Core\Utility\ExtensionManagementUtility::getExtensionVersion('yoast_seo'),
'software_version' => ExtensionManagementUtility::getExtensionVersion('yoast_seo'),
'role' => ''
];

Expand Down
11 changes: 11 additions & 0 deletions Documentation/Configuration/SnippetPreview/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ also disable the snippet preview based on PageTs. Below an example to hide page
disableSnippetPreview = 1
}
[global]
Basic auth configuration
------------------------
For environments which are protected by Basic auth, it's possible to set username and password through :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth']`

.. code-block:: php
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['previewSettings']['basicAuth'] = [
'username' => 'authUsername',
'password' => 'authPassword'
];
7 changes: 6 additions & 1 deletion ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@
'showSocialTab' => true,
'showAdvancedTab' => true
],
'previewUrlTemplate' => '/?id=%d&type=%d&L=%d',
'previewSettings' => [
'basicAuth' => [
'username' => '',
'password' => '',
]
],
'overview_filters' => [
'10' => [
'key' => 'cornerstone',
Expand Down

0 comments on commit 4e70d86

Please sign in to comment.