Kentico CMS 7.0 Developer's Guide

Roles and users

Roles and users

Previous topic Next topic Mail us feedback on this topic!  

Roles and users

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example assigns a role to a user.

 

private bool CreateUserRole()
{

  // Get role and user objects
  RoleInfo role = RoleInfoProvider.GetRoleInfo("MyNewRole", CMSContext.CurrentSiteID);
  UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");

 
    if ((role != null) && (user != null))
    {
        // Create new user role object
        UserRoleInfo userRole = new UserRoleInfo();
 
        // Set the properties
        userRole.UserID = user.UserID;
        userRole.RoleID = role.RoleID;
 
        // Save the user role
        UserRoleInfoProvider.SetUserRoleInfo(userRole);
 
        return true;
    }
 
    return false;
}

 

The following example removes a role from a user.

 

private bool DeleteUserRole()
{

  // Get role and user objects
  RoleInfo role = RoleInfoProvider.GetRoleInfo("MyNewRole", CMSContext.CurrentSiteID);
  UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");

 
    if ((role != null) && (user != null))
    {

      // Get the user role
      UserRoleInfo deleteRole = UserRoleInfoProvider.GetUserRoleInfo(user.UserID, role.RoleID);

 
        // Delete the user role
        UserRoleInfoProvider.DeleteUserRoleInfo(deleteRole);
 
        return true;
    }
 
    return false;
}