UserService.RemoveUserRoles
Reference
void UserService.RemoveUserRoles(Guid userId, string[] roleNames)
Use RemoveUserRoles to remove roles of user.
Parameters
userId: The unique Guid for a Protrak UserProfile type instance or Protrak user. This id can be used to get user and assign role(s).roleNames: Array of role names to assign roles to user. it cannot be empty.
Returns
Caveats
- This API add remove roles of user does not add the roles.
- This API is accessible to admin user and through trigger programs for all users.
- This API gives error
Roles are required.if roleNames is null. - This API gives error
You are not authorized to change user role or user type.if API is called by non-admin user. - This API gives error
User with id <Guid> does not exist.when protrak user not found with userId or UserProfile type instance id.
Usage
var userRoles = new string[] { ADMIN, TENANT_ADMINISTRATOR };
UserService.RemoveUserRoles(Guid userId, roleNames);
Special case
- API will be useful in case of UserProfile type instances.
Troubleshooting
- If UserService.RemoveUserRolesis called through Post Triggers and Roles not removed form user, confirm that program is executed successfully.
Sample Codes
Post Create Trigger Program
public class RemoveUserRoles: IPostCreateTriggerProgram
{
public IInstanceService InstanceService { get; set; }
public IUserService UserService { get; set; }
public ILoggingService LoggingService { get; set; }
//Picklist Attribute with OptionValues 'Roles' and associated with 'UserProfile' type
private readonly string Attribute_User_Roles = "Roles";
public void Run(Guid instanceId)
{
try
{
var attributes = new string[] { Attribute_User_Roles };
var userProfile = InstanceService.GetInstance(instanceId, attributes);
var rolesToRemove = userProfile != null && userProfile.Attributes != null &&
userProfile.Attributes.Any(x => x.Name == Attribute_User_Roles && x.ArrayValue != null) ?
userProfile.Attributes.First(x => x.Name == Attribute_User_Roles && x.ArrayValue != null).ArrayValue : new string[0];
if (rolesToRemove.Length > 0)
{
UserService.RemoveUserRoles(instanceId, rolesToRemove);
}
}
catch (Exception ex)
{
LoggingService.Error(ex.Message, ex);
}
}
}
Post Update Trigger Program
public class RemoveUserRoles : IPostUpdateTriggerProgram
{
public IInstanceService InstanceService { get; set; }
public IUserService UserService { get; set; }
public ILoggingService LoggingService { get; set; }
//Picklist Attribute with OptionValues 'Roles' and associated with 'UserProfile' type
private readonly string Attribute_User_Roles = "Roles";
public ProgramResult Run(Guid instanceId)
{
bool programResult = true;
string error = string.Empty;
try
{
var attributes = new string[] { Attribute_User_Roles };
var userProfile = InstanceService.GetInstance(instanceId, attributes);
var rolesToRemove = userProfile != null && userProfile.Attributes != null &&
userProfile.Attributes.Any(x => x.Name == Attribute_User_Roles && x.ArrayValue != null) ?
userProfile.Attributes.First(x => x.Name == Attribute_User_Roles && x.ArrayValue != null).ArrayValue : new string[0];
if (rolesToRemove.Length > 0)
{
UserService.RemoveUserRoles(instanceId, rolesToRemove);
}
}
catch (Exception ex)
{
programResult = false;
error = ex.Message;
LoggingService.Error(ex.Message, ex);
}
if (programResult)
{
return new ProgramResult() { IsSuccess = true, Errors = null };
}
else
{
return new ProgramResult() { IsSuccess = false, Errors = new string[] { error } };
}
}
}