# Retrieving roles array of user

**URL:** <https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635>\
**Category:** Development\
**Tags:** forms, array, configuration, roles\
**Created:** [December 26, 2021, 10:29pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635 "2021-12-26T22:29:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![uensal](https://communityhealthtoolkit.b-cdn.net/letter_avatar_proxy/v4/letter/u/e47c2d/32.png) [@uensal](https://forum.communityhealthtoolkit.org/u/uensal)\
**Post date:** [December 26, 2021, 10:29pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/1 "2021-12-26T22:29:07Z")

</div>

Dear community

How am I able to get all the roles that the logged-in user has?  
I tried it with the following line in the xlsxForm, but I never get any results.

 ![Screenshot 2021-12-26 at 23.20.08](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/d/dc906f472b98eb13f7901d434cbc35189391f2f3.png)

 ![Screenshot 2021-12-26 at 23.28.24](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/7/72835794d79ea35cf77059f6cd2e7b5ae5d9bf1c.png)

@diana or @mrjones would any of you maybe be able to answer my question above? \>\< I’d highly appreciate it.

Thank you very much in advance for your help  
Bests,  
Ünsal

---

<div class="post-metadata">

**Author:** ![marc](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/marc/32/602_2.png) [@marc](https://forum.communityhealthtoolkit.org/u/marc)\
**Post date:** [January 3, 2022, 6:49pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/2 "2022-01-03T18:49:08Z")

</div>

Hi Ünsal, sorry for the slow response over the holidays. I haven’t had a chance to test this hypothesis out, but it is possible that you can’t directly assign the role _array_ to a _string_. Have you tried assigning it to a different field type, such as a repeat group to get the inner value(s) that way? That may work, and if not perhaps someone else has encountered this situation before and could offer advice.

---

<div class="post-metadata">

**Author:** ![diana](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/diana/32/2049_2.png) [@diana](https://forum.communityhealthtoolkit.org/u/diana)\
**Post date:** [January 4, 2022, 1:18pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/3 "2022-01-04T13:18:44Z")

</div>

Hi @uensal

Currently, I don’t think there is no way to access user roles in forms. You do, however, have access to the user’s associated contact.

We had a similar request some time ago, that resulted in adding an API to verify the permissions of the logged in user - this new api is available in tasks, targets and contact-summary, and will be available in the purge function in the future (this improvement is already scheduled). For more details, please check this issue: [https://github.com/medic/cht-core/issues/6914](https://github.com/medic/cht-core/issues/6914)

We could make this available in forms by adding another xpath extension function for the APIs (details about CHT xPath functions: [app | Community Health Toolkit](https://docs.communityhealthtoolkit.org/apps/reference/forms/app/#cht-xpath-functions)).

---

<div class="post-metadata">

**Author:** ![uensal](https://communityhealthtoolkit.b-cdn.net/letter_avatar_proxy/v4/letter/u/e47c2d/32.png) [@uensal](https://forum.communityhealthtoolkit.org/u/uensal)\
**Post date:** [January 4, 2022, 2:04pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/4 "2022-01-04T14:04:53Z")

</div>

Hi @diana and @marc

Thank you for your responses.  
I am currently not working anymore for the project and have already handed it over to my successor. If he wants to follow up on this, I think he will come forward and continue here.

Nevertheless, thank you very much for your support.  
Bests,  
Ünsal

---

<div class="post-metadata">

**Author:** ![bamatic](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/bamatic/32/1318_2.png) [@bamatic](https://forum.communityhealthtoolkit.org/u/bamatic)\
**Post date:** [April 15, 2022, 12:10pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/5 "2022-04-15T12:10:06Z")

</div>

Hi,  
How could I access user’s associated contact in a contact form ? I’ve tried with a group contact into the user group but it doesn’t work for me, is the correct way ?

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [June 17, 2022, 7:28am UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/6 "2022-06-17T07:28:19Z")

</div>

Hi @bamatic, sorry for the late reply.

As you can see in the [default config](https://github.com/medic/cht-core/tree/master/config/default/forms/contact) contact forms, we can get `contact_id` from the `user` group.  
 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/4/4a2f277b52efee7dc135da4a276bc6ad6f50510c.png)

This is the UUID of the associated contact document of the current user.  
We can use this UUID to pull other information stored in the contact document such as `name`, `role`, and `date_of_birth`.

 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/3/3925361b95534da6ad4e33fad10f46824f11501b.png)

Here, we are using a [CHT XForm widget](https://docs.communityhealthtoolkit.org/apps/reference/forms/app/#cht-xform-widgets): `select-contact`. For v3.9.0 and below, we need to set the appearance to `db-object` instead of `select-contact`.

The contact form will look like this:  
 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/a/ae1d65d11a534fdbe7df21474c05c0dd338bf169.png)

---

<div class="post-metadata">

**Author:** ![raf](https://communityhealthtoolkit.b-cdn.net/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@raf](https://forum.communityhealthtoolkit.org/u/raf)\
**Post date:** [July 19, 2022, 9:20am UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/7 "2022-07-19T09:20:25Z")

</div>

Dear @binod,  
Amazing what is all possible.  
For me this is not working. When I check directy in the database, there is no `role` entry for `Persons`, only for `user` and there it is not `role` but `roles`.  
Still it is not working for me, that field seems not to load anything.

Do you by chance have an explanation how the `input` and `init` groups actually work, so that implementers can do this on their own?  
Why do you have to add a calculate field for `contact_uuid` that just copies the content of the field `contact_id`, a field that can already be used in `note` and other fields.

Why do you have to write `../role` when `role` is in your level and not in the parent level? Why is it not just `./role`

I just want to have the user role of the current user in the form, but no matter how I do it, it always empty.

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [July 20, 2022, 6:09pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/8 "2022-07-20T18:09:38Z")

</div>

Hi @raf, I will try to address your questions one by one.

### role

> [@raf](#):
>
> Still it is not working for me, that field seems not to load anything.
> 
> I just want to have the user role of the current user in the form, but no matter how I do it, it always empty.

Please add the `role` field to your user’s contact document, then it should load as `role` inside the `user` group. Also, please note this comment:

> [@diana](#):
>
> Currently, I don’t think there is no way to access user roles in forms. You do, however, have access to the user’s associated contact.

### inputs

You can read about the `inputs` group in the app and contact forms [here](https://docs.communityhealthtoolkit.org/apps/guides/forms/form-inputs). In short:

> Data needed during the completion of the form (eg patient’s name, prior information) is passed into the `inputs` group.

### init

I couldn’t find any resource explaining the `init` group, sorry about that. I have seen it being used mostly in the contact forms to store or calculate something temporarily (which you don’t want to store in the resulting database document).

For example, in the `person-create` form in [the default config](https://github.com/medic/cht-core/tree/master/config/default/forms/contact), inside the `init` group, we are pulling the parent information such as `name`, and `type`. You can also see a calculate field: `type_label` which is used as a hint (_Household_). The user can see these fields (unless they are hidden or a calculated field), but they are not saved in the database document upon submission.

 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/d/d66cdbae6400b498d63b99f78c3d3b14e574288c.png)

### contact\_uuid

> [@raf](#):
>
> Why do you have to add a calculate field for `contact_uuid` that just copies the content of the field `contact_id`, a field that can already be used in `note` and other fields.

The `contact_uuid` field above is used to bind the contact document so that the fields after it (`name`, `role`, `date_of_birth`) are populated. The binding is done by the special syntax used in the `appearance` column. You can read about it [here](https://docs.communityhealthtoolkit.org/apps/guides/forms/form-inputs/#contact-selector).

### `./` vs `../`

> [@raf](#):
>
> Why do you have to write `../role` when `role` is in your level and not in the parent level? Why is it not just `./role`

In [XPath](https://www.w3.org/TR/1999/REC-xpath-19991116/), `.` selects the context node and `..` selects the parent of the context node. Since `role` and `user_contact_info`, are at the same level (siblings), we need to go to the parent and come to it.

I hope that all your questions have been answered. If I missed something, or need to clarify more, please let me know.

---

<div class="post-metadata">

**Author:** ![raf](https://communityhealthtoolkit.b-cdn.net/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@raf](https://forum.communityhealthtoolkit.org/u/raf)\
**Post date:** [July 26, 2022, 5:10pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/9 "2022-07-26T17:10:04Z")

</div>

@binod  
Thank you for taking the time and explaining. I had read that already.  
According to your docs:

> **[Input data available in forms](https://docs.communityhealthtoolkit.org/apps/guides/forms/form-inputs/#user-data)**
>
> Data accessible from within CHT forms

I can use the `doc for the user`.

An exemplary user of mine would be:

```auto
{
  "_id": "org.couchdb.user:head-niger",
  "_rev": "3-6f8ccf28ec163124c481eeab9e38a56d",
  "name": "head-niger",
  "type": "user",
  "roles": [
    "program_officer",
    "mm-online"
  ],
  "facility_id": "930e8b2d-a7d3-57a3-8fbf-540d785e464f",
  "password_scheme": "pbkdf2",
  "iterations": 10,
  "derived_key": "5785a9687e67d92ba60d38e7828d77bdf716435e",
  "salt": "c398a9f86fabc6d50bb2af17041464ee",
  "known": true
}

```

For testing, I have tried to load `name`, `type`, `facility_id`, `roles`, `password_scheme`, etc.  
Beside `name`, `facility_id` and `contact_id`, none of them gives any output, despite using the same approach for all of them.  
The only difference is the output for `type`. It gives as output `user-settings` although the correct answer would be `user`.

All a big mystery to me.

---

<div class="post-metadata">

**Author:** ![raf](https://communityhealthtoolkit.b-cdn.net/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@raf](https://forum.communityhealthtoolkit.org/u/raf)\
**Post date:** [July 26, 2022, 5:11pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/10 "2022-07-26T17:11:04Z")

</div>

![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/9/99e290768776aa5c3ac5c4016a79bc61d9c5a3d6.png)

---

<div class="post-metadata">

**Author:** ![raf](https://communityhealthtoolkit.b-cdn.net/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@raf](https://forum.communityhealthtoolkit.org/u/raf)\
**Post date:** [July 26, 2022, 5:12pm UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/11 "2022-07-26T17:12:22Z")

</div>

![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/5/5ab00832f09da2704ccd57b7bd606b7262351727.png)

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [July 27, 2022, 11:51am UTC](https://forum.communityhealthtoolkit.org/t/retrieving-roles-array-of-user/1635/12 "2022-07-27T11:51:35Z")

</div>

Hi @raf, sorry for the confusion.

There is another place where the user document is stored. The user information is pulled from there.  
By default, the database is `medic`, and the document `_id` would be the same as above: `org.couchdb.user:head-niger`.  
For your user, there should be a document inside `medic` database which should look something like this:

```json
{
  "_id": "org.couchdb.user:head-niger",
  "_rev": "2-f06f5d47c37d1bd762f42c53b88376e6",
  "name": "head-niger",
  "type": "user-settings",
  "roles": [
    "program_officer",
    "mm-online"
  ],
  "facility_id": "930e8b2d-a7d3-57a3-8fbf-540d785e464f",
  "contact_id": "8a22818c-6c5e-43cd-bcfa-ae8f04279070",
  "known": true
}

```

All these fields are available in the form, except for `roles`, which is an array.

```xml
<user>
    <name>head-niger</name>
    <contact_id>8a22818c-6c5e-43cd-bcfa-ae8f04279070</contact_id>
    <facility_id>930e8b2d-a7d3-57a3-8fbf-540d785e464f</facility_id>
    <known>true</known>
    <type>user-settings</type>
    <_id>org.couchdb.user:head-niger</_id>
    <_rev>2-f06f5d47c37d1bd762f42c53b88376e6</_rev>
    <roles/>
</user>

```

That’s why we take the `contact_id`, go to the associated contact document, and look for the `role` field there. For that to work, the `role` field should be added to the contact form in the first place.

If you are interested to learn more about how the data is transferred from CouchDB to the XML form, you can find the binding taking place [here](https://github.com/medic/cht-core/blob/7dbb0ee46f4629f0d2838f007c4e633e5f1eeb82/webapp/src/ts/services/enketo-prepopulation-data.service.ts#L43) in the `cht-core` source.
