class RBCA
{
Dictionary<string, string> userToRole = new Dictionary<string, string>();
Dictionary<string, List<string>> roleToPermission = new Dictionary<string, List<string>>();
public RBCA()
{
//初始化manager的权限
string manager = "Manager";
List<string> managerlist = new List<string>();
managerlist.Add("OpenFile");
managerlist.Add("CloseFile");
managerlist.Add("SaveFile");
//初始化worker的权限
string worker = "Worker";
List<string> workerlist = new List<string>();
workerlist.Add("OpenFile");
roleToPermission.Add(manager, managerlist);
roleToPermission.Add(worker, workerlist);
//初始化用户pixysoft的角色
string user = "pixysoft";
userToRole.Add(user, manager);
}
public void OpenFile(string user)
{
string role = userToRole[user];
//检测权限
if (!roleToPermission[role].Contains("OpenFile"))
return;
Console.Write("Open file succeed!");
}
public void CloseFile(string user)
{
string role = userToRole[user];
//检测权限
if (!roleToPermission[role].Contains("CloseFile"))
return;
Console.Write("CloseFile succeed!");
}
public void SaveFile(string user)
{
string role = userToRole[user];
//检测权限
if (!roleToPermission[role].Contains("SaveFile"))
return;
Console.Write("SaveFile succeed!");
}
}