API
Version 7.x > API > Remove user from all roles. View modes: 
User avatar
Member
Member
idorko - 12/20/2012 12:05:00 PM
   
Remove user from all roles.
I'm working on a custom authentication handler in which I need to remove a user from all roles in Kentico, then re-add them to roles based on their roles in our external database.

Adding new roles from the database and adding a user to roles is no problem, but I'm having an issue removing the user from the roles they are currently assigned to.
//remove all user roles
foreach (DictionaryEntry roleName in user.SitesRoles["intranet"])
{
RoleInfo role = RoleInfoProvider.GetRoleInfo(roleName.Key.ToString(), CMSContext.CurrentSiteID);
UserInfoProvider.RemoveUserFromRole(user.UserID, role.RoleID);
}

It seems like this code should work, however the RemoveUserFromRole doesn't seem to actually remove the user from the role...any thoughts?

Thanks!

User avatar
Member
Member
idorko - 12/21/2012 9:53:28 AM
   
RE:Remove user from all roles.
I managed to fix this issue by adding
AuthenticationHelper.ImportExternalRoles = false;

to CMSSessionStart in CMSAppBase.
The roles were in fact being removed, but Kentico was re-adding them after my authentication handler returned.

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 12/22/2012 7:12:48 AM
   
RE:Remove user from all roles.
Hi,

Please take a look at our Developers Guide - API Examples - Roles and users.

Maybe you can try also to Set Role info, e.g. RoleInfoProvider.SetRoleInfo(role); after making a changes.

I hope this will help you.

Best regards,
Martin Danko

User avatar
Member
Member
brian.severa-navitus - 2/19/2014 2:08:44 PM
   
RE:Remove user from all roles.
Is there a timing issue with changing the roles (adding or deleting) using the api code referenced in this post? I'm using the code you referenced in the API and it seems as though the change does not take hold for several minutes after I've made the change. Just wondering if I'm missing a detail.

Thanks,

Brian

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 2/21/2014 9:31:03 PM
   
RE:Remove user from all roles.
Hello Brian,

I'm not sure what exact code are you using but try to take a look at the Developers Guide for the right methods to update the data: Roles and users

Regards,
Martin