Using bitwise operation OR & AND on integer for validation
Today I'm going to revisit bitwise OR (|) and AND (&) operation on integer specifically for role based security checking scenario.
Scenario: There are few reason why bitwise operation on integer is needed in an application, but more frequently seen to be use for validation for e.g. user's role validation. This is a very useful scenario, the application stores only one integer on the database to represent all the role that is assigned to that user. And on the application, user role is usually represented as Enum (Enum is a kind of integer).
Lets get into some coding. Following is small C# program I wrote on LINQPad.
In Part (A), I define user roles, this would be Enum in the application. Then in Part (B), I assigned the roles to user, this is stores in a column in the user table. Later in Part (C) I use the bitwise operation AND (&) to do checking if a user as a specific role. The output of the above code are:
Scenario: There are few reason why bitwise operation on integer is needed in an application, but more frequently seen to be use for validation for e.g. user's role validation. This is a very useful scenario, the application stores only one integer on the database to represent all the role that is assigned to that user. And on the application, user role is usually represented as Enum (Enum is a kind of integer).
Lets get into some coding. Following is small C# program I wrote on LINQPad.
void Main() { // PART (A) Define user role int Public = 1; int Sales = 2; int Marketing = 4; int Engineering = 8; // PART (B) Assign some role to user. Assigned role is stored as an integer in the db of the user table. int currentUser1 = Sales | Public; int currentUser2 = Marketing | Public; int currentUser3 = Sales | Marketing | Public; // Display the integer representation of the user role currentUser1.Dump("CurrentUser1"); currentUser2.Dump("CurrentUser2"); currentUser3.Dump("CurrentUser3"); // PART (C) Checking if user belongs to a role. bool isCurrentUser1Public = (currentUser1 & Public ) == Public; isCurrentUser1Public.Dump("currentUser1 is Public"); bool isCurrentUser1Sales = (currentUser1 & Sales ) == Sales; isCurrentUser1Sales.Dump("currentUser1 is Sales"); bool isCurrentUser3Marketing = (currentUser3 & Marketing) == Marketing; isCurrentUser3Marketing.Dump("currentUser3 is Marketing"); bool isCurrentUser3Engineering = (currentUser3 & Engineering) == Engineering; isCurrentUser3Engineering.Dump("currentUser3 is Engineering"); }
In Part (A), I define user roles, this would be Enum in the application. Then in Part (B), I assigned the roles to user, this is stores in a column in the user table. Later in Part (C) I use the bitwise operation AND (&) to do checking if a user as a specific role. The output of the above code are:
CurrentUser1 3 CurrentUser2 5 CurrentUser3 7 currentUser1 is Public True currentUser1 is Sales True currentUser3 is Marketing True currentUser3 is Engineering False
Note: I'm not going to cover into details on why using the OR operator the number will added up or why using the AND operator the number will be minus out. For that details you can visit MSDN - C# Operators.
Comments
Post a Comment