Alex Tech Adventures The webs best tutorials!

Welcome, Guest
Please Login or Register.    Lost Password?

ACL dynamic assertions
(0 viewing) 
Go to bottomPage: 123
TOPIC: ACL dynamic assertions
#377
Re:ACL dynamic assertions 2 Years ago Karma: 16
As far as I know ACL does not accept arrays.
Maybe something like:
Code:


$roles = array('read', 'write', 'whatever');
foreach($roles as $role){
$this->allow('user', 'article', $role); //other parts can come from pre-existing arrays too.
}



or am I thinking too simplistically?
alexanderrv
Administrator
Posts: 279
graph
User Offline Click here to see the profile of this user
Gender: Male tmthv2 alexchatonly@hotmail.com Location: Freeport, Bahamas Birthdate: 1989-04-14
The administrator has disabled public write access.
 
#382
Re:ACL dynamic assertions 2 Years ago Karma: 0
I used this before I tried assertions. With the assertions in as well I couldn't get it to work. However I do think I might have implemented the assertions slightly incorrect as well. Next weekend I'll give it another try...

I don't really understand why multiple roles handling isn't build into ACL by default though. Many websites, even very small ones, often use multiple roles per user..
Yorian
Junior Boarder
Posts: 29
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#383
Re:ACL dynamic assertions 2 Years ago Karma: 16
Actually I lied.
I don't know what made me think you can't pass an array as a parameter. In one of the code examples at framework.zend.com/manual/en/zend.acl.refining.html they do just that.
So you can have:
$permissions = array('read', 'write');
$this->allow('reporter', 'article', $permissions);
alexanderrv
Administrator
Posts: 279
graph
User Offline Click here to see the profile of this user
Gender: Male tmthv2 alexchatonly@hotmail.com Location: Freeport, Bahamas Birthdate: 1989-04-14
The administrator has disabled public write access.
 
#384
Re:ACL dynamic assertions 2 Years ago Karma: 0
Adding permissions as an array works, but that doesn't solve the multiple roles per user problem. But I'll have another proper look into it next weekend. Maybe I went wrong somewhere with the resources or something..
Yorian
Junior Boarder
Posts: 29
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#390
Re:ACL dynamic assertions 2 Years ago Karma: 0
Looking at your source code, I still don't really understand it..

Where do "Library_Model_UserRole" and "Library_Model_CommentResource" get instantiated? Am I missing something?

Second I still wonder where to fix the multiple roles problem, since you would need "Library_Model_UserRole" to allow an array instead of a string. You could ofcourse create a "Library_Model_UserRole" for every role, which is not very smart...

Maybe I am just missing the point, but I can't seem to figure it out.
Yorian
Junior Boarder
Posts: 29
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#426
Re:ACL dynamic assertions 2 Years ago Karma: 0
Finally, I fixed it.

I did need some ticks and magic to get it done though. It isn't the most elegant solution but it does the trick. For others that might have the same problem a bit of explanation:

First of all, notice that the tutorial Alexander has made (which really is great and helped me out a lot) might not be exactly the same as what you might need in your website. In my case things are a bit different:
- my users have an array of roles instead of just one as a string
- my roles can't be hardcoded (except for my guest role which is an exception) since they come from the database
- some users are allowed certain priveleges depending on the resource, not just the role
- roles can conflict
- I used the assertions for the routing in my system, user (or guests) are allowed to go to a certain page depending on their role AND the assertion

Especially the last point is quite hard to solve in my case since a user has for example the priveleges to add a book, but not to edit or delete it. If you would look just at the roles you might never get to the page where the use can edit his own book details (which it is allowed to do->assertions)

Here is some relevant code I used:


/* accessCheck.php (plugin), as you can see I don't act on just the roles */

<?php
public function preDispatch(Zend_Controller_Request_Abstract $request){
$module = $request->getModuleName();
$controller = $request->getControllerName();
$resource = $module . ':' . $controller;
$action = $request->getActionName();

$allowed = FALSE;
$roles = Zend_Registry::get('userRoles');
foreach($roles as $id => $role_data){
if($this->_acl->isAllowed($role_data['role'], $resource, $action)){
$allowed = TRUE;
}
}

if(!$allowed){
Zend_Registry::set('Acl_Role_Allowed', FALSE);
}else{
Zend_Registry::set('Acl_Role_Allowed', TRUE);
}

$this->_acl->setDynamicPermissions();
}
?>


/* The assertion: */

<?php

public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role = null, Zend_Acl_Resource_Interface $resource = null, $privelege = null){
if(Zend_Registry::get('Acl_Role_Allowed')){
return TRUE;
}else{
if($role->getUserId() == $resource->getOwnerId()){
return TRUE;
}else{
return FALSE;
}
}
}
?>


/* Adding a book: No assertion need, just the roles need to be checked */

<?php
public function addAction()
{
if(!Zend_Registry::get('Acl_Role_Allowed')){
if(Zend_Auth::getInstance()->hasIdentity()){
$this->_redirect('/');
}else{
$this->_redirect('/authentication/login');
}
}
etc.
}
?>


/* editting a book, if either a role or the user is allowed, there is no redirect */

<?php
public function editAction()
{
$userRole = new Model_Acl_UserRole();
$albumResource = new Administration_Model_Acl_Resource_Album();

$album_id = $this->_getParam('id', 0);
if($album_id > 0){
$albums = new Administration_Model_DbTable_Albums();
$data = $albums->getAlbum($album_id);
}else{
throw new Exception('Error with album id');
}

$albumResource->setOwnerId($data['user_id']);

if(!Zend_Registry::get('Acl_Role_Allowed') && !Zend_Registry::get('acl')->isAllowed($userRole, $albumResource, 'edit')){
if(Zend_Auth::getInstance()->hasIdentity()){
$this->_redirect('/');
}else{
$this->_redirect('/authentication/login');
}
}
etc.
}
?>


I actually don't like doing this much work in the controllers, but haven't found an easy work around to fix this. What I might do next:
- make one folder contain all the assertions.
- if the assertion file for a certain module, controller, privelege combination exists: handle the rest like I do above in the controller and assertion
- if the file does not exist: let the preDispatcher handle it (since it only depends on the roles).

Hope this helps anybody, if there is anybody that can make it all a bit more elegant, let me know!
Yorian
Junior Boarder
Posts: 29
graphgraph
User Offline Click here to see the profile of this user
Last Edit: 2010/01/26 12:11 By Yorian.
The administrator has disabled public write access.
 
Go to topPage: 123
Moderators: alexanderrv
You are here: Home Forum

Statistics

Members : 1388
Content : 42
Web Links : 1
Content View Hits : 190538

Poll

Interested in TinyBrowser and TinyMce plugin for ZF?
 

Who's Online

We have 24 guests online