I'm working on a PHP application, and I'd like to add access control to some of my objects. I didn't tag this question as PHP, as I feel this question is not language specific.
Say I have a 'Service class'
abstract class Service {
}
Many services use this as a baseclass. One pseudo example would be:
class Companies extends Service {
  function getCompanyInfo($id) {
      //...
  }
}
Later down the road I want to add access control. The example 'getCompanyInfoById' method is a 'read' operation, so this would require a 'read' privilege.
At this point I can implement this in the following way:
Cons for every option:
Are there better ways to approach this altogether?
Another solution could be a little variant of your 1.
ex.
class Service
{
  var $ACL = //some hash map with acl
}
class Companies extends Service
{
  function getCompanyById($id)
  {
    //real code
  }
}
class SafeCompanies extends Companies
{
//If a method must be "protected" with an ACL, you must override them in this way
  function getCompanyById($id)
  {
    $this->check('read'); //raise an exception if current user haven't READ privilege
    parent::getCompanyById($id);    
  }  
} 
in this way you dont mix responsibilities and still can use polymorphism
my 2 cents
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With