From: Matthias Schmidt Date: Fri, 22 May 2015 15:52:03 +0000 (+0200) Subject: Add permissions and options support for template listeners X-Git-Tag: 3.0.0_Beta_1~2355 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=512b71762e;p=GitHub%2FWoltLab%2FWCF.git Add permissions and options support for template listeners --- 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) );