Forum Discussion

jcracraft's avatar
7 years ago

Conditional based on rank

I'm trying to have a sub-nav with line items dependent on whether a user has the rank of administrator. I've evaluated a LiQL query that evaluates correctly on the homepage, but when you navigate to any other page, the query does not successfully work. 

 

 
 
<#attempt>
  <#assign userSearch= user.id />
  <#assign rolesQuery= "SELECT rank FROM users WHERE id='${userSearch}'" />
  <#assign userData= rest("2.0","/search?q=" + rolesQuery?url) />
  <#assign role = userData.data.items[0].rank.simple_criteria.role.id />
  <#if role == 't:Administrator'>
    <#assign navItems = [blog, groups, training, events, faq] />
  </#if>
  <#recover>
    <#assign navItems = [groups, training, events, faq] />
</#attempt>
When I log in, the blog appears in the sub-nav, and then when I go to any page, the subnav omit's the blog item. I've created variables just to see if the query is even working and get "success" on the home page and nothing on any other page. 
 
I am new to LiQL and Freemarker so I don't really understand how a query can work on some pages but not others. Any help would be much appreciated
  • I found my own solution. Apparently it matters where you put the <#attempt> The LiQL query needs to be before<#attempt>. This was my solution in case anybody else has the same issue.

    <#--  LiQL query for active user  -->
      <#assign adminRole = 't:Administrator' />
      <#assign userSearch= user.id />
      <#assign queryRole= "SELECT id FROM roles WHERE users.id='${userSearch}'" />
      <#assign queryData= rest("2.0","/search?q=" + queryRole?url) />
      <#assign queryResult = queryData.data.items[0].id />
    
      <#attempt>
        <#if queryResult == adminRole >
          <#-- Adding blog to secondary nav if user is administrator   -->
          <#assign navItems = [blog, groups, training, events, faq] />
        </#if>
        <#recover>
          <#assign navItems = [groups, training, events, faq] />
      </#attempt>

3 Replies

  • jcracraft,

     

    You can directly fetch user role using this query and can add to your condition based on roles.

    SELECT id FROM roles WHERE users.id = '1'
  • jcracraft's avatar
    jcracraft
    Guide
    7 years ago

    Thank you for improving my syntax Parshant, my LiQL query now works on all the pages.

    Unfortunately, my if statement seems to be a problem. It's still evaluating false anywhere but the Homepage. I even changed the condition to something that would evaluate to true and it's still not working on any other page: 

    <#if 1 == 1>
        <#assign navItems = [blog, groups, training, events, faq] />
      </#if>
      <#recover>
        <#assign navItems = [groups, training, events, faq] />
    </#attempt>

    Even with this i'm not getting the blog on any page but the homepage.

  • jcracraft's avatar
    jcracraft
    Guide
    7 years ago

    I found my own solution. Apparently it matters where you put the <#attempt> The LiQL query needs to be before<#attempt>. This was my solution in case anybody else has the same issue.

    <#--  LiQL query for active user  -->
      <#assign adminRole = 't:Administrator' />
      <#assign userSearch= user.id />
      <#assign queryRole= "SELECT id FROM roles WHERE users.id='${userSearch}'" />
      <#assign queryData= rest("2.0","/search?q=" + queryRole?url) />
      <#assign queryResult = queryData.data.items[0].id />
    
      <#attempt>
        <#if queryResult == adminRole >
          <#-- Adding blog to secondary nav if user is administrator   -->
          <#assign navItems = [blog, groups, training, events, faq] />
        </#if>
        <#recover>
          <#assign navItems = [groups, training, events, faq] />
      </#attempt>