Sic.Framework-Nanjing-Baishi/MECF.Framework.Common/MECF/Framework/Common/Account/Extends/RoleLoader.cs

398 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace MECF.Framework.Common.Account.Extends
{
/// <summary>
/// 角色加载器从Account.xml文件加载角色。
/// </summary>
public class RoleLoader : XmlLoader
{
#region Variables
private List<Role> _roleList;
private List<AccountEx> _accountList;
#endregion
#region Constructors
public RoleLoader(string fileName)
: base(fileName)
{
}
#endregion
#region Properties
/// <summary>
/// 返回角色列表。
/// </summary>
public List<Role> RoleList
{
get => _roleList;
set => _roleList = value;
}
/// <summary>
/// 返回账号列表。
/// </summary>
public List<AccountEx> AccountList
{
get => _accountList;
set => _accountList = value;
}
#endregion
#region Methods
/// <summary>
/// 获取所有非超级用户角色列表。
/// </summary>
/// <returns></returns>
public List<Role> GetRoles()
{
return _roleList.Where((Role e) => !e.IsSuper).ToList();
}
/// <summary>
/// 获取所有非超级用户账号列表。
/// </summary>
/// <returns></returns>
public List<AccountEx> GetAccounts()
{
return _accountList.Where((AccountEx e) => !e.IsSuper).ToList();
}
/// <summary>
/// 解析Account.xml文件。
/// </summary>
protected override void AnalyzeXml()
{
if (XmlDoc == null)
{
return;
}
var roleItems = from r in XmlDoc.Descendants("roleItem")
select (r);
var list = new List<Role>();
foreach (var roleNode in roleItems)
{
var strRoleId = roleNode.Attribute("id").Value;
var strRoleName = roleNode.Attribute("name").Value;
var strIsAutoLogout = roleNode.Attribute("autologout").Value;
var strAutoLogoutTime = roleNode.Attribute("logouttime").Value;
int.TryParse(strAutoLogoutTime, out var logoutTime);
var isAutoLogout = ((strIsAutoLogout == "1"));
var strReserved = roleNode.Attribute("buildin")?.Value ?? "false";
if (!bool.TryParse(strReserved, out var isBuildIn))
isBuildIn = false;
var strDescription = roleNode.Attribute("description")?.Value ?? "";
var permContent= roleNode.Value;
var role = new Role(strRoleId, strRoleName, isAutoLogout, logoutTime, permContent, isBuildIn, strDescription);
list.Add(role);
}
var roleAdmin = new Role("-1", "Administrators", true, 20, null, true, null)
{
IsSuper = true
};
list.Add(roleAdmin);
_roleList = list;
var users = from r in XmlDoc.Descendants("userItem")
select (r);
var lstUsers = new List<AccountEx>();
foreach (var userNode in users)
{
var lstRolesBelongTo = new List<string>();
var strUserId = userNode.Attribute("id").Value;
var strLoginName = userNode.Attribute("loginname").Value;
var strPassword = Decrypt(userNode.Attribute("password").Value);
var strFirstName = userNode.Attribute("firstname").Value;
var strLastName = userNode.Attribute("lastname").Value;
var strEmail = userNode.Attribute("email").Value;
var strDescription = "";
if (userNode.Attribute("description") != null)
{
strDescription = userNode.Attribute("description").Value;
}
var userRoles = from ro in userNode.Descendants("role")
select (ro);
foreach (var userRoleNode in userRoles)
{
var strRoleId = userRoleNode.Attribute("id").Value;
lstRolesBelongTo.Add(strRoleId);
}
var item3 = new AccountEx(strUserId, strLoginName, strPassword, strFirstName, strLastName, strEmail, lstRolesBelongTo, strDescription);
lstUsers.Add(item3);
}
var item4 = new AccountEx("-1", "admin", "admin", "", "", "", new List<string> { "-1" })
{
IsSuper = true
};
lstUsers.Add(item4);
_accountList = lstUsers;
}
/// <summary>
/// 更新指定的角色。
/// </summary>
/// <param name="newRole"></param>
/// <returns></returns>
public bool UpdateRole(Role newRole)
{
var oldRole = _roleList.Find((Role item) => item.RoleId == newRole.RoleId);
if (oldRole == null)
{
_roleList.Add(newRole);
}
else
{
_roleList[_roleList.IndexOf(oldRole)] = newRole;
}
var doc = XmlDoc;
var list = (from m_xRole in doc.Descendants("roleItem")
where m_xRole.Attribute("id").Value == newRole.RoleId
select m_xRole).ToList();
if (list.Count > 0)
{
list[0].Attribute("name").Value = newRole.RoleName;
list[0].Attribute("autologout").Value = (newRole.IsAutoLogout ? "1" : "0");
list[0].Attribute("logouttime").Value = newRole.LogoutTime.ToString();
if(list[0].Attribute("buildin")!= null)
list[0].Attribute("buildin").Value = newRole.IsBuildIn.ToString();
if (list[0].Attribute("description") != null)
list[0].Attribute("description").Value = newRole.Description;
list[0].Value = newRole.PermissionContent;
}
else
{
var content = new XElement("roleItem", new XAttribute("id", newRole.RoleId), new XAttribute("name", newRole.RoleName), new XAttribute("autologout", newRole.IsAutoLogout ? "1" : "0"), new XAttribute("logouttime", newRole.LogoutTime), new XAttribute("description", newRole.Description))
{
Value = newRole.PermissionContent
};
doc.Root.Element("roles").Add(content);
}
doc.Save(FileName);
return true;
}
public bool DeleteRole(string roleId)
{
Load();
var roleToDelete = _roleList.Find((Role item) => item.RoleId == roleId);
if (roleToDelete != null)
{
_roleList.Remove(roleToDelete);
var xmlDoc = XmlDoc;
var list = (from m_xRole in xmlDoc.Descendants("roleItem")
where m_xRole.Attribute("id").Value == roleId
select m_xRole).ToList();
if (list.Count > 0)
{
list[0].Remove();
foreach (var item in _accountList)
{
if (item.RoleIDs.Contains(roleToDelete.RoleId))
{
item.RoleIDs.Remove(roleToDelete.RoleId);
}
}
list = (from m_xRole in xmlDoc.Descendants("role")
where m_xRole.Attribute("id").Value == roleToDelete.RoleId
select m_xRole).ToList();
if (list.Count > 0)
{
list.Remove();
}
xmlDoc.Save(FileName);
return true;
}
return false;
}
return false;
}
private List<string> GetRolePermission(string roleId)
{
var result = new List<string>();
foreach (var item in _roleList)
{
if (item.RoleId == roleId)
{
result = item.PermissionContent.Split(';').ToList();
break;
}
}
return result;
}
private int GetMenuPermission(List<string> rolePermissions, string menuid)
{
foreach (var rolePermission in rolePermissions)
{
if (rolePermission.IndexOf(menuid) >= 0)
{
var array = rolePermission.Split(',');
if (array.Length > 1 && array[0].Trim() == menuid)
{
return int.Parse(array[1].Trim());
}
}
}
return 1; //MP_NONE
}
public List<AppMenu> GetMenusByRole(string roleId, List<AppMenu> menuList)
{
var list = new List<AppMenu>();
var rolePermission = GetRolePermission(roleId);
foreach (var menu in menuList)
{
var list2 = new List<AppMenu>();
foreach (var menuItem in menu.MenuItems)
{
var appMenu = new AppMenu(menuItem.MenuID, menuItem.ViewModel, menuItem.ResKey, null,
menuItem.Description)
{
System = menuItem.System,
AlarmModule = menuItem.AlarmModule,
Permission = GetMenuPermission(rolePermission, menuItem.MenuID)
};
if (appMenu.Permission > 1)
{
list2.Add(appMenu);
}
}
if (list2.Count > 0)
{
var appMenu2 = new AppMenu(menu.MenuID, menu.ViewModel, menu.ResKey, list2, menu.Description);
appMenu2.System = menu.System;
appMenu2.AlarmModule = menu.AlarmModule;
list.Add(appMenu2);
}
}
return list;
}
public int GetMenuPermission(string roleid, string menuName)
{
var rolePermission = GetRolePermission(roleid);
return GetMenuPermission(rolePermission, menuName);
}
public bool UpdateAccount(AccountEx p_newAccount)
{
var accountEx = _accountList.Find((AccountEx item) => item.UserId == p_newAccount.UserId);
if (accountEx == null)
{
_accountList.Add(p_newAccount);
}
else
{
_accountList[_accountList.IndexOf(accountEx)] = p_newAccount;
}
var xdoc = XmlDoc;
var list = (from xAccount in xdoc.Descendants("userItem")
where xAccount.Attribute("id").Value == p_newAccount.UserId
select xAccount).ToList();
if (list.Count > 0)
{
list[0].SetAttributeValue("loginname", p_newAccount.LoginName);
list[0].SetAttributeValue("password", Encrypt(p_newAccount.Password));
list[0].SetAttributeValue("firstname", p_newAccount.FirstName);
list[0].SetAttributeValue("lastname", p_newAccount.LastName);
list[0].SetAttributeValue("email", p_newAccount.Email);
list[0].SetAttributeValue("description", p_newAccount.Description);
list[0].Element("rolegroup").RemoveAll();
foreach (var roleID in p_newAccount.RoleIDs)
{
list[0].Element("rolegroup").Add(new XElement("role", new XAttribute("id", roleID)));
}
}
else
{
var xElement = new XElement("userItem", new XAttribute("id", p_newAccount.UserId), new XAttribute("loginname", p_newAccount.LoginName), new XAttribute("password", Encrypt(p_newAccount.Password)), new XAttribute("firstname", p_newAccount.FirstName), new XAttribute("lastname", p_newAccount.LastName), new XAttribute("email", p_newAccount.Email), new XAttribute("description", p_newAccount.Description), new XElement("rolegroup"));
foreach (var roleID2 in p_newAccount.RoleIDs)
{
xElement.Element("rolegroup").Add(new XElement("role", new XAttribute("id", roleID2)));
}
xdoc.Root.Element("users").Add(xElement);
}
xdoc.Save(FileName);
return true;
}
public bool DeleteAccount(string p_strUserID)
{
var accountEx = _accountList.Find((AccountEx item) => item.UserId == p_strUserID);
if (accountEx != null)
{
_accountList.Remove(accountEx);
var xdoc = XmlDoc;
var list = (from xAccount in xdoc.Descendants("userItem")
where xAccount.Attribute("id").Value == p_strUserID
select xAccount).ToList();
if (list.Count > 0)
{
list[0].Remove();
xdoc.Save(FileName);
return true;
}
return false;
}
return false;
}
public string Encrypt(string encrytStr)
{
if (string.IsNullOrWhiteSpace(encrytStr))
{
return string.Empty;
}
try
{
var bytes = Encoding.UTF8.GetBytes(encrytStr);
return Convert.ToBase64String(bytes);
}
catch
{
return encrytStr;
}
}
public string Decrypt(string decryptStr)
{
if (string.IsNullOrWhiteSpace(decryptStr))
{
return string.Empty;
}
try
{
var bytes = Convert.FromBase64String(decryptStr);
return Encoding.UTF8.GetString(bytes);
}
catch
{
return decryptStr;
}
}
#endregion
}
}