Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/auth/roles-permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ The department names above come from the PRD. The admin chooses the role that ma
| --- | --- |
| The employee is not signed in, or their session expired | 401: the app asks them to sign in |
| The employee's role does not allow the action | 403: access refused |
| The employee signed in with Microsoft but was never added to the app | 403 for every protected action |
| The admin removed the employee's access | 403 for every protected action, even for a former Admin |
| The employee's account is already linked to another Microsoft account | 403 for every protected action |
| The employee signed in with Microsoft but was never added to the app | 403 for every action |
| The admin removed the employee's access | 403 for every action, even for a former Admin |
| The employee's account is already linked to another Microsoft account | 403 for every action |

The first time an employee does an action that requires a permission, their Microsoft account is linked to the account the admin created with their email. After that, the app recognizes them by their Microsoft account, even if their email changes. An account whose access was removed can still be linked, but the API keeps refusing its actions.
The first time the employee uses the app after signing in, their Microsoft account is linked to the account the admin created with their email. After that, the app recognizes them by their Microsoft account, even if their email changes. An account whose access was removed can still be linked, but the API keeps refusing its actions.

If the employee's Microsoft account is recreated (for example, IT deleted and recreated it), the app no longer recognizes them and refuses all their actions. The app cannot reset this link yet (see Open points).

Expand Down Expand Up @@ -89,9 +89,11 @@ Use the constants in `Permissions`, never a role name. The roles allowed for eac
| `PermissionHandler` | `src/MoissonMontreal.Api/Authorization/` | Allows the request when the employee's role is in the list, otherwise the API answers 403 |
| `UserLookupService` | `src/MoissonMontreal.Api/Authorization/` | Finds the employee's active account in the `users` table and links it at the first permission check |

The role is read from the database only when an endpoint requires a permission. Endpoints such as `/health` and `api/me` do not read it.
The default policy, set in `src/MoissonMontreal.Api/Program.cs`, also requires an active account with any role (`RoleCodes.All`). So an endpoint without a permission still refuses a deactivated or never-added employee.

The Microsoft account is linked by `UserLookupService`, called from `PermissionHandler`, the first time the employee calls an endpoint that requires a permission. Signing in or calling `/api/me` does not link the account, so `entra_object_id` can still be empty after a sign-in.
Every endpoint reads the employee's account and role from the database, except `/health`. `api/me` returns the role, so the desktop app can show only what the employee is allowed to use.

The Microsoft account is linked by `UserLookupService`, called from `PermissionHandler`, the first time the employee calls any endpoint, usually `/api/me` right after signing in.

### Tests

Expand Down
Binary file modified docs/database/images/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/database/images/users-roles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 38 additions & 2 deletions docs/database/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Matches the migrations 20261103000127_CreateUsersAndRoles, 20261105163138_CreateDonorsAndFoodItems,
// 20261107041912_CreateReceipts, 20261110175320_CreateCatalogItemsAndPrices,
// 20261112060008_CreateQualityClasses, 20261117040905_AddCatalogItemDetails,
// 20261120031640_MakeCatalogItemQualityClassOptional, 20261124214039_AddBcItemMappings, 20261127052039_AddReceiptStatus
// 20261127053347_AddReceiptNumbers, 20261128005314_AddDonorProfile and 20261128222105_AddDonorListSearch.
// 20261120031640_MakeCatalogItemQualityClassOptional, 20261124214039_AddBcItemMappings, 20261127052039_AddReceiptStatus,
// 20261127053347_AddReceiptNumbers, 20261128005314_AddDonorProfile, 20261128222105_AddDonorListSearch
// and 20261204061822_AddUserAccountChanges.

