CS Dev Guide: User Roles

One of other aspects of dealing with users in each enterprise system is the ability to work with roles.  It was important enough to have this functionality in ASP.NET 2.0 as built-in feature.

To work with roles in Community Server you need to use CommunityServer.Components.Role object and CommunityServer.Components.Roles namespace methods so these are all references I need for my samples in this post:

using System;

using CommunityServer.Components;

First thing is to create a new instance of a Role object.  It can be done with two constructors: first one needs nothing and second one can get a Guid and a role name as constructor to assign them to this new role.

// Create a new Role

Role objRole = new Role();

Role objRole2 = new Role(Guid.NewGuid(), "Fans");

You can set some properties for a Role object:

// Set some properties for Role

objRole.Name = "Fans";

objRole.Description = "A role for fans";

objRole.RoleID = Guid.NewGuid();

Simply you can add, remove or update a Role as well.  Just note that Roles.AddRole() method gets the string value of Role's name you want to add to system as parameter.

// Add a new Role to system

Roles.AddRole("Fans");

// Delete a Role from system

Roles.DeleteRole(objRole);

// Update a Role

Roles.UpdateRole(objRole);

Also you can add a user to a Role, remove a user from a specific Role and get the list of all users in a Role as a UserSet object.  Note that here Username is what you use to identify a user in methods.

// Add a user to a Role

Roles.AddUserToRole(CSContext.Current.UserName, "Fans");

// Remove a user from a Role

Roles.RemoveUserFromRole(CSContext.Current.UserName , "Fans");

// Get all users in a Role

UserSet objUsers = Roles.UsersInRole(10, 15, SortUsersBy.Username,

    SortOrder.Descending, objRole.RoleID);

The ability to get the list of Role names or an ArrayList of Role objects that a user has been added to them is provided in Community Server platform.

// Get list of Role names for a specific user

string[] UserRoles = Roles.GetUserRoleNames(CSContext.Current.UserName);

// Get all Roles for a specific user

Roles.GetRoles(CSContext.Current.UserID, false);

And finally it's possible to get an array of string names of all site's Roles as well as getting an instance of Role object from a Role name.

// Get the list of Role names of site

string[] SiteRoles = Roles.GetRoleNames();

// Get a Role object for a Role name

objRole = Roles.GetRole("Fans");

+ I need vacation!!

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

6 Comments : 07.24.06

Feedbacks

 avatar
#1
Anders Jensen
10.04.2006 @ 5:16 AM
Hej Keyvan I think its some good bolgs your got about CS here... they are very handy.. :) Just have one comment: // Get all Roles for a specific user Roles.GetRoles(CSContext.Current.UserID, false); CSContext.Current.UserID, returns -1 for me, until I found out that i to do it like this : CSContext.Current.User.UserId.
admin avatar
#2
Keyvan Nayyeri
10.04.2006 @ 8:44 AM
Hi Anders, Actually I faced with same issues in other APIs. I think this is by Community Server APIs because they don't do what they should do in some parts.
 avatar
#3
Patrick
10.12.2006 @ 9:08 AM
You solved my problem exactly. Thanks so much for taking the time to write it out. I am making a system to allow users to add themselves to a single group...this enables a newsletter registration sort of thing. thanks again!
 avatar
#4
courtney
12.10.2007 @ 1:28 PM

hi I'm working with roles and singles sign on authentication system. When authenticated users hit the site they profile is created, and there roles set depending on data pass from the authentication module. Creating standard users isn't a problem

but giving a dynamically created user admin

privileges sends a security response...is there a way to turn of this security measure?

 avatar
#5
Minhaj
07.24.2008 @ 2:02 PM

Hi Keyvan,

I created a custom role and assign that role to a blog. It shows up in cs_sectionPermissions table. But when I remove that role from that blog in control panels, it still shows in cs_sectionPermissions table.

I am using the above mentioned table in a webservice to expose blogs and roles associated to it. Do you know whey the communityserver does not remove roles from cs_sectionPermissions table. Should I be using some other table?

thanks..

Minhaj

 avatar
#6
Minhaj
07.24.2008 @ 2:20 PM

I found my answer in "AllowMask" field of cs_sectionPermissions table. When a role is assigned to a blog, it inserts a row in this table. When admins remove this role from a blog, it just changes the "AllowMask" from "0x0000000000000000" to "0x0000000000000001". What a funny way of doing things.

thanks..

Minhaj

Leave a Comment