Commit 27af33a
feat(plugin-auth): cap self-created orgs per user (OS_ORG_LIMIT) (#2357)
Forward better-auth's `organizationLimit` in FUNCTION form, counting only the
caller's `role=owner` memberships (orgs they created) — never orgs they were
invited into — read via `withSystemReadContext` so the cross-tenant `sys_member`
count isn't filtered to empty by org-scoping. Threshold from `OS_ORG_LIMIT`
(new `resolveOrgLimit()`); unset → unlimited (self-host default). Fail-open if
the count can't be taken, so an infra hiccup never blocks a legitimate user.
Why owner-scoped, not a flat count: better-auth's number form counts ALL of a
user's memberships, which would block a collaborator invited into many orgs from
creating their own. We only want to bound scripted org/free-env spam (each new
org auto-provisions a free environment on the cloud control plane), so capping
self-created orgs is the right axis. No-op in single-org mode (create is already
denied by beforeCreateOrganization).
Verified on the local rig: a user owning 4 orgs but a member of 5 is allowed to
create at limit=5 (the invited org is not counted), then blocked once they own 5.
Co-authored-by: Jack Zhuang <277994282+os-zhuang@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 911c7aa commit 27af33a
2 files changed
Lines changed: 46 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
910 | 910 | | |
911 | 911 | | |
912 | 912 | | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
913 | 939 | | |
914 | 940 | | |
915 | 941 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
106 | 125 | | |
107 | 126 | | |
108 | 127 | | |
| |||
0 commit comments