Roles
List of examples:
Dependency injection
Initialize required services
C#
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
private readonly IUserInfoProvider userInfoProvider;
private readonly IRoleInfoProvider roleInfoProvider;
private readonly IUserRoleInfoProvider userRoleProvider;
private readonly IInfoProvider<ApplicationPermissionInfo> applicationPermissionInfoProvider;
public RolesServices(
IUserInfoProvider userInfoProvider,
IRoleInfoProvider roleInfoProvider,
IUserRoleInfoProvider userRoleProvider,
IInfoProvider<ApplicationPermissionInfo> applicationPermissionInfoProvider)
{
this.userInfoProvider = userInfoProvider;
this.roleInfoProvider = roleInfoProvider;
this.userRoleProvider = userRoleProvider;
this.applicationPermissionInfoProvider = applicationPermissionInfoProvider;
}
Roles
Create a role
C#
// Creates a new `RoleInfo` object
RoleInfo newRole = new()
{
RoleDisplayName = "New role's display name",
// Role's code name. Can contain only alphanumeric characters,
// some special characters (_, -, .) and cannot start or end with '.'.
// Cannot be "Administrator".
RoleName = "New-role-code-name",
// Role's optional description
RoleDescription = "New role's description."
};
// Writes a role in the database
roleInfoProvider.Set(newRole);
Assign a user to a role
C#
// Example: Assign the "NewUser" user to a "DigitalChannelManager" role
// Retrieves a `RoleInfo` object for the "DigitalChannelManager" role. Use the role's code name.
RoleInfo selectedRoleInfo = roleInfoProvider.Get("DancingGoat.DigitalChannelManager");
// Retrieves a `UserInfo` object for the "NewUser" user. Use the user's code name
UserInfo selectedUserInfo = userInfoProvider.Get("NewUser");
// Creates a `UserRoleInfo` object that represents the assignment
UserRoleInfo newRoleAssignment = new()
{
RoleID = selectedRoleInfo.RoleID,
UserID = selectedUserInfo.UserID
};
// Writes the user-role relationship to the database
userRoleInfoProvider.Set(newRoleAssignment);
Define application permissions for a specific role
C#
// Example: Add "Update" permission for the "Content hub"
// application to users with the "DigitalChannelManager" role.
// Prerequisites:
// 1. Add the following namespace: `using Kentico.Xperience.Admin.Base.UIPages;`.
// 2. Check the `UI tree` under the `System` application in the Xperience administration
// for the class that represents the application under "Back-end page definition".
// 3. Check "Role management" application to see the available permissions for the given application.
// Gets a `RoleInfo` object representing the "DigitalChannelManager" role
RoleInfo selectedRoleInfo = roleInfoProvider.Get("DancingGoat.DigitalChannelManager");
// Gets the `IDENTIFIER` constant for the selected application
string applicationName = Kentico.Xperience.Admin.Base.UIPages.ContentHubApplication.IDENTIFIER;
// Creates an `ApplicationPermissionInfo` object that represents the assignment
ApplicationPermissionInfo newApplicationPermissionRoleAssignment = new()
{
RoleID = selectedRoleInfo.RoleID,
// Use `System.Permissions`.<permission> to set the PermissionName
PermissionName = SystemPermissions.UPDATE,
ApplicationName = applicationName
};
// Writes the application permissions for a specific role to the database
applicationPermissionInfoProvider.Set(newApplicationPermissionRoleAssignment);