Beispiel #1
0
        /// <summary>
        /// 获取用户可(查看、编辑、删除)模块中哪些组织下的数据,
        /// 返回可(查看、编辑、删除)的组织Id集合
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="moduleId">模块Id</param>
        /// <param name="dataPermissionType">数据权限类型,0-查看,1-编辑,2-删除</param>
        /// <returns>返回组织Id集合字典</returns>
        public List <string> GetUserDataPermissions(Guid userId, Guid moduleId, int dataPermissionType)
        {
            List <string> orgIds = new List <string>();
            //如果存在用户自定义权限则取自定义用户权限
            string errMsg = string.Empty;
            BaseDAL <Sys_UserPermissionData> userPermissionDal = new BaseDAL <Sys_UserPermissionData>(this.CurrUser);
            Sys_UserPermissionData           permissionData    = userPermissionDal.GetEntity(out errMsg, x => x.Sys_UserId == userId && x.Sys_ModuleId == moduleId && !x.IsDeleted);
            string orgIdsStr = string.Empty;

            if (permissionData != null)
            {
                orgIdsStr = dataPermissionType == 0 ? permissionData.CanViewOrgIds :
                            (dataPermissionType == 1 ? permissionData.CanEditOrgIds : permissionData.CanDelOrgIds);
            }
            if (!string.IsNullOrWhiteSpace(orgIdsStr))
            {
                string[] token = orgIdsStr.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (token.Contains("-1"))
                {
                    return new List <string>()
                           {
                               "-1"
                           }
                }
                ;
                foreach (string orgId in token)
                {
                    if (!orgIds.Contains(orgId))
                    {
                        orgIds.Add(orgId);
                    }
                }
                return(orgIds);
            }
            //取用户角色权限
            List <Guid> roleIds = ObjectReferenceClass.GetUserRoleIds(userId);

            foreach (Guid roleId in roleIds)
            {
                Dictionary <string, bool> tempOrgIds = GetRoleDataPermissions(roleId, moduleId, dataPermissionType, true);
                if (tempOrgIds.ContainsKey("-1"))
                {
                    return new List <string>()
                           {
                               "-1"
                           }
                }
                ;
                foreach (string orgId in tempOrgIds.Keys)
                {
                    if (!orgIds.Contains(orgId))
                    {
                        orgIds.Add(orgId);
                    }
                }
            }
            return(orgIds);
        }
    }
}
Beispiel #2
0
        /// <summary>
        /// 获取用户(查看、新增、编辑)字段权限
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="moduleId">模块Id</param>
        /// <param name="fieldPermissionType">字段权限类型,0-查看,1-新增,2-编辑</param>
        /// <returns>返回允许(查看、新增、编辑)的字段名集合</returns>
        public List <string> GetUserFieldsPermissions(Guid userId, Guid moduleId, int fieldPermissionType)
        {
            List <string> fieldNames = new List <string>();
            //如果存在用户自定义权限则取自定义用户权限
            string errMsg = string.Empty;
            BaseDAL <Sys_UserPermissionField> userPermissionDal = new BaseDAL <Sys_UserPermissionField>();
            Sys_UserPermissionField           permissionField   = userPermissionDal.GetEntity(out errMsg, x => x.Sys_UserId == userId && x.Sys_ModuleId == moduleId && !x.IsDeleted);
            string fields = string.Empty;

            if (permissionField != null)
            {
                fields = fieldPermissionType == 0 ? permissionField.CanViewFields :
                         (fieldPermissionType == 1 ? permissionField.CanAddFields : permissionField.CanEditFields);
            }
            if (!string.IsNullOrWhiteSpace(fields))
            {
                string[] token = fields.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (token.Contains("-1"))
                {
                    return new List <string>()
                           {
                               "-1"
                           }
                }
                ;                                       //所有字段权限
                foreach (string field in token)
                {
                    if (!fieldNames.Contains(field))
                    {
                        fieldNames.Add(field);
                    }
                }
                return(fieldNames);
            }
            //取用户角色权限
            List <Guid> roleIds = ObjectReferenceClass.GetUserRoleIds(userId);

            foreach (Guid roleId in roleIds)
            {
                Dictionary <string, bool> tempFields = GetRoleFieldPermissions(roleId, moduleId, fieldPermissionType, true);
                if (tempFields.ContainsKey("-1"))
                {
                    return new List <string>()
                           {
                               "-1"
                           }
                }
                ;                                                                     //所有字段权限
                foreach (string field in tempFields.Keys)
                {
                    if (!fieldNames.Contains(field))
                    {
                        fieldNames.Add(field);
                    }
                }
            }
            return(fieldNames);
        }
    }
}
Beispiel #3
0
        /// <summary>
        /// 获取角色可(查看、编辑、删除)模块中哪些组织下的数据,返回可(查看、编辑、删除)
        /// 的组织Id集合字典,
        /// 第一个参数为组织Id,第二个参数为是否来自父角色的权限,非当前
        /// 角色的权限
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="moduleId">模块Id</param>
        /// <param name="dataPermissionType">数据权限类型,0-查看,1-编辑,2-删除</param>
        /// <param name="read">是否为应用读取</param>
        /// <returns>返回组织Id集合字典</returns>
        public Dictionary <string, bool> GetRoleDataPermissions(Guid roleId, Guid moduleId, int dataPermissionType, bool read = false)
        {
            string errMsg = string.Empty;
            Dictionary <string, bool> dic = new Dictionary <string, bool>();
            //取当前角色的数据权限
            Sys_PermissionData permissionData = this.GetEntity(out errMsg, x => x.Sys_RoleId == roleId && x.Sys_ModuleId == moduleId && !x.IsDeleted);
            string             orgIdsStr      = string.Empty;

            if (permissionData != null)
            {
                orgIdsStr = dataPermissionType == 0 ? permissionData.CanViewOrgIds :
                            (dataPermissionType == 1 ? permissionData.CanEditOrgIds : permissionData.CanDelOrgIds);
            }
            if (!string.IsNullOrWhiteSpace(orgIdsStr))
            {
                string[] token = orgIdsStr.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (read && token.Contains("-1"))
                {
                    dic.Add("-1", false);
                    return(dic);
                }
                foreach (string orgId in token)
                {
                    dic.Add(orgId, false);
                }
            }
            //取父角色的数据权限
            List <Guid> parentsRoleIds = ObjectReferenceClass.GetParentsRoleId(roleId); //获取所有父角色

            foreach (Guid tempRoleId in parentsRoleIds)
            {
                Sys_PermissionData tempPermission = this.GetEntity(out errMsg, x => x.Sys_RoleId == tempRoleId && x.Sys_ModuleId == moduleId && !x.IsDeleted);
                string             tempOrgIdsStr  = string.Empty;
                if (tempPermission != null)
                {
                    tempOrgIdsStr = dataPermissionType == 0 ? tempPermission.CanViewOrgIds :
                                    (dataPermissionType == 1 ? tempPermission.CanEditOrgIds : tempPermission.CanDelOrgIds);
                }
                if (!string.IsNullOrWhiteSpace(tempOrgIdsStr))
                {
                    string[] token = tempOrgIdsStr.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (read && token.Contains("-1"))
                    {
                        dic.Add("-1", false);
                        return(dic);
                    }
                    foreach (string orgId in token)
                    {
                        if (dic.ContainsKey(orgId))
                        {
                            continue;
                        }
                        dic.Add(orgId, true);
                    }
                }
            }
            return(dic);
        }
        /// <summary>
        /// 获取角色(查看、新增、编辑)字段权限,返回允许(查看、新增、编辑)的字段名字典,
        /// 第一个参数为字段名,第二个参数为是否来自父角色的权限,
        /// 非当前角色权限
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="moduleId">模块Id</param>
        /// <param name="fieldPermissionType">字段权限类型,0-查看,1-新增,2-编辑</param>
        /// <param name="read">是否为应用读取,应用读取快速返回</param>
        /// <returns>返回允许(查看、新增、编辑)的字段名字典</returns>
        public Dictionary <string, bool> GetRoleFieldPermissions(Guid roleId, Guid moduleId, int fieldPermissionType, bool read = false)
        {
            string errMsg = string.Empty;
            Dictionary <string, bool> dic = new Dictionary <string, bool>();
            //取当前角色的字段权限
            Sys_PermissionField permissionField = this.GetEntity(out errMsg, x => x.Sys_RoleId == roleId && x.Sys_ModuleId == moduleId && !x.IsDeleted);
            string fields = string.Empty;

            if (permissionField != null)
            {
                fields = fieldPermissionType == 0 ? permissionField.CanViewFields :
                         (fieldPermissionType == 1 ? permissionField.CanAddFields : permissionField.CanEditFields);
            }
            if (!string.IsNullOrWhiteSpace(fields))
            {
                string[] token = fields.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (read && token.Contains("-1")) //应用读取
                {
                    dic.Add("-1", false);
                    return(dic);
                }
                foreach (string field in token)
                {
                    dic.Add(field, false);
                }
            }
            //取父角色的字段权限
            List <Guid> parentsRoleIds = ObjectReferenceClass.GetParentsRoleId(roleId); //获取所有父角色

            foreach (Guid tempRoleId in parentsRoleIds)
            {
                Sys_PermissionField tempPermission = this.GetEntity(out errMsg, x => x.Sys_RoleId == tempRoleId && x.Sys_ModuleId == moduleId && !x.IsDeleted);
                string tempFields = string.Empty;
                if (tempPermission != null)
                {
                    tempFields = fieldPermissionType == 0 ? tempPermission.CanViewFields :
                                 (fieldPermissionType == 1 ? tempPermission.CanAddFields : tempPermission.CanEditFields);
                }
                if (!string.IsNullOrWhiteSpace(tempFields))
                {
                    string[] token = tempFields.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach (string field in token)
                    {
                        if (dic.ContainsKey(field))
                        {
                            continue;
                        }
                        dic.Add(field, true);
                    }
                }
            }
            return(dic);
        }
