views:

13

answers:

1

Let's say I'm storing an integer for each user in a database that represents their assigned permissions. The number stored is the sum of the powers of two associated with each permission assigned.

What's the easiest way to check to see if a certain permission was granted (within VBScript/ASP, or generally)? The best idea I have is to convert the integer to a binary string and check the bit I want, but I sense there is a better solution (and I feel guilty for not realizing it yet.)

A: 
DIM ADMIN as Integer = 128

DIM UserPermissionCode as Intger

if (UserPermissionCode and ADMIN) = ADMIN Then
    ' user is an admin
endif

That was pretty much just a guess at the VB.NET syntax. Here's a correct version in C#

[Flags]
enum Permissions 
{ 
   User = 0x01;
   PowerUser = 0x02;
   Admin = 0x80
}

Permissions UserCode;

if (UserCode & Permissions.Admin == Permissions.Admin)
{
    // user is admin
}
James Curran

related questions