PHP Object Oriented is the Best Way to Implement Multiple Custom Roles

I have three types of users: A, B, C .. Therefore, the base class of the user, and then we have 3 derived classes. However, the user can have 2 types at the same time. How can we deal with this in decent mode, bearing in mind that the type of user will determine the type of access that they have in the application.

+4
source share
3 answers

I would use a decorator pattern like

$user = new User (); $user = new Role1 ($user); $user = new Role2 ($user); 
+5
source

Perhaps you should redefine your design. I would say that it would be preferable to define two classes:

  • role class
  • Custom class

A custom class can have a set of roles. This will simplify and simplify the entire design.

A user can have one, one, or different roles. But the User is not inherently any of the roles that he may have. He simply has the ability to take on the role.

But of course, you can use the Decorator template here, as indicated by another poster.

+5
source

I would look at the Zend Framework ACL class. Good job when it comes to multiple roles for different users. There are also many articles and blogs discussing the use of the Zend ACL. More General ACL Details: wikipedia - acl

Another way to address different roles and permissions is through role-based access control: wikipedia - role-based access control

+3
source

Source: https://habr.com/ru/post/1309535/


All Articles