Suppose we've a permissionManager
which tell us if user has some privileges to some action or not. We've quite a lot of them(dozen, maybe more).
I see two ways to implement checks for a user, like a:
Dedicated method:
permissionManager.canAdminister(user);
Method with a parameter:
permissionManager.hasPermission(Permissions.ADMINISTER, user);
(where Permissions
is a class with a bunch of static fields for each permission or a namespace with global constants)
There are pros and cons for both ways, I see these:
Dedicated method
Tracking usages and adding additional logging/validation/etc to method is simpler(we need just to modify specific method).
No additional entities and global variables.
Method with a parameter:
Multiple checks for permissions is easier(just a loop with sequential validation of each permission).
Ability to have a combination of permissions(bit-or style
ADMINISTER | VIEW
).
Any additional cons/pros for each implementation?
What is more maintainable and preferable?
PS. maybe moving functionality to User
is even better, like: user.hasPermission(Permissions.ADMINISTER)
but this breaks Single responsibility principle.