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>

6 Replies

  • ChiaraS's avatar
    ChiaraS
    Lithium Alumni (Retired)
    9 years ago

    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
    9 years ago

    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)
    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>
  • simantel's avatar
    simantel
    Guide
    9 years ago

    You. You are awesome. I bust my budget trying to get this to work and I wasn't even close to getting the first two lines, which seem to be key to getting the correct context. I removed the return ${author_id} and added a second <#if> for the Dealer and wrapped the returns in stylized spans and got just what I was looking for.

     

    Thank you. THANK YOU.

  • ChiaraS's avatar
    ChiaraS
    Lithium Alumni (Retired)
    9 years ago

    You're welcome! And yes, I left a debugging line that was outputting the author_id, I've adjusted the code ;)

  • simantel's avatar
    simantel
    Guide
    9 years ago

    For future reference for anyone interested, I also wanted to display the role icons on each user's profile page. Thanks to these helpful forums, I was able to find the new selectors I needed to make it work. Replace the first four line with this:

     

    <#if page.context.user?? && page.context.user.id?? >
        <#assign profile_id = page.context.user.id />
        <#attempt>
            <#list restadmin("/users/id/${profile_id?c}/roles").roles.role as role>

     

    Cheers, all.