Access Services Developer Guide - edmozley/freeitsm GitHub Wiki
Access services - Developer Guide
How access services work inside (GH #505, stage 1 - 5.19.0). The design and its decisions: Access-Services-Design.
The files
| File | What it does |
|---|---|
includes/services/access_services.php |
AccessServicesService - the rules, once: services and their groups, grant / remove, running each change, settling a grant's state, retry / done / cancel, the task hook, the directory comparison, telling people about failures |
includes/access_entra.php |
Graph writes: the write app's config, accessEntraChange() (add / remove one person), accessEntraWriteTest() (reads the token's roles) |
api/forms/access_services.php |
The screen's endpoint: options (any Forms user - the editor's list), list, people, members, and the POST actions. Everything but options needs Cap::FORMS_ACCESS_SERVICES |
api/system/test_entra_write.php |
Test on the Directory writes tab - admin only, changes nothing |
assets/js/forms-access-services.js, assets/css/forms-access-services.css |
The tab, the editor, the Members and Remove windows. Classes are asv-* - the approval-step editor on the same page already owns as-* |
forms/help/access-services.php |
The in-app help topic |
tests/access-services.php |
46 checks, service layer and end to end (submit -> owner approves on the portal -> grant) |
Touched: includes/services/forms.php (save, createVersion, submit), includes/catalogue_approvals.php (grant after an approval), includes/catalogue_approval_steps.php + catalogue_approval_people.php (the svc_owner step), includes/entra_sync.php (group flags, the comparison), includes/services/tasks.php (the task hook), includes/watchtower_queries.php + watchtower/index.php, system/sso/entra.php + api/system/save_entra_sync.php (Directory writes tab), includes/services/notifications.php + includes/entity_links.php (the bell).
The tables
access_services -- id, tenant_id (NOT NULL: one company's), name, description,
-- owner_user_id | owner_group_id, deputy_user_id,
-- removal_mode none|owner|form, removal_form_id, min_members,
-- owner_change_mode immediate|manager (read by stage 3)
-- grant_without_approval: an item with NO approval gives it at once (default 0)
access_service_groups -- service_id, directory_group_id (NULL only for a by-hand label),
-- method entra|manual (ldap|workflow in stage 2), label, manual_team_id
access_grants -- one row per person per SPELL of access; removed rows are history
-- state adding|active|removing|removed|failed
-- source request|analyst|owner|directory, submission_id, *_by_*, removal_source,
-- removed_reason, expires_at (empty until time-limited access)
access_changes -- the work queue AND the log: one row per group per add/remove
-- state pending|waiting|done|failed|cancelled, method, task_id, attempts,
-- http_status, server_response (the directory's own words), message
New columns: forms.access_service_id (the item gives it), form_submissions.access_service_id (snapshot at submit, like the price), directory_groups.on_prem_synced / role_assignable, auth_providers.graph_write_enabled / graph_write_client_id / graph_write_client_secret.
🔴 TRAP:
forms.access_service_idmust be carried bycreateVersion()-AccessServicesService::copyForForm()is called besidecatalogueRoutingCopy(). Left out, pressing Save on the item would quietly stop it giving access.tests/access-services.phpchecks it.
Every reader is guarded for an install that has not run Database Verification: ready(), formsReady(), groupFlagsReady(), accessEntraWriteColumnsReadyLight(). Proven by dropping the tables and columns in a test database and loading every touched screen.
A grant, end to end
// includes/catalogue_approvals.php - catalogueAfterDecision(), AFTER the commit
if ($decision === 'approved') {
require_once __DIR__ . '/services/access_services.php';
AccessServicesService::afterApproval($conn, $sub); // never throws
}
grant() writes the grant (adding) and one pending change per group in a transaction, commits, then runs each change with runChange() outside it - a slow or failing directory can never undo the decision. runChange() never throws: anything that goes wrong becomes a failed change with a message.
// runChange(), by the group's method
'entra' -> accessEntraChange($conn, $directoryGroupRow, $userId, 'add'|'remove')
// ok -> mirrorMembership(): directory_group_members updated now, not at the next import
'manual' -> startTask(): TasksService::saveTask() as a system actor, assigned to the group's team;
// the change goes to 'waiting' with task_id
settleGrant() works out the grant's state from its latest action's changes: all done -> active / removed; any failed -> failed; else adding / removing. A re-grant of a failed or half-removed spell reuses the row (its old changes are cancelled), so there is only ever one live grant per person per service.
The task hook
TasksService::completedDispatch() (every open -> closed move, by save, drag or API) calls AccessServicesService::taskStatusChanged(). Closed in the status one-click Complete uses (completeStatusId() - "Done") = the change was made; closed in any other closed status ("Cancelled") = a failed change, never left waiting.
Entra
// includes/access_entra.php
POST /groups/{gid}/members/$ref {"@odata.id": ".../directoryObjects/{oid}"}
DELETE /groups/{gid}/members/{oid}/$ref
- 🔴 The object id is
user_sso_identities.source_reffor the group's provider - neversubject(the identity trap). A person with nosource_reffails with "has not been imported from the connection". - Refused before any call: writes off, no write app, a synced group, a role-assignable group.
- "Already a member" (400) and "not a member" (404 that does not name the group) are success. A 404 naming the group means the group has gone - a failure.
- 429 / 5xx retried with Retry-After, as
entraGet()does. graph_write_*is a separate app on purpose;save_entra_sync.phprefuses the sign-in or import client id there.
The owner step (svc_owner)
A third people-kind beside manager and cc_owner. catalogueStepPeople() reads the service from the request's snapshot (form_submissions.access_service_id) and hands owner / group / deputy to catalogueOwnerPeople() - the cost-centre owner rules, now shared: never the requester, the people group's current members, the deputy as an equal or only as cover. No service -> reason no_service -> the step's fallback, as for any step that finds nobody. The portal list adds step_service so the card says "as the owner of {service}".
After an import
entraSyncRun() (live runs only) calls AccessServicesService::reconcileProvider() after the groups. For each service following an active imported group:
- people in every followed group with no live grant -> a grant with
source = 'directory',active; - an
activegrant whose person is not in every followed group ->removed,removal_source = 'directory'.
Grants still adding / removing are left alone - the import may predate the change. A retired service records no newcomers. FreeITSM never removes anybody from a group because a comparison said so.
Failures are never silent
finishChange() with failed calls tellFailure(): the bell type access.change_failed (entity access_service, opening forms/settings/?tab=access_services&service=ID) to every analyst holding Cap::FORMS_ACCESS_SERVICES and to the analyst who triggered it. failedRecently() feeds Watchtower's Workflows card (access_failed, red).
Stage 2 and 3 hooks
- LDAP / Workflow: add the method to
METHODS, a branch inrunChange(), and the editor's method list. LDAP also needs the group import (design section 3). - Portal pages:
removal_mode,removal_form_id,min_members,owner_change_modeare stored and validated already;remove()takessource = 'owner' | 'request'andby_user_id.