Managing roles

To manage roles, use the Telerik.Sitefinity.Security.RoleManager class. To get an instance of the manager with the default roles data provider use the following line of code:

RoleManager roleManager = RoleManager.GetManager();


For more information, see Roles data providers.

The following sections explain how to use the API of the roles:

Creating roles

The following example creates a role.

First, you get an instance of the roles manager. Then, you create the role by calling the CreateRole method of the manager. Finally, you save the changes.

public void CreateRole(string roleName)
{
    RoleManager roleManager = RoleManager.GetManager();
 
    if (roleManager.GetRoles().Where(r => r.Name == roleName).FirstOrDefault() == null)
    {
        roleManager.CreateRole(roleName);
    }
    else
    {
        // Handle appropriately when the role already exists
    }
 
    roleManager.SaveChanges();
}

Querying roles

You can query a role by:

  • ID
  • Name

Querying a role by ID

The following example queries for a role by its ID.

First, you get an instance of the roles manager. Then, you get all roles by calling the GetRoles method of the manager and filter based on the ID property.

public Role GetRole(Guid roleId)
{
    RoleManager roleManager = RoleManager.GetManager();
 
    return roleManager.GetRoles().Where(r => r.Id == roleId).SingleOrDefault();
}

Querying a role by name

The following example queries for a role by its name.

First, you get an instance of the roles manager. Then, you get all roles by calling the GetRoles method of the manager and filter based on the Name property.

public Role GetRoleByName(string roleName)
{
    RoleManager roleManager = RoleManager.GetManager();
 
    return roleManager.GetRoles().Where(r => r.Name == roleName).SingleOrDefault();
}

Querying all roles

The following example queries for all roles

First, you get an instance of the roles manager. Then, you get all roles by calling the GetRoles method of the manager.

public List<Role> GetAllRoles()
{
    RoleManager roleManager = RoleManager.GetManager();
 
    return roleManager.GetRoles().ToList();
}

Modifying roles

The following example modifies a role by changing its name.

To modify a role, you get an instance of the role. In this example, you get in instance of the role by its ID. For more information, see Querying roles. Then, you change the name and, finally, you save the changes.

public void ModifyRole(Guid roleId, string newRoleName)
{
    RoleManager roleManager = RoleManager.GetManager();
 
    Role role = roleManager.GetRoles().Where(r => r.Id == roleId).FirstOrDefault();
    if (role != null)
    {
        role.Name = newRoleName;
        roleManager.SaveChanges();
    }
}

Deleting roles

The following example deletes a role.

To delete a role, you get an instance of the role. In this example, you get an instance of the role by its ID. For more information, see Querying roles. Then, you delete the role by calling the Delete method of the manager and passing the role as an argument. Finally, you save the changes.

public void DeleteRole(Guid roleId)
{
    RoleManager roleManager = RoleManager.GetManager();
 
    Role role = roleManager.GetRoles().Where(r => r.Id == roleId).FirstOrDefault();
    if (role != null)
    {
        roleManager.Delete(role);
        roleManager.SaveChanges();
    }
}

See also

Roles

Roles data providers

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK