Roles and purposes

This page covers who may process personal data and why. You map each application role to a database role and to the purposes it may work for. Application code then declares a purpose before it touches personal data. The page also shows what happens when nobody declares one, and what happens when a role reads outside its purposes.

1. Name your purposes

A purpose is a name. Every personal entity declares at least one, as Classifying personal data describes. Keep the names in one class of constants, as Purposes does. The entities, the role map and the application code then all use the same names.

A purpose does not need an entity. No Shop entity is held for Analytics. A role that works for it is outside the purpose of every personal field it reads. Section 6 shows what that role sees.

2. Map roles to database roles and purposes

An application role is a role name the signed-in user carries. o.Roles.Map gives it two things. The first is the database role its connections are opened as. The second is the list of purposes it may process for.

The Clinic maps its roles directly.

o.Roles.Map(ClinicRoles.Clinician, DbRole.Writer, purposes: [Purposes.Treatment, Purposes.Scheduling]);
o.Roles.Map(ClinicRoles.Reception, DbRole.Writer, purposes: [Purposes.Scheduling]);
o.Roles.Map(ClinicRoles.Analyst, DbRole.Reader, purposes: [Purposes.Research]);
o.Roles.Map(ClinicRoles.PrivacyOfficer, DbRole.Privacy, purposes: []);
o.Roles.Map(ClinicRoles.Administrator, DbRole.Writer, purposes: [Purposes.Employment]);

The Shop keeps its roles as data in ShopRoles and maps them in a loop. The same block sets what happens on a violation, which sections 5 and 6 explain.

foreach (var role in ShopRoles.All)
{
    _ = role.AssistedOnly
        ? o.Roles.MapAssistedOnly(role.Name, role.DbRole, [.. role.Purposes])
        : o.Roles.Map(role.Name, role.DbRole, purposes: [.. role.Purposes]);
}

o.Access.OnPurposeViolation(ViolationMode.Deny);
o.Access.ForRole(ShopRoles.Analyst.Name).OnPurposeViolation(ViolationMode.Mask);

Some things to know about the map.

3. Declare a purpose

Custodian knows who is signed in and which tenant they work in. The middleware supplies that, as ASP.NET Core describes. It does not know why the code is reading. The code says so with IProcessingContext.Begin.

SubjectId subject;
using (processing.Begin(Purposes.CustomerAccount))
{
    subject = await subjects.RegisterAsync(newCustomer.Email, cancellationToken);
}

var customer = new Customer
{
    SubjectId = subject.Value,
    Name = newCustomer.Name,
    Email = newCustomer.Email,
    Phone = newCustomer.Phone,
};

using (processing.Begin(Purposes.CustomerAccount, subject))
{
    db.Customers.Add(customer);
    await db.SaveChangesAsync(cancellationToken);
}

Begin returns a scope. The purpose holds until the scope is disposed, and scopes nest. The second argument is optional. It names the data subject the work is about.

Inside the scope the code is plain EF Core. Custodian checks every personal field the query reads against two questions.

  1. Is the data held for this purpose?
  2. May one of the user's roles process for this purpose?

Both must hold. A warehouse worker may work for Fulfilment, and a Shipment is held for it, so the read succeeds. A_warehouse_worker_reads_where_a_parcel_goes shows it.

4. Fail closed

Without a signed-in user, a tenant and a purpose, Custodian returns no personal data. It throws ProcessingContextMissingException. Being signed in is not enough.

[Theory, EachProvider]
public async Task A_sales_clerk_who_declares_no_purpose_cannot_read_customers(DatabaseProvider provider)
{
    await using var shops = await servers.OpenShop(provider);
    await shops.Nordic.SignUp(Customers.Astrid());

    var reading = () => shops.Nordic.As(Staff.Sales).WithoutPurpose.Query(db => db.Customers);

    await Assert.ThrowsAsync<ProcessingContextMissingException>(reading);
}

The same holds with nobody signed in, for reads and for writes. See Nobody_signed_in_cannot_read_customers and Nobody_signed_in_cannot_store_a_customer. The Clinic proves the same in A_signed_in_clinician_who_declares_no_purpose_gets_no_patient_data.

A forgotten Begin therefore shows up as an exception, not as a leak.

5. Outside the purpose, the default is Deny

A read outside the purpose is a purpose violation. o.Access.OnPurposeViolation sets what happens then. There are three modes. Deny is the default and throws PurposeViolationException. Mask is the subject of section 6. BreakGlass returns the data, flags the audit entry and opens an item the reader must explain, as Health data and break-glass describes.

[Theory, EachProvider]
public async Task A_warehouse_worker_cannot_read_a_customers_billing_details(DatabaseProvider provider)
{
    await using var shops = await servers.OpenShop(provider);
    var astrid = await shops.Nordic.SignUp(Customers.Astrid());
    await shops.Nordic.PlaceOrder(astrid);

    var reading = () => shops.Nordic.As(Staff.Warehouse).For(Purposes.Fulfilment).Query(db => db.Orders);

    await Assert.ThrowsAsync<PurposeViolationException>(reading);
}

The warehouse worker declared Fulfilment, which is theirs. The read fails on the first question. An Order is held for Billing, not for Fulfilment.

Declaring another purpose does not help. The read then fails on the second question, because the warehouse role may not process for Billing. See A_warehouse_worker_cannot_claim_the_billing_purpose.

The exception names the entity, the field and the purpose. It never carries a value. The refusal is written to the audit trail, as A_refused_read_is_audited_as_a_purpose_violation and A_receptionists_attempt_to_read_the_reason_for_a_visit_is_audited show.

6. Mask

Mask returns the row with its out-of-purpose personal fields masked. It suits a role that needs the shape of the data and not the people in it. Both samples set it for their analyst with o.Access.ForRole(...), which overrides the default for one role.

[Theory, EachProvider]
public async Task An_analyst_sees_a_masked_email_address(DatabaseProvider provider)
{
    await using var shops = await servers.OpenShop(provider);
    var astrid = await shops.Nordic.SignUp(Customers.Astrid());

    var customers = await shops.Nordic.As(Staff.Analyst).For(Purposes.Analytics).Query(db => db.Customers);

    Masked.ShouldHideEmail(astrid.Email, Assert.Single(customers).Email);
}

The assertion checks that the mailbox name is gone and the domain remains. Fields that are not personal data come back unchanged, so the analyst still gets sales figures. See An_analyst_sees_what_was_sold_but_not_who_bought_it.

Masking covers encrypted and special-category fields as well. The Clinic's analyst never sees a national id, the reason for a visit or a clinical note. See An_analyst_sees_a_masked_national_id, An_analyst_never_sees_why_a_patient_came_in and An_analyst_never_sees_what_the_doctor_wrote.

A violation mode applies to reads only. A write outside the purpose is refused with PurposeViolationException whatever the mode. A_clinician_cannot_write_in_the_record_of_a_patient_outside_her_care shows it for a role whose reads would be let through.

A masked read is not a break-glass access and leaves nothing to explain. See An_analyst_reading_out_of_purpose_never_opens_a_break_glass_item.

Next