Beispiel #1
0
        /// <summary>
        /// 检查角色权限
        /// </summary>
        /// <param name="systemCode"></param>
        /// <param name="roleId"></param>
        /// <param name="permissionCode">权限编码</param>
        /// <returns></returns>
        public static bool CheckPermissionByRoleByCache(string systemCode, string roleId, string permissionCode)
        {
            var permissionId = string.Empty;

            permissionId = new BaseModuleManager().GetIdByCodeByCache(systemCode, permissionCode);
            // 没有找到相应的权限
            if (string.IsNullOrEmpty(permissionId))
            {
                return(false);
            }

            var permissionIds = BasePermissionManager.GetPermissionIdsByCache(systemCode, new string[] { roleId });

            return(Array.IndexOf(permissionIds, permissionId) >= 0);
        }
Beispiel #2
0
        /// <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);
        }