<分区>
我正在尝试为特权/权限编写一个类。
我正在尝试使用 C# 中的二进制基本权限系统。
我在我认为它应该起作用的地方得到了它。但是,它没有按预期工作。
之后,我逐步检查了我的代码,我可以在名为 _mapPermissions
的方法中看到一个问题,即 if (dic.ContainsKey(vv))
行每次都返回 false。
键是具有 2 个值(即 secionName
、KeyIndex
)的类的实例
这是我的整个类(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;
}
}
我是这样使用这个类的
var role = new Roles();
string[] aa = { "view", "use" };
if (role.hasAccess("system", aa))
{
Common.Alert("Welcome");
}
else
{
Common.Alert("NOP!");
}
请注意方法 Common.Alert()
它只是显示一条消息(即 MessageBox
)
以上代码运行时显示如下
1)systemPermissions
字典的内容按以下顺序PermissionsKey.sectionName
_PermissionsKey.KeyIndex
=> 值 (即 1、2、4、8、16、32、64 .....)
系统_do = > 1
系统使用 => 2
系统 View => 4
测试使用 => 1
测试 View => 2
2)usePermissions
字典的内容
system_do = > 1
test_use => 1
问题是 2 个变量 systemReq
和 userPerm
每次都返回 0。我不知道为什么
int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
int userPerm = this._mapPermissions(sectionName, keys, userPermissions);