I am trying to write a class for privileges/permissions.
I am trying to use the Binary base permission System in C#.
I got it where I believe it should work. However, it is not working as expected.
After, I stepped thru my code I can see one issue in the method called _mapPermissions the line if (dic.ContainsKey(vv)) returns false every time.
the key is an instance of a class which have 2 values (ie. secionName, KeyIndex)
This is my entire class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;
namespace POS
{
    class Roles
    {
        private Dictionary<PermissionsKey, int> systemPermissions = new Dictionary<PermissionsKey, int>();
        private Dictionary<PermissionsKey, int> userPermissions = new Dictionary<PermissionsKey, int>();
        public Roles()
        {
            this._getSystemPrivleges();
            this._getUserPrivleges();
            this._display(systemPermissions);
            this._display(userPermissions);
        }
        public bool hasAccess(string sectionName, string[] keys)
        {
            if (    this.systemPermissions.Count == 0
                 || this.userPermissions.Count == 0
                 || keys.Count() == 0
                 || String.IsNullOrEmpty(sectionName) 
             )
            {
                return false;
            }
            int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
            int userPerm = this._mapPermissions(sectionName, keys, userPermissions);
            int check =  systemReq & userPerm;
            Common.Alert("System Value" + systemReq + Environment.NewLine +"User Value " + userPerm + Environment.NewLine + "AND Results " + check);
            if (check == 0)
            {
                return false;
            }
            return true;
        }
        private int _mapPermissions(string secName, string[] keys, Dictionary<PermissionsKey, int> dic)
        {
            int newKey = 0;
            var vv = new PermissionsKey();
            foreach (string k in keys)
            {
                vv.sectionName = secName;
                vv.KeyIndex = k;
                if (dic.ContainsKey(vv))
                {
                    newKey |= dic[vv] ;
                }
            }
            return newKey;
        }
        private void _getSystemPrivleges()
        {
            var db = new dbConnetion();
            string sql =   " SELECT SQL_CACHE "
                         + " KeyIndex, Value, sectionName "
                         + " FROM role_keys "
                         + " WHERE status = 'active' ";
            foreach (var i in db.getData(sql, null, r => new SystemPermissions()
                                                           {
                                                                 pIndex = r["KeyIndex"].ToString()
                                                               , pSec = r["sectionName"].ToString()
                                                               , pValue = r["Value"].ToString()
                                                           }
                                           )
                    )
            {
                var vv = new PermissionsKey();
                vv.sectionName = i.pSec;
                vv.KeyIndex = i.pIndex;
                systemPermissions.Add(vv, Convert.ToInt32(i.pValue));
            }
        }
        private void _getUserPrivleges()
        {
            var db = new dbConnetion();
            string sql = " SELECT SQL_CACHE k.sectionName, k.KeyIndex, k.value "
                        +" FROM users AS su "
                        +" INNER JOIN role_relation AS r ON r.roleID = su.roleID "
                        +" INNER JOIN role_keys AS k ON k.KeyID = r.KeyID "
                        +" WHERE su.status = 'active' AND su.userID = @userid ";
            var parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter("@userid", UserInfo.UserID));
            foreach (var i in db.getData(sql, parms , r => new SystemPermissions()
                                                            {
                                                                  pIndex = r["KeyIndex"].ToString()
                                                                , pSec = r["sectionName"].ToString()
                                                                , pValue = r["Value"].ToString()
                                                            }
                                           )
                    )
            {
                var vv = new PermissionsKey();
                vv.sectionName = i.pSec;
                vv.KeyIndex = i.pIndex;
                userPermissions.Add(vv, Convert.ToInt32(i.pValue));
            }
        }
        private void _display(Dictionary<PermissionsKey, int> dic)
        {
            string str = "";
            foreach (KeyValuePair<PermissionsKey, int> d in dic)
            {
                var vv = new PermissionsKey();
                var c = d.Key;
                str += c.sectionName + "_" + c.KeyIndex + "  =>  " + d.Value.ToString() + Environment.NewLine;
            }
            Common.Alert(str);
        }
        private int _BinToInt(string str)
        {
            Regex binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
            int val = -1;
            if (binary.IsMatch(str))
            {
                val = Convert.ToInt32(str, 2);
            }
            return val;
        }
        private string _IntToBin(int number)
        {
            return Convert.ToString(number, 2);
        }
    }
    class SystemPermissions{
        public string pIndex;
        public string pSec;
        public string pValue;
    }
    class PermissionsKey
    {
        public string sectionName;
        public string KeyIndex;
    }
}
And I use this class like so
    var role = new Roles();
    string[] aa = { "view", "use" };
    if (role.hasAccess("system", aa))
    {
        Common.Alert("Welcome");
    }
    else
    {
        Common.Alert("NOP!");
    }
Note that the method Common.Alert() it simply display a Message (ie. MessageBox)
The above code displays the following when it runs
1) the content of the systemPermissions dictionary in the following order PermissionsKey.sectionName_PermissionsKey.KeyIndex => the value (ie. 1 , 2, 4, 8, 16, 32, 64 .....)
    system_do = > 1
    system_use => 2
    system_view => 4
    test_use => 1
    test_view => 2
2) The content of the usePermissions dictionary 
system_do = > 1
test_use => 1
The issue is that the 2 variables systemReq  and userPerm return 0 every time. I am not sure why
 int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
 int userPerm = this._mapPermissions(sectionName, keys, userPermissions);
 
     
    