0

我需要更新安全角色的用户列表。如何避免重新关联已分配角色的用户?

我正在尝试构建一个查询以获取用户 x 不在角色的用户列表中的角色,然后在找不到用户时将其用于关联。这是我到目前为止所拥有的:

LinkEntity userRoles = new LinkEntity
{
    LinkFromEntityName = "role",
    LinkToEntityName = "systemuserroles",
    LinkFromAttributeName = "roleid",
    LinkToAttributeName = "roleid",
    JoinOperator = JoinOperator.LeftOuter,
    Columns = new ColumnSet(true),
    EntityAlias = "userroles",
    LinkCriteria =
    {
        Conditions =
        {
            new ConditionExpression{
                //not sure how to build the "where user x does not exists"
           }
        }
    }
};

QueryExpression roleQuery = new QueryExpression
{
    EntityName = "role",
    ColumnSet = new ColumnSet(true),
    Criteria =
    {
        Conditions = { 
            new ConditionExpression { 
                AttributeName = "name", 
                Operator = ConditionOperator.Equal, 
                Values = { "RoleName"}
            } 
        }
    },
    LinkEntities = { userRoles }
};

我需要构建类似这样的查询:

select * from role 
where role.name = "RoleName"
and not exists
(select 1 from userRoles 
where userRoles.roleid = role.roleid
and userRoles.user = "xyz")
4

1 回答 1

0

根据这篇使用 Fetch XML 进行解释的帖子,只要我指定的用户不存在,我就能够构建获取角色的查询:

LinkEntity userRoles = new LinkEntity
{
    LinkFromEntityName = "role",
    LinkToEntityName = "systemuserroles",
    LinkFromAttributeName = "roleid",
    LinkToAttributeName = "roleid",
    JoinOperator = JoinOperator.LeftOuter,
    Columns = new ColumnSet(true),
    EntityAlias = "userroles",
    LinkCriteria =
    {
        Conditions =
         {                         
             new ConditionExpression {
                 AttributeName = "systemuserid",
                 Operator = ConditionOperator.Equal,
                 Values = { "xyz" }
             }
         }
    }
};

QueryExpression roleQuery = new QueryExpression
{
    EntityName = "role",
    ColumnSet = new ColumnSet(true),
    Criteria =
    {
        Filters = {
            new FilterExpression{
                FilterOperator = LogicalOperator.And,
                Conditions = {
                    new ConditionExpression{
                        EntityName = "systemuserroles",
                        AttributeName = "roleid",
                        Operator = ConditionOperator.Null
                    }
                }
            }                    
        },
        Conditions = { 
                    new ConditionExpression { 
                        AttributeName = "name", 
                        Operator = ConditionOperator.Equal, 
                        Values = { "RoleName"}
                    }
                }
    },
    LinkEntities = { userRoles }
};
于 2018-01-03T17:38:01.427 回答