UserService.AddUserRoles
Reference
void UserService.AddUserRoles(Guid userId, string[] roleNames)
Use AddUserRoles to assign roles to 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 new roles to user does not remove the existing 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.AddUserRoles(Guid userId, roleNames);
Special case
- API will be useful in case of UserProfile type instances.
Troubleshooting
- If UserService.AddUserRoles is called through Post Triggers and Roles not assigned to user, confirm that program is executed successfully.
Sample Codes
Post Create Trigger Program
public class AddUserRoles: 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 rolesToAdd = 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 (rolesToAdd.Length > 0)
{
UserService.AddUserRoles(instanceId, rolesToAdd);
}
}
catch (Exception ex)
{
LoggingService.Error(ex.Message, ex);
}
}
}
Post Update Trigger Program
public class AddUserRoles: 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 rolesToAdd = 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 (rolesToAdd.Length > 0)
{
UserService.AddUserRoles(instanceId, rolesToAdd);
}
}
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 } };
}
}
}