-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsys-member.object.ts
More file actions
194 lines (183 loc) · 6.75 KB
/
Copy pathsys-member.object.ts
File metadata and controls
194 lines (183 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { ObjectSchema, Field } from '@objectstack/spec/data';
/**
* sys_member — System Member Object
*
* Organization membership linking users to organizations with roles.
* Backed by better-auth's organization plugin.
*
* @namespace sys
*/
export const SysMember = ObjectSchema.create({
name: 'sys_member',
label: 'Member',
pluralLabel: 'Members',
icon: 'user-check',
isSystem: true,
managedBy: 'better-auth',
// ADR-0010 §3.7 — managed by better-auth; tenants may not edit schema,
// but may add overlay row-level config. Use `no-overlay` if you need to
// forbid sys_metadata overlays entirely.
protection: {
lock: 'full',
reason: 'Identity table managed by better-auth — see ADR-0010.',
docsUrl: 'https://docs.objectstack.ai/adr/0010-metadata-protection',
},
description: 'Organization membership records',
// Org-independent title: organization_id is null in single-org mode, so a
// '{user_id} in {organization_id}' format renders "… in null". User + role
// identifies the membership in both single- and multi-org deployments.
titleFormat: '{user_id} ({role})',
highlightFields: ['user_id', 'organization_id', 'role'],
// Row-level actions: better-auth `organization/update-member-role` and
// `organization/remove-member`. Generic CRUD is suppressed on better-auth
// managed tables, so these are the canonical edit/delete entry points.
// The `add_member` toolbar action covers the admin "attach an existing
// user directly without sending an invitation" flow.
actions: [
{
// Admin-only: directly attach an existing user to the active org,
// bypassing the invite-accept flow. Better-auth:
// `organization/add-member { userId, role, organizationId?, teamId? }`.
// organizationId/teamId default to the caller's active org/team when
// omitted, so we leave them as optional params.
name: 'add_member',
label: 'Add Member',
icon: 'user-plus',
variant: 'primary',
locations: ['list_toolbar'],
type: 'api',
target: '/api/v1/auth/organization/add-member',
// Gated on the org CAPABILITY, not multi-org (ADR-0081 D1): the
// better-auth endpoints resolve the session's active org, which
// single-org mode now guarantees via plugin-auth's default-org
// bootstrap. Same gate on every membership mutation below.
requiresFeature: 'organization',
successMessage: 'Member added',
refreshAfter: true,
params: [
{ name: 'userId', field: 'user_id', required: true },
{ field: 'role', required: true },
{ name: 'organizationId', field: 'organization_id' },
],
},
{
name: 'update_member_role',
label: 'Change Role',
icon: 'shield',
mode: 'edit',
locations: ['list_item'],
type: 'api',
target: '/api/v1/auth/organization/update-member-role',
recordIdParam: 'memberId',
requiresFeature: 'organization',
successMessage: 'Member role updated',
refreshAfter: true,
params: [
{ field: 'role', required: true, defaultFromRow: true },
],
},
{
name: 'remove_member',
label: 'Remove Member',
icon: 'user-minus',
variant: 'danger',
mode: 'delete',
locations: ['list_item'],
type: 'api',
target: '/api/v1/auth/organization/remove-member',
recordIdParam: 'memberIdOrEmail',
requiresFeature: 'organization',
confirmText: 'Remove this member from the organization? They will lose access to all org resources.',
successMessage: 'Member removed',
refreshAfter: true,
},
// Transfer ownership is modeled as `update-member-role` with role=owner
// (better-auth's organization plugin auto-demotes the previous owner
// to admin). Kept as a separate action so the row menu can present a
// distinct destructive-style affordance with the right confirm copy —
// mixing it into `update_member_role` would hide the ownership-handoff
// semantics behind a generic role dropdown.
{
name: 'transfer_ownership',
label: 'Transfer Ownership',
icon: 'crown',
variant: 'danger',
mode: 'custom',
locations: ['list_item'],
type: 'api',
target: '/api/v1/auth/organization/update-member-role',
recordIdParam: 'memberId',
bodyExtra: { role: 'owner' },
// The residual row predicate stays hand-written; the feature gate is
// AND-composed onto it by the requiresFeature lowering.
visible: "record.role != 'owner'",
requiresFeature: 'organization',
confirmText: 'Transfer ownership of this organization to the selected member? You will be demoted to admin and lose owner-only privileges.',
successMessage: 'Ownership transferred',
refreshAfter: true,
},
],
listViews: {
mine: {
type: 'grid',
name: 'mine',
label: 'My Memberships',
data: { provider: 'object', object: 'sys_member' },
columns: ['organization_id', 'role', 'created_at'],
filter: [{ field: 'user_id', operator: 'equals', value: '{current_user_id}' }],
sort: [{ field: 'created_at', order: 'desc' }],
pagination: { pageSize: 50 },
emptyState: {
title: 'No organizations yet',
message: 'You haven\'t joined any organizations.',
},
},
},
fields: {
id: Field.text({
label: 'Member ID',
required: true,
readonly: true,
}),
created_at: Field.datetime({
label: 'Created At',
defaultValue: 'NOW()',
readonly: true,
}),
organization_id: Field.lookup('sys_organization', {
label: 'Organization',
// Optional: single-tenant has no sys_organization row and no auto-stamp
// (org-scoping is multi-tenant-only). Multi-tenant: OrgScopingPlugin stamps it
// and tenant-isolation RLS hides null-org rows (fail-closed). ADR-0057 addendum.
required: false,
}),
user_id: Field.lookup('sys_user', {
label: 'User',
required: true,
}),
role: Field.select({
label: 'Role',
required: false,
description: 'Member role within the organization',
options: [
{ label: 'Owner', value: 'owner' },
{ label: 'Admin', value: 'admin' },
{ label: 'Member', value: 'member' },
],
defaultValue: 'member',
}),
},
indexes: [
{ fields: ['organization_id', 'user_id'], unique: true },
{ fields: ['user_id'] },
],
enable: {
trackHistory: true,
searchable: false,
apiEnabled: true,
// #1591 — reads only: writes are refused by the identity write guard
// (ADR-0092 D2) and owned by better-auth. HTTP answers 405 before the 403.
apiMethods: ['get', 'list'],
},
});