# Limit access to forms based on Health District ID

**URL:** <https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613>\
**Category:** Development\
**Created:** [March 29, 2023, 12:00pm UTC](https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613 "2023-03-29T12:00:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Job\_Isabai](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/job_isabai/32/1417_2.png) [@Job\_Isabai](https://forum.communityhealthtoolkit.org/u/Job_Isabai)\
**Post date:** [March 29, 2023, 12:00pm UTC](https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613/1 "2023-03-29T12:00:35Z")

</div>

Hello @binod  
How do I limit access to forms based on Health District ID?  
Thanks,  
Job

---

<div class="post-metadata">

**Author:** ![niraj](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/niraj/32/1330_2.png) [@niraj](https://forum.communityhealthtoolkit.org/u/niraj)\
**Post date:** [March 30, 2023, 4:34am UTC](https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613/2 "2023-03-30T04:34:52Z")

</div>

Hi @Job_Isabai,

Welcome to the CHT Forum! It is great to have you here.

I have moved the post to new topic since the [earlier question](https://forum.communityhealthtoolkit.org/t/restrict-app-forms-to-user-types/2195/3) was about restricting to user types and was marked as solved. thanks.

---

<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:** [March 30, 2023, 6:24am UTC](https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613/3 "2023-03-30T06:24:47Z")

</div>

Hi @Job_Isabai, welcome to the forum and thanks for tagging me.

Depending on where your patient and district are in the hierarchy, you might be able to limit the access to forms using [context.expression](https://docs.communityhealthtoolkit.org/apps/reference/forms/app/#formsappform_namepropertiesjson) in your `{form_name}.properties.json` file.

Example:

```auto
{
"title": "Screening", 
"context": {
  "place": false,
  "person": true,
  "expression": "contact.parent.parent.parent._id === 'uuid_of_district'"
  }
}

```

The above examples assumes:

- Your district is 3 steps above the patient (District → Town → Household → Patient)
- You want to limit your form to the patients in only one district that has the UUID: `uuid_of_district`

You can also specify the UUID relative from the logged in user, if that is easier:

```auto
"expression": "user.parent.parent._id === 'uuid_of_district'"

```

If there are multiple districts, then it can get difficult to specify them in the JSON file. In that case, you can consider adding context information in the [`contact-summary`](https://docs.communityhealthtoolkit.org/apps/reference/contact-page/) and accessing it from the form context.

Here is a working example form the [default configuration](https://github.com/medic/cht-core/blob/master/config/default):

1. In `contact-summary-templated`, we set the context variable as `show_pregnancy_form`:

> <https://github.com/medic/cht-core/blob/22ee1b6fd0de86b228dddbda53fb52bf117b2df1/config/default/contact-summary.templated.js#L16>

1. Now the above context variable is consumed in `pregnancy.properties.json` as `summary.show_pregnancy_form`:

> <https://github.com/medic/cht-core/blob/22ee1b6fd0de86b228dddbda53fb52bf117b2df1/config/default/forms/app/pregnancy.properties.json#L7>

The function used above: `isReadyForNewPregnancy` is defined [here](https://github.com/medic/cht-core/blob/22ee1b6fd0de86b228dddbda53fb52bf117b2df1/config/default/contact-summary-extras.js#L247), but you can define your own function that suits your purpose i.e. returning true for certain districts.

In the [contact-summary,](https://docs.communityhealthtoolkit.org/apps/reference/contact-page/) we have two variabes available that can hold the parent information: `contact` and `lineage`.

> | Variable | Description |
> | --- | --- |
> | contact | The currently selected contact. This has minimal stubs for the contact.parent, so if you want to refer to a property on the parent use lineage below. |
> | `lineage` | An array of the contact’s parents (2.13+), eg `lineage[0]` is the parent, `lineage[1]` is the grandparent, etc. Each lineage entry has full information for the contact, so you can use `lineage[1].contact.phone`. `lineage` will include only those contacts which are visible to the logged in profile |

If you want to make the form available to only certain districts based on their UUIDs, then you can use the `contact` variable:

```auto
const UUIDS = ['uuid_1', 'uuid_2'];
return UUIDS.includes(contact.parent.parent.parent._id);

```

If you have any specific field in the district document that can decide the form availability, then you can go for `lineage` which provides access to more fields.

```auto
return lineage[2].show_form === 'yes';

```

However, in order to use the `lineage` information about the district, your logged in user should also be able to see the `district` contact. If it is an offline user at some level below the district level, then the user might not be able to see the district. You can check that in the `People/Contacts` tab whether you can see the district or not.

Plese let us know how it goes.

---

<div class="post-metadata">

**Author:** ![Job\_Isabai](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/job_isabai/32/1417_2.png) [@Job\_Isabai](https://forum.communityhealthtoolkit.org/u/Job_Isabai)\
**Post date:** [March 30, 2023, 7:18am UTC](https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613/4 "2023-03-30T07:18:11Z")

</div>

@binod thank you very much. It works perfectly.

---

<div class="post-metadata">

**Author:** ![niraj](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/niraj/32/1330_2.png) [@niraj](https://forum.communityhealthtoolkit.org/u/niraj)\
**Post date:** [May 10, 2023, 2:31pm UTC](https://forum.communityhealthtoolkit.org/t/limit-access-to-forms-based-on-health-district-id/2613/5 "2023-05-10T14:31:08Z")

</div>

A post was split to a new topic: [Showing or hiding the log-out button depending on conditioin](https://forum.communityhealthtoolkit.org/t/showing-or-hiding-the-log-out-button-depending-on-conditioin/2736)
