Forum Discussion

Quelyn's avatar
Quelyn
Genius
10 years ago

Question on "user_has_role"

Quick question.  When using the following: 

 

<#if user_has_role == "customersupport" >

Is there a away to list more than one role at a time here?  If so, what's the syntax? 

 

Thanks!

  • rwm's avatar
    rwm
    10 years ago

    You can do the following to combine and/or logic:

     

     <#if user_has_roll?? && (user_has_role == "registered") || (user_has_role == "customer") >

     

     

  • Hi Quelyn

     

    you have an error in the expression. The first two conditions are put in AND (&&) and there is a typo in the first one  (user_has_roll), which means the first one will always fail.

     

    You actually need to write it like this for it to work (note the corrected typo and the wrapping parentheses) because the && (AND) always takes precedence onto || (OR) 

     

    <#if user_has_role?? && (user_has_role == "admin" || user_has_role == "partner" || user_has_role == "employee" || user_has_role =="customer") >

    Which means "if the variable user_has_role exists AND has one of the values in the brackets ..."

     

    Try this and let us know,

     

     

     

  • Quelyn - Try this:

     

    <#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?? && (role.name == "Moderator")>    
    <#assign user_has_role = true />
    </#if>
    </#list>
    </#if>
    <#if user_has_role >
    Content will go here
    </#if>

    One such solution is here for ranks as well, 

     

    https://community.lithium.com/t5/Developers-Discussion/Conditional-based-on-rank/m-p/164891#M6547

     

    I hope this helps.

    • Quelyn's avatar
      Quelyn
      Genius
      That's a great example, and thanks for that. I've seen stuff like this but could not really figure out how to get it to pertain to the menu I am making. IDEALLY I would be able to just style the 'Community Browser" to make it look like a horizontal menu and take advantage of its dynamic nature yet, but I haven't quite figured it out.
  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)

    Hi Quelyn

     

    I am not aware of that variable being generally available. I think the answer to your question depends on the full code that has populated the variable itself.  Usually a user can have a list of more roles so I am not sure comparing it exactly to a single value would always work. Cannot really tell without context.

     

    Thanks,

     

    • Quelyn's avatar
      Quelyn
      Genius

      Ah ok, sorry I thought that folks who knew more than me would get where I was going, lol.  So I am building a custom navigation menu where we show users only the boards they have access to see.  Originally Lithium built this out for us, and we have the following to define the roles:

       

      <!-- Roles for navigation menu-->
      <#assign user_has_role = "none" /> 
         <#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
            <#if role.name?? && ((role.name == "Customer"))>
               <#assign user_has_role = "customer" />
               <#break />
             <#elseif role.name?? && ((role.name == "Registered User"))>
              <#assign user_has_role = "registered" /> 
            </#if>
         </#list>
      <!-- Roles for navigation menu ends -->

      So then, when I want to make the menu section for the user, it looks something like this:

      <#if user_has_role == "registered" >
              <li><a class="chevron-before" href="t5/Kb-name/tkb-p/kb_name1">KB Example 1</a></li>
          <#elseif user_has_role == "customer" >
              <li><a class="chevron-before" href="t5/Kb-name/tkb-p/kb_name1">KB Example 1</a></li>
              <li><a class="chevron-before" href="t5/Kb-name2/tkb-p/kb_name2">KB Example 2</a></li>
      </#if>

      Now, I am aware I can combine the roles in the definitions to make one since they always see the same thing.  But sometimes, I have people that see the same thing, but only in that instance. 

       

      So, I was looking for something like:

      <#if user_has_role == "registered || customer" >

       That it would show both.  But the formatting on how to combine options there wasn't entirely intuitive to me, and I checked all the documentation I could but still didn't find anything :(

       

      <#if user_has_role == "registered" >  <-- Would like if I could say "If you are registered OR customer" here you can see this option.
              <li><a class="chevron-before" href="t5/Kb-name/tkb-p/kb_name1">KB Example 1</a></li>
          <#elseif user_has_role == "customer" >
              <li><a class="chevron-before" href="t5/Kb-name/tkb-p/kb_name1">KB Example 1</a></li>
              <li><a class="chevron-before" href="t5/Kb-name2/tkb-p/kb_name2">KB Example 2</a></li>
      </#if>

       

      HOpe that makes sense, and thanks in advance.

      • rwm's avatar
        rwm
        Advisor

        You can do the following to combine and/or logic:

         

         <#if user_has_roll?? && (user_has_role == "registered") || (user_has_role == "customer") >

         

         

  • KaelaC's avatar
    KaelaC
    Lithium Alumni (Retired)
    Like Paolo says, we would need to see how user_has_role is defined. It should look something like:

    <#assign user_has_role = .....