# Implementing User Roles in Experiences

**URL:** <https://forums.losant.com/t/implementing-user-roles-in-experiences/2183>\
**Category:** Losant Tips\
**Tags:** experience\
**Created:** [September 10, 2019, 3:34pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183 "2019-09-10T15:34:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [September 10, 2019, 3:34pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/1 "2019-09-10T15:34:34Z")

</div>

Most applications have user roles. For example, in some applications, certain pages and functionality are available to everyone, and others only available to an admin user. You can implement your own custom user roles with a [Losant Experience](https://docs.losant.com/experiences/overview/).

> _ **This tip is adapted from** _ _ **“** _ [How to Securely Upload Files with Losant Experiences](https://www.losant.com/blog/how-to-securely-upload-files-with-losant-experiences) _ **”** _ _, by Brandon Cannaday_

Let’s look at an example. Here is an example of an [Experience View:](https://docs.losant.com/experiences/views/)

![](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/1/14b2a485d22739dbaba859e029829b552f304f19.png)

For this application, the maintenance interval data (stored in a Losant Data Table), should be available to everyone. But, we only want administrators (a new role type) to upload new maintenance interval data to the underlying Losant Application. This use case requires us to show/hide the upload form you see above based on the user’s “role.”

There are two main ways to “group” Experience Users:

[Experience Groups](https://docs.losant.com/experiences/groups/) - An Experience Group is a mechanism for associating your application’s [devices](https://docs.losant.com/devices/overview/) with your [Experience Users](https://docs.losant.com/experiences/users/). Experience Groups are there to help you manage the relationship between your groups of customers and their devices.

[Experience User Tags](https://docs.losant.com/experiences/users/#user-tags) - Tags allow for the storing of arbitrary data against any Experience User, like user roles (ex. admin or not). Tags can be used to group Experience Users in the most flexible way possible.

**We recommend Experience User Tags for user roles.** Since user roles don’t usually impact any relationships to devices, but they do impact the UI, we don’t need to create Experience Groups for user roles.

In the image of the Experience you see above, here is the code to hide/show the upload form based on the user’s role (experience.user.userTags.role) and using the [#eq](https://docs.losant.com/workflows/accessing-payload-data/#conditional-block-helpers) helper:

```auto

{{! Only show the upload form if the user is an admin and a member of the Kanarra Technologies group }}
{{#eq experience.user.experienceGroups.[0].name 'Kanarra Technologies'}}
{{#eq experience.user.userTags.role 'admin'}}
  <form action="/maintenance-intervals" method="POST" enctype="multipart/form-data">
    <div style="font-size: 16px; text-align:center;margin-bottom:15px;">
      Upload new maintenance intervals
    </div>
    <div>
      <div style="text-align:center;">
        <input type="file" name="maintenanceIntervals" id="maintenanceIntervals" accept=".csv">
      </div>
      <div style="text-align:center;margin-top:10px;">
        <button class="button">Upload</button>
      </div>
    </div>
  </form>
{{/eq}}
{{/eq}}

```

Using techniques in this tip, you can start to build out user roles in your Experiences today. We’ll be back with a tip next week! 🙂

---

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [October 14, 2019, 3:11pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/2 "2019-10-14T15:11:05Z")

</div>

> [@anaptfox](#):
>
> {{#eq experience.user.experienceGroups.[0].name ‘Kanarra Technologies’}}

Hi Taron, thank you for a good example. I am currently implementing a better user management in our instance and a few things are not clear to me, for example:

1. Where to find a reference doc for Syntax of Losant Helpers available in custom pages. I mean #eq and #if etc

2. What if I want to give an access to users who are part of a certain group, but there are a few groups they are members of? It results in 0-N array records on `experience.user.experienceGroups.[index]`. In other words, what if I don’t know the exact index of the group. Should I browser through all available groups?

3. Could you correct my expression for the example described in 2), please?

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [October 14, 2019, 3:22pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/3 "2019-10-14T15:22:36Z")

</div>

@Alexander_Kondrov,

1. Yes, they are hard to find. It’s currently on our list for doc improvements. I’ll bump that up for you.

> **[Accessing Payload Data](https://docs.losant.com/workflows/accessing-payload-data/#format-helpers)**
>
> Learn more about Losant workflows: how to work with and access payload data.

1. & 3.

I don’t actually think you can nest helpers in a conditional like that. ( I’ll have to double-check the best way to do that. ). But, my best recommendation would be to move that logic into a workflow and pass it in as page data. You can do the logic in a Workflow and pass `userShouldSeeSomething` ( or something of the like) to your page. Then, you can reduce down to a simple `#if`:

{{#if userShouldSeeSomething}} Aweomse IoT Things {{/if}}

I recommend keeping the logic in your views super simple.

---

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [October 14, 2019, 4:07pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/4 "2019-10-14T16:07:06Z")

</div>

Taron,

Thank you for the advice to pass view rights stuff from the workflow. I haven’t think about it before receiving your hint. I will try to check it out.

---

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [October 14, 2019, 4:13pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/5 "2019-10-14T16:13:34Z")

</div>

Taron,

In addition, I have a feedback on the syntax of your and Brandon’s example.  
‘Kanarra Technologies’ - didn’t work for me  
**"Kanarra Technologies"** - worked for me

---

<div class="post-metadata">

**Author:** ![Jonas\_Helmstetter](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jonas\_Helmstetter](https://forums.losant.com/u/Jonas_Helmstetter)\
**Post date:** [October 11, 2022, 9:55am UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/6 "2022-10-11T09:55:38Z")

</div>

@anaptfox

Hi, I’m currently working on a similar topic but we face the challenge that we want to have users in different groups different roles. Is there any recommendation on your side how to implement such a thing?

Many thanks

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [October 11, 2022, 12:07pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/7 "2022-10-11T12:07:43Z")

</div>

@Jonas_Helmstetter I would still use one of the two methods described at the top of the post:

1. Store a `role` tag on each Experience User and set it to, for example, “admin”, “editor”, “viewer”, etc. This is what I recommend even for your use case.
2. If you want to maintain them in groups, you can build experience groups that exist outside of your multitenant hierarchy and add users to those groups. An experience user may be in multiple groups, so an administrative user could be in a top-level group calles “Administrators” as well as in an experience group called “Company X” - which could then be a descendent of other groups if desired.

---

<div class="post-metadata">

**Author:** ![Jonas\_Helmstetter](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jonas\_Helmstetter](https://forums.losant.com/u/Jonas_Helmstetter)\
**Post date:** [October 11, 2022, 2:32pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/8 "2022-10-11T14:32:25Z")

</div>

Hi @Dylan_Schuster Many thanks for your fast reply. So in order to have a User A in Group A and B but with different roles (Group A: Admin, Group B: Viewer) you would suggest Method 2 to have an experience group hierachy like this:

- Group A:
  - Group: Admins
    - Members: User A

  - Group: Viewers

- Group B:
  - Group Admins:
  - Group Viewers:
    - Members: User A

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [October 11, 2022, 2:35pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/9 "2022-10-11T14:35:47Z")

</div>

That could work, if that fits your model. More what I was thinking was that your administrators existed outside of your hierarchy altogether, but that may not be appropriate for your solution. Something like:

- Admins
  - (Members: User A)

- My Root Group
  - Client A
    - SubGroup A.1
      - (Members: User A)

    - SubGroup A.2

  - Client B
    - SubGroup B.1
    - SubGroup B.2

---

<div class="post-metadata">

**Author:** ![Jonas\_Helmstetter](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jonas\_Helmstetter](https://forums.losant.com/u/Jonas_Helmstetter)\
**Post date:** [October 11, 2022, 2:51pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/10 "2022-10-11T14:51:58Z")

</div>

I see the advantages of your shown model. But this would mean the “admin role” would not be specific for one group but it would be valid for all groups. Would it be possible to store all experience user ids in an experience group tag named admins? As far as I know, arrays in a tag are not supported or am I wrong? Many thanks for your help. I really appreciate the great support you and your team offer for the platform.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [October 11, 2022, 3:29pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/11 "2022-10-11T15:29:56Z")

</div>

> Would it be possible to store all experience user ids in an experience group tag named admins? As far as I know, arrays in a tag are not supported or am I wrong?

Possible? Yes - you could store it as a stringified array and then parse it when needed. But I wouldn’t recommend it. A syntax error would break the whole solution, and tag values have a maximum length of 255 characters, meaning that you could fit a maximum of 9 admins into a tag.

If the admins still need to be scoped to specific groups in your hierarchy, you could try something like this for a structure …

- Top-level Group A
  - Administrators A
    - (admin user is a member)

  - Sub-group A.1
    - (admin user is a member)

  - Sub-group A.2

- Top-level Group B
  - Administrators B
  - Sub-group B.1
  - Sub-group B.2

For what it’s worth, I still think identifying administrators is best done as a tag on the experience user and not through separate experience groups, but Losant is flexible enough to support either of those implementations.

---

<div class="post-metadata">

**Author:** ![Jonas\_Helmstetter](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jonas\_Helmstetter](https://forums.losant.com/u/Jonas_Helmstetter)\
**Post date:** [October 12, 2022, 9:03am UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/12 "2022-10-12T09:03:18Z")

</div>

> [@Dylan\_Schuster](#):
>
> For what it’s worth, I still think identifying administrators is best done as a tag on the experience user and not through separate experience groups, but Losant is flexible enough to support either of those implementations.

Many thanks. I just want to make sure that I understood it correctly. Your recommendation is to create the role as experience user tag. So that means if I have a user in multiple groups with different roles I would need to create an experience user tag for each group the user is in with a specifc role. The key could be for example the experience group id.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [October 12, 2022, 2:50pm UTC](https://forums.losant.com/t/implementing-user-roles-in-experiences/2183/13 "2022-10-12T14:50:57Z")

</div>

OK, your use case is a little clearer to me now. If a user should be an admin in Group A but not have admin privileges in Group B, then perhaps maintaining the admins as sub-groups in each top-level group would be the way to go. But you could still accomplish it with tags on the experience user - for example, a tag with a key of `isAdmin-EXPERIENCE_GROUP_ID` and a value of `true`.
