I have 5 tables: user > user_has_role > role > role_has_permission > permission
An user can have 0 or * roles.
And a role can have 0 or * permissions
now i have a query which will get all permissions belonging to a user.
This work but now i want that if the user has the role “super_admin” then all permissions get joined. But without specifing them in the table role_has_permission.
This is query:
SELECT p.name,r.name role
FROM user_has_role AS ur
JOIN role AS r
ON ur.role_id = r.id
JOIN role_has_permission AS rp
ON r.id = rp.role_id
LEFT JOIN permission AS p
ON rp.permission_id = p.id
WHERE ur.user_id = [USER_ID]
You could just do a union: