jcracraft
7 years agoGuide
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>