Share » Learn » eZ Publish » Adding custom security policy...

Adding custom security policy limitations to your modules

Tuesday 25 May 2010 7:44:59 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Defining our own limitations


As you probably noticed in the module.php example above, variable $FunctionList['myfunction'] is an empty array, which means that the function doesn't have any limitation. We just need to fill it with right values to add some.

Example for a language level limitation :

$Module = array('name' => 'mymodule');

$ViewList = array();
$ViewList['myview'] = array(
    'script'                    => 'view.php',
    'params'                    => array(),
    'functions'                    => array( 'myfunction' )

$Language = array(
    'name'=> 'Language',
    'values'=> array(),
    'path' => 'classes/',
    'file' => 'ezcontentlanguage.php',
    'class' => 'eZContentLanguage',
    'function' => 'fetchLimitationList',
    'parameter' => array( false )

$FunctionList['myfunction'] = array('Language' => $Language);

Here we tell eZ Publish that myfunction has a limitation named Language and that we need to fetch possible values list with eZContentLanguage::fetchLimitationList() method, with parameter false. This simply stands for an old school way to define a callback function, probably here since first releases of eZ Publish 3 (remind you first versions of PHP4 and all its limitations). For curious minds, the important stuffs can be found in eZPolicyLimitation::allValuesAsArrayWithNames(), from line 249 (in a 4.3.0 eZ Publish instance).

Here we call a class from the kernel to fill our limitation array, but it is of course possible to use a class from an extension. However, autoload system cannot be used here and an old good include_once is made in the backend. So, in order to use a class from an extension, we need to add another key to our $Language array :

$Language = array(
    'name' => 'Language',
    'values' => array(),
    'extension' => 'myextension',
    'path' => 'classes/',
    'file' => 'myclass.php',
    'class' => 'MyClass',
    'function' => 'fetchLanguageLimitationList',
    'parameter' => array( false )

This way, extension/myextension/classes/myclass.php class will be included.

What does our method MyClass:fetchLanguageLimitationList() have to return ? A quick look to eZContentLanguage::fetchLimitationList() shows us that it must be a simple array with each entry being an associative array itself containing id and name keys.

  • id will be the value stored in the database for the security policy limitation
  • name will simply be the label displayed in the admin interface

Here what should be our class MyClass :

class MyClass
     * Fetches the array with names and IDs of the languages used on the site. This method is used by the permission system.
     * @return Array with names and IDs of the languages used on the site.
     * @static
    public static function fetchLanguageLimitationList()
        $langList = eZINI::instance( 'site.ini' )->variable( 'RegionalSettings', 'SiteLanguageList' );
        $aResult = array();
        foreach($langList as $lang)
                $aResult[] = array(
                    'id'    => $lang,
                    'name'    => $lang

        return $aResult;
36 542 Users on board!

Tutorial menu


Printer Friendly version of the full article on one page with plain styles