Add permissions and options support for template listeners
authorMatthias Schmidt <gravatronics@live.com>
Fri, 22 May 2015 15:52:03 +0000 (17:52 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 22 May 2015 15:52:03 +0000 (17:52 +0200)
CHANGELOG.md
wcfsetup/install/files/lib/system/cache/builder/TemplateListenerCodeCacheBuilder.class.php
wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php
wcfsetup/setup/db/install.sql

index f0224e10ac7f081e45bd7cafbf6eb4e369a12fa8..8307cdf49194dd45adaaa07a40ba54bd10ff70a0 100644 (file)
@@ -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.
 
index 36feca87f3ad454a9becbb14ee52daa595b95262..185049c890b09604c6ac6b9d2daaf9b0514efc51 100644 (file)
@@ -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;
index 5c2d906b5a93e7cfc030a23859208d57d0fa8167..2b5bab46edde29fd901e24c0bae87b760231807a 100644 (file)
@@ -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']
                );
index 25464ad0788a22e723d6336f790a23489d70c946..3e66b04853de0e74a36adbb6df83a187c8d04059 100644 (file)
@@ -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)
 );