Beispiel #5
0
        /// <summary>
        /// 获取用户的功能(菜单或网格按钮)权限,返回字典,
        /// 字典的第一个参数为功能Id(菜单或按钮Id),第二个
        /// 参数为是否来自父角色的权限,非当前角色权限
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="functionType">功能类型(菜单、网格按钮),0-菜单,1-按钮</param>
        /// <param name="moduleId">模块ID,针对按钮需要</param>
        /// <returns></returns>
        public Dictionary <Guid, bool> GetUserFunPermissions(Guid userId, int functionType, Guid?moduleId)
        {
            Dictionary <Guid, bool> funIds = new Dictionary <Guid, bool>();
            //如果存在用户自定义权限则取自定义用户权限
            string errMsg = string.Empty;
            BaseDAL <Sys_UserPermissionFun> userPermissionDal    = new BaseDAL <Sys_UserPermissionFun>();
            Expression <Func <Sys_UserPermissionFun, bool> > exp = x => x.Sys_UserId == userId && x.FunType == functionType && !x.IsDeleted;

            if (functionType == 1 && moduleId.HasValue)
            {
                string mId = moduleId.Value.ToString().ToUpper();
                Expression <Func <Sys_UserPermissionFun, bool> > tempExp = x => x.Des == mId;
                exp = ExpressionExtension.And(exp, tempExp);
            }
            List <Sys_UserPermissionFun> userFuns = userPermissionDal.GetEntities(out errMsg, exp, null, false);

            if (userFuns != null && userFuns.Count > 0)
            {
                List <Guid> tempFunIds = userFuns.Select(x => x.FunId).Distinct().ToList();
                foreach (Guid funId in tempFunIds)
                {
                    if (funIds.Keys.Contains(funId))
                    {
                        continue;
                    }
                    funIds.Add(funId, false);
                }
                return(funIds);
            }
            //取用户角色权限
            List <Guid> roleIds = ObjectReferenceClass.GetUserRoleIds(userId);

            foreach (Guid roleId in roleIds)
            {
                Dictionary <Guid, bool> dic = GetRoleFunPermissions(roleId, functionType, moduleId);
                foreach (Guid funId in dic.Keys)
                {
                    if (funIds.ContainsKey(funId))
                    {
                        continue;
                    }
                    funIds.Add(funId, dic[funId]);
                }
            }
            return(funIds);
        }
