I’ve spent the last few days working out a permission model for the application I’m developing. I’ve always wanted to use bits and bitwise operators for some sort of authorization system and it’s everything I ever thought it would be.
What’s really cool is that I’ve encapsulated the permissions to the user object so that all my fellow developers have to do to use it is call
User.CheckPermissions and then pass in the desired enumeration values. It returns a true or false. .NET makes it terribly easy, requiring nothing more than the Flag attribute on the enumeration and setting the enumeration values to powers of 2.
What’s more, thanks to the ease of bitwise operation, they can pass in several permissions in bitwise AND/OR formation. This makes for an incredibly powerful authorization system. In other words, my co-workers can say
User.CheckPermissions(Permissions.CanDoThis & Permissions.CanDoThat) and find out whether the user can do both this and that. Having just spent the last day or two actually implementing permissions throughout the app, I can say that this is very intuitive and clear.
There’s some other little nifty features, but I can’t go into them unfortunately.
[NOTE: The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of Go Daddy Software, Inc.]