Exemplo n.º 1
0
        /// <summary>
        /// 获取数据权限目标主键
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="resourceCategory">资源类别</param>
        /// <param name="targetId">目标资源主键</param>
        /// <param name="targetResourceCategory">目标资源类别</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>资源主键数组</returns>
        public string[] GetPermissionScopeResourceIds(UserInfo userInfo, string resourceCategory, string targetId, string targetResourceCategory, string permissionItemCode)
        {
            string[] returnValue = null;
            var      parameter   = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                var permissionItemId      = permissionItemManager.GetId(PiPermissionItemTable.FieldCode, permissionItemCode);
                var names   = new string[6];
                var values  = new object[6];
                names[0]    = PiPermissionScopeTable.FieldResourceCategory;
                values[0]   = resourceCategory;
                names[1]    = PiPermissionScopeTable.FieldTargetId;
                values[1]   = targetId;
                names[2]    = PiPermissionScopeTable.FieldPermissionId;
                values[2]   = permissionItemId;
                names[3]    = PiPermissionScopeTable.FieldTargetCategory;
                values[3]   = targetResourceCategory;
                names[4]    = PiPermissionScopeTable.FieldDeleteMark;
                values[4]   = 0;
                names[5]    = PiPermissionScopeTable.FieldEnabled;
                values[5]   = 1;
                returnValue = DbCommonLibary.GetIds(dbProvider, PiPermissionScopeTable.TableName, names, values, PiPermissionScopeTable.FieldResourceId);
            });
            return(returnValue);
        }
        /// <summary>
        /// 设置约束条件
        /// </summary>
        /// <param name="resourceCategory">资源类别</param>
        /// <param name="resourceId">资源主键</param>
        /// <param name="tableName">表名</param>
        /// <param name="constraint">约束</param>
        /// <param name="enabled">有效</param>
        /// <param name="permissionCode">操作权限项</param>
        /// <returns>主键</returns>
        public string SetConstraint(string resourceCategory, string resourceId, string tableName, string permissionCode, string constraint, bool enabled = true)
        {
            string returnValue = string.Empty;

            string permissionId = string.Empty;
            PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(this.UserInfo);

            permissionId = permissionItemManager.GetIdByAdd(permissionCode);

            PiPermissionScopeManager manager = new PiPermissionScopeManager(this.DBProvider, this.UserInfo);

            string[] names =
            {
                PiPermissionScopeTable.FieldResourceCategory
                , PiPermissionScopeTable.FieldResourceId
                , PiPermissionScopeTable.FieldTargetCategory
                , PiPermissionScopeTable.FieldTargetId
                , PiPermissionScopeTable.FieldPermissionId
                , PiPermissionScopeTable.FieldDeleteMark
            };
            Object[] values =
            {
                resourceCategory
                , resourceId
                , "Table"
                , tableName
                , permissionId
                , 0
            };

            // 1:先获取是否有这样的主键,若有进行更新操作。
            // 2:若没有进行添加操作。
            returnValue = manager.GetId(names, values);
            if (!string.IsNullOrEmpty(returnValue))
            {
                string[] targetFields = { PiPermissionScopeTable.FieldPermissionConstraint, PiPermissionScopeTable.FieldEnabled };
                Object[] targetValues = { constraint, enabled ? 1 : 0 };
                manager.SetProperty(PiPermissionScopeTable.FieldId, returnValue, targetFields, targetValues);
            }
            else
            {
                PiPermissionScopeEntity entity = new PiPermissionScopeEntity
                {
                    ResourceCategory     = resourceCategory,
                    ResourceId           = resourceId,
                    TargetCategory       = "Table",
                    TargetId             = tableName,
                    PermissionConstraint = constraint,
                    PermissionId         = permissionId,
                    DeleteMark           = 0,
                    Enabled = enabled ? 1 : 0
                };
                returnValue = manager.Add(entity);
            }
            return(returnValue);
        }
        /// <summary>
        /// 移动实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="permissionItemId">权限项主键</param>
        /// <param name="parentId">父主键</param>
        /// <returns>影响行数</returns>
        public int MoveTo(UserInfo userInfo, string permissionItemId, string parentId)
        {
            var returnValue = 0;
            var parameter   = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_MoveTo, "权限项主键:" + permissionItemId + ",父主键:" + parentId);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                returnValue = new PiPermissionItemManager(dbProvider, userInfo).MoveTo(permissionItemId, parentId);
            });
            return(returnValue);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchDelete(UserInfo userInfo, string[] ids)
        {
            var returnValue = 0;
            var parameter   = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_BatchDelete);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                returnValue = new PiPermissionItemManager(dbProvider, userInfo).Delete(ids);
            });
            return(returnValue);
        }
        /// <summary>
        /// 按父节点获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="parentId">父节点主键</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByParent(UserInfo userInfo, string parentId)
        {
            var dataTable = new DataTable(PiPermissionItemTable.TableName);
            var parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_GetDTByParent);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                dataTable           = new PiPermissionItemManager(dbProvider, userInfo).GetDT(PiPermissionItemTable.FieldParentId, parentId, PiPermissionItemTable.FieldDeleteMark, 0, PiPermissionItemTable.FieldSortCode);
                dataTable.TableName = PiPermissionItemTable.TableName;
            });
            return(dataTable);
        }
        /// <summary>
        /// 批量移动实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="permissionItemIds">权限项主键数组</param>
        /// <param name="parentId">父主键</param>
        /// <returns>影响行数</returns>
        public int BatchMoveTo(UserInfo userInfo, string[] permissionItemIds, string parentId)
        {
            var returnValue = 0;
            var parameter   = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_BatchMoveTo);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                returnValue += permissionItemIds.Sum(t => permissionItemManager.MoveTo(t, parentId));
            });

            return(returnValue);
        }
        /// <summary>
        /// 按主键数组获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">角色主键</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
        {
            var dataTable = new DataTable(PiRoleTable.TableName);
            var parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_GetDTByIds);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                dataTable           = new PiPermissionItemManager(dbProvider, userInfo).GetDT(PiPermissionItemTable.FieldId, ids, PiPermissionItemTable.FieldSortCode);
                dataTable           = BusinessLogic.SetFilter(dataTable, PiPermissionItemTable.FieldDeleteMark, "0");
                dataTable.TableName = PiPermissionItemTable.TableName;
            });
            return(dataTable);
        }
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public PiPermissionItemEntity GetEntity(UserInfo userInfo, string id)
        {
            var permissionItemEntity = new PiPermissionItemEntity();
            var parameter            = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_GetEntity);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                var returnDataTable       = new DataTable(PiPermissionItemTable.TableName);
                returnDataTable           = permissionItemManager.GetDTById(id);
                permissionItemEntity      = BaseEntity.Create <PiPermissionItemEntity>(returnDataTable);
            });
            return(permissionItemEntity);
        }
        /// <summary>
        /// 获取授权范围
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <returns>数据表</returns>
        public DataTable GetLicensedDT(UserInfo userInfo, string userId)
        {
            var dataTable = new DataTable(PiPermissionItemTable.TableName);
            var parameter = ParameterUtil.CreateWithOutMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var permissionAdminManager = new PiPermissionItemManager(dbProvider, userInfo);
                var permissionItemId       = permissionAdminManager.GetId(PiPermissionItemTable.FieldDeleteMark, 0, PiPermissionItemTable.FieldCode, "Resource.ManagePermission");
                dataTable           = permissionAdminManager.GetDTByUser(userId, permissionItemId);
                dataTable.TableName = PiPermissionItemTable.TableName;
            });
            return(dataTable);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 获取关联的权限项列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="moduleId">主键</param>
        /// <returns>数据表</returns>
        public DataTable GetPermissionDT(UserInfo userInfo, string moduleId)
        {
            DataTable dataTable = new DataTable(PiPermissionTable.TableName);
            var       parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.ModuleService_GetPermissionDT);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var modulePermissionManager = new PermissionModuleManager(dbProvider, userInfo);
                string[] ids = modulePermissionManager.GetPermissionIds(moduleId);
                var permissionAdminManager = new PiPermissionItemManager(dbProvider, userInfo);
                dataTable           = permissionAdminManager.GetDT(ids);
                dataTable.TableName = PiPermissionItemTable.TableName;
            });
            return(dataTable);
        }
        /// <summary>
        /// 获取权限项列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public List <PiPermissionItemEntity> GetList(UserInfo userInfo)
        {
            List <PiPermissionItemEntity> list = new List <PiPermissionItemEntity>();
            var parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_GetDT);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager = new PiPermissionItemManager(dbProvider, userInfo);
                List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>(PiOrganizeTable.FieldDeleteMark, 0)
                };
                list = manager.GetList <PiPermissionItemEntity>(parameters, PiPermissionItemTable.FieldSortCode);
            });
            return(list);
        }
        /// <summary>
        /// 按详细信息添加
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="code">编号</param>
        /// <param name="fullName">名称</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>主键</returns>
        public string AddByDetail(UserInfo userInfo, string code, string fullName, out string statusCode, out string statusMessage)
        {
            string returnValue   = string.Empty;
            string returnCode    = string.Empty;
            string returnMessage = string.Empty;
            var    parameter     = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, "按详细信息添加");

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                returnValue   = permissionItemManager.AddByDetail(code, fullName, out returnCode);
                returnMessage = permissionItemManager.GetStateMessage(returnCode);
            });
            statusCode    = returnCode;
            statusMessage = returnMessage;
            return(returnValue);
        }
        /// <summary>
        /// 添加操作权限项
        /// </summary>
        /// <param name="userInfo">当前用户</param>
        /// <param name="permissionItemEntity">权限定义实体</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>主键</returns>
        public string Add(UserInfo userInfo, PiPermissionItemEntity permissionItemEntity, out string statusCode, out string statusMessage)
        {
            string returnValue   = string.Empty;
            string returnCode    = string.Empty;
            string returnMessage = string.Empty;
            var    parameter     = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.PermissionItemService_Add);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                returnValue   = permissionItemManager.Add(permissionItemEntity, out returnCode);
                returnMessage = permissionItemManager.GetStateMessage(returnCode);
            });
            statusCode    = returnCode;
            statusMessage = returnMessage;
            return(returnValue);
        }
        public PiPermissionScopeEntity GetConstraintEntity(string resourceCategory, string resourceId, string tableName, string permissionCode = "Resource.AccessPermission")
        {
            PiPermissionScopeEntity entity = null;

            string permissionId = string.Empty;
            PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(this.UserInfo);

            permissionId = permissionItemManager.GetIdByAdd(permissionCode);

            PiPermissionScopeManager manager = new PiPermissionScopeManager(this.DBProvider, this.UserInfo);

            string[] names =
            {
                PiPermissionScopeTable.FieldResourceCategory
                , PiPermissionScopeTable.FieldResourceId
                , PiPermissionScopeTable.FieldTargetCategory
                , PiPermissionScopeTable.FieldTargetId
                , PiPermissionScopeTable.FieldPermissionId
                , PiPermissionScopeTable.FieldDeleteMark
            };
            Object[] values =
            {
                resourceCategory
                , resourceId
                , "Table"
                , tableName
                , permissionId
                , 0
            };

            // 1:先获取是否有这样的主键,若有进行更新操作。
            DataTable dt = manager.GetDT(names, values);

            if (dt.Rows.Count > 0)
            {
                entity = BaseEntity.Create <PiPermissionScopeEntity>(dt);
            }
            return(entity);
        }
        /// <summary>
        /// 获取用户的条件约束表达式
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="permissionCode">权限代码</param>
        /// <returns>主键</returns>
        public string GetUserConstraint(string tableName, string permissionCode = "Resource.AccessPermission")
        {
            string returnValue = string.Empty;
            // 这里是获取用户的条件表达式
            // 1: 首先用户在哪些角色里是有效的?
            // 2: 这些角色都有哪些哪些条件约束?
            // 3: 组合约束条件?
            // 4:用户本身的约束条件?
            string permissionId = string.Empty;
            PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(this.UserInfo);

            permissionId = permissionItemManager.GetIdByAdd(permissionCode);

            PiUserRoleManager manager = new PiUserRoleManager(this.DBProvider, this.UserInfo);

            string[] roleIds = manager.GetAllRoleIds(UserInfo.Id);
            if (roleIds == null || roleIds.Length == 0)
            {
                return(returnValue);
            }
            PiPermissionScopeManager scopeManager = new PiPermissionScopeManager(this.DBProvider, this.UserInfo);

            string[] names =
            {
                PiPermissionScopeTable.FieldResourceCategory
                , PiPermissionScopeTable.FieldResourceId
                , PiPermissionScopeTable.FieldTargetCategory
                , PiPermissionScopeTable.FieldTargetId
                , PiPermissionScopeTable.FieldPermissionId
                , PiPermissionScopeTable.FieldEnabled
                , PiPermissionScopeTable.FieldDeleteMark
            };
            Object[] values =
            {
                PiRoleTable.TableName
                , roleIds
                , "Table"
                , tableName
                , permissionId
                , 1
                , 0
            };

            DataTable dtPermissionScope    = scopeManager.GetDT(names, values);
            string    permissionConstraint = string.Empty;

            foreach (DataRow dataRow in dtPermissionScope.Rows)
            {
                permissionConstraint = dataRow[PiPermissionScopeTable.FieldPermissionConstraint].ToString();
                permissionConstraint = permissionConstraint.Trim();
                if (!string.IsNullOrEmpty(permissionConstraint))
                {
                    returnValue += " AND " + permissionConstraint;
                }
            }

            //得到当前用户的约束条件
            string userConstraint = this.GetConstraint(PiUserTable.TableName, this.UserInfo.Id, tableName) ?? "";

            if (!string.IsNullOrEmpty(userConstraint))
            {
                returnValue += " AND " + userConstraint;
            }

            if (!string.IsNullOrEmpty(returnValue))
            {
                returnValue = returnValue.Substring(5);
                // 解析替换约束表达式标准函数
                returnValue = ConstraintUtil.PrepareParameter(this.UserInfo, returnValue);
            }

            return(returnValue);
        }
        /// <summary>
        /// 获取约束条件(所有的约束)
        /// </summary>
        /// <param name="resourceCategory">资源类别</param>
        /// <param name="resourceId">资源主键</param>
        /// <param name="permissionCode">权限代码</param>
        /// <returns>数据表</returns>
        public DataTable GetConstraintDT(string resourceCategory, string resourceId, string permissionCode = "Resource.AccessPermission")
        {
            DataTable dataTable = new DataTable(CiTableColumnsTable.TableName);

            /*
             * -- 这里是都有哪些表?
             * SELECT ItemValue, ItemName
             * FROM PiTablePermissionScope
             * WHERE (DeleteMark = 0)
             * AND (Enabled = 1)
             * ORDER BY PiTablePermissionScope.SortCode
             */

            /*
             * -- 这里是都有有哪些表达式
             * SELECT     Id, TargetId, PermissionConstraint   -- 对什么表有什么表达式?
             * FROM         PiPermissionScope
             * WHERE (ResourceId = 10000000)
             * AND (ResourceCategory = 'PiRole')   -- 什么角色?
             * AND (TargetId = 'PiUser')
             * AND (TargetCategory = 'Table')
             * AND (PermissionId = 10000001)  -- 有什么权限?(资源访问权限)
             * AND (DeleteMark = 0)
             * AND (Enabled = 1)
             */

            string permissionId = string.Empty;
            PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(this.UserInfo);

            permissionId = permissionItemManager.GetIdByAdd(permissionCode);

            string sqlQuery = @" SELECT PIPERMISSIONSCOPE.ID
		                                    , PITABLEPERMISSIONSCOPE.ITEMVALUE  TABLECODE
		                                    , PITABLEPERMISSIONSCOPE.ITEMNAME  TABLENAME
		                                    , PIPERMISSIONSCOPE.PERMISSIONCONSTRAINT
		                                    , PITABLEPERMISSIONSCOPE.SORTCODE
                                    FROM  (
	                                    SELECT ITEMVALUE
		                                     , ITEMNAME
		                                     , SORTCODE
	                                    FROM PITABLEPERMISSIONSCOPE
                                       WHERE (DELETEMARK = 0) 
		                                      AND (ENABLED = 1)                                              
                                        )  PITABLEPERMISSIONSCOPE LEFT OUTER JOIN
                                        (SELECT ID
			                                    , TARGETID
			                                    , PERMISSIONCONSTRAINT  
                                           FROM PIPERMISSIONSCOPE
                                         WHERE (RESOURCECATEGORY = '" + resourceCategory + @"') 
			                                    AND (RESOURCEID = '"             + resourceId + @"') 
			                                    AND (TARGETCATEGORY = 'Table') 
			                                    AND (PERMISSIONID = '"             + permissionId + @"') 
			                                    AND (DELETEMARK = 0) 
			                                    AND (ENABLED = 1)
	                                     )  PIPERMISSIONSCOPE 
                                    ON PITABLEPERMISSIONSCOPE.ITEMVALUE = PIPERMISSIONSCOPE.TARGETID
                                    ORDER BY PITABLEPERMISSIONSCOPE.SORTCODE ";

            dataTable           = this.Fill(sqlQuery);
            dataTable.TableName = CiTableColumnsTable.TableName;

            return(dataTable);
        }