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
| Table | Role |
|---|---|
sysuser | Users; password is currently MD5, isopen enables the account |
sysperms | Permission catalogue: urlpath + maskcode, tenant-scoped |
sysrole / sysroleperms | Roles and role-to-permission bindings |
sysuserrole | User-to-role bindings |
syscompanyperms | Platform-assigned permissions; the ceiling for role grants |
logininfo | Login audit log |
Session key contract
| Key | Actual meaning |
|---|---|
adminid | sysuser.adminid; 0 means not logged in |
userid | Actually companyid (tenant ID), not the user ID |
isadmin | Tenant administrator flag |
superid | Super administrator ID |
csrf_token | CSRF token |
Add a mask and install the hook
//@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 "";
}#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.
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
Register the catalogue
/superadmin/registerfunccollects routes withmask_code != 0and writes them tosysperms.Assign to a tenant
/superadmin/rbac/companypermreplacessyscompanyperms, the ceiling for that tenant.Create roles and bind grants
Use
/admin/rbac/role/*androleperm/bind. Bindings are narrowed by the platform-assigned set.Bind users to roles
/admin/rbac/userrole/savecallsbind_user_role/unbind_user_role.Log in and access
Login writes the session keys. Requests to masked routes hit the hook and are allowed or get 401 / 403.
Invalidate on change
Call
invalidate_all()orinvalidate_user()after changes; snapshots also have a 60-second TTL fallback.
Programmatic checks
//@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.