Jump to content

Record Level Security: Difference between revisions

From NusaATK
Samk (talk | contribs)
No edit summary
Samk (talk | contribs)
mNo edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Howto|Advanced|Sam Kapilivsky}}
{{Howto|Advanced|Sam Kapilivsky}}


Sometimes you want to allow a user to edit certain records but not others from the same node.
Sometimes you want to allow your users to edit certain records but not others from the same node.


To do so, override the allowed() action in your node, and either (1) override recordActions() to hide the Edit link from records you may not edit, or (2) use setFilter() to completely hide records you may not edit.
To do so, you should override the allowed() action in your node. You may optionally also use setFilter() to hide the records users may not edit.
 
Notice that just using setFilter will hide the records, but not provide security in case a user hand-crafts a URL to edit a record. In order to provide security you MUST code the allowed() function.


==Per-record action security==
==Per-record action security==
Line 20: Line 22:
     return parent::allowed($action, $record);  
     return parent::allowed($action, $record);  
}  
}  
</syntaxhighlight>
==Hiding the Records that should not be edited==
To show only those records that the user can edit just set a filter on the node:
<syntaxhighlight lang="php">$this->setFilter(... </syntaxhighlight>
==Hiding the Edit Link==
If a filter is not set, all records will be shown with an Edit icon next to each of them. To remove the Edit icons for the records that the user is not authorized to edit, use the folowing code.
<syntaxhighlight lang="php">
function recordActions($record, &$actions, &$mraactions)
{
    if( ! allowed("edit",$record)
    {
        unset($actions["edit"]);
    }
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 18:43, 11 November 2008

ATK Howto: Record Level Security

Complexity: Advanced
Author: Sam Kapilivsky

List of other Howto's

Sometimes you want to allow your users to edit certain records but not others from the same node.

To do so, you should override the allowed() action in your node. You may optionally also use setFilter() to hide the records users may not edit.

Notice that just using setFilter will hide the records, but not provide security in case a user hand-crafts a URL to edit a record. In order to provide security you MUST code the allowed() function.

Per-record action security

The following code allows a user to edit only the records that belong to them:

function allowed($action, $record="") 
{ 
    $user = getUser(); 
    if($action=="edit" && $record['owner']['id'] <> $user['id']) 
    { 
        return false; // not allowed if not your own record 
    } 
    // call base class method to perform default authorization in all 
    // other cases. 
    return parent::allowed($action, $record); 
}