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
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?
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
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

#1
Anders Jensen
10.04.2006 @ 5:16 AM