ENHANCEMENT Allowing item-specific permissions in TableListField and subclasses (through TableListField_Item->Can() and DataObject->can*() methods). Adding "disabled" icons to template.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73028 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-03-13 10:07:27 +00:00
parent 982c2142de
commit 4499d1a8d0
3 changed files with 58 additions and 5 deletions

View File

@ -133,16 +133,19 @@ class ComplexTableField extends TableListField {
'show' => array( 'show' => array(
'label' => 'Show', 'label' => 'Show',
'icon' => 'cms/images/show.png', 'icon' => 'cms/images/show.png',
'icon_disabled' => 'cms/images/show_disabled.png',
'class' => 'popuplink showlink', 'class' => 'popuplink showlink',
), ),
'edit' => array( 'edit' => array(
'label' => 'Edit', 'label' => 'Edit',
'icon' => 'cms/images/edit.gif', 'icon' => 'cms/images/edit.gif',
'icon_disabled' => 'cms/images/show_disabled.png',
'class' => 'popuplink editlink', 'class' => 'popuplink editlink',
), ),
'delete' => array( 'delete' => array(
'label' => 'Delete', 'label' => 'Delete',
'icon' => 'cms/images/delete.gif', 'icon' => 'cms/images/delete.gif',
'icon_disabled' => 'cms/images/show_disabled.png',
'class' => 'popuplink deletelink', 'class' => 'popuplink deletelink',
), ),
); );

View File

@ -102,13 +102,19 @@ class TableListField extends FormField {
* Actions can be disabled through $permissions. * Actions can be disabled through $permissions.
* Format (key is used for the methodname and CSS-class): * Format (key is used for the methodname and CSS-class):
* array( * array(
* 'delete' => array('label' => 'Delete', 'icon' => 'cms/images/delete.gif') * 'delete' => array(
* 'label' => 'Delete',
* 'icon' => 'cms/images/delete.gif',
* 'icon_disabled' => 'cms/images/delete_disabled.gif',
* 'class' => 'deletelink',
* )
* ) * )
*/ */
public $actions = array( public $actions = array(
'delete' => array( 'delete' => array(
'label' => 'Delete', 'label' => 'Delete',
'icon' => 'cms/images/delete.gif', 'icon' => 'cms/images/delete.gif',
'icon_disabled' => 'cms/images/delete_disabled.gif',
'class' => 'deletelink' 'class' => 'deletelink'
) )
); );
@ -677,7 +683,11 @@ JS
*/ */
/** /**
* Template accessor for Permissions * Template accessor for Permissions.
* See {@link TableListField_Item->Can()} for object-specific
* permissions.
*
* @return boolean
*/ */
function Can($mode) { function Can($mode) {
if($mode == 'add' && $this->isReadonly()) { if($mode == 'add' && $this->isReadonly()) {
@ -1199,7 +1209,17 @@ JS
* @see TableListField * @see TableListField
*/ */
class TableListField_Item extends ViewableData { class TableListField_Item extends ViewableData {
protected $item, $parent;
/**
* @var DataObject The underlying data record,
* usually an element of {@link TableListField->sourceItems()}.
*/
protected $item;
/**
* @var TableListField
*/
protected $parent;
function __construct($item, $parent) { function __construct($item, $parent) {
$this->failover = $this->item = $item; $this->failover = $this->item = $item;
@ -1272,8 +1292,26 @@ class TableListField_Item extends ViewableData {
return $this->parent->Markable; return $this->parent->Markable;
} }
/**
* Checks global permissions for field in {@link TableListField->Can()}.
* If they are allowed, it checks for object permissions by assuming
* a method with "can" + $mode parameter naming, e.g. canDelete().
*
* @param string $mode See {@link TableListField::$permissions} array.
* @return boolean
*/
function Can($mode) { function Can($mode) {
return $this->parent->Can($mode); $canMethod = "can" . ucfirst($mode);
if(!$this->parent->Can($mode)) {
// check global settings for the field instance
return false;
} elseif($this->item->hasMethod($canMethod)) {
// if global allows, check object specific permissions (e.g. canDelete())
return $this->item->$canMethod();
} else {
// otherwise global allowed this action, so return TRUE
return true;
}
} }
function Link() { function Link() {
@ -1303,9 +1341,11 @@ class TableListField_Item extends ViewableData {
'Name' => $actionName, 'Name' => $actionName,
'Link' => $this->{ucfirst($actionName).'Link'}(), 'Link' => $this->{ucfirst($actionName).'Link'}(),
'Icon' => $actionSettings['icon'], 'Icon' => $actionSettings['icon'],
'IconDisabled' => $actionSettings['icon_disabled'],
'Label' => $actionSettings['label'], 'Label' => $actionSettings['label'],
'Class' => $actionSettings['class'], 'Class' => $actionSettings['class'],
'Default' => ($actionName == $this->parent->defaultAction), 'Default' => ($actionName == $this->parent->defaultAction),
'IsAllowed' => $this->Can($actionName),
))); )));
} }
} }

View File

@ -4,6 +4,16 @@
<td class="field-$Title.HTMLATT $FirstLast">$Value</td> <td class="field-$Title.HTMLATT $FirstLast">$Value</td>
<% end_control %> <% end_control %>
<% control Actions %> <% control Actions %>
<td width="16"><a class="$Class" href="$Link"><% if Icon %><img src="$Icon" alt="$Label" /><% else %>$Label<% end_if %></a></td> <td width="16">
<% if IsAllowed %>
<a class="$Class" href="$Link">
<% if Icon %><img src="$Icon" alt="$Label" /><% else %>$Label<% end_if %>
</a>
<% else %>
<span class="disabled">
<% if IconDisabled %><img src="$IconDisabled" alt="$Label" /><% else %>$Label<% end_if %>
</span>
<% end_if %>
</td>
<% end_control %> <% end_control %>
</tr> </tr>