Paozhu logo Paozhu Docs
Documentation
DEV

RBAC

Login sessions, route masks, grant snapshots and fail-closed checks.

Understand the split

The framework provides the route_auth_fn hook contract and the :maskcode annotation. Tables, models, admin pages and grant loading are application-side code, with a complete reference implementation in the repo. The hook blocks; the tables decide who passes.

Tables

TableRole
sysuserUsers; password is currently MD5, isopen enables the account
syspermsPermission catalogue: urlpath + maskcode, tenant-scoped
sysrole / sysrolepermsRoles and role-to-permission bindings
sysuserroleUser-to-role bindings
syscompanypermsPlatform-assigned permissions; the ceiling for role grants
logininfoLogin audit log

Session key contract

KeyActual meaning
adminidsysuser.adminid; 0 means not logged in
useridActually companyid (tenant ID), not the user ID
isadminTenant administrator flag
superidSuper administrator ID
csrf_tokenCSRF token

Add a mask and install the hook

controller/src/my_admin.cppcpp
//@urlpath(null,hello)
// mask_code = 0: the RBAC hook is never called

//@urlpath(admin_islogin,admin/article/list):maskcode(64):title("Article list")
std::string admin_article_list(std::shared_ptr<httppeer> peer)
{
    httppeer &client = peer->get_peer();
    client.output = "ok";
    return "";
}
startup/main_dev.cppcpp
#include "rbac/route_guard.h"

int main(int argc, char **argv) {
    // Install before run(). Without the hook, masked routes fail closed with 403.
    http::rbac::install_route_guard();
    httpmy.run(argv_str);
}

The pre-filter still performs the login check: @ok continues, a view name or @exit stops the chain. The auth hook runs before it.

libs/rbac/route_guard.cppcpp
unsigned int check_masked_route(httppeer &client,
                                 const std::string &urlpath,
                                 unsigned int mask_code)
{
    unsigned int adminid   = client.session["adminid"].to_int();
    unsigned int companyid = client.session["userid"].to_int();
    if (adminid == 0)   return 401;
    if (companyid == 0) return 403;
    return get_route_cache().allow(adminid, companyid, urlpath, mask_code) ? 0 : 403;
}

Access is granted when the route urlpath exists in the grant snapshot and one granted maskcode is exactly equal to the route mask. There is no bitmask containment and no role inheritance.

Grant loading flow

  1. Register the catalogue

    /superadmin/registerfunc collects routes with mask_code != 0 and writes them to sysperms.

  2. Assign to a tenant

    /superadmin/rbac/companyperm replaces syscompanyperms, the ceiling for that tenant.

  3. Create roles and bind grants

    Use /admin/rbac/role/* and roleperm/bind. Bindings are narrowed by the platform-assigned set.

  4. Bind users to roles

    /admin/rbac/userrole/save calls bind_user_role / unbind_user_role.

  5. Log in and access

    Login writes the session keys. Requests to masked routes hit the hook and are allowed or get 401 / 403.

  6. Invalidate on change

    Call invalidate_all() or invalidate_user() after changes; snapshots also have a 60-second TTL fallback.

Programmatic checks

controller/src/rbac/check.cppcpp
//@urlpath(admin_isloginjson,admin/rbac/check)
std::string check(std::shared_ptr<httppeer> peer) {
    httppeer &client = peer->get_peer();
    bool allowed = urlpath.empty()
        ? rbac::user_has_permscode(adminid, companyid, permscode)
        : rbac::user_has_url(adminid, companyid, urlpath);
    // ... return JSON
}

route_cache is an in-process snapshot keyed by (adminid, companyid), with 16 shards and singleflight. In multi-instance deployments invalidation is not broadcast; cross-instance convergence relies on the 60-second TTL.

Known boundaries