From 512b71762eb9426b8ae336ddace622cb39ac3f70 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 22 May 2015 17:52:03 +0200 Subject: [PATCH] Add permissions and options support for template listeners --- CHANGELOG.md | 1 + ...TemplateListenerCodeCacheBuilder.class.php | 35 ++++++++++++++++++- ...istenerPackageInstallationPlugin.class.php | 2 ++ wcfsetup/setup/db/install.sql | 3 ++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0224e10ac..8307cdf491 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 2.2.0 Alpha 1 (XXXX-YY-ZZ) * `permissions` and `options` support for event listeners. +* `permissions` and `options` support for template listeners. * `wcf\data\TDatabaseObjectOptions` and `wcf\data\TDatabaseObjectPermissions` for database object-bound options and permissions validation. * `wcf\system\cache\builder\EventListenerCacheBuilder` returns `wcf\data\event\listener\EventListener` objects instead of data arrays. diff --git a/wcfsetup/install/files/lib/system/cache/builder/TemplateListenerCodeCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/TemplateListenerCodeCacheBuilder.class.php index 36feca87f3..185049c890 100644 --- a/wcfsetup/install/files/lib/system/cache/builder/TemplateListenerCodeCacheBuilder.class.php +++ b/wcfsetup/install/files/lib/system/cache/builder/TemplateListenerCodeCacheBuilder.class.php @@ -29,7 +29,40 @@ class TemplateListenerCodeCacheBuilder extends AbstractCacheBuilder { $data[$templateListener->templateName] = array(); } - $data[$templateListener->templateName][$templateListener->eventName][] = $templateListener->templateCode; + $templateCode = $templateListener->templateCode; + // wrap template listener code in if condition for options + // and permissions check + if ($templateListener->options || $templateListener->permissions) { + $templateCode = '{if '; + + $options = $permissions = [ ]; + if ($templateListener->options) { + $options = explode(',', strtoupper($templateListener->options)); + + array_walk($options, function(&$value, $key) { + $value = "('".$value."'|defined && '".$value."'|constant)"; + }); + + $templateCode .= '('.implode(' || ', $options).')'; + } + if ($templateListener->permissions) { + $permissions = explode(',', $templateListener->permissions); + + array_walk($permissions, function(&$value, $key) { + $value = "\$__wcf->session->getPermission('".$value."')"; + }); + + if (!empty($options)) { + $templateCode .= " && "; + } + + $templateCode .= '('.implode(' || ', $permissions).')'; + } + + $templateCode .= '}'.$templateListener->templateCode.'{/if}'; + } + + $data[$templateListener->templateName][$templateListener->eventName][] = $templateCode; } return $data; diff --git a/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php index 5c2d906b5a..2b5bab46ed 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php @@ -58,6 +58,8 @@ class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstal 'eventName' => $data['elements']['eventname'], 'niceValue' => $niceValue, 'name' => $data['attributes']['name'], + 'options' => (isset($data['elements']['options']) ? $data['elements']['options'] : ''), + 'permissions' => (isset($data['elements']['permissions']) ? $data['elements']['permissions'] : ''), 'templateCode' => $data['elements']['templatecode'], 'templateName' => $data['elements']['templatename'] ); diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index 25464ad078..3e66b04853 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -1058,6 +1058,9 @@ CREATE TABLE wcf1_template_listener ( eventName VARCHAR(50) NOT NULL DEFAULT '', templateCode TEXT NOT NULL, niceValue TINYINT(3) NOT NULL DEFAULT 0, + permissions TEXT, + options TEXT, + KEY templateName (environment, templateName) ); -- 2.20.1