So I have an application that has several modules (think of modules as different pages), each module has a set of permissions; view, add, edit, delete
I want each user role to have privileges for each module, for example
Role A Permissions
Module 1 -> view
Module 2 -> add, edit
Module 3 -> view, add, edit, delete
etc.
How can I design the database to support that and how would I go about implementing it using bitwise operators (or would there be a more efficient way for this particular case?)
I already have the user, user_role and role tables but I'm unsure on how to design the Module table.
If you decide to use a bitmask, remember that the number of permissions you can keep track of is limited (you can track 31 permissions in a signed 4-byte integer database column). Each permission would then be assigned a value that is a power of two (1, 2, 4, 8, etc), and you could perform bitwise operations to check for permission matches.
From what you're looking to accomplish, I would suggest creating a role_has_module_privs table instead. This approach is much more scalable, and more efficient from a querying perspective. But if you have a finite number of combinations, bitmasks may be more efficient.