Forum Discussion

diptikaushikkar's avatar
5 years ago

how to customize custom component based on role ?

What is the best way to customize a custom component to be visible to only some users with some specific roles ??

  • diptikaushikkar 

    You can use below code in your custom component with this function to re-use in areas.

    <#function user_has_role (id, roles)>
      <#assign liql = "SELECT id FROM users WHERE roles.name IN (${roles}) and id = '${id}'" />
      <#assign query = rest("2.0","/search?q=" + liql?url) />
      <#if (query.status == "success") && query.data.size gt 0>
      	<#return true>
      <#else>
        <#return false>
      </#if>
    </#function>

    and call it like this:

    <#if user.registered && user_has_role(user.id,"'Administrator', 'Developer'")>
    ...
    </#if>

     

    • christiewebb's avatar
      christiewebb
      Guide

      Parshant  This works great for me!  But I would like to also PREVENT a link from showing up for a certain role.  Could you help me with that?  I'm showing Software Notifications to anyone with role "CARRIER_CUSTOMER" - how can I hide Discussions from that same role?

       

      <#function user_has_role (id, roles)>
      <#assign liql = "SELECT id FROM users WHERE roles.name IN (${roles}) and id = '${id}'" />
      <#assign query = rest("2.0","/search?q=" + liql?url) />
      <#if (query.status == "success") && query.data.size gt 0>
      <#return true>
      <#else>
      <#return false>
      </#if>
      </#function>

      <a href="/t5/Discussion/ct-p/discussion">Discussions</a><br>
      <a href="/t5/Product-Notices/ct-p/product-notices-doc">Product Notices</a><br>
      <#if user.registered && user_has_role(user.id,"'Administrator'")>
      <a href="/t5/Software-Notifications/ct-p/software-notifications">Software Notifications</a><br>
      </#if>

      • Parshant's avatar
        Parshant
        Boss

        christiewebb,

        Your code should be,

        <#function user_has_role (id, roles)>
        <#assign liql = "SELECT id FROM users WHERE roles.name IN (${roles}) and id = '${id}'" />
        <#assign query = rest("2.0","/search?q=" + liql?url) />
        <#if (query.status == "success") && query.data.size gt 0>
        <#return true>
        <#else>
        <#return false>
        </#if>
        </#function>
        
        <#if user.registered && user_has_role(user.id,"'CARRIER_CUSTOMER'")>
        <#else>
        <a href="/t5/Discussion/ct-p/discussion">Discussions</a><br>
        </#if>
        <a href="/t5/Product-Notices/ct-p/product-notices-doc">Product Notices</a><br>
        <#if user.registered && user_has_role(user.id,"'CARRIER_CUSTOMER'")>
        <a href="/t5/Software-Notifications/ct-p/software-notifications">Software Notifications</a><br>
        </#if>

         

    • Is there any way where i can avoid making this rest call to fetch roles?? Some predefined function which returns me roles?? 

      • Parshant's avatar
        Parshant
        Boss

        No, there is no other way then this for custom component.

        If you are using in custom page for a category, then you can set the role permission to the whole page.

  • Something like this should work:

    <#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
    <#if role.name?? && (role.name == "Administrator")>