QGIS viewer user context (expression variables)¶
The map viewer loads authenticated user metadata before the form and expression runtimes initialize. This enables QGIS-style expressions that reference the signed-in user.
API¶
GET /api/auth/context (session cookie, credentials: include)
Authenticated:
{
"authenticated": true,
"user": {
"id": "42",
"username": "alice",
"email": "alice@example.com",
"roles": ["editor", "viewer"],
"groups": ["Editors", "field-team"],
"attributes": {
"district_id": 3,
"tenant_id": "abc"
}
}
}
Anonymous (no session or central auth disabled):
User attributes (optional)¶
To expose custom keys such as district_id in expressions, add a JSONB column on the central auth database:
ALTER TABLE dashboard_users
ADD COLUMN IF NOT EXISTS attributes JSONB NOT NULL DEFAULT '{}'::jsonb;
UPDATE dashboard_users
SET attributes = '{"district_id": 3, "tenant_id": "abc"}'::jsonb
WHERE username = 'alice';
If the column does not exist, attributes is always {}.
Viewer bootstrap¶
js/qgis-user-context.js runs on DOMContentLoaded before fetchProject(). It sets:
window.__qgisCurrentUser— user object ornull
Debug logging:
Expression debug snapshot (updated when auth context loads):
Standalone variables return values, not booleans:
evaluateQgisExpression('@user'); // { ok: true, value: "admin" }
evaluateQgisExpression('@district_id'); // { ok: true, value: null }
evaluateQgisExpression('current_user()'); // { ok: true, value: "admin" }
Comparisons still evaluate to boolean (owner = @user → true / false).
Expression variables¶
Evaluated in js/qgis-expr-runtime.js (visibility, editable, required, defaults, constraints):
| Token | Resolves to |
|---|---|
current_user() |
Signed-in username (empty when anonymous) |
@user, @username |
Username |
@roles |
Array of role slugs (e.g. ["admin","editor","viewer"]) |
@groups |
Array of group names |
@district_id, @tenant_id, … |
Matching key from user.attributes |
Example expressions¶
Use in QGIS field visibility, editable, or constraint expressions (published via forms manifest):
Manual acceptance check¶
- Sign in to Central / dashboard so the session cookie is present.
- Open
viewer.html?id=<project>for a layer with: - Visibility:
owner = @useron a field users should only see for their rows. - Editable:
district_id = @district_idon a field that should lock when district differs. - Set
window.__qgisDebugUserContext = trueand reload — confirm[qgis-user-context] loadedand username in the debug payload. - In the browser console:
resolveQgisVariable('user');
resolveQgisVariable('district_id');
evaluateQgisExpression('owner = @user', function (n) { return n === 'owner' ? 'alice' : undefined; });
- Edit a feature and confirm visibility/editable rules react when attribute values change.