Multiple permission types (roles) stored in database as single decimal -
i going ask question here whether or not design users/roles database tables acceptable, after research came across question:
what best way handle multiple permission types?
it sounds innovative approach, instead of many-to-many relationship users_to_roles table, have multiple permissions defined single decimal (int data type presume). means permissions single user in 1 row. won't make sense until read other question , answer
i can't brain around one. can please explain conversion process? sounds "right", i'm not getting how convert roles decimal before goes in db, , how gets converted when comes out of db. i'm using java, if stubbed out, cool well.
here original answer in off chance other question gets deleted:
"personally, use flagged enumeration of permissions. way can use and, or, not , xor bitwise operations on enumeration's items.
[flags] public enum permission { viewusers = 1, // 2^0 // 0000 0001 editusers = 2, // 2^1 // 0000 0010 viewproducts = 4, // 2^2 // 0000 0100 editproducts = 8, // 2^3 // 0000 1000 viewclients = 16, // 2^4 // 0001 0000 editclients = 32, // 2^5 // 0010 0000 deleteclients = 64, // 2^6 // 0100 0000 }
then, can combine several permissions using , bitwise operator.
for example, if user can view & edit users, binary result of operation 0000 0011 converted decimal 3. can store permission of 1 user single column of database (in our case 3).
inside application, need bitwise operation (or) verify if user has particular permission or not."
you use bitwise operations. pseudo-code like:
bool haspermission(user user, permission permission) { return (user.permission & permission) != 0; } void setpermission(user user, permission permission) { user.permission |= permission; } void clearpermission(user user, permission permission) { user.permission &= ~permission; }
permission enum type defined in post, though whatever type needs based on integer-like type. same applies user.permission field.
if operators (&, |=, , &=) don't make sense you, read on bitwise operations (bitwise , and bitwise or).
Comments
Post a Comment