> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fga.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Object to Object Relationships

> Modeling relationships between objects (e.g. folder parent of a document)

In this guide you'll learn how to model your application with [objects](https://docs.fga.dev/fga-concepts.md#what-is-an-object) that are not specifically tied to a user. For example, a `folder` is a `parent` of a `document`.

**When to use**

This design pattern is helpful in the case where there are relationships between different objects. With Auth0 Fine-Grained Authorization (FGA), so long as both objects are in a type defined in the [authorization model](https://docs.fga.dev/fga-concepts.md#what-is-an-authorization-model), relationship tuples can be added to indicate a relationship between them.

For example:

* `communities` can contain `channels`
* `channels` can contain `posts`
* `channels` can contain `threads`
* `threads` can contain `posts`
* `bookshelf` can have `books`
* `trips` can have `bookings`
* `account` can contain `transactions`
* `buildings` can have `doors`

## Before you start

To better follow this guide, make sure you're familiar with some [Auth0 FGA Concepts](https://docs.fga.dev/fga-concepts.md) and know how to develop the things listed below.

<details>
  <summary>You will start with the *[authorization model](https://docs.fga.dev/fga-concepts.md#what-is-an-authorization-model)* below, it represents a `document` *[type](https://docs.fga.dev/fga-concepts.md#what-is-a-type)* that can have users **[related](https://docs.fga.dev/fga-concepts.md#what-is-a-relation)** as `editor`, and `folder` type that can have users related as `viewer`.</summary>

  ***

  In addition, you will need to know the following:

  ### Modeling user groups

  You need to know how to add users to groups and grant groups access to resources. [Learn more →](https://docs.fga.dev/modeling/basics/user-groups.md)

  ### Auth0 FGA concepts

  * A [Type](https://docs.fga.dev/fga-concepts.md#what-is-a-type): a class of objects that have similar characteristics
  * A [User](https://docs.fga.dev/fga-concepts.md#what-is-a-user): an entity in the system that can be related to an object
  * A [Relation](https://docs.fga.dev/fga-concepts.md#what-is-a-relation): is a string defined in the type definition of an authorization model that defines the possibility of a relationship between an object of the same type as the type definition and a user in the system
  * An [Object](https://docs.fga.dev/fga-concepts.md#what-is-an-object): represents an entity in the system. Users' relationships to it can be define through relationship tuples and the authorization model
  * A [Relationship Tuple](https://docs.fga.dev/fga-concepts.md#what-is-a-relationship-tuple): a grouping consisting of a user, a relation and an object stored in Auth0 FGA
</details>

[The Playground
Try this guide out on the Auth0 FGA Playground](https://play.fga.dev/)

## Step by step

### 01. Create parent relations in document

To represent that a `folder` can be a `parent` of a `document`, we first need to modify our `document` [type definition](https://docs.fga.dev/fga-concepts.md#what-is-a-type-definition) to allow a `parent` [relation](https://docs.fga.dev/fga-concepts.md#what-is-a-relation).

### 02. Add Parent Relationship Tuples

Once the type definition is updated, we can now create the [relationship](https://docs.fga.dev/fga-concepts.md#what-is-a-relationship) between a `folder` as a `parent` of a `document`. To do this, we will create a new **[relationship tuple](https://docs.fga.dev/fga-concepts.md#what-is-a-relationship-tuple)** that describes: **folder:budgets** is a `parent` of **document:may\_budget.doc**. In Auth0 Fine-Grained Authorization (FGA), [users](https://docs.fga.dev/fga-concepts.md#what-is-a-user) in the relationship tuples can not only be IDs, but also other objects in the form of `type:object_id`.

### 03. Check that parent folders have permissions

Once that relationship tuple is added to Auth0 FGA, we can [check](https://docs.fga.dev/fga-concepts.md#what-is-a-check-request) if the relationship is valid by asking the following: **"is folder:budgets a parent of document:may\_budget.doc?"**

It is important to note that the current authorization model does not imply inheritance of permissions. Even though **folder:budgets** is a `parent` of **document:may\_budget.doc**, **it does not inherit the `editor` relation from `parent` to `document`.** Meaning `editors` on **folder:budgets** are not `editors` on **document:may\_budget.doc**. Further configuration changes are needed to indicate that and will be tackled in a later guide.

> [!WARNING]
>
> When creating relationship tuples for Auth0 FGA make sure to use unique ids for each object and user within your application domain. We are using first names and simple ids to just illustrate an easy-to-follow example.

## Advanced object to object relationships

Object to object can be used for more advanced use case, such as [entitlements](https://docs.fga.dev/modeling/advanced/patterns/entitlements.md). An example use case is to allow subscribers to be entitled to different plans.

### 01. Create authorization model with object to object relationships

To do this, the authorization model will have two [types](https://docs.fga.dev/fga-concepts.md#what-is-a-type) - feature and plan.

Type `feature` has two relations, associated\_plan and access. Relation `associated_plan` allows associating plans with features while `access` defines who can access the feature. In our case, the access can be achieved either from

* [direct relationship](https://docs.fga.dev/fga-concepts.md#what-are-direct-and-implied-relationships) via  [direct relationship type restrictions](https://docs.fga.dev/modeling/configuration-language.md#direct-relationship-type-restrictions).
  or `this`
* object to object relationship where a user can access because it is a subscriber\_member of a particular plan AND that plan is associated with the feature.

Here, we define `plan` as the user of object `feature` with relationship `associated_plan` rather than defining `feature` as the user of object `plan` with relationship `feature`. The reason we choose the former is that we want to describe our system in the following [plain language](https://docs.fga.dev/modeling/getting-started.md#write-it-in-plain-language):

* A user can access a feature in a plan if they are a subscriber member of a plan that is the associated plan of a feature.

This will give us a flow of user->organization->plan->feature and allows us to answer the question of whether user can access a feature rather than whether user is subscriber of a plan.

### 02. Adding relationship tuples

To realize the relationship, we will need to add the following relationship tuples.

### 03. Check to see if access is allowed without direct relationship

To validate that the authorization model and relationship tuples are correct, we can ask the question:

We see that `anne` is allowed to `access` `feature:data_preview` without requiring direct relationship.

### 04. Disassociating plan from feature

At any point in time, `plan:advanced` may be disassociated from `feature:data_preview`.

When this is the case, `anne` will no longer have `access` to `feature:data_preview` even though she is still a `subscriber_member` of `plan:advanced`.

## Related Sections

Check the following sections for more on how object-to-object relationships can be used.

[Advanced Modeling Patterns: Entitlements
Learn how to model entitlement access patterns.](https://docs.fga.dev/modeling/advanced/patterns/entitlements.md)

[Modeling Parent-Child Relationships
Learn how to model parent and child relationships.](https://docs.fga.dev/modeling/basics/parent-child.md)

[Modeling User Groups
Learn how to model user groups.](https://docs.fga.dev/modeling/basics/user-groups.md)
