Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Associating permission sets for specific roles within a forum

New

 Using the InstantForum .NET API you can programmatically associate permission sets with roles within any forum providing granular control over the features available to end users within individual forums. This articles aims to demonstrate how to achieve this through our server side .NET API however you can of course configure everything discussed here via the InstantForum Admin CP. For further information on how to disable new topics manually via the Admin CP please see "How to disable new topics via Permission Sets".

InstantForum allows you to associate a permission set globally to a group of users or an individual user or override these setting within a specific forum for each role that can access the forum. This is better demonstrated within our "How InstantForum Permission Sets Work" article.

In our code example below we'll take advantage of the fact you can associate a permission set to a role within any InstantForum forum. This allows us to easily override any global settings and apply different permission sets to each role that can access any forum.

Associating a permission set to a specific role within a specific forum

For this example let's say we have a forum called "News & Announcements" where we wanted to allow one group of users to post new topics (say User Group 1) and all other users to post only replies to existing topics.

To achieve this we'll first create a new permission set via the InstantForum Admin CP for the purpose of this example called "Allow Only Replies". This permission set will have the "Can Post New Topics" option disabled. With the "Can Post New Topics" option disabled for our "Allow Only Replies" permission set whenever this permission set is associated to a user group or individual user the group or user won't have the ability to post new topics.

Then via code the following would associate the "Allow New Topics" permission set would be associated with User Group 1 within the News & Announcements forum. All other roles that have access to the News & Announcements forum would have the "Allow Only Replies" permission set associated. You can see this below...

// create forums object - optionally pass in current users object to apply role base security
var forumsRepository = new InstantASP.InstantForum.Business.Forums();
// select all forums
var forums = forumsRepository.SelectForums();
// ensure we have forums
if (forums != null)
{
// get forum called "News & Announcements" using linq
var forum = forums.SingleOrDefault(f => f.Name == "News & Announcements");
// ensure we found the forum
if (forum != null)
{
 
// add all forum roles back with correct permission ids
var roles = InstantASP.Common.Business.Roles.SelectRoles();
// ensure we have roles
if (roles != null)
{

// select all permission sets
var permissionSets = InstantASP.InstantForum.Business.PermissionSets.SelectPermissionSets();
// get restricted access permission set
InstantASP.InstantForum.Components.PermissionSet allowTopics = null;
InstantASP.InstantForum.Components.PermissionSet allowReplies = null;
if (permissionSets != null)
{
allowTopics = permissionSets.SingleOrDefault(p => p.PermissionName == "Allow New Topics");
allowReplies = permissionSets.SingleOrDefault(p => p.PermissionName == "Allow Only Replies");
}

// delete existing role relationship for "News & Announcements" forum
InstantASP.InstantForum.Business.ForumRoles.DeleteRolesForForum(forum.ForumID);

foreach (var role in roles)
{

// associate our allowTopics permission set with User Group 1 within our "News & Announcements" forum
// all other roles will have our "Allow Only Replies permission set applied
// For further information on how permission sets are aqpplied please see...
// http://support.instantasp.co.uk/Knowledgebase/13857/How-InstantForum-Permission-Sets-Work
var permissionSetId = role.RoleName == "User Group 1"
? allowTopics?.PermissionID ?? 0
: allowReplies?.PermissionID ?? 0;
  
// add forum & role relationship with specific permission ID
InstantASP.InstantForum.Business.ForumRoles.AddRelationship(
forum.ForumID,
role.RoleID,
permissionSetId);
}

}

}

}

The InstantForum_ForumsRoles Table

To control which roles can view a forum and which permission set is applied for each role this information is stored within the InstantForum_ForumsRoles database table. This table has 3 columns ForumID, RoleID & PermissionID. These correspond to the identities from InstantForum_Forums, InstantASP_Roles & InstantForum_PermissionSets.