Forum Discussion

DougS's avatar
DougS
Khoros Oracle
14 years ago

Display different custom content modules to users of different ranks (Freemarker)

Big thanks to Kaela for this information!

 

Description

This component displays different "custom content" modules based on what rank a user has. If they have the "Lithium Guru" rank, they see custom content module 3. If they have the "Wayfarer" rank they see custom content module 2. If they are logged in but have neither rank, they see custom content module 1. If they are not logged in, they do not see a custom content module in this component.

 

Requirements

Lithium version 9.18 or higher, Lithium Studio

 

How to add this to a Lithium community

There are two parts to displaying custom content based on user ranks:

  • Create a component that contains the display logic
  • Add the component to pages

To create the logic component:

    1. Go to Studio > Components.
    2. Click New Component. Enter a name for the component (for example, custom-content-by-rank) and click Create.
    3. Add the following markup to the component:

 

<#if user.registered >
    <#assign rank = restadmin("/users/id/${user.id?c}/ranking").ranking />
    <#if rank.name?? && (rank.name == "Lithium Guru")>
        <@component id="common.widget.custom-content" name="3" panel="true" />
    <#elseif rank.name?? && (rank.name == "Wayfarer")>
        <@component id="common.widget.custom-content" name="2" panel="true" />
    <#else>
        <@component id="common.widget.custom-content" name="1" panel="true" />
    </#if>
</#if>
  1. Click Save.

To place the element on a page:

  1. Choose Community Admin > Content > Custom Content
  2. Select each of the first three custom content modules and add the content you want to display to each rank.

 

Example

Go to the Code Sample 4 Example Page to see this in action.

  • I tried to adapt this example to display different custom content based on different roles but I can't get hold of the current user's roles.

     

    And as a bonus question: When I try to use this freemarker code in Custom Content it shows a "This widget could not be displayed." message instead. Is Freemarker code not allowed within Custom Content?

    • DougS's avatar
      DougS
      Khoros Oracle

      Hi Claudius,

       

      Regarding your quesiton about freemarker in custom components: freemarker in custom content should be ok.  If it wasn't rendering freemarker then you would probably see some of the freemarker code showing up as plan text or as invalid markup.  If you are getting a "this widget cannot be displayed message" it is most likely because there is an exception when some of the freemarker runs.  If you are logged in as someone who has full admin access, you should see the freemarker stack trace instead of the "widget cannot be displayed" message and this will help you debug the issue.

    • AdamN's avatar
      AdamN
      Khoros Oracle

      Hi Claudius,

       

      A couple of notes...

       

      First, I'd suggest double checking to ensure that you're using the correct REST API method. To get the user's roles, the call should look like:

      /users/id/${user.id?c}/roles

       

      Second, a user can have only one rank at a time, whereas they can have multiple roles. So the approach for determining whether a user has a particular role is a bit different than just seeing what their rank is. Here's an example I posted previously of showing specific content to only Administrators or Moderators:

       

       

      <#assign user_has_role = false />
      <#if user.registered >
      	<#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
      		<#if role.name?? && (role.name == "Administrator" || role.name == "Moderator")>
      			<#assign user_has_role = true />
      		</#if>
      	</#list>
      </#if>
      
      <#if user_has_role >
      
      Show to users with role
      
      </#if>

       

       

      It may be easier to adapt the example above. I hope this helps!

  • DougS: is there any possibility to evaluate the user within the ranking hierarchy? E.g. if a user has reached a certain (minimal) rank and from there on he/she can see the content that is only meant for users with that rank and higher?
    • that-pat's avatar
      that-pat
      Contributor

      DougS, I too am interested in figuring out if you can display content based on where someone falls within the ranking hierarchy.

      luk, did you ever get an answer on this? 

      • DougS's avatar
        DougS
        Khoros Oracle

        Hi that-pat,

        We do not return any information in the API that tells you the rank hierarchy.

        What we usually recommend for this type of customization is to configure the ranks where you want someone to have a new level of access so they assign a role when you achieve that rank. Then you can key off of that role in your customizations to grant different users access to different stuff.

        I hope that helps!

        -Doug