Skip to content
Directory & LifecycleMarkdown

Rule-based membership

Work out who belongs from a rule on users' attributes, so a change of department is the whole move, kept current in both directions.

Available, with limitsAPIJSON Schema 2020-12

Instead of adding and removing members by hand, give a set of users a rule, such as “everyone whose department is Finance”, and the realm keeps membership current: a user who starts matching joins in the same write, and one who stops matching leaves. Today the rule lives on a type of your own, not on the shipped group.

  • Privilege: RealmOwner, or a privilege of your own that covers type definitions (*REALM/entity/type/*) and the objects of your type. See Who may do what.
  • Where: the API, with your session in $CVYUH_SESSION (see Call the API).
  • The attribute the rule reads exists on your user type, such as department: see Attributes and validation.

A link can be worked out from a rule instead of made by hand. Give a type of your own a condition attribute, and mark the link’s other end, on the user, with "x-conditional-membership": true. A condition is a filter, written as for a search (see Search and paging): every user it matches is linked, and every user who stops matching is unlinked.

The realm keeps it current in both directions, inside the write that caused the change. Change a user’s attributes, and their memberships are worked out again in the same write: when the call returns, they’re already in or out. Change a condition, or create an object with one, and every user it now matches is linked at once, not only on their next change. Remove the condition, and every link it made is removed.

How a missing attribute matches. An attribute the user hasn’t set reads as its default, if the type gives one. With no value at all, it matches no condition but is null: a user without a department is in neither department = Finance nor department != Finance. Write { "field": "department", "op": "is null" } to catch them. Conditions match case exactly: Finance and finance are different values.

A rule hands the choice of members to whoever may write the attributes it reads. If a team may change users’ department, that team decides who is in every set whose rule reads department, and what those sets give. Base rules on attributes only your administrators, or your HR load, write, and keep the privileges that change them as narrow as those that change the set itself.

This makes a team type whose members are found by their department.

  1. Define the type, with a condition and a members link to users:

    Terminal window
    curl https://foobar.cvyuh.com/api/v0/am/types/v1 \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
    -H "Content-Type: application/json" \
    -d '{
    "name": "team", "category": "managed", "cardinality": "collection",
    "description": "A team, its members found by rule",
    "schema": { "type": "object", "properties": {
    "displayName": { "type": "string" },
    "condition": { "type": "object" },
    "members": { "type": "array",
    "x-relationship": { "target": "user", "cardinality": "many", "reverse": "teams" } }
    } },
    "policies": { "displayName": ["required"] }
    }'

    The realm answers 201.

  2. Add the other end, teams, to your realm’s user type, marked for rule-based membership. Read the type, add this under schema.properties, and save it back with PUT and the revision you read, as Attributes and validation describes:

    "teams": { "type": "array", "x-conditional-membership": true,
    "x-relationship": { "target": "team", "cardinality": "many", "reverse": "members" } }
  3. Create a team with its rule:

    Terminal window
    curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/team?id=finance" \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
    -H "Content-Type: application/json" \
    -d '{ "displayName": "Finance",
    "condition": { "field": "department", "op": "=", "value": "Finance" } }'

    Every user whose department is Finance is a member at once. Set department on Ben, and he joins in the same write; change it, and he leaves.

WhatLimitPast it
Users a rule is applied to, when the rule is created or changed10,000The rule is applied to the first 10,000 users it matches, in id order. The others aren’t linked, and links the rule made to them before are removed. No error is returned
Sets with a rule checked, when a user changes10,000 objects of all your rule-bearing types togetherSets past the 10,000th aren’t checked for that user: their links to the user stay as they were. No error is returned
Where rules workLinks of a type of your ownThe shipped group can’t take a rule yet

Keep each rule narrow enough to match well under 10,000 users, and keep the number of rule-bearing sets under 10,000. To check a rule, compare its members (list the link, paged) with a search using the same condition.

AnswerCodeWhat it meansWhat to do
400INVALID_REQUESTYou tried to remove a link the rule madeChange the rule, or the user’s attributes

Saving the type is recorded as CREATED or UPDATED with its definition. Creating or changing a set with a rule, and changing the user attributes it reads, are recorded on each object’s change history, with who made the change.

SymptomCauseFix
Some users who match aren’t membersThe rule matches more than 10,000 users, or the realm has more than 10,000 rule-bearing setsSplit the rule into narrower ones; remove sets you no longer use
A user without the attribute isn’t in a != setMissing values match no condition but is nullAdd an or with is null
A user moved department but kept the old setThe old set’s link was made by hand, which takes the rule’s placeRemove the hand-made link
  • The shipped user and group can’t use rules on their own links. memberOf and member are carried attributes, so rule-based membership needs a link of your own, as above. See What’s next for groups.
  • A rule-made link can’t be removed by hand. Unlinking it is refused, since the next change would make it again. Change the rule or the user’s attributes instead. A link you make by hand to the same object takes the rule’s place.
  • The attribute must exist. Add department, or whatever the rule reads, to your user type first.