Roles


List of examples:

Roles

Creating a new role



// Creates a new role object
RoleInfo newRole = new RoleInfo()
{
    // Sets the role properties
    RoleDisplayName = "New role",
    RoleName = "NewRole",
    SiteID = SiteContext.CurrentSiteID
};

// Verifies that the role is unique for the current site
if (RoleInfo.Provider.Get(newRole.RoleName, SiteContext.CurrentSiteID) == null)
{
    // Saves the role to the database
    RoleInfo.Provider.Set(newRole);
}
else
{
    // A role with the same name already exists on the site
}

> Back to list of examples

Updating an existing role



// Gets the role
RoleInfo updateRole = RoleInfo.Provider.Get("NewRole", SiteContext.CurrentSiteID);
if (updateRole != null)
{
    // Updates the role's properties
    updateRole.RoleDisplayName = updateRole.RoleDisplayName.ToLowerCSafe();

    // Saves the changes to the database
    RoleInfo.Provider.Set(updateRole);
}

> Back to list of examples

Updating multiple roles



// Gets all roles whose name starts with 'NewRole'
var roles = RoleInfo.Provider.Get().WhereStartsWith("RoleName", "NewRole");

// Loops through individual roles
foreach (RoleInfo modifyRole in roles)
{
    // Updates the role properties
    modifyRole.RoleDisplayName = modifyRole.RoleDisplayName.ToUpper();

    // Saves the changes
    RoleInfo.Provider.Set(modifyRole);
}

> Back to list of examples

Deleting a role



// Gets the role
RoleInfo deleteRole = RoleInfo.Provider.Get("NewRole", SiteContext.CurrentSiteID);

if (deleteRole != null)
{
    // Deletes the role
    RoleInfo.Provider.Delete(deleteRole);
}

> Back to list of examples

Global roles

Creating a new global role



// Creates a new role object
RoleInfo newRole = new RoleInfo()
{
    // Sets the role properties
    // All roles that omit the 'SiteID' property are treated as global by the system
    RoleDisplayName = "New role",
    RoleName = "NewRole"
};

// When retrieving global roles, provide 0 as the site ID value.
// This ensures the system only retrieves global roles (in case a role with a matching
// code name also exists as a site role)
if (RoleInfo.Provider.Get(newRole.RoleName, 0) == null)
{
    // Saves the global role to the database
    RoleInfo.Provider.Set(newRole);
}

> Back to list of examples

Updating an existing global role



// Gets the global role to update.
// When retrieving global roles, provide 0 as the site ID value.
// This tells the system to only retrieve global roles (in case a role with a matching
// code name also exists as a site role)
RoleInfo updateRole = RoleInfo.Provider.Get("NewRole", 0);
if (updateRole != null)
{
    // Updates the role's properties
    updateRole.RoleDisplayName = updateRole.RoleDisplayName.ToLowerCSafe();

    // Saves the changes to the database
    RoleInfo.Provider.Set(updateRole);
}

> Back to list of examples

User-role relationships

Checking if a user is in a role



// Gets the user
UserInfo user = UserInfo.Provider.Get("Username");

bool checkGlobalRoles = true;
bool checkMembership = true;

// Checks whether the user is assigned to a role with the "Rolename" code name
// The role can be assigned for the current site, as a global role, or indirectly through a membership
bool result = user.IsInRole("Rolename", SiteContext.CurrentSiteName, checkGlobalRoles, checkMembership);

> Back to list of examples

Getting all roles of a user



// Gets the user
UserInfo user = UserInfo.Provider.Get("Username");

if (user != null)
{
    // Gets the user's roles
    var userRoleIDs = UserRoleInfo.Provider.Get().Column("RoleID").WhereEquals("UserID", user.UserID);
    var roles = RoleInfo.Provider.Get().WhereIn("RoleID", userRoleIDs);

    // Loops through the roles
    foreach (RoleInfo role in roles)
    {
        // Process the role
    }
}

> Back to list of examples

Getting all users in a role



// Gets the role from the current site
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

if (role != null)
{
    // Gets the role's users
    var roleUserIDs = UserRoleInfo.Provider.Get().Column("UserID").WhereEquals("RoleID", role.RoleID);
    var users = UserInfo.Provider.Get().WhereIn("UserID", roleUserIDs);

    // Loops through the users
    foreach (UserInfo user in users)
    {
        // Process the user
    }
}

> Back to list of examples

Adding a user to a role



// Gets the user
UserInfo user = UserInfo.Provider.Get("Username");

// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

if ((user != null) && (role != null))
{
    // Adds the user to the role
    UserInfoProvider.AddUserToRole(user.UserName, role.RoleName, SiteContext.CurrentSiteName);
}

> Back to list of examples

Removing a user from a role



// Gets the user
UserInfo user = UserInfo.Provider.Get("Username");

// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

if ((user != null) && (role != null))
{
    // Removes the user from the role
    UserInfoProvider.RemoveUserFromRole(user.UserName, role.RoleName, SiteContext.CurrentSiteName);
}

> Back to list of examples

Role permissions

Assigning a module permission to a role



// Gets the module permission
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "CMS.Content", null);

// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

if ((permission != null) && (role != null))
{
    // Creates an object representing the role-permission relationship
    RolePermissionInfo newRolePermission = new RolePermissionInfo
    {
        // Assigns the permission to the role
        PermissionID = permission.PermissionId,
        RoleID = role.RoleID
    };

    // Saves the role-permission relationship into the database
    RolePermissionInfo.Provider.Set(newRolePermission);
}

> Back to list of examples

Removing a module permission from a role



// Gets the module permission
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "CMS.Content", null);

// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

if ((permission != null) && (role != null))
{
    // Gets the object representing the role-permission relationship
    RolePermissionInfo deleteRolePermission = RolePermissionInfo.Provider.Get(role.RoleID, permission.PermissionId);

    if (deleteRolePermission != null)
    {
        // Removes the permission from the role
        RolePermissionInfo.Provider.Delete(deleteRolePermission);
    }
}

> Back to list of examples

UI personalization

Assigning a UI element to a role



// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

// Gets the UI element (the element representing the Design tab in the Pages application in this case)
UIElementInfo element = UIElementInfoProvider.GetUIElementInfo("CMS.Design", "Design");

if ((role != null) && (element != null))
{
    // Creates an object representing the role-UI element relationship
    RoleUIElementInfo newRoleElement = new RoleUIElementInfo
    {
        // Assigns the UI element to the role
        RoleID = role.RoleID,
        ElementID = element.ElementID
    };

    // Saves the new relationship to the database
    RoleUIElementInfo.Provider.Set(newRoleElement);
}

> Back to list of examples

Removing a UI element from a role



// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);

// Gets the UI element (the element representing the Design tab in the Pages application in this case)
UIElementInfo element = UIElementInfoProvider.GetUIElementInfo("CMS.Design", "Design");

if ((role != null) && (element != null))
{
    // Gets the object representing the relationship between the role and the UI element
    RoleUIElementInfo deleteRoleElement = RoleUIElementInfo.Provider.Get(role.RoleID, element.ElementID);

    if (deleteRoleElement != null)
    {
        // Removes the UI element from the role
        RoleUIElementInfo.Provider.Delete(deleteRoleElement);
    }
}

> Back to list of examples