Capabilities: naming an admin check
Status: implemented. You can declare a capability, map it to groups (the ir_capability table) and check
it with Transaction::has_capability. The design is in ADR 0035 decision 7 and
docs/design/m3-access-engine.md §2.1.
Some checks are not about records but about who may do an admin action: switching to another
user, explaining another user’s access, approving above a limit. Odoo writes these as hard-coded
group checks (user.has_group('base.group_system')). In ERPForAll you give the check a name,
a capability. A database then decides which groups hold it, and you can change that later without
touching code (fine-grained admin roles are planned for M7).
Declaring a capability
Section titled “Declaring a capability”Declare a capability once, at the top level of your addon crate, with erp_sdk::capability!:
erp_sdk::capability!( /// Approve sale orders above the company's approval limit. pub APPROVE_ABOVE_LIMIT = "my_sale.approve_above_limit");
fn main() { assert_eq!(APPROVE_ABOVE_LIMIT.name(), "my_sale.approve_above_limit");}- The name has dot-separated parts made of lowercase ASCII letters, digits and
_, and is at most 64 bytes long. Start it with your addon’s name, so two addons never pick the same name. - The macro makes a
staticof typeerp_sdk::Capabilityand registers it at link time. There is no other way to make aCapabilityvalue, so a check can only name a declared capability. - The server checks every linked capability when it builds the ORM of a database. A malformed name,
or the same name declared twice (in your addon or in another one), stops it with an error that
names both declarations:
capability 'my_sale.approve_above_limit' is declared twice (in erp_addon_my_sale and in erp_addon_other).
Mapping and checking
Section titled “Mapping and checking”The ir.capability model has one row per capability: its name and a groups text in the same
syntax as a field’s groups (field-access.md):
base.group_system, a.x,b.y (any of them), a.x,!b.y. base ships rows for its own capabilities,
for example principal.impersonate; an addon ships its rows in its data files.
principal.impersonate is the one the engine asks itself: a with_user switch to another user by a
principal that is not System is admitted only when that principal’s own user holds it (ADR 0164 A4,
SPEC-PRIN-029; see models.md, environments). erp-security declares it
(erp_security::principal::PRINCIPAL_IMPERSONATE) and the binary names it to the ORM with
OrmBuilder::impersonate_capability; an ORM built without it refuses every such switch.
# erp_sdk::addon!(name = "capability_guide");use erp_sdk::prelude::*;
erp_sdk::capability!(pub APPROVE = "capability_guide.approve");
async fn approve(tx: &mut Transaction) -> Result<()> { let env = tx.env(); if !tx.has_capability(&env, &APPROVE).await? { return Err(Error::access("You may not approve.")); } Ok(())}# fn main() {}- The answer is about the user the environment is for. Superuser mode (
sudo()) and elevation blocks never grant a capability. - It costs no statement for the transaction’s own user.
- A capability linked into the server but missing its row is an error when the policy is built (the
build report lists it), and so is checking it: a check never quietly answers “no”. The same for a
row whose group text names a group that does not exist: the check fails with an
AccessErrorthat names the text, and the report lists it. - A row naming no linked capability (an uninstalled addon’s) grants nothing and is reported as a warning.