Suppose I have a users table and in that users table I have a role_id.
In my roles table I have:
users
- id
- role_id
roles
- id
- name
roles
id | name
1 | admin
2 | normal
Now in code I want to disallow access to the admin.php page. So at the top I put
if ($user->role_id != 1){
header('location: forbidden.php');
}
It seems like bad practice to rely on role_id being 1. I would think it is better to do something like:
if ($user->role_name == 'admin'){ ...
Now I am relying on the text of the role, which isn't great. So I have created const classes like:
class RoleType{
const ADMIN = "admin";
const NORNAML = "normal";
}
....
if ($user->role_name == RoleType::ADMIN){ ...
The issue here is now if I change it in the database I have to change my RoleType class.
I feel like none of these is ideal.
What is the best way to handle this?
if ($user->hasPrivilege("thisPermission")) {
– Robert Harvey Jul 20 at 21:14