Forum Discussion

sateesh999's avatar
sateesh999
Contributor
6 years ago

How to restrict a custom component only for user logged in?

Hi Team,

Can any one help regarding - How to restrict (code) a custom component only for user logged in?

 

Thanks,

Sateesh.

6 Replies

  • sateesh999 
    You can use below code

     

    <#if !user.anonymous>
    <#-- component code goes here  -->  
    </#if>

    or

    <#if user.id != -1>
    <#-- component code goes here  -->  
    </#if>

     

  • sateesh999's avatar
    sateesh999
    Contributor
    6 years ago

    Hi Vikas,

    Thanks for your reply!,

    I have created two custom components:

    <component id="common.widget.custom-content" name="13"/>

    <component id="common.widget.custom-content" name="14"/>

    13-Managers and 14- Users. 

    If Managers logged-in then, the both custom components should be visible and If user's logged-in then only one user's component should be visible.

    I want to restrict 13- (custom component) for users? How can I do this?

    I tried to add your code in wrapper--> Page head top content.

    <#if !user.anonymous>
    <component id="common.widget.custom-content" name="13"/>
    </#if>

    The above given code is not working.

    And please provide me a clear code. 

     

    Thanks,

    Sateesh.

     

     

     

  • StanGromer's avatar
    StanGromer
    Boss
    6 years ago

    sateesh999 are your users who are logged in part of a role?  If you have multiple personas, you may want to go off role rather than logged in/logged out (else a combo may be needed).  Example:

     

    <#attempt>
    <#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 == "Customers" || role.name == "Administrator" || role.name == "Banana")>
    <#assign user_has_role = true />
    </#if>
    </#list>
    </#if>
    <#if user_has_role >

    CONTENT IF IN THE ROLE SHOWN HERE

    <#else>

    CONTENT IF NOT IN ROLE SHOWN HERE

    </#if>
    <#recover>
    <!-- Something bad happened -->
    </#attempt>

  • rnewell's avatar
    rnewell
    Mentor
    4 years ago

    Hey StanGromer can i leverage <#elseif> for another role.name and<#if user_has_role > display this content. ? 

     

    In theory, I'd like to Display role-based links to boards for role A. Display the same links, however, if I have a role B, display 1 more link. While displaying the same content, if I have role 3, display 1 more link or a different set of links. 

     

    Any ideas around the code you already have implemented for numerous roles under the same component?

     

  • StanGromer's avatar
    StanGromer
    Boss
    4 years ago

    I don’t know the specific code to make that work but I’m sure there is a way! Alternatively what I have done is just reuse the code/component multiple times over, different roles for each.

    First time = OnlyRole A sees it, leave else blank  

    second time = Only content for Role B, leave else blank 

    third time = only content for role C, leave else blank.

    fourth time = no roles, leave content blank, set else message to what you want all others to see.

    Probably better ways to do it then that but i’m not an engineer, just making stuff up as I go, lol  🥳

  • SuzieH's avatar
    SuzieH
    Khoros Alumni (Retired)
    4 years ago

    Hi sateesh999 

    To call a component from another component, you'll need something like this using the @component FreeMarker directive

    <#if !user.anonymous>
    <@component id="common.widget.custom-content" name="13"/>
    </#if>

    The @component directive also takes parameters, which you could pass to and use in the custom component you're calling.

    Other helpful directives might be @override and @delegate.

    Here are a few dev guides to take a look at:

    Pass parameters to a custom component

    Override core components