Forum Discussion

PerBonomi's avatar
7 years ago

Feels like a noob question, but..

Why does this fire for an anonymous user?

<#if user?? && user.id?? && user.login != "">
  • PerBonomi - 

     

    1. user?? - it is checking for the object is available or not. For the anonymous user, an object is available. 

    2.  user.id?? - it is checking for the user.id key available or not. Even for the anonymous user, this key is available. 

    3. user.login != "" - It is checking if user.login is not blank, however, for anonymous user user.login is being returned as anonymous.

    Solution: 

    if you want to check for an anonymous user you can use below code. 

     

    <#if user.anonymous>
    </#if>
    
    OR
    <#if !user.registered> </#if> OR <#if user.id != -1> </#if>
  • PerBonomi - 

     

    1. user?? - it is checking for the object is available or not. For the anonymous user, an object is available. 

    2.  user.id?? - it is checking for the user.id key available or not. Even for the anonymous user, this key is available. 

    3. user.login != "" - It is checking if user.login is not blank, however, for anonymous user user.login is being returned as anonymous.

    Solution: 

    if you want to check for an anonymous user you can use below code. 

     

    <#if user.anonymous>
    </#if>
    
    OR
    <#if !user.registered> </#if> OR <#if user.id != -1> </#if>
    • PerBonomi's avatar
      PerBonomi
      Boss

      OK, thanks. I just always assumed if there is technically no user there can't be a user.id, but it looks like it'll revert to -1 for the system to keep functioning.