Lee posted on January 28, 2008 12:05

I was goofing off with bitmasking again and decided to do some in c#. I came up with this helper class to aid in working with bit fields as an example. This class takes advantage of an enum with flags attribute to assist with assigning values to the flags. An interesting addition might be to create the logic so that, for example, the particular database permissions would take precedence over another. So, when you assign a user DB data reader and DB denydatareader, the permission "deny" would be the prevailing permission. Likewise, if assigning Administrator would not allow a mask for db datareader, for instance, because that permission set has higher privileges than the latter.

Lee

using System;
using
System.Collections.Generic;

namespace BitWiseExample_Permissions
{
    [FlagsAttribute]
    public enum DBPermissionValues : int
   
{
        None = 0,
        DbDataReader = 1,
        DbDataWriter = 2,
        Administrator = 4
    }; 

   
public class DBPermissions
   
{
        public DBPermissions()
        {
            _permissions = DBPermissionValues.None;
       

       
public DBPermissions(DBPermissionValues permissions)
        {
            _permissions = permissions;
       

       
private DBPermissionValues _permissions;
        public bool IsDbWriter
        {
           
get
           
{
                return ((_permissions
                        & DBPermissionValues.DbDataWriter)
                            == DBPermissionValues.DbDataWriter);
           
           
set
           
{
                if (value == true)
                    _permissions = _permissions
                        | DBPermissionValues.DbDataWriter;
            }
        }

        public bool IsDbReader
        {
           
get
           
{
                return ((_permissions &
                        DBPermissionValues.DbDataReader)
                           == DBPermissionValues.DbDataReader);
           
           
set
           
{
                if (value == true)
                    _permissions =
                        _permissions
                        | DBPermissionValues.DbDataReader;
            }
       

       
public bool IsAdmin
        {
           
get
           
{
                return ((_permissions
                            & DBPermissionValues.Administrator)
                        == DBPermissionValues.Administrator);
           
           
set
           
{
                if (value == true)
                    _permissions =
                        _permissions
                        | DBPermissionValues.Administrator;
            }
        }
    }
}

-------------- 


Glad this isn't a database blog


Posted in: .Net  Tags: , , ,

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




by Lee Everest, M.S.

Search


Ads

Calendar

«  July 2010  »
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2010 Lee Everest's SQL Server, etc. weblog