Beispiel #6
0
        /// <summary>
        /// 获取角色功能权限,子角色默认继承父角色的所有权限,当前返回字典,
        /// 字典第一个参数为功能Id(菜单或按钮Id),第二个参数为是否该功能Id继承父角色,
        /// 非当前角色所有
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="functionType">功能类型,菜单或网格按钮,0-菜单,1-按钮</param>
        /// <param name="moduleId">模块ID,针对按钮需要</param>
        /// <returns>返回功能Id、是否来自父角色的权限字典</returns>
        public Dictionary <Guid, bool> GetRoleFunPermissions(Guid roleId, int functionType, Guid?moduleId)
        {
            string errMsg = string.Empty;
            //取当前角色的功能权限
            Expression <Func <Sys_PermissionFun, bool> > exp = x => x.Sys_RoleId == roleId && x.FunType == functionType && x.IsDeleted == false;

            if (functionType == 1 && moduleId.HasValue)
            {
                string mId = moduleId.Value.ToString().ToUpper();
                Expression <Func <Sys_PermissionFun, bool> > tempExp = x => x.Des == mId;
                exp = ExpressionExtension.And <Sys_PermissionFun>(exp, tempExp);
            }
            List <Sys_PermissionFun> permissionFuns = this.GetEntities(out errMsg, exp, null, false);
            Dictionary <Guid, bool>  dic            = new Dictionary <Guid, bool>();

            if (permissionFuns != null && permissionFuns.Count > 0)
            {
                List <Guid> funIds = permissionFuns.Select(x => x.FunId).Distinct().ToList();
                dic = funIds.ToDictionary(x => x, y => false);
            }
            //取所有父角色的功能权限
            List <Guid> parentsRoleIds = ObjectReferenceClass.GetParentsRoleId(roleId); //获取所有父角色

            foreach (Guid tempRoleId in parentsRoleIds)
            {
                List <Sys_PermissionFun> tempFuns = this.GetEntities(out errMsg, x => x.Sys_RoleId == tempRoleId && x.FunType == functionType && !x.IsDeleted, null, false);
                if (tempFuns != null && tempFuns.Count > 0)
                {
                    List <Guid> funIds = tempFuns.Select(x => x.FunId).ToList();
                    foreach (Guid funId in funIds)
                    {
                        if (dic.Keys.Contains(funId))
                        {
                            continue;
                        }
                        dic.Add(funId, true);
                    }
                }
            }
            return(dic);
        }