Forum Discussion

rizalvi's avatar
rizalvi
Mentor
12 years ago

How to display custom authentication error messages for specific categories

A few categories of our community site are limited only to users with specific roles. We would like for users without those roles to be presented with an error message explaining who they should contact to obtain the necessary permissions if they try to access these categories. Our current implementation involves using Freemarker code to replace the main body of each page with an error message when a user des not have the required role, but this involves a lot of code duplication and does not work for some pages, such as forums. Is there a better, built-in way to display custom authentication error messages?

1 Reply

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)
    12 years ago

    Hi Riz,

     

    There is nothing OOTB to set role to pages or components as I aware, but you may want to try something like this,

     

    • create a macro via Studio->Endpoint->New Macro
    <#function user_has_role role_name >
        <#if user.registered>
            <#list restadmin("/authentication/sessions/current/user/roles").roles.role as my_role>
                <#if my_role.name?trim == role_name >
                    <#return true />
                </#if>
            </#list>
        </#if>
        <#return false />
    </#function>
    
    <#macro render_no_role >
     You should contact XYZ by XYZ@ABC
    </#macro>
    •  in your components, you can access them as long as you include the macro
    <#include "your-macro-name.ftl" />

     

    so, you may have some code like

     

    <#assign expected_role = "company_vip" />
    <#if user_has_role(expected_role) >
           ....acting normal....
    <#else>
    	<@render_no_role />
    </#if>