Forum Discussion

simantel's avatar
9 years ago

Get Each Author's Role With Rest API

I'm attempted to create a custom component that will display each author's role beneath the stock Author component on the message page. Ideally, I'd add the information to the stock Author component, but stock components appear to be untouchable. I feel like I'm close. I've got

 

<#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>		
	${role.name}
</#list>

But the bold portion is looking at me, myself, and I as the user instead of looking at the message author. It's not as simple as ${author.id?c}. I can't tell if I'm really close to a solution or way off course. Any guidance would be very much appreciated.

  • ChiaraS's avatar
    ChiaraS
    9 years ago

    Hi,

     

    ok in that case I think it would be ok, I would probably add a check on the name and only show the role if it's equal "Employee" or "Dealer", and not for other roles like "Administrator" or "Moderator".

     

    Back to your question, you could try the following approach:

     

    <#if env.context?? && env.context.message?? && env.context.message.author?? >
        <#assign author_id = env.context.message.author.id />
       
        <#attempt>
        <#list restadmin("/users/id/${author_id?c}/roles").roles.role as role>		
            <#if role.name == "Employee">
                ${role.name}
            </#if>
        </#list>
        <#recover>
        </#attempt>
    </#if>
  • ChiaraS's avatar
    ChiaraS
    Lithium Alumni (Retired)

    Hi,

     

    what would be the use case for showing user's roles on the page? Roles are something that are normally "hidden" and not exposed to final users (as opposed to ranks)...

     

     

    • simantel's avatar
      simantel
      Guide

      Good question, ChiaraS. We've got two roles, Employee and Dealer, that are assigned on a user-by-user basis. We'd like to be able to show when a user is both a New Member, Visitor, Contributor, etc. and also a dealer or employee, so that's why we put the standard titles as ranks and these two unique identifiers as roles, since you can only have one rank.

      • ChiaraS's avatar
        ChiaraS
        Lithium Alumni (Retired)

        Hi,

         

        ok in that case I think it would be ok, I would probably add a check on the name and only show the role if it's equal "Employee" or "Dealer", and not for other roles like "Administrator" or "Moderator".

         

        Back to your question, you could try the following approach:

         

        <#if env.context?? && env.context.message?? && env.context.message.author?? >
            <#assign author_id = env.context.message.author.id />
           
            <#attempt>
            <#list restadmin("/users/id/${author_id?c}/roles").roles.role as role>		
                <#if role.name == "Employee">
                    ${role.name}
                </#if>
            </#list>
            <#recover>
            </#attempt>
        </#if>