Add/Remove users from roles via code

rated by 0 users
This post has 2 Replies | 2 Followers

mcneilp
Not Ranked
Posts 23
Points 325
By: mcneilp
Posted: Thu, Oct 12 2006 10:06 AM

I need to be able to add and remove users from a role in code. I found how to detect if a user is in a role using this code:

 if (CSContext.Current.User.RoleKey.IndexOf("Newsletter") != -1)
{
    // member
}
else
{
    // non member
}

 

I need to allow users to opt in or out of the role so as to populate the Newsletter group/role.

I have been looking for quite some time and can't find any samples of this code.

 

-Patrick 

 

 
TechLINKS
Top 500 Contributor
Posts 46
Points 720
By: TechLINKS
Posted: Thu, Jul 3 2008 7:35 PM In reply to

This is what I used, for when a user selects an access level... You could do something along similar lines. I manually created the roles through the Control Panel.

CSContext csContext = CSControlUtility.Instance().GetCurrentCSContext(this.Page);

CommunityServer.Components.Roles.AddUserToRole(csContext.User.Username, this.DirectoryLevelsRadioButtonList.SelectedValue);

 

 
  • Post Points: 5 |
Alex Crome
Top 10 Contributor
Posts 2,780
Points 41,105
TelligentSupportTeam
By: Alex Crome
Posted: Fri, Jul 4 2008 9:16 AM In reply to

In addition to what TechLinks said above, To check if a user is in a role

User user; // populate this user

if (user.IsInRole(new string[]{"YOUR ROLE"}))
{
  //Is in Role
}
else
{
  //Is not in role
}

 
  • Post Points: 5 |
Page 1 of 1 (3 items) | RSS
Powered by Community Server (Commercial Edition), by Telligent Systems

Copyright© 2008 Telligent Systems Inc. All rights reserved
CommunityServer.com  •  Telligent.com