I’m working on a sort of MVC framework, and I’m implementing a simple ACL permissions check system.
I’m wondering if anyone can shed some light on, or direct me towards some good examples of this sort of implementation (or criticize to the point of warranting scrapping it, whichever is necessary)
Since I’m building the framework with REST API in mind, I’ve created two base controllers, WebController and ApiController.
Requests made to /index.php route to WebController_* and requests made to /api/index.php route to ApiController_*
An extended WebController is responsible for building output from a template with data returned from the consolidated calls to the necessary ApiControllers.
An extended ApiController is responsible for querying the Model for data. If a call is made directly to /api/index.php it returns JSON.
But I digress; In order to facilitate ACL, I figured implementing it at the ApiController layer made sense, and if there is a denial it is returned in JSON or back up to a WebController and handled accordingly, depending on the request type.
I’m thinking to simplify things, I could make use of __call() and private methods. __call() would verify the existence of the requested method, and prior to calling it, check the user permissions on the method it against the ACL.
class ApiController{
public function __call($method, $arguments){
if(method_exists($this, $method)){
//haven't written ACL classes yet, but something like this
if(ACL::check(...)){
return call_user_func_array(array($this, $method), $arguments);
}else{
return false;
}
}else{
//throw catchable exception or something
}
}
}
Good idea? Bad idea? Thoughts? Maybe I’m in over my head here, I’m still learning and this is more for education than profit, however finishing something that has future use would be nice.
I’ve decided to go with an approach similar to my initially proposed method, creating a
Gatewayobject to act as a proxy to theModel. Essentially theGatewayobject holds a reference to theAcl,Request, andModelobjects, and performs checks against theAclobject before forwarding any method calls to theModel. Simplified for brevity:Are there any issues that can be seen with this approach? So far as I can tell, this should serve the purpose quite effectively, as I can sub in different
AclandModelobjects based on the needs of theController(Though likely there will only be the need for a singleAcl)Furthermore, my routing of API calls becomes simplified, as using a REST/RPC hybrid architecture, API calls can be passed to the
Gatewaywithout much need for modification.