Permissions hell

CryoEnix

Regular
Joined
Jan 11, 2003
Messages
93
Location
Wrexham, Wales
Hey guys, long time no speak - I've got a bit of an issue here, but I'm sure one of you will be able to help...


Lets say that I've got several applications with a number of permissions allowed for each, stored within a single long data type:


App A is worth 1
App B is worth 2
App C is worth 4
App D is worth 8
App E is worth 16
App F is worth 32

And so on, ad infinitum.

Now the problem is, say if I need to know if the user has an access code of 45, how can I calculate whether he has access to application C or not - I know there's an algorithm for it, I just can't put my finger on it...
 
Sorry for not clarifying, but I'm working in C# for this one - although the algorithm itself shouldn't be too difficult to translate should it be in any other language (just no PERL please ^^)
 
Hi Cryo,


Look up the <Flags()> attribute for enumerations - very very useful...
Visual Basic:
<Flags()> _
Public Enum Apps
    None = 0
    AppA = 1
    AppB = 2
    AppC = 4
    AppD = 8
End Enum
After this, you simply need to set the property to point to this enumeration and use logical operators to manage the checking...
Visual Basic:
Public Class MyApp
    ...
    Private _AppType As Apps
    ...
    Public Property AppType() As Apps
        Get
            Return _AppType
        End Get
        Set(ByVal value As Apps)
            _AppType = value
        End Set
    End Property
    ...
End Class
...
...
    If (thisApp.AppType And Apps.AppA) = Apps.AppA Then
        'Do your stuff...
        ...
    End If
Does that help?


Paul.
 
Cheers paul, I have that in VB, but the C# port doesn't seem to like this - is disagrees with the double ampersand. Any ideas?

Code:
if ((thisApp.AppType && Apps.AppA) == Apps.AppA ){//'Do your stuff...        ...   
}
 
It's alright Paul, I've figured it out. Turns out that the double ampersand is just for boolean comparisons - a single is for binary, which is why it didn't work. It seems that the Basic 'And' statement does either, depending on the situation.

Thanks anyway!
 
Back
Top