I need to update the list of users for a security role. How can I avoid re-associating an user that already has the role assigned?
I'm trying to build a query to get the role where user x is not in the role's list of users and then use it for the association if the user was not found. Here is what I have so far:
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 }
};
I need to build something like this query:
select * from role
where role.name = "RoleName"
and not exists
(select 1 from userRoles
where userRoles.roleid = role.roleid
and userRoles.user = "xyz")