Ali
Ali

Reputation: 267287

Tricky class design issue

I'm working on implementing a class for managing user permissions on my website.

For example: employees can view customer records but nothing else, employers can view customers as well as manage employees, and admins can do both those things as well as manage employers.

So far, what I've got is this:

However the tricky part is that in some places, I need to be more specific. For example: a customer has got the permission: readMsgs which allows him to read the messages between himself and an employee. However, if he has that permission, then he can simply change the url from:

site.com/messages/read/100

to

site.com/messages/read/101

And read message # 101 as well, which might be between another customer and employee. A customer shouldn't be able to read anyone's messages except himself.

Similarly, a customer has got the editCustomer permission, which allows him to edit his own profile by going to:

site.com/customers/99

(where 99 is his customer id)

But if he goes to site.com/customers/100

He should not be allowed to access that page.

How can I solve this problem? Ideally I'd like to be able to pass on an id to the permissions class. E.g:

if (! $permissions->can('readMsg', $msgId))
   echo 'not allowed';

if (! $permissions->can('editCustomer', $requestedCustomerId))
   echo 'not allowed';

Any ideas how I'd have to restructure my class structure to allow the above kind of thing?

Upvotes: 0

Views: 147

Answers (2)

Jollymorphic
Jollymorphic

Reputation: 3530

I would be more granular in my taxonomy of permissions (e.g., "readOwnMsgs" vs. "readAnyMsg"). This would elaborate your permission-checking code (e.g., site.com/messages/read/### goes something along the lines of "proceed if canReadAnyMsg or if canReadOwnMsg and message author is current user"), suggesting that this logic should be encapsulated in separate classes broken down by resource type or whatever other circumstances might have an effect on contextual information required to make such decisions.

Upvotes: 1

corsiKa
corsiKa

Reputation: 82599

I would have a message class with a canRead(User) function. This would check the user's permissions and say "Oh, I'm a message from a manager to an employee. Unless the user is the reciepient of the message, they can't read it." or just as easily "I'm a message from a manager to an employee. The user is a manager, so he can read it."

I'm typing it out in English because I suck a php (which appears to be the language of choice.)

Upvotes: 1

Related Questions