Skip to main content

Relations

A relation consists of three elements:

  • A source object
  • A relation type
  • A target object

Relations can therefore be seen as (source, type, target) triplets.

The type of the source object in a relation must match the object type specified in the relation type.

Finding Users

Before we create relations between our departments and users, let's find our users in the directory.

If you haven't added the demo Citadel identity provider to your Aserto account, please follow step 2 of this guide's preerquisites now. It will populate your directory with a handful of demo users.

We'll use the objects query to find objects of type user:

Query
query ListUsers {
objects(first: 10, where: {type: {name: {equals: "user"}}}) {
nodes {
id
displayName
}
}
}
Results
{
"data": {
"objects": {
"nodes": [
{
"id": "fd0614d3-c39a-4781-b7bd-8b96f5a5100d",
"displayName": "Rick Sanchez"
},
{
"id": "fd1614d3-c39a-4781-b7bd-8b96f5a5100d",
"displayName": "Morty Smith"
},
{
"id": "fd2614d3-c39a-4781-b7bd-8b96f5a5100d",
"displayName": "Summer Smith"
},
{
"id": "fd3614d3-c39a-4781-b7bd-8b96f5a5100d",
"displayName": "Beth Smith"
},
{
"id": "fd4614d3-c39a-4781-b7bd-8b96f5a5100d",
"displayName": "Jerry Smith"
}
]
}
}
}

Creating Relations

We'll start by making Rick an owner of all three departments. The following request creates all three of Rick's owner relations:

Query
mutation CreateRickOwnerRelations {
spaceships: setRelation(relation: {
source: {key: {key: "spaceships", type: {name: "department"}}}
relationType: {name: {name: "owner", objectType: {name: "department"}}}
target: {id: "fd0614d3-c39a-4781-b7bd-8b96f5a5100d"}
}) {
relation {
source {
displayName
type {
name
}
}
type {
displayName
}
target {
displayName
type {
name
}
}
}
}

teleporters: setRelation(relation: {
source: {key: {key: "teleporters", type: {name: "department"}}}
relationType: {name: {name: "owner", objectType: {name: "department"}}}
target: {id: "fd0614d3-c39a-4781-b7bd-8b96f5a5100d"}
}) {
relation {
source {
displayName
type {
name
}
}
type {
displayName
}
target {
displayName
type {
name
}
}
}
}

lasers: setRelation(relation: {
source: {key: {key: "lasers", type: {name: "department"}}}
relationType: {name: {name: "owner", objectType: {name: "department"}}}
target: {id: "fd0614d3-c39a-4781-b7bd-8b96f5a5100d"}
}) {
relation {
source {
displayName
type {
name
}
}
type {
displayName
}
target {
displayName
type {
name
}
}
}
}
}
Results
{
"data": {
"spaceships": {
"relation": {
"source": {
"displayName": "Spaceships",
"type": {
"name": "department"
}
},
"type": {
"displayName": "department::owner"
},
"target": {
"displayName": "Rick Sanchez",
"type": {
"name": "user"
}
}
}
},
"teleporters": {
"relation": {
"source": {
"displayName": "Teleporters",
"type": {
"name": "department"
}
},
"type": {
"displayName": "department::owner"
},
"target": {
"displayName": "Rick Sanchez",
"type": {
"name": "user"
}
}
}
},
"lasers": {
"relation": {
"source": {
"displayName": "Lasers",
"type": {
"name": "department"
}
},
"type": {
"displayName": "department::owner"
},
"target": {
"displayName": "Rick Sanchez",
"type": {
"name": "user"
}
}
}
}
}
}

In the same way we can assign other users to roles in different departments by creating relations of the desired type.

Listing Object Relations

We can verify that Rick has the new relations to the correct departments by inspecting his user object and selecting the relations in which it appears as the target object.

Query
query RicksRelations {
object(selector: {id: "fd0614d3-c39a-4781-b7bd-8b96f5a5100d"}) {
displayName
relations(side: TARGET, first: 3) {
nodes {
type {
displayName
}
source {
type {
displayName
}
displayName
}
}
}
}
}
Results
{
"data": {
"object": {
"displayName": "Rick Sanchez",
"relations": {
"nodes": [
{
"type": {
"displayName": "department::owner"
},
"source": {
"type": {
"displayName": "Department"
},
"displayName": "Teleporters"
}
},
{
"type": {
"displayName": "department::owner"
},
"source": {
"type": {
"displayName": "Department"
},
"displayName": "Lasers"
}
},
{
"type": {
"displayName": "department::owner"
},
"source": {
"type": {
"displayName": "Department"
},
"displayName": "Spaceships"
}
}
]
}
}
}
}