Project moisson_montreal {
database_type: 'PostgreSQL'
Expand Down Expand Up @@ -49,6 +50,32 @@ Table users {
Note: 'Employees allowed to use the app'
}

Table user_account_changes {
id uuid [not null, default: `gen_random_uuid()`]
user_id integer [not null, note: 'Account that was changed']
action varchar(20) [not null, note: 'Created, Updated, RoleChanged, Deactivated or Reactivated']
old_email varchar(256) [null, note: 'Empty when the action did not touch the email']
new_email varchar(256) [null]
old_display_name varchar(200) [null, note: 'Empty when the action did not touch the name']
new_display_name varchar(200) [null]
old_role_id integer [null, note: 'Empty when the action did not touch the role']
new_role_id integer [null]
old_is_active boolean [null, note: 'Empty when the action did not touch the access']
new_is_active boolean [null]
changed_by_user_id integer [not null, note: 'Administrator who made the change']
changed_at timestamptz [not null, default: `now()`, note: 'Stored in UTC']

indexes {
id [pk, name: 'pk_user_account_changes']
user_id [name: 'ix_user_account_changes_user_id']
changed_by_user_id [name: 'ix_user_account_changes_changed_by_user_id']
old_role_id [name: 'ix_user_account_changes_old_role_id']
new_role_id [name: 'ix_user_account_changes_new_role_id']
}

Note: 'History of every change made to an account, with the before and after values. Only the values the action touched are filled'
}

Table donors {
id uuid [not null, default: `gen_random_uuid()`]
name varchar(255) [not null, note: 'Full name of a person, or name of an organization']
Expand Down Expand Up @@ -316,6 +343,14 @@ Table bc_item_mapping_changes {

Ref fk_users_roles_role_id: users.role_id > roles.id [delete: restrict]

Ref fk_user_account_changes_users_user_id: user_account_changes.user_id > users.id [delete: restrict]

Ref fk_user_account_changes_users_changed_by_user_id: user_account_changes.changed_by_user_id > users.id [delete: restrict]

Ref fk_user_account_changes_roles_old_role_id: user_account_changes.old_role_id > roles.id [delete: restrict]

Ref fk_user_account_changes_roles_new_role_id: user_account_changes.new_role_id > roles.id [delete: restrict]

Ref fk_food_items_donors_donor_id: food_items.donor_id > donors.id [delete: restrict]

Ref fk_food_items_catalog_items_catalog_item_id: food_items.catalog_item_id > catalog_items.id [delete: restrict]
Expand Down Expand Up @@ -349,6 +384,7 @@ Ref fk_bc_item_mapping_changes_users_changed_by_user_id: bc_item_mapping_changes
TableGroup accounts {
roles
users
user_account_changes
}

TableGroup donations {
Expand Down
9 changes: 8 additions & 1 deletion docs/database/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The database has four parts:

| Part | Tables | What it holds |
| --- | --- | --- |
| Accounts | roles, users | Who can use the app and what each person is allowed to do |
| Accounts | roles, users, user_account_changes | Who can use the app, what each person is allowed to do, and every change made to their access |
| Donations | donors, food_items, quality_classes | Who gave food, what they gave, when, and in what condition |
| Tax receipts | receipts, receipt_items, receipt_status_changes, receipt_number_counters | The receipts given to donors, the value of each food item on them, every change of their status, and their numbers |
| Price grid | catalog_items, prices, bc_item_mappings, bc_item_mapping_changes | The types of food the app knows, the price of each one over time, and which Business Central item each one values |
Expand Down Expand Up @@ -35,6 +35,12 @@ One row per employee who has access to the app: their work email, their name, th

More detail: [Users and roles](users-roles.md).

### user_account_changes

The history of every change an administrator made to an account: added, name or email edited, role changed, access removed or given back. Each row keeps who made the change, when, and the values before and after it.

More detail: [Users and roles](users-roles.md).

### donors

The organizations and people who give food: name, type (individual or organization), street address, city, province, postal code, contact person, email, phone, notes and Business Central code. The name and the address are printed on the tax receipt. Two donors can have the same name, for example two branches of the same grocery chain, but two donors cannot share a Business Central code. A donor can be deactivated, which hides it from the donor list. Each donor also keeps its name without accents and in lowercase, which the search by name reads.
Expand Down Expand Up @@ -223,6 +229,7 @@ Each table comes from one of these migrations:
| receipt number, receipt_number_counters | [20261127053347_AddReceiptNumbers](../../src/MoissonMontreal.Api/Data/Migrations/20261127053347_AddReceiptNumbers.cs) | [0010_add_receipt_numbers.sql](sql/0010_add_receipt_numbers.sql) | [receipts.md](receipts.md) |
| donor profile | [20261128005314_AddDonorProfile](../../src/MoissonMontreal.Api/Data/Migrations/20261128005314_AddDonorProfile.cs) | [0011_add_donor_profile.sql](sql/0011_add_donor_profile.sql) | [donors.md](donors.md) |
| donor list and search | [20261128222105_AddDonorListSearch](../../src/MoissonMontreal.Api/Data/Migrations/20261128222105_AddDonorListSearch.cs) | [0012_add_donor_list_search.sql](sql/0012_add_donor_list_search.sql) | [donors.md](donors.md) |
| user_account_changes | [20261204061822_AddUserAccountChanges](../../src/MoissonMontreal.Api/Data/Migrations/20261204061822_AddUserAccountChanges.cs) | [0013_add_user_account_changes.sql](sql/0013_add_user_account_changes.sql) | [users-roles.md](users-roles.md) |

The SQL scripts must be run in order on an empty database.

Expand Down
35 changes: 35 additions & 0 deletions docs/database/sql/0013_add_user_account_changes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
START TRANSACTION;
CREATE TABLE user_account_changes (
id uuid NOT NULL DEFAULT (gen_random_uuid()),
user_id integer NOT NULL,
action character varying(20) NOT NULL,
old_email character varying(256),
new_email character varying(256),
old_display_name character varying(200),
new_display_name character varying(200),
old_role_id integer,
new_role_id integer,
old_is_active boolean,
new_is_active boolean,
changed_by_user_id integer NOT NULL,
changed_at timestamp with time zone NOT NULL DEFAULT (now()),
CONSTRAINT pk_user_account_changes PRIMARY KEY (id),
CONSTRAINT fk_user_account_changes_roles_new_role_id FOREIGN KEY (new_role_id) REFERENCES roles (id) ON DELETE RESTRICT,
CONSTRAINT fk_user_account_changes_roles_old_role_id FOREIGN KEY (old_role_id) REFERENCES roles (id) ON DELETE RESTRICT,
CONSTRAINT fk_user_account_changes_users_changed_by_user_id FOREIGN KEY (changed_by_user_id) REFERENCES users (id) ON DELETE RESTRICT,
CONSTRAINT fk_user_account_changes_users_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE RESTRICT
);

CREATE INDEX ix_user_account_changes_changed_by_user_id ON user_account_changes (changed_by_user_id);

CREATE INDEX ix_user_account_changes_new_role_id ON user_account_changes (new_role_id);

CREATE INDEX ix_user_account_changes_old_role_id ON user_account_changes (old_role_id);

CREATE INDEX ix_user_account_changes_user_id ON user_account_changes (user_id);

INSERT INTO "__EFMigrationsHistory" (migration_id, product_version)
VALUES ('20261204061822_AddUserAccountChanges', '10.0.13');

COMMIT;

36 changes: 35 additions & 1 deletion docs/database/users-roles.dbml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Users and roles
// Paste this file into https://dbdiagram.io to render the diagram.
// Matches the migration 20261103000127_CreateUsersAndRoles.
// Matches the migrations 20261103000127_CreateUsersAndRoles and 20261204061822_AddUserAccountChanges.

Table roles {
id integer [not null, note: 'Fixed values, inserted by the migration']
Expand Down Expand Up @@ -40,8 +40,42 @@ Table users {
Note: 'Employees allowed to use the app'
}

Table user_account_changes {
id uuid [not null, default: `gen_random_uuid()`]
user_id integer [not null, note: 'Account that was changed']
action varchar(20) [not null, note: 'Created, Updated, RoleChanged, Deactivated or Reactivated']
old_email varchar(256) [null, note: 'Empty when the action did not touch the email']
new_email varchar(256) [null]
old_display_name varchar(200) [null, note: 'Empty when the action did not touch the name']
new_display_name varchar(200) [null]
old_role_id integer [null, note: 'Empty when the action did not touch the role']
new_role_id integer [null]
old_is_active boolean [null, note: 'Empty when the action did not touch the access']
new_is_active boolean [null]
changed_by_user_id integer [not null, note: 'Administrator who made the change']
changed_at timestamptz [not null, default: `now()`, note: 'Stored in UTC']

indexes {
id [pk, name: 'pk_user_account_changes']
user_id [name: 'ix_user_account_changes_user_id']
changed_by_user_id [name: 'ix_user_account_changes_changed_by_user_id']
old_role_id [name: 'ix_user_account_changes_old_role_id']
new_role_id [name: 'ix_user_account_changes_new_role_id']
}

Note: 'History of every change made to an account, with the before and after values. Only the values the action touched are filled'
}

Ref fk_users_roles_role_id: users.role_id > roles.id [delete: restrict]

Ref fk_user_account_changes_users_user_id: user_account_changes.user_id > users.id [delete: restrict]

Ref fk_user_account_changes_users_changed_by_user_id: user_account_changes.changed_by_user_id > users.id [delete: restrict]

Ref fk_user_account_changes_roles_old_role_id: user_account_changes.old_role_id > roles.id [delete: restrict]

Ref fk_user_account_changes_roles_new_role_id: user_account_changes.new_role_id > roles.id [delete: restrict]

Records roles(id, code, name) {
1, 'Supply', 'Supply'
2, 'SupplyDirector', 'Supply Director'
Expand Down
57 changes: 52 additions & 5 deletions docs/database/users-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,33 @@ One row per employee who has been given access to the app.
| --- | --- |
| id | Number that identifies the user, assigned automatically |
| email | Microsoft work email of the employee. Two users cannot share the same email. |
| entra_object_id | Identifier of the employee's Microsoft account. It stays empty until the employee's first action that requires a permission. |
| entra_object_id | Identifier of the employee's Microsoft account. It stays empty until the employee signs in for the first time. |
| display_name | Name shown in the app |
| role_id | The role of the employee (see the roles table) |
| is_active | Yes while the employee has access. Set to no when the admin removes access. |
| created_at | Date and time the access was given |
| updated_at | Date and time of the last change, empty if never changed |

The table also uses PostgreSQL's hidden `xmin` column as a version number: it changes on every update, so if two administrators edit the same account at the same time, the second save is refused instead of silently overwriting the first one. No column is added for this.

### user_account_changes

One row per change an administrator made to an account. Only the values the change touched are filled: for example, removing access only fills the two active columns.

| Column | Meaning |
| --- | --- |
| id | Identifier of the change, assigned automatically |
| user_id | The account that was changed |
| action | Created, Updated (name or email), RoleChanged, Deactivated or Reactivated |
| old_email / new_email | Email before and after the change |
| old_display_name / new_display_name | Name before and after the change |
| old_role_id / new_role_id | Role before and after the change (see the roles table) |
| old_is_active / new_is_active | Access before and after the change |
| changed_by_user_id | The administrator who made the change |
| changed_at | Date and time of the change |

The history keeps old emails and names, including those of employees who left, so it is always known who did what.

## How the tables are linked

Each user has exactly one role. A role can be given to many users. For example, several people in Finance all share the Finance role.
Expand All @@ -57,13 +77,28 @@ Passwords are not stored in the app. Employees sign in with their Microsoft work

## Life of an account

1. The admin adds the employee with their work email, name and role.
2. The first time an employee's action requires a permission, the app links the employee's Microsoft account to that row.
3. The admin can change the role at any time.
4. When the admin removes access, the account is deactivated rather than deleted, so the history of who did what is kept.
1. The admin adds the employee with their work email, name and role, from the Accounts screen.
2. The first time the employee signs in, the app links their Microsoft account to that row.
3. The admin can edit the name and email, and change the role at any time. A role change takes effect on the employee's next action.
4. When the admin removes access, the account is deactivated rather than deleted, so the history of who did what is kept. The employee is refused from their next action and sees the same message as someone who was never added. The admin can give the access back later.

Every change is written to user_account_changes with the admin who made it.

Two rules protect the app from being left without an administrator:
- An admin cannot change their own role or remove their own access.
- The last active admin cannot lose the role or be deactivated. An admin who has never signed in still counts as active.

The very first Admin account cannot be added from the app, since no admin exists yet. It is inserted once directly in the database during the setup on Azure.

### Handing over the administrator role

When an administrator leaves, for example at the end of the project:
1. The leaving admin gives the Admin role to the person who takes over.
2. The new admin signs in once, so their account shows as linked. This confirms the email is right before the old admin loses access.
3. The new admin removes the access of the old admin.

The app cannot prevent the only admin from becoming unreachable, for example if they leave Moisson Montréal and IT closes their Microsoft account before they hand over. Their account still counts as an admin in the app, but nobody can sign in with it. The role then has to be given to someone else directly in the database, as for the very first admin. Keeping at least two active admins avoids this.

## Migration

| Item | Location |
Expand All @@ -73,3 +108,15 @@ The very first Admin account cannot be added from the app, since no admin exists
| Table settings | [UserConfiguration.cs](../../src/MoissonMontreal.Api/Data/Configurations/UserConfiguration.cs) and [RoleConfiguration.cs](../../src/MoissonMontreal.Api/Data/Configurations/RoleConfiguration.cs) |

The SQL script is generated from the migration with `dotnet ef migrations script 0 CreateUsersAndRoles --project src/MoissonMontreal.Api --output docs/database/sql/0001_create_users_and_roles.sql`. It can be run on an empty Azure Database for PostgreSQL server and records itself in the migration history, so Entity Framework will not apply it a second time.

### Account history

| Item | Location |
| --- | --- |
| Entity Framework migration | [20261204061822_AddUserAccountChanges](../../src/MoissonMontreal.Api/Data/Migrations/20261204061822_AddUserAccountChanges.cs) |
| Equivalent SQL script | [0013_add_user_account_changes.sql](sql/0013_add_user_account_changes.sql) |
| Table settings | [UserAccountChangeConfiguration.cs](../../src/MoissonMontreal.Api/Data/Configurations/UserAccountChangeConfiguration.cs) |

This migration adds the user_account_changes table. The version of users uses the system column `xmin`, so the SQL script adds no column to users.

The SQL script is generated from the migration with `dotnet ef migrations script AddDonorListSearch AddUserAccountChanges --project src/MoissonMontreal.Api --output docs/database/sql/0013_add_user_account_changes.sql`, and the byte order mark (BOM) at the start of the generated file is removed. It must be run after [0012_add_donor_list_search.sql](sql/0012_add_donor_list_search.sql).
Loading
Loading