Skip to content

Commit

Permalink
Support for limiting editing of attributes
Browse files Browse the repository at this point in the history
Content controllers is a new feature for controlling which
attributes can be edited by the current user.
Each content controller is called when fetching input for an
attribute and may deny access to editing that attribute.

A content controller is defined by adding the class name to
EditSettings/ContentControllers in content.ini. If no controllers
are defined then all attributes are allowed as before.
The class must then implement the `canEditAttribute` method
which accepts the object attribute as the first parameter.
  • Loading branch information
am0s committed Feb 22, 2021
1 parent 746a762 commit 65ad27a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions kernel/classes/ezcontentobject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,38 @@ function commitInputRelations( $editVersion )
return true;
}

/**
* Returns a list of content controller instances.
* A content controller can help limit access to certain attributes.
*
* Controllers are defined in content.ini, EditSettings/ContentControlls
* and are cached in a global variable.
*
* @return object[]
*/
public static function getContentControllers()
{
if ( isset( $GLOBALS['EZP_CONTENT_CONTROLLERS'] ) )
{
return $GLOBALS['EZP_CONTENT_CONTROLLERS'];
}

$contentControllers = array();
$contentIni = eZINI::instance('content.ini');
if ( $contentIni->hasVariable( 'EditSettings', 'ContentControllers' ) )
{
$contentControllerNames = $contentIni->variable( 'EditSettings', 'ContentControllers' );
foreach ($contentControllerNames as $contentControllerName)
{
$controller = new $contentControllerName();
$contentControllers[] = $controller;
}
}
$GLOBALS['EZP_CONTENT_CONTROLLERS'] = $contentControllers;

return $contentControllers;
}

/**
* @param eZContentObjectAttribute[] $contentObjectAttributes
* @param string $attributeDataBaseName
Expand Down Expand Up @@ -2447,6 +2479,8 @@ function validateInput( $contentObjectAttributes, $attributeDataBaseName,

$this->resetInputRelationList();

$contentControllers = self::getContentControllers();

$editVersion = null;
$defaultLanguage = $this->initialLanguageCode();
foreach( $contentObjectAttributes as $contentObjectAttribute )
Expand All @@ -2471,6 +2505,16 @@ function validateInput( $contentObjectAttributes, $attributeDataBaseName,
$doNotValidate = true;
}

// Check if attribute access is denied from custom controllers
foreach ($contentControllers as $contentController)
{
if ( !$contentController->canEditAttribute( $contentObjectAttribute ) )
{
$doNotValidate = true;
break;
}
}

if ( $doNotValidate == true )
{
$status = eZInputValidator::STATE_ACCEPTED;
Expand Down Expand Up @@ -2575,6 +2619,8 @@ function fetchInput( $contentObjectAttributes, $attributeDataBaseName,

$defaultLanguage = $this->initialLanguageCode();

$contentControllers = self::getContentControllers();

$this->fetchDataMap();
foreach ( $contentObjectAttributes as $contentObjectAttribute )
{
Expand All @@ -2597,6 +2643,16 @@ function fetchInput( $contentObjectAttributes, $attributeDataBaseName,
}
}

// Check if attribute access is denied from custom controllers
foreach ($contentControllers as $contentController)
{
if ( !$contentController->canEditAttribute( $contentObjectAttribute ) )
{
$fetchInput = false;
break;
}
}

// Do not handle input for non-translateable attributes.
// Input will be copyed from the std. translation on storage
if ( $fetchInput )
Expand Down

0 comments on commit 65ad27a

Please sign in to comment.