tags:

views:

155

answers:

3

I have three types of users: A, B, C .. Hence a user base class and then we have 3 derived classes. However it is possible for a user to be of 2 types at the same time. How would we go about dealing with this in a decent fashion, keeping in mind the type(s) of the user will define the kind of access they have in an application.

+3  A: 

I would use decorator pattern, e.g.

$user = new User ();
$user = new Role1 ($user);
$user = new Role2 ($user);
Piotr Pankowski
+1  A: 

Hi, I would have had a look at the ACL class of the Zend Framework. Does a good job when it comes to have multiple roles for different users. There's also many articles and blogs discussing the use of the Zend ACL. More general about ACL: wikipedia - acl

Another way of dealing with different roles and permissions is Role-based access control: wikipedia - role-based access control

rakke
thanks for the suggestion, however i am sure you realize that i am not looking for a platform/ out of the box solution for this. I want to understand how best to deal with this problem on the conceptual level.
Sabeen Malik
yes, I understand that you are not looking for a out of the box solution, I just suggested that you had a look at the way they do it in the zend framework. I often find it easier to understand concepts by looking at good examples, and the imho zend framework has a good implementation of the acl with documentation.I've pasted a more general link in the post + a link to a pretty similar approach in role-based access control.cheers, Jørgen
rakke
+1 to suggest the DRY principle. I also think the Zend_Auth + Zend_Acl solution is handy.
nuqqsa
+2  A: 

Maybe you should redefine your design. I'd say it'd be preferable to define two classes:

  • a Role class
  • an User class

An User class could then have a collection of roles. That will turn the whole design simpler and cleaner.

An User can have none, one or different roles. But an User is not intrinsically any of the roles it may have. It just has the ability of assuming a role.

But of course you can use the Decorator Pattern here, as stated by other poster.

devoured elysium