/// <summary> /// 获取用户的权限主键数组 /// </summary> /// <param name="systemCode">系统编号</param> /// <param name="userId">用户主键</param> /// <param name="companyId">公司主键</param> /// <param name="containPublic"></param> /// <param name="useBaseRole">使用基础角色权限</param> /// <returns>拥有权限数组</returns> public static string[] GetPermissionIdsByUserByCache(string systemCode, string userId, string companyId = null, bool containPublic = true, bool useBaseRole = false) { // 公开的操作权限需要计算 string[] result = null; var errorMark = 0; var tableName = BaseModuleEntity.CurrentTableName; if (string.IsNullOrWhiteSpace(systemCode)) { systemCode = "Base"; } // 就不需要参合基础的角色了 if (systemCode.Equals("Base", StringComparison.OrdinalIgnoreCase)) { useBaseRole = false; } tableName = systemCode + "Module"; try { errorMark = 1; // 01: 把公开的部分获取出来(把公开的主键数组从缓存里获取出来,减少数据库的读取次数) if (containPublic) { var moduleEntities = new BaseModuleManager().GetEntitiesByCache(systemCode); if (moduleEntities != null) { result = moduleEntities.Where((t => t.IsPublic == 1 && t.Enabled == 1 && t.Deleted == 0)).Select(t => t.Id.ToString()).ToArray(); } } // 02: 获取用户本身拥有的权限 var userPermissionIds = BasePermissionManager.GetPermissionIdsByCache(systemCode, userId); result = StringUtil.Concat(result, userPermissionIds); // 03: 用户角色的操作权限 // 用户都在哪些角色里?通过缓存读取?没有角色的,没必要进行运算了 var roleIds = BaseUserManager.GetRoleIdsByCache(systemCode, userId, companyId); if (useBaseRole && !systemCode.Equals("Base", StringComparison.OrdinalIgnoreCase)) { var baseRoleIds = BaseUserManager.GetRoleIdsByCache("Base", userId, companyId); if (baseRoleIds != null && baseRoleIds.Length > 0) { roleIds = StringUtil.Concat(roleIds, baseRoleIds); } } if (roleIds != null && roleIds.Length > 0) { var userRolePermissionIds = BasePermissionManager.GetPermissionIdsByCache(systemCode, roleIds); result = StringUtil.Concat(result, userRolePermissionIds); } } catch (Exception ex) { var writeMessage = "BasePermissionManager.GetPermissionIdsByUser:发生时间:" + DateTime.Now + Environment.NewLine + "errorMark = " + errorMark + Environment.NewLine + "Message:" + ex.Message + Environment.NewLine + "Source:" + ex.Source + Environment.NewLine + "StackTrace:" + ex.StackTrace + Environment.NewLine + "TargetSite:" + ex.TargetSite + Environment.NewLine; LogUtil.WriteLog(writeMessage, "Exception"); } return(result); }
/// <summary> /// 获取用户的权限主键数组 /// </summary> /// <param name="systemCode">系统编号</param> /// <param name="userId">用户主键</param> /// <param name="companyId">公司主键</param> /// <param name="useBaseRole">使用基础角色权限</param> /// <returns>拥有权限数组</returns> public static string[] GetPermissionIdsByUserByCache(string systemCode, string userId, string companyId = null, bool containPublic = true, bool useBaseRole = false) { // 公开的操作权限需要计算 string[] result = null; int errorMark = 0; string tableName = BaseModuleEntity.TableName; if (string.IsNullOrWhiteSpace(systemCode)) { systemCode = "Base"; } // 就不需要参合基础的角色了 if (systemCode.Equals("Base")) { useBaseRole = false; } tableName = systemCode + "Module"; try { errorMark = 1; // 01: 把公开的部分获取出来(把公开的主键数组从缓存里获取出来,减少数据库的读取次数) if (containPublic) { List <BaseModuleEntity> moduleEntities = BaseModuleManager.GetEntitiesByCache(systemCode); if (moduleEntities != null) { result = moduleEntities.Where((t => t.IsPublic == 1 && t.Enabled == 1 && t.DeletionStateCode == 0)).Select(t => t.Id.ToString()).ToArray(); } } // 02: 获取用户本身拥有的权限 string[] userPermissionIds = BaseUserPermissionManager.GetPermissionIdsByCache(systemCode, userId); result = StringUtil.Concat(result, userPermissionIds); // 03: 用户角色的操作权限 // 用户都在哪些角色里?通过缓存读取?没有角色的,没必要进行运算了 string[] roleIds = BaseUserManager.GetRoleIdsByCache(systemCode, userId, companyId); if (useBaseRole && !systemCode.Equals("Base", StringComparison.OrdinalIgnoreCase)) { string[] baseRoleIds = BaseUserManager.GetRoleIdsByCache("Base", userId, companyId); if (baseRoleIds != null && baseRoleIds.Length > 0) { roleIds = StringUtil.Concat(roleIds, baseRoleIds); } } if (roleIds != null && roleIds.Length > 0) { string[] userRolePermissionIds = BaseRolePermissionManager.GetPermissionIdsByCache(systemCode, roleIds); result = StringUtil.Concat(result, userRolePermissionIds); } // 04: 按部门(组织机构)获取权限项 if (BaseSystemInfo.UseOrganizePermission && !string.IsNullOrEmpty(companyId)) { // 2016-02-26 吉日嘎拉,公司权限进行优化简化 string[] organizePermission = BaseOrganizePermissionManager.GetPermissionIdsByCache(systemCode, companyId); result = StringUtil.Concat(result, organizePermission); } } catch (System.Exception ex) { string writeMessage = "BasePermissionManager.GetPermissionIdsByUser:发生时间:" + DateTime.Now + System.Environment.NewLine + "errorMark = " + errorMark.ToString() + System.Environment.NewLine + "Message:" + ex.Message + System.Environment.NewLine + "Source:" + ex.Source + System.Environment.NewLine + "StackTrace:" + ex.StackTrace + System.Environment.NewLine + "TargetSite:" + ex.TargetSite + System.Environment.NewLine; FileUtil.WriteMessage(writeMessage, BaseSystemInfo.StartupPath + "//Exception//Exception" + DateTime.Now.ToString(BaseSystemInfo.DateFormat) + ".txt"); } return(result); }