# Conditional Permissions

{% hint style="info" %}
**Premium feature**

Conditional Permissions is a premium feature. Availability is determined by your subscription. Please check your current subscription or contact customer success to ensure you have access to this functionality.
{% endhint %}

Conditional Permissions are configured as part of the [Object Class Permissions](https://docs.appfarm.io/reference/security/permissions/..#object-classes). Object Class Permissions serve as the maximum permission a given Role may have towards a given Object Class (create, read, update or delete).  With Conditional Permissions, you may grant conditional access for different Roles to different Object Classes, and it is an important concept when defining [data silos](https://docs.appfarm.io/how-to/data-modeling/data-silos) for a multi-tenant application.

As with normal Object Class Permissions, the Conditional Permissions are enforced server side (back end) and are tamper-proof as opposed to hiding data or UI using conditions inside your App.

{% hint style="info" %}
**Good to know**

If your Solution relies heavily on Conditional Permissions, you might benefit from Data Aggregation Service. Conditional Permissions rely on aggregations, and this add-on provides access to specialized database infrastructure that caters exclusively to aggregation workloads.  Read more in our [Product Glossary](https://policies.appfarm.io/glossary/product-glossary#data-aggregation-service).
{% endhint %}

{% tabs %}
{% tab title="Example" %}
The Role `End Users` have `Read` and `Update` permissions towards the `Person` Object Class, for users to update their own `Person` object from a view in an App.

However, on a permissions level on the back end, all `End Users` have access to *all* `Person` objects - not only their own record/object. Without Conditional Permissions, you must be very strict with filters and what data to load into your App.

With Conditional Permission, you may set up a rule that is enabled for End Users, only allowing you to read and update your own `Person` object.&#x20;
{% endtab %}
{% endtabs %}

## Setup Guide

This is a short guide on how to set up Conditional Permissions given a specific use case.

### Use Case

Consider the following Data Model

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FwVSd3a9FZecYsjS6Du7u%2Fimage.png?alt=media&#x26;token=1f442e62-c103-4408-820f-90599ff00c11" alt=""><figcaption></figcaption></figure>

A logged-in `User` has an `Person` object. A `Person` belongs to a `Company` and may have a set of `Person Documents`. A `Company` may have a set of `Orders`.

We want to make sure

1. The user only has read access to the `Person` object (the one connected to the Current User) and the belonging `Person Documents`
2. The user only has access to the connected `Company` and the belonging `Orders`

### Configuration

For configuration of Conditional Permissions, go to **Security** -> **Permissions** -> **Object Classes.**

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2Fch17kD0PNS3TwZK1I9bC%2FConditional%20Permissions%20-%20Overview.png?alt=media&#x26;token=62fb3c98-7e78-47ac-a1e6-f380401496bd" alt=""><figcaption><p>Configuration of Conditional Permissions</p></figcaption></figure>

#### Read-permissions on the Person Object Class

Clicking the Filter icon for the `Person` Object Class in the above screenshot opens a dialog for configuring the conditional Read-permissions for the Person Object Class.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2Fp0kPTWOPiUDWx5bmUPZ2%2FConditional%20Permissions%20-%20Read%201.png?alt=media&#x26;token=3e6e9afd-a752-49d6-9300-a94238afc263" alt=""><figcaption></figcaption></figure>

* **Name**: Optional, this is just a describing name for the rule
* **Enabled**: This is where you set the condition on when this rule should apply. In this example, this rule should only apply when the logged-in user has the Role `End user`
* **Filter**: The filter that always should be applied when reading Persons (enabled when ). In this case, always apply the filter `Person.User = Current User`.

Note that when Read permission has been set up conditionally for a given Role (or Roles), and some other Roles should have access to all, we need to add a rule for that as well. In the below screenshot, any user with a membership in any of the Roles `Owner`, `Maintainer` or `Developers` has read access to all `Persons`.&#x20;

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2F82XNHybsnpdFk28xwLtd%2Fimage.png?alt=media&#x26;token=a2fc1c31-a9b4-4e95-a822-80f48941b3ca" alt=""><figcaption></figcaption></figure>

In other words: With Conditional Permissions set up, a user will get the sum of permissions defined by the rules in the setup above. If a user is both a `Maintainer` and an `End User`, the user will have read access to all `Persons`.

#### Read-permissions on the Person Documents Object Class

The same Enabled conditions and set of rules apply to this Object Class. But the Filter is different.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2F4clkufUnSeAZ7VjZRreh%2FConditional%20Permissions%20-%20Read%202.png?alt=media&#x26;token=4f27415b-5a79-4dd5-8395-dfe7ea5f2205" alt=""><figcaption><p>Filter for the Read Permission</p></figcaption></figure>

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FTNSQMT3n1V0F3JKUQu9m%2Fimage.png?alt=media&#x26;token=40107e67-9e1c-414f-b18a-755f5bc3befc" alt=""><figcaption><p>The filter for the selection of Persons</p></figcaption></figure>

We need to define the path from `Person Document` to `Current User`, for the Read-permissions to work. The above screenshots illustrate how to set up a filter on *all Person Documents belonging to a Person that the Current User is connected to*.

#### Read permissions on the Company Object Class

For these permissions, we need to define a filter on all *Companies belonging to the Person that the Current User is connected to*.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FPxkSJhOSOMO77KIpTWE7%2Fimage.png?alt=media&#x26;token=f07eb323-4d7d-412e-b4a0-2e0eec89b4e0" alt=""><figcaption><p>Filter for the Read Permission</p></figcaption></figure>

In the above screenshot, the `Person` Object Class has the following filter:

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FscrNbKIhRfBzqoHlBwHx%2Fimage.png?alt=media&#x26;token=28758246-09b2-488a-9d9a-51b2da7b03c6" alt=""><figcaption><p>The filter for the selection of Persons</p></figcaption></figure>

#### Read permissions on the Order Object Class

For these permissions, we need to define a filter for *all Orders belonging to the Companies of the Person object that the Current User is connected to*. It is the same setup as for the `Company` permissions, except the left side of the filter (where `Order.Company` is the property we are filtering on).

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FFF1leRfWFKPotrY7y6OC%2Fimage.png?alt=media&#x26;token=49957ec8-aff9-4703-be21-6e577f4fcc9f" alt=""><figcaption><p>Filter for the Read Permission</p></figcaption></figure>

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FmMpJrewSUsSkitSBFm3o%2Fimage.png?alt=media&#x26;token=101372f2-0d03-4c0f-8d5b-0509f5b2d455" alt=""><figcaption><p>The filter for the selection of Persons</p></figcaption></figure>
