Forum Discussion

SophiaCiocca1's avatar
SophiaCiocca1
Contributor
8 years ago

Role management in custom components

Hi Can anyone help me with role management in custom components. I would like to know the code which i need to add in the component  to make a component not viewable for certain users ?

 

Thanks in Advance

 

Thanks,

Sophia

 

  • Let me just simplify things for you 

     

    <#if coreNode.permissions.hasPermission("update_community")>

      <!-- Content goes here -->

    </#if>

    Hope this helps to you ... :)

     

    NOTE :- That ties the logic to permissions as opposed to roles

5 Replies

  • SophiaCiocca1- Try below code.

     

    <#assign isAdmin = false />
    <#list restadmin("/users/id/${user.id?c}/roles").roles.role as maintrole>
    	<#if maintrole.name?? && ((maintrole.name == "Administrator") || (maintrole.name == "Moderator"))>
    		<#assign isAdmin = true />
    		<#break>
    	</#if>
    </#list>
    
    <#if isAdmin>
    <!-- your component code -->
    </#if>

     

  • Hi SophiaCiocca1,

     

    You can try this

    <#assign user_has_role = false />
    <#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
    	<#if (role.name == "required role name") >
    		<#assign user_has_role = true />
        </#if>
    </#list>
    <#if user_has_role>
    /* write your code here */
    </#if>

    Give kudos if you find my posts helpful or mark as solution if it answers your query.

    Thanks,
    Srujana Satya

  • ClaudiusH's avatar
    ClaudiusH
    Khoros Alumni (Retired)
    8 years ago

    Since this role checking code is likely to be re-used in multiple different components it's a great candidate to put it into a FreeMarker macro. Then you can simply include the macro and use that function in whichever component needs a role check.

  • Let me just simplify things for you 

     

    <#if coreNode.permissions.hasPermission("update_community")>

      <!-- Content goes here -->

    </#if>

    Hope this helps to you ... :)

     

    NOTE :- That ties the logic to permissions as opposed to roles

  • ClaudiusH's avatar
    ClaudiusH
    Khoros Alumni (Retired)
    8 years ago
    Checking permissions might be sufficient in certain scenarios, e.g. if you want to show/hide a certain action button that is tied to that permission.

    We generally recommend as best practice to tie permissions to roles and assign roles to users though. Therefor checking for roles is the safer way since a member could have obtained a permission through various ways (wrong defaults, a role membership or even individual role adjustments [Which you should only use as a last resort]).