Forum Discussion

rnewell's avatar
rnewell
Mentor
4 years ago

Create Custom Component Based On Roles

Hey Dev Discussion, right now in my community we're trying to leverage a component to show permissioned based boards depending on your role in the community. 

 

For example, we have 3 Roles: A, B, and C

Role A has permission to the component and 2 links.

Role B has permission to the component and 3 links are displayed but has Role A also.

Role C has permission to the component and 4 links are displayed but have Role A and C. 

 

I believe I saw this code elsewhere and was trying to make edits to make this possible without creating separate components. When I did create separate components, for example, Role A could see their component.

 

When signed into Role B could see Role A's links. And Role Bs could see their links in another component and Role A's component was still there. 

I hope this makes sense thus far! Haha! 

 

Here is the script I was going to leverage and attempt however, didn't seem to work when I tried to throw in <#elseifs>. 

 

<#attempt>
<#assign user_has_role = false />
<#if user.registered >
<#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
<#if role.name?? && (role.name == "Employee" || role.name == "Administrator" || role.name == "Ambassador")>
<#assign user_has_role = true />
</#if>
</#list>
</#if>
<#if user_has_role >

Role A

<#elseif>

Role B

<#/elseif>

<#elseif>

Role C

<#/elseif>

<#/if>

</#attempt>

 

Open to other suggestions around this! Thank you!

 

 

  • Hi,

    maybe you can use something like this:

     

    <#switch role.name>
      <#case "Employee">
         This will be processed if it is Employee
         <#break>
      <#case "Administrator">
         This will be processed if it is Administrator
         <#break>
      <#case "Ambassador">
         This will be processed if it is Ambassador
         <#break>
      <#default>
         This will be processed if it is neither
    </#switch>