Filter GroupList Based on Permissions of a Role


In a past project i was asked to build a Grouplist like widget for Community Server. This Grouplist widget was different from the existing because the data should be filtered by the permissions of a role (in my case Members). i have searched for a standard way to do this with the existing controls but eventually i had to write my own  <CSControl:CustomCondition />. i implemented the widget as follows (staying closely to the existing widget implementation in CS 5.5).

<%@ Control Language=”C#” AutoEventWireup=”true” %>

<%@ Import Namespace=”System.Collections.Generic” %>
<%@ Import Namespace=”System.Collections.ObjectModel” %>

<script runat=”server” language=”C#”>

    public bool IsApproved(CommunityServer.Components.Group group)
    {
     CommunityServer.Components.ISecurityService securityService = Telligent.Common.Services.Get<CommunityServer.Components.ISecurityService>();
     CommunityServer.Components.IRoleService roleService = Telligent.Common.Services.Get<CommunityServer.Components.IRoleService>();

        IEnumerable<CommunityServer.Components.Role> roles = roleService.GetRolesForGroup(group.ID);
 
     CommunityServer.Components.Role member = null;
     foreach(CommunityServer.Components.Role role in roles)
     {
      if(role.Name == “Members”)
      {
       member = role;
      }
     }
     if(member != null)
     {
      CommunityServer.Components.PermissionList p = securityService.GetImmediatePermissions(group.NodeId);
     
      foreach(CommunityServer.Components.PermissionEntry m in p)
      {
       if(m.Name == “Community – Create New Communities” && m.RoleId == member.Id && m.IsAllowed)
       {
        return true;
       }
      }
     }
 
        return false;
    }
</script>

<CSControl:GroupData runat=”server” CssClass=”content-item simple” Tag=”Li”>
 <DisplayConditions Operator=”And”>
  <CSControl:CustomCondition 
     CustomResult=”<%# IsApproved(CSControlUtility.Instance().GetCurrentGroup(Container)) %>” 
     runat=”server” />
  </DisplayConditions>   
    <ContentTemplate>
        <CSControl:GroupData runat=”server” 
           Property=”Name”
           TruncateAt=”25″
           LinkTo=”HomePage” />
    </ContentTemplate>
</CSControl:GroupData>

 

Leave a comment