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:
- Add accesscontrol to the Service class. Every method (such as getCompanyInfoById) must call the hasPrivilege method internally before completing the operation and returning the result.
- Wrap all Service objects in a some kind of Proxy object that will checks privileges before calling the method in the inner-object.
- Completely separate access control, and force the caller to check privileges before calling the method.
Cons for every option:
- This requires changing all Services, and requires them to be aware of Access control. I feel this goes against Separation of concerns.
- This breaks OOP features, such as Polymorphism. The caller no longer knows what interfaces any service supports.
- This is the most flexible, but the big drawback is that checking permission is now implicit. Developers can forget or complex codepaths can cause unauthorized services to be called.
Are there better ways to approach this altogether?