Mon, 03/11/2024 - 15:55 By Lloyd Sebag Contributor Lloyd Sebag

If for any reason you need to assign a specific Privileges to a Security Role, you can use this code : 

// The ID of the role without the privileges
Guid RoleId = new Guid("7ad78e66-a4df-ee11-904c-000d3a8312b6");

// The privileges to add to the role
string[] privileges = { "prvAllowTDSAccess" };

// Retrieve the privileges
var query = new QueryExpression
{
    EntityName = "privilege",
    ColumnSet = new ColumnSet("privilegeid", "name")
};
query.Criteria.AddCondition("name", ConditionOperator.In, privileges);

DataCollection<Entity> privilegeRecords =
   service.RetrieveMultiple(query).Entities;

// Define a list to hold the RolePrivileges we'll need to add
List<RolePrivilege> rolePrivileges = new List<RolePrivilege>();

//Populate the RolePrivileges parameter
foreach (Entity privilege in privilegeRecords)
{
    RolePrivilege rolePrivilege = new RolePrivilege(
       (int)PrivilegeDepth.Global,
       privilege.GetAttributeValue<Guid>("privilegeid"));

    rolePrivileges.Add(rolePrivilege);
}

// Prepart the request
AddPrivilegesRoleRequest request = new AddPrivilegesRoleRequest()
{
    Privileges = rolePrivileges.ToArray(),
    RoleId = RoleId
};
// Send the request
service.Execute(request);

 

Add new comment

Image CAPTCHA
Enter the characters shown in the image.