Field access: hiding and protecting single fields
Status: implemented. The engine checks field access on every ORM path, and the policy erp-security
compiles from the group tables decides it: which groups a user is in, and what groups='…' means
for them. In tests the testkit’s FieldPolicy can stand in for the tables. The design is in ADR 0035
and docs/design/m3-access-engine.md §2.2 and §3.3.
Every Rust example in this guide is compiled and run as a doctest of erp-test-models.
Declaring who may use a field
Section titled “Declaring who may use a field”Put groups = "…" on the field, as in Odoo:
# erp_sdk::addon!(name = "field_guide");use erp_sdk::prelude::*;
#[model(name = "field_guide.employee", description = "Employee")]pub struct Employee { #[field(char)] pub name: Option<Arc<str>>, /// Only members of the HR managers group (a comma-separated list means "any of them"). #[field(integer, groups = "hr.group_hr_manager")] pub salary: i32, /// `.` is Odoo's "nobody": no user, whatever the groups; only superuser mode reaches it. #[field(integer, groups = ".")] pub legacy_code: i32,}# fn main() {}The group names are external ids (module.name). A field without groups is open to everyone who
may use the model.
What a groups text means
Section titled “What a groups text means”| Text | Who may use the field |
|---|---|
a.x,b.y | members of a.x or of b.y (implied groups count: a member of a group that implies a.x is in a.x) |
a.x,!b.y | members of a.x who are not in b.y |
!b.y | everyone who is not in b.y |
a.x,!a.x | nobody (the negation is checked first) |
. | nobody, in any group; only superuser mode reaches the field |
base.group_no_one | members of the technical-features group, in debug mode only (in either position) |
The text is parsed once when the policy is built, against the groups of the database. An external id
that names no group (a typo, an uninstalled addon) is an error in the build report
(PolicySnapshot::report, erp access report) and closes the field to everyone outside
superuser mode: it is never skipped, so a typo cannot leave a field open. With the technical group,
the denial error in debug mode names the broken text.
Custom fields (x_…, created in the database) take their groups from the field’s group list
(ir.model.fields), and a list with several groups means “any of them”. A custom field is also
subject to a registry groups of the same name; both must hold.
Asking about groups yourself
Section titled “Asking about groups yourself”Transaction::has_groups answers env.user.has_groups(spec) with the same texts, from the groups the
transaction already resolved (no statement for the transaction’s own user):
# erp_sdk::addon!(name = "field_guide_groups");use erp_sdk::prelude::*;
async fn can_see_salaries(tx: &mut Transaction) -> Result<bool> { let env = tx.env(); tx.has_groups(&env, "hr.group_hr_manager,!base.group_portal").await}# fn main() {}On a user record, res.users.has_group(xmlid) asks about that user. A user who is not internal gets
an AccessError when asking about someone else; internal users and superuser mode may.
A related field that declares no groups takes the groups of the field it points to, along a
chain of related fields; one that declares its own keeps them (SPEC-SEC-050). A field an
_inherits child takes over from its parent carries the parent field’s groups.
A related path that does not resolve, or a cycle of related fields, stops the registry build with a
Programming error naming model.field and the path (it would otherwise leave the field open).
What the engine checks
Section titled “What the engine checks”Reading and writing are checked separately, field by field, with AccessError (kind Access):
| Operation | What is checked |
|---|---|
read, fetch, search_fetch, attribute access | each requested field; read() without names returns only the readable ones |
write | every key of the values, before any value is compared, so writing a restricted field back with its stored value is refused too (SPEC-SEC-051) |
create | every given key and every default_<field> context key, before any INSERT |
| x2many commands | the restricted relational field itself; for UPDATE commands also the updated fields of the comodel |
search, search_count, read_group | the fields of the domain (direct, through a path, in an any), of the order (any position) and of the group-by and aggregates (SPEC-SEC-024) |
fields_get | unreadable fields are left out, unwritable ones are readonly (SPEC-SEC-021) |
default_get | on an _inherits child, fields the user may not write get no delegated default (SPEC-CRUD-007) |
The checks cost no statement. Superuser mode (sudo()) bypasses field groups, as in Odoo; the
engine decides that in one place, so your code never tests it.
A denied operation reads:
You do not have enough rights to access the field "salary" on Employee (field_guide.employee).Please contact your system administrator.
Operation: writeIn debug mode, members of base.group_no_one also see a User: and a Groups: line, as in Odoo:
User: 7Groups: allowed for groups 'HR / Manager', 'Role / Portal'The groups are listed by id with their full names (privilege / name, the name alone without a
privilege); a negated group shows as '!Role / Portal'. Groups: always forbidden is the . text,
and Groups: custom field access rules a field without groups that an override of the model denied.
Narrowing a model’s field access with code
Section titled “Narrowing a model’s field access with code”Odoo lets a model override _has_field_access. Here you write a #[field_access] function in a
#[methods] block. It receives the mask the policy built (and the overrides of earlier addons),
calls sup.call(…) for the rest of the chain, and may only narrow the result: setting a bit
that was not set is a Programming error that names the override and the field, so an addon cannot
open what the groups closed.
# erp_sdk::addon!(name = "field_guide_override");use erp_sdk::prelude::*;
#[model(name = "field_guide_override.task", description = "Task")]pub struct Task { #[field(char)] pub name: Option<Arc<str>>, #[field(integer)] pub priority: i32,}
#[methods(Task)]impl Task { /// Only members of `base.group_system` write the priority. Membership is what the policy /// knows about the caller, so this is a real restriction. #[field_access] fn priority_for_administrators( ctx: &FieldAccessCtx<'_>, op: FieldOp, mask: FieldMask, sup: FieldAccessSuper, ) -> Result<FieldMask> { let mut mask = sup.call(ctx, op, mask)?; if op == FieldOp::Write && !ctx.has_group("base.group_system")? { mask.deny(ctx.field("priority")?, FieldOp::Write)?; } Ok(mask) }}# fn main() {}- The function is synchronous and sees no uid, no companies and no transaction: only the model,
the kind of principal,
ctx.has_group("module.name")and the context keys it declared incontext = [..]. Reading another key is aProgrammingerror. This keeps the result a function of what the access cache is keyed by. - A context key is never a security boundary. The caller controls the context: an RPC client
sends whatever
contextit likes, and leaves a key out just as easily. Declare a key (#[field_access(context = ["compact_view"])]) only to narrow what a caller asked for, for example to leave a heavy field out of a list the client requested in compact form. Do not write “deny the field when the key is set” to protect data (a caller just leaves the key out), and note that an override cannot widen on a key either. A rule that must hold for everyone is a group rule:groupson the field, orctx.has_group(..)in the override, as in the example above. - The chain runs once for each of the two operations: the
Readrun decides the readable fields, theWriterun the writable ones. Branch onopand narrow that operation’s set. - An addon has one such function per model. The chain is ordered like method overrides: newest addon first, the policy’s mask at the bottom.
Testing
Section titled “Testing”In tests install erp_testkit::FieldPolicy:
let policy = FieldPolicy::new() .enforce_groups() // `groups` text decides, by membership .member(demo_uid, ["base.group_user"]) // the groups the test user is in .deny("field_guide.employee", "salary", FieldOp::Write);let h = Harness::builder([&base_users::ADDON, &my_addon::ADDON]) .policy(policy.clone()) .build() .await?;policy.join(demo_uid, "hr.group_hr_manager"); // join a group mid-test