Skip to main content

ACL implementation



First question





Please, could you explain me how simpliest ACL could be implemented in MVC.





Here is the first approach of using Acl in Controller...







<?php

class MyController extends Controller {



public function myMethod() {

//It is just abstract code

$acl = new Acl();

$acl->setController('MyController');

$acl->setMethod('myMethod');

$acl->getRole();

if (!$acl->allowed()) die("You're not allowed to do it!");

...

}



}

?>







It is very bad approach, and it's minus is that we have to add Acl piece of code into each controller's method, but we don't need any additional dependencies!





Next approach is to make all controller's methods private and add ACL code into controller's __call method.







<?php

class MyController extends Controller {



private function myMethod() {

...

}



public function __call($name, $params) {

//It is just abstract code

$acl = new Acl();

$acl->setController(__CLASS__);

$acl->setMethod($name);

$acl->getRole();

if (!$acl->allowed()) die("You're not allowed to do it!");

...

}



}

?>







It is better than previous code, but main minuses are...





  • All controller's methods should be private



  • We have to add ACL code into each controller's __call method.







The next approach is to put Acl code into parent Controller, but we still need to keep all child controller's methods private.





What is the solution? And what is the best practice? Where should I call Acl functions to decide allow or disallow method to be executed.





Second question





Second question is about getting role using Acl. Let's imagine that we have guests, users and user's friends. User have restricted access to viewing his profile that only friends can view it. All guests can't view this user's profile. So, here is the logic..





  • we have to ensure that method being called is profile



  • we have to detect owner of this profile



  • we have to detect is viewer is owner of this profile or no



  • we have to read restriction rules about this profile



  • we have to decide execute or not execute profile method







The main question is about detecting owner of profile. We can detect who is owner of profile only executing model's method $model->getOwner(), but Acl do not have access to model. How can we implement this?





I hope that my thoughts are clear. Sorry for my English.





Thank you.



Source: Tips4all

Comments

  1. One possibility is to wrap all your controllers in another class that extends Controller and have it delegate all the function calls to the wrapped instance after checking for authorization.

    You could also do it more upstream, in the dispatcher (if your application does indeed have one) and lookup the permissions based on the URLs, instead of control methods.

    edit: Whether you need to access a database, a LDAP server, etc. is orthogonal to the question. My point was that you could implement an authorization based on URLs instead of controller methods. These is more robust because you typically won't be changing your URLs (URLs area kind of public interface), but you might as well change the implementations of your controllers.

    Typically, you have one or several configuration files where you map specific URL patterns to specific authentication methods and authorization directives. The dispatcher, before dispatching the request to the controllers, determines if the user is authorized and aborts the dispatching if he's not.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex