Users, roles and permissions
Database-backed roles, permissions, seller memberships and invitations.
Status: foundation implemented (schema, effective-permission resolution, platform role APIs, seller membership APIs).
Scope: platform operators, sellers and customers. B2B company memberships are intentionally out of scope for this phase.
1. Model
Authorization is split into three layers:
| Layer | Question | Stored in |
|---|---|---|
| Account type | What kind of identity is this? | "user".user_type |
| Membership | Which shop/platform does the user act in? | shop_member, auth_user_role |
| Role + permission | What may they do? | auth_role, auth_permission, auth_role_permission |
Account types
Rules:
ADMINonly grants access to the admin console. Permissions come from roles.SUPER_ADMINis retained as a legacy enum value and mapped to theplatform:super_adminrole. It will be removed after existing rows and clients are migrated.- Guest checkout uses
user.is_guest, not an account type. - Staff is never an account type. A staff member is
user + membership + role.
2. Tables
| Table | Purpose |
|---|---|
auth_permission |
Permission catalog (resource:action) |
auth_role |
Platform (GLOBAL) and shop (SHOP) roles |
auth_role_permission |
Role → permission mapping |
auth_user_role |
Platform role assignment |
shop_member |
User membership in a shop |
shop_member_role |
Shop role assignment |
shop_invitation |
Pending staff invitation (token hash, expiry) |
member_warehouse |
Optional warehouse scoping for shop staff |
"user".authz_version |
Invalidates cached permissions |
3. Permission codes
The existing Permission enum now carries a stable code:
| Enum | Code |
|---|---|
USER_MANAGE |
user:manage |
CATALOG_MANAGE |
catalog:manage |
ORDER_MANAGE |
order:manage |
PAYMENT_MANAGE |
payment:manage |
REFUND_MANAGE |
refund:manage |
PAYOUT_MANAGE |
payout:manage |
COUPON_MANAGE |
coupon:manage |
SHOP_MANAGE |
shop:manage |
INVENTORY_MANAGE |
inventory:manage |
POLICY_MANAGE |
policy:manage |
DASHBOARD_VIEW |
dashboard:read |
AUDIT_VIEW |
audit:read |
NOTIFICATION_SEND |
notification:send |
TAX_MANAGE |
tax:manage |
FX_MANAGE |
fx:manage |
OUTBOX_MANAGE |
outbox:manage |
API_KEY_MANAGE |
api_key:manage |
FEATURE_FLAG_MANAGE |
feature_flag:manage |
ROLE_MANAGE |
role:manage |
SUPPORT_MANAGE |
support:manage |
FRAUD_MANAGE |
fraud:manage |
CMS_MANAGE |
cms:manage |
SUBSCRIPTION_MANAGE |
subscription:manage |
REVIEW_MANAGE |
review:manage |
SHIPPING_MANAGE |
shipping:manage |
B2B_MANAGE |
b2b:manage |
Custom roles may include codes that do not have an enum constant yet. Route gates still use enum constants, so new route-level checks require a code constant; business-level checks can use raw codes through the resolver.
4. Effective permissions
effective(user) =
union(permissions of active platform roles)
∪ union(permissions of active shop memberships)
Rules:
- If the user has no assignments, the legacy
UserType.defaultPermissions()mapping is used for customers and sellers. This keeps existing behavior working. - Platform admins are the exception: an
ADMIN/SUPER_ADMINwith no role assignment gets no permissions, so an orphaned admin can never fall back to full access. - If the user has any assignment, the result is exact. An assigned role with no permissions grants nothing.
- Results are cached by
userId + authz_versionfor 60 seconds. - Any role assignment or membership change increments
authz_version, so the next request resolves fresh permissions.
5. Authentication flow
ConfigureAuth now:
- Validates the JWT and loads the user from the database.
- Resolves effective permissions through
RbacService. - Builds
JwtTokenRequestwithpermissionsandauthzVersion.
JwtTokenRequest.hasPermission():
- JWT: checks the resolved permission set (code or legacy enum name).
- API key: checks explicit scopes (legacy enum names or codes).
- Fallback: role defaults only when no resolved set was supplied.
API keys are validated against the owner's effective permissions at creation and at authentication, so a role change narrows an existing key.
6. Seeded roles
Platform (GLOBAL)
| Role | Notes |
|---|---|
platform:super_admin |
All permissions; break-glass |
platform:admin |
All except feature_flag:manage |
platform:ops |
Users, shops, dashboards, audit |
platform:catalog |
Catalog and inventory |
platform:orders |
Orders, refunds, shipping |
platform:finance_approver |
Payments, refunds, payouts, tax, FX |
platform:finance_payer |
Payments and payouts |
platform:support |
Support tickets |
platform:content |
CMS, policies, coupons, notifications |
platform:auditor |
Audit and dashboards (read-only) |
platform:developer |
Feature flags, outbox, API keys |
Shop (SHOP)
shop:owner, shop:manager, catalog:manager, order:fulfillment, inventory:manager, finance:viewer, support:agent, analyst, warehouse:operator, pos:cashier.
7. API
OpenAPI tag: RBAC.
Platform
GET /api/v1/admin/rbac/permissions
GET /api/v1/admin/rbac/roles?scope=GLOBAL|SHOP
POST /api/v1/admin/rbac/roles
GET /api/v1/admin/rbac/roles/{id}
PUT /api/v1/admin/rbac/roles/{id}
DELETE /api/v1/admin/rbac/roles/{id}
PUT /api/v1/admin/rbac/roles/{id}/permissions
GET /api/v1/admin/rbac/users/{id}/roles
PUT /api/v1/admin/rbac/users/{id}/roles
GET /api/v1/admin/rbac/users/{id}/effective-permissions
All platform RBAC routes require role:manage.
Authorization rules enforced by the service:
- A non-super admin can only grant permissions they already hold.
- Only a super admin can grant
platform:super_adminor edit system roles. - The last super admin cannot be removed.
- Role and membership changes bump
authz_versionand are audited.
Seller
GET /api/v1/seller/members
POST /api/v1/seller/members/invite
PUT /api/v1/seller/members/{id}/roles
DELETE /api/v1/seller/members/{id}
The seller must own the shop. The invitation token is returned once; deliver it by email.
Shared
8. Migration
V66__rbac_and_seller_members.sql:
- Adds
user.authz_version. - Creates the RBAC and shop-membership tables.
- Seeds the permission catalog and all system roles.
- Backfills
SUPER_ADMIN→platform:super_admin. - Backfills
ADMIN→platform:admin. - Creates owner memberships for existing sellers with a shop.
V67__b2b_permission.sql adds the b2b:manage permission and grants it to
platform:admin and platform:super_admin.
Backward compatibility:
- Existing admin JWTs keep working because permissions are resolved server-side.
- Existing API keys keep working because scopes accept both enum names and codes.
SUPER_ADMINremains accepted byadminAuthuntil the enum value is retired.
9. Admin route gates
The following admin groups now require explicit permissions instead of relying on adminAuth alone:
| Group | Permission |
|---|---|
admin/auth, admin/users |
user:manage |
admin/rbac |
role:manage |
admin/brands, categories, products |
catalog:manage |
admin/inventories |
inventory:manage |
admin/orders |
order:manage |
admin/payments, webhooks, gift cards, wallets |
payment:manage |
admin/refunds, admin/rma-requests |
refund:manage |
admin/payouts, commission rules |
payout:manage |
admin/fraud, admin/chargebacks |
fraud:manage |
admin/support/tickets, admin/disputes |
support:manage |
admin/cms, admin/seo |
cms:manage |
admin/policies, policy consents |
policy:manage |
admin/shipping-methods |
shipping:manage |
admin/subscriptions, plans |
subscription:manage |
admin/reviews, seller reviews |
review:manage |
admin/feature-flags |
feature_flag:manage |
admin/outbox |
outbox:manage |
admin/tax-* |
tax:manage |
admin/fx-rates |
fx:manage |
admin/audit-logs |
audit:read |
admin/dashboard, queues, analytics |
dashboard:read |
admin/b2b |
b2b:manage |
Mixed customer/seller/admin routes use requireAdminPermission(...) so a narrowly-scoped admin cannot bypass the permission gate through a customer or seller endpoint. sellerAuth is exact SELLER only; admins use the admin console.
10. Behavior changes from the old model
sellerAuthnow allows exactSELLERaccounts only. Admins use the admin console instead of inheriting seller routes.- Customer order detail, refund evidence and invoice routes no longer give admins implicit access. Admin access is checked with
requireAdminPermission(...)(order:manage/refund:manage) or through the admin routes. notifications/sendrequiresnotification:sendinstead of any admin/seller role.- Feature flags now require
feature_flag:manage; a plainADMINno longer gets implicit access (onlySUPER_ADMIN/platform:developerdo). - Invoice, packing-slip and shipment endpoints require
order:managefor admin principals. - B2B admin routes require
b2b:manage. - Changing a role's permissions increments
authz_versionfor every holder of that role, so cached permissions are invalidated immediately.
11. Known limitations / next phase
- Seller service authorization still uses
requireSellerByUserIdandsellerOwnsShop. Invited staff have a membership, but seller endpoints that require asellerrow will reject them. The next phase is to replace those checks withrequireShopPermission(user, shopId, permission). - Invited staff should be
SELLERaccounts to enter the seller center in this phase. ACUSTOMERinvitee can accept and receive a membership, but the seller portal gate will reject them until membership-based portal access lands. - Route gates are still coarse (
CATALOG_MANAGE, notcatalog:read/catalog:write). Granular route gates can be added incrementally. platform:finance_approverandplatform:finance_payercurrently share the coarsepayout:managecode. Split intopayout:approveandpayout:paywhen payout endpoints are migrated.- Invitation emails are not sent yet; the token is returned by the API.
SUPER_ADMINshould be removed fromUserTypeafter a data backfill and client migration.- B2B company memberships are out of scope.
12. Code layout
| File | Responsibility |
|---|---|
RbacModels.kt |
API request/response DTOs |
RbacService.kt |
Permission resolution, roles, platform assignments |
ShopMembershipService.kt |
Shop staff memberships and invitations |
RbacSupport.kt |
Shared role/member lookup and mapping helpers |
constants/Rbac.kt |
Role scope, membership/invitation status and seeded ids |
RbacRoutes.kt |
Admin, seller member, /me and invitation routes |
Shared utilities: utils/common/Hashing.kt and Iterable<String>.entityIDs(...) in StringExt.kt.
13. Verification
./gradlew buildpasses: 637 tests, 0 failures.mkdocs build --strictpasses.- The module API pages and Swagger UI cover 464 operations / 47 tags, including the 16 RBAC operations.
./gradlew detektreports only the four pre-existing structural warnings.V66was applied against a fresh PostgreSQL schema; seeds produced 25 permissions, 21 roles and 98 role-permission links.V67addsb2b:manageand two role links (26 permissions, 100 links).platform:adminintentionally excludesfeature_flag:manage.RbacPrincipalTestcovers permission codes, resolved permission checks, legacy fallback and API-key scopes.