@lowdefy/plugin-better-auth 0.0.0-experimental-20260714122106
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/auth/GenericOAuth.js +26 -0
- package/dist/auth/createSocialProvider.js +27 -0
- package/dist/auth/providers.js +53 -0
- package/dist/auth/strategies/apiKey.js +60 -0
- package/dist/auth/strategies/jwt.js +113 -0
- package/dist/auth/strategies.js +20 -0
- package/dist/steps/BanUser.js +34 -0
- package/dist/steps/CancelInvitation.js +32 -0
- package/dist/steps/CreateOrganization.js +46 -0
- package/dist/steps/DeleteUser.js +103 -0
- package/dist/steps/InviteMember.js +56 -0
- package/dist/steps/ListMembers.js +45 -0
- package/dist/steps/ListUsers.js +37 -0
- package/dist/steps/RemoveMember.js +49 -0
- package/dist/steps/RevokeUserSessions.js +32 -0
- package/dist/steps/UnbanUser.js +32 -0
- package/dist/steps/UpdateMemberAttributes.js +58 -0
- package/dist/steps/UpdateMemberRoles.js +94 -0
- package/dist/steps/UpdateUserAttributes.js +48 -0
- package/dist/steps/UpdateUserProfile.js +106 -0
- package/dist/steps/support/callPluginEndpoint.js +98 -0
- package/dist/steps/support/createActingMemberAdapter.js +51 -0
- package/dist/steps/support/getPluginEndpoint.js +27 -0
- package/dist/steps/support/invokeEndpoint.js +30 -0
- package/dist/steps/support/resolveOrganizationId.js +33 -0
- package/dist/steps/support/splitRoles.js +25 -0
- package/dist/steps/support/syncUserAdminRole.js +98 -0
- package/dist/steps.js +28 -0
- package/dist/types.js +76 -0
- package/package.json +58 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
// The design routes member and invitation cleanup "via the org plugin's APIs",
|
|
18
|
+
// but at better-auth 1.6.23 removeMember refuses once the user row is gone (it
|
|
19
|
+
// re-reads the user and 400s). The cascade therefore uses the adapter layer -
|
|
20
|
+
// the same layer the engine already uses for org seeding and tenant minting.
|
|
21
|
+
// The admin removeUser endpoint hard-deletes the user row and (confirmed by the
|
|
22
|
+
// phase-0 probe at 1.6.23) also clears the user's session and account rows, so
|
|
23
|
+
// those are not cleared here. App-owned data is left untouched.
|
|
24
|
+
async function DeleteUser({ acting, auth, properties }) {
|
|
25
|
+
const { userId } = properties;
|
|
26
|
+
if (type.isNone(userId)) {
|
|
27
|
+
throw new Error('DeleteUser requires a "userId" property.');
|
|
28
|
+
}
|
|
29
|
+
const { adapter } = await auth.$context;
|
|
30
|
+
const user = await adapter.findOne({
|
|
31
|
+
model: 'user',
|
|
32
|
+
where: [
|
|
33
|
+
{
|
|
34
|
+
field: 'id',
|
|
35
|
+
value: userId
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
});
|
|
39
|
+
if (type.isNone(user)) {
|
|
40
|
+
throw new Error(`DeleteUser found no user with id "${userId}".`);
|
|
41
|
+
}
|
|
42
|
+
await callPluginEndpoint({
|
|
43
|
+
acting,
|
|
44
|
+
auth,
|
|
45
|
+
body: {
|
|
46
|
+
userId
|
|
47
|
+
},
|
|
48
|
+
endpointKey: 'removeUser',
|
|
49
|
+
pluginId: 'admin'
|
|
50
|
+
});
|
|
51
|
+
const members = await adapter.findMany({
|
|
52
|
+
model: 'member',
|
|
53
|
+
where: [
|
|
54
|
+
{
|
|
55
|
+
field: 'userId',
|
|
56
|
+
value: userId
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
});
|
|
60
|
+
for (const member of members ?? []){
|
|
61
|
+
await adapter.delete({
|
|
62
|
+
model: 'member',
|
|
63
|
+
where: [
|
|
64
|
+
{
|
|
65
|
+
field: 'id',
|
|
66
|
+
value: member.id
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const invitations = await adapter.findMany({
|
|
72
|
+
model: 'invitation',
|
|
73
|
+
where: [
|
|
74
|
+
{
|
|
75
|
+
field: 'email',
|
|
76
|
+
value: user.email
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
field: 'status',
|
|
80
|
+
value: 'pending'
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
});
|
|
84
|
+
for (const invitation of invitations ?? []){
|
|
85
|
+
await adapter.delete({
|
|
86
|
+
model: 'invitation',
|
|
87
|
+
where: [
|
|
88
|
+
{
|
|
89
|
+
field: 'id',
|
|
90
|
+
value: invitation.id
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// Returns the removed rows so routines can write audit events.
|
|
96
|
+
return {
|
|
97
|
+
success: true,
|
|
98
|
+
user,
|
|
99
|
+
members: members ?? [],
|
|
100
|
+
invitations: invitations ?? []
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export default DeleteUser;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
import resolveOrganizationId from './support/resolveOrganizationId.js';
|
|
18
|
+
async function InviteMember({ acting, auth, organization, properties }) {
|
|
19
|
+
// profile and attributes are registered invitation additionalFields in the
|
|
20
|
+
// engine config; accepting the invitation shallow-merges profile onto the
|
|
21
|
+
// user's profile bag (invitation wins per key) and copies attributes onto
|
|
22
|
+
// the minted member row.
|
|
23
|
+
const { attributes, email, profile, resend, role } = properties;
|
|
24
|
+
if (type.isNone(email)) {
|
|
25
|
+
throw new Error('InviteMember requires an "email" property.');
|
|
26
|
+
}
|
|
27
|
+
if (type.isNone(role)) {
|
|
28
|
+
throw new Error('InviteMember requires a "role" property.');
|
|
29
|
+
}
|
|
30
|
+
if (!type.isNone(attributes) && !type.isObject(attributes)) {
|
|
31
|
+
throw new Error(`InviteMember "attributes" is not an object. Received ${JSON.stringify(attributes)}.`);
|
|
32
|
+
}
|
|
33
|
+
if (!type.isNone(profile) && !type.isObject(profile)) {
|
|
34
|
+
throw new Error(`InviteMember "profile" is not an object. Received ${JSON.stringify(profile)}.`);
|
|
35
|
+
}
|
|
36
|
+
const organizationId = resolveOrganizationId({
|
|
37
|
+
organization,
|
|
38
|
+
organizationId: properties.organizationId,
|
|
39
|
+
step: 'InviteMember'
|
|
40
|
+
});
|
|
41
|
+
return callPluginEndpoint({
|
|
42
|
+
acting,
|
|
43
|
+
auth,
|
|
44
|
+
body: {
|
|
45
|
+
attributes,
|
|
46
|
+
email,
|
|
47
|
+
organizationId,
|
|
48
|
+
profile,
|
|
49
|
+
resend,
|
|
50
|
+
role
|
|
51
|
+
},
|
|
52
|
+
endpointKey: 'createInvitation',
|
|
53
|
+
pluginId: 'organization'
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export default InviteMember;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
import resolveOrganizationId from './support/resolveOrganizationId.js';
|
|
18
|
+
async function ListMembers({ acting, auth, organization, properties }) {
|
|
19
|
+
const { filterField, filterOperator, filterValue, limit, offset, organizationSlug, sortBy, sortDirection } = properties;
|
|
20
|
+
// An explicit organizationSlug is an explicit org selection too - only
|
|
21
|
+
// default the id when the step names no organization at all.
|
|
22
|
+
const organizationId = type.isNone(organizationSlug) ? resolveOrganizationId({
|
|
23
|
+
organization,
|
|
24
|
+
organizationId: properties.organizationId,
|
|
25
|
+
step: 'ListMembers'
|
|
26
|
+
}) : properties.organizationId;
|
|
27
|
+
return callPluginEndpoint({
|
|
28
|
+
acting,
|
|
29
|
+
auth,
|
|
30
|
+
endpointKey: 'listMembers',
|
|
31
|
+
pluginId: 'organization',
|
|
32
|
+
query: {
|
|
33
|
+
filterField,
|
|
34
|
+
filterOperator,
|
|
35
|
+
filterValue,
|
|
36
|
+
limit,
|
|
37
|
+
offset,
|
|
38
|
+
organizationId,
|
|
39
|
+
organizationSlug,
|
|
40
|
+
sortBy,
|
|
41
|
+
sortDirection
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export default ListMembers;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
16
|
+
async function ListUsers({ acting, auth, properties }) {
|
|
17
|
+
const { filterField, filterOperator, filterValue, limit, offset, searchField, searchOperator, searchValue, sortBy, sortDirection } = properties;
|
|
18
|
+
return callPluginEndpoint({
|
|
19
|
+
acting,
|
|
20
|
+
auth,
|
|
21
|
+
endpointKey: 'listUsers',
|
|
22
|
+
pluginId: 'admin',
|
|
23
|
+
query: {
|
|
24
|
+
filterField,
|
|
25
|
+
filterOperator,
|
|
26
|
+
filterValue,
|
|
27
|
+
limit,
|
|
28
|
+
offset,
|
|
29
|
+
searchField,
|
|
30
|
+
searchOperator,
|
|
31
|
+
searchValue,
|
|
32
|
+
sortBy,
|
|
33
|
+
sortDirection
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export default ListUsers;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
import resolveOrganizationId from './support/resolveOrganizationId.js';
|
|
18
|
+
import syncUserAdminRole from './support/syncUserAdminRole.js';
|
|
19
|
+
async function RemoveMember({ acting, auth, organization, properties, userAdminRole }) {
|
|
20
|
+
const { memberIdOrEmail } = properties;
|
|
21
|
+
if (type.isNone(memberIdOrEmail)) {
|
|
22
|
+
throw new Error('RemoveMember requires a "memberIdOrEmail" property.');
|
|
23
|
+
}
|
|
24
|
+
const organizationId = resolveOrganizationId({
|
|
25
|
+
organization,
|
|
26
|
+
organizationId: properties.organizationId,
|
|
27
|
+
step: 'RemoveMember'
|
|
28
|
+
});
|
|
29
|
+
const result = await callPluginEndpoint({
|
|
30
|
+
acting,
|
|
31
|
+
auth,
|
|
32
|
+
body: {
|
|
33
|
+
memberIdOrEmail,
|
|
34
|
+
organizationId
|
|
35
|
+
},
|
|
36
|
+
endpointKey: 'removeMember',
|
|
37
|
+
pluginId: 'organization'
|
|
38
|
+
});
|
|
39
|
+
// Membership removal is followed in-band by the engine's user.role
|
|
40
|
+
// denormalization - losing the pinned membership clears the role.
|
|
41
|
+
await syncUserAdminRole({
|
|
42
|
+
auth,
|
|
43
|
+
organization,
|
|
44
|
+
userAdminRole,
|
|
45
|
+
userId: result?.member?.userId
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
export default RemoveMember;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
async function RevokeUserSessions({ acting, auth, properties }) {
|
|
18
|
+
const { userId } = properties;
|
|
19
|
+
if (type.isNone(userId)) {
|
|
20
|
+
throw new Error('RevokeUserSessions requires a "userId" property.');
|
|
21
|
+
}
|
|
22
|
+
return callPluginEndpoint({
|
|
23
|
+
acting,
|
|
24
|
+
auth,
|
|
25
|
+
body: {
|
|
26
|
+
userId
|
|
27
|
+
},
|
|
28
|
+
endpointKey: 'revokeUserSessions',
|
|
29
|
+
pluginId: 'admin'
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export default RevokeUserSessions;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
async function UnbanUser({ acting, auth, properties }) {
|
|
18
|
+
const { userId } = properties;
|
|
19
|
+
if (type.isNone(userId)) {
|
|
20
|
+
throw new Error('UnbanUser requires a "userId" property.');
|
|
21
|
+
}
|
|
22
|
+
return callPluginEndpoint({
|
|
23
|
+
acting,
|
|
24
|
+
auth,
|
|
25
|
+
body: {
|
|
26
|
+
userId
|
|
27
|
+
},
|
|
28
|
+
endpointKey: 'unbanUser',
|
|
29
|
+
pluginId: 'admin'
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export default UnbanUser;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import resolveOrganizationId from './support/resolveOrganizationId.js';
|
|
17
|
+
// Adapter-direct per the mongodb design Decision 5: the adapter applies the
|
|
18
|
+
// json additionalField transform (native sub-document storage on MongoDB via
|
|
19
|
+
// supportsJSON) on both ends. Fires NO member.update database hooks -
|
|
20
|
+
// attributes are admin-set authorization inputs, not user-driven edits.
|
|
21
|
+
async function UpdateMemberAttributes({ auth, organization, properties }) {
|
|
22
|
+
const { attributes, memberId } = properties;
|
|
23
|
+
if (type.isNone(memberId)) {
|
|
24
|
+
throw new Error('UpdateMemberAttributes requires a "memberId" property.');
|
|
25
|
+
}
|
|
26
|
+
if (!type.isObject(attributes)) {
|
|
27
|
+
throw new Error(`UpdateMemberAttributes requires an "attributes" object. Received ${JSON.stringify(attributes)}.`);
|
|
28
|
+
}
|
|
29
|
+
const organizationId = resolveOrganizationId({
|
|
30
|
+
organization,
|
|
31
|
+
organizationId: properties.organizationId,
|
|
32
|
+
step: 'UpdateMemberAttributes'
|
|
33
|
+
});
|
|
34
|
+
const { adapter } = await auth.$context;
|
|
35
|
+
const member = await adapter.update({
|
|
36
|
+
model: 'member',
|
|
37
|
+
where: [
|
|
38
|
+
{
|
|
39
|
+
field: 'id',
|
|
40
|
+
value: memberId
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
field: 'organizationId',
|
|
44
|
+
value: organizationId
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
update: {
|
|
48
|
+
attributes
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
if (type.isNone(member)) {
|
|
52
|
+
// Mirrors the rails' member-not-found semantics - a memberId outside the
|
|
53
|
+
// resolved organization must fail loudly, not skip the write silently.
|
|
54
|
+
throw new Error(`UpdateMemberAttributes found no member "${memberId}" in organization "${organizationId}".`);
|
|
55
|
+
}
|
|
56
|
+
return member;
|
|
57
|
+
}
|
|
58
|
+
export default UpdateMemberAttributes;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import callPluginEndpoint from './support/callPluginEndpoint.js';
|
|
17
|
+
import resolveOrganizationId from './support/resolveOrganizationId.js';
|
|
18
|
+
import splitRoles from './support/splitRoles.js';
|
|
19
|
+
import syncUserAdminRole from './support/syncUserAdminRole.js';
|
|
20
|
+
async function UpdateMemberRoles({ acting, auth, organization, properties, userAdminRole }) {
|
|
21
|
+
const { memberId, role } = properties;
|
|
22
|
+
if (type.isNone(memberId)) {
|
|
23
|
+
throw new Error('UpdateMemberRoles requires a "memberId" property.');
|
|
24
|
+
}
|
|
25
|
+
if (type.isNone(role)) {
|
|
26
|
+
throw new Error('UpdateMemberRoles requires a "role" property.');
|
|
27
|
+
}
|
|
28
|
+
const organizationId = resolveOrganizationId({
|
|
29
|
+
organization,
|
|
30
|
+
organizationId: properties.organizationId,
|
|
31
|
+
step: 'UpdateMemberRoles'
|
|
32
|
+
});
|
|
33
|
+
// At better-auth 1.6.23, updateMemberRole's "cannot leave the organization
|
|
34
|
+
// without an owner" check only fires when the updater edits their own row.
|
|
35
|
+
// The step acts with a virtual owner session, so demoting the LAST real owner
|
|
36
|
+
// would slip through - guard here, mirroring BetterAuth's
|
|
37
|
+
// YOU_CANNOT_LEAVE_THE_ORGANIZATION_WITHOUT_AN_OWNER semantics.
|
|
38
|
+
const { adapter } = await auth.$context;
|
|
39
|
+
// Scoped to the resolved organization so a memberId from another org falls
|
|
40
|
+
// through to the endpoint's member-not-found error instead of this guard.
|
|
41
|
+
const member = await adapter.findOne({
|
|
42
|
+
model: 'member',
|
|
43
|
+
where: [
|
|
44
|
+
{
|
|
45
|
+
field: 'id',
|
|
46
|
+
value: memberId
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
field: 'organizationId',
|
|
50
|
+
value: organizationId
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
});
|
|
54
|
+
if (!type.isNone(member)) {
|
|
55
|
+
const currentRoles = splitRoles(member.role);
|
|
56
|
+
const newRoles = splitRoles(role);
|
|
57
|
+
if (currentRoles.includes('owner') && !newRoles.includes('owner')) {
|
|
58
|
+
const members = await adapter.findMany({
|
|
59
|
+
model: 'member',
|
|
60
|
+
where: [
|
|
61
|
+
{
|
|
62
|
+
field: 'organizationId',
|
|
63
|
+
value: organizationId
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
});
|
|
67
|
+
const ownerCount = (members ?? []).filter((row)=>splitRoles(row.role).includes('owner')).length;
|
|
68
|
+
if (ownerCount <= 1) {
|
|
69
|
+
throw new Error('You cannot leave the organization without an owner.');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const updatedMember = await callPluginEndpoint({
|
|
74
|
+
acting,
|
|
75
|
+
auth,
|
|
76
|
+
body: {
|
|
77
|
+
memberId,
|
|
78
|
+
organizationId,
|
|
79
|
+
role
|
|
80
|
+
},
|
|
81
|
+
endpointKey: 'updateMemberRole',
|
|
82
|
+
pluginId: 'organization'
|
|
83
|
+
});
|
|
84
|
+
// Member-role writes are followed in-band by the engine's user.role
|
|
85
|
+
// denormalization - a sync failure fails the step.
|
|
86
|
+
await syncUserAdminRole({
|
|
87
|
+
auth,
|
|
88
|
+
organization,
|
|
89
|
+
userAdminRole,
|
|
90
|
+
userId: updatedMember?.userId ?? member?.userId
|
|
91
|
+
});
|
|
92
|
+
return updatedMember;
|
|
93
|
+
}
|
|
94
|
+
export default UpdateMemberRoles;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
// Adapter-direct per the mongodb design Decision 5: the adapter applies the
|
|
17
|
+
// json additionalField transform (native sub-document storage on MongoDB via
|
|
18
|
+
// supportsJSON) on both ends. Fires NO user.update database hooks -
|
|
19
|
+
// attributes are admin-set authorization inputs, not user-driven edits.
|
|
20
|
+
async function UpdateUserAttributes({ auth, properties }) {
|
|
21
|
+
const { attributes, userId } = properties;
|
|
22
|
+
if (type.isNone(userId)) {
|
|
23
|
+
throw new Error('UpdateUserAttributes requires a "userId" property.');
|
|
24
|
+
}
|
|
25
|
+
if (!type.isObject(attributes)) {
|
|
26
|
+
throw new Error(`UpdateUserAttributes requires an "attributes" object. Received ${JSON.stringify(attributes)}.`);
|
|
27
|
+
}
|
|
28
|
+
const { adapter } = await auth.$context;
|
|
29
|
+
const user = await adapter.update({
|
|
30
|
+
model: 'user',
|
|
31
|
+
where: [
|
|
32
|
+
{
|
|
33
|
+
field: 'id',
|
|
34
|
+
value: userId
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
update: {
|
|
38
|
+
attributes
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (type.isNone(user)) {
|
|
42
|
+
// Mirrors UpdateMemberAttributes - an unknown userId must fail loudly,
|
|
43
|
+
// not skip the write silently.
|
|
44
|
+
throw new Error(`UpdateUserAttributes found no user with id "${userId}".`);
|
|
45
|
+
}
|
|
46
|
+
return user;
|
|
47
|
+
}
|
|
48
|
+
export default UpdateUserAttributes;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
const allowedProperties = [
|
|
17
|
+
'image',
|
|
18
|
+
'name',
|
|
19
|
+
'profile',
|
|
20
|
+
'userId'
|
|
21
|
+
];
|
|
22
|
+
// The sanctioned write path for the opaque user.profile bag (display and app
|
|
23
|
+
// data - the platform never validates, indexes, or reads inside it). The
|
|
24
|
+
// profile update is a shallow merge per top-level key onto the user's
|
|
25
|
+
// existing bag - a key set to null is removed - so the row is read first;
|
|
26
|
+
// the adapter's json additionalField transform handles storage on both ends.
|
|
27
|
+
// name and image are the display copies. The step writes nothing else - not
|
|
28
|
+
// email, not emailVerified, not attributes, not roles. Adapter-direct like
|
|
29
|
+
// UpdateUserAttributes: fires NO user.update database hooks.
|
|
30
|
+
async function UpdateUserProfile({ auth, properties }) {
|
|
31
|
+
const { image, name, profile, userId } = properties;
|
|
32
|
+
const unknownProperties = Object.keys(properties).filter((key)=>!allowedProperties.includes(key));
|
|
33
|
+
if (unknownProperties.length > 0) {
|
|
34
|
+
const received = unknownProperties.map((key)=>`"${key}"`).join(', ');
|
|
35
|
+
throw new Error(`UpdateUserProfile received unknown properties ${received}. ` + 'Allowed properties are "userId", "profile", "name", and "image".');
|
|
36
|
+
}
|
|
37
|
+
if (type.isNone(userId)) {
|
|
38
|
+
throw new Error('UpdateUserProfile requires a "userId" property.');
|
|
39
|
+
}
|
|
40
|
+
if (!type.isNone(profile) && !type.isObject(profile)) {
|
|
41
|
+
throw new Error(`UpdateUserProfile "profile" is not an object. Received ${JSON.stringify(profile)}.`);
|
|
42
|
+
}
|
|
43
|
+
if (!type.isNone(name) && !type.isString(name)) {
|
|
44
|
+
throw new Error(`UpdateUserProfile "name" is not a string. Received ${JSON.stringify(name)}.`);
|
|
45
|
+
}
|
|
46
|
+
if (!type.isNone(image) && !type.isString(image)) {
|
|
47
|
+
throw new Error(`UpdateUserProfile "image" is not a string. Received ${JSON.stringify(image)}.`);
|
|
48
|
+
}
|
|
49
|
+
if (type.isNone(profile) && type.isNone(name) && type.isNone(image)) {
|
|
50
|
+
throw new Error('UpdateUserProfile requires at least one of "profile", "name", or "image" to write.');
|
|
51
|
+
}
|
|
52
|
+
const { adapter } = await auth.$context;
|
|
53
|
+
const user = await adapter.findOne({
|
|
54
|
+
model: 'user',
|
|
55
|
+
where: [
|
|
56
|
+
{
|
|
57
|
+
field: 'id',
|
|
58
|
+
value: userId
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
});
|
|
62
|
+
if (type.isNone(user)) {
|
|
63
|
+
// Mirrors UpdateUserAttributes - an unknown userId must fail loudly,
|
|
64
|
+
// not skip the write silently.
|
|
65
|
+
throw new Error(`UpdateUserProfile found no user with id "${userId}".`);
|
|
66
|
+
}
|
|
67
|
+
const update = {};
|
|
68
|
+
if (!type.isNone(profile)) {
|
|
69
|
+
const merged = {
|
|
70
|
+
...user.profile ?? {}
|
|
71
|
+
};
|
|
72
|
+
Object.entries(profile).forEach(([key, value])=>{
|
|
73
|
+
if (value === null) {
|
|
74
|
+
delete merged[key];
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
merged[key] = value;
|
|
78
|
+
});
|
|
79
|
+
update.profile = merged;
|
|
80
|
+
}
|
|
81
|
+
if (!type.isNone(name)) {
|
|
82
|
+
update.name = name;
|
|
83
|
+
}
|
|
84
|
+
if (!type.isNone(image)) {
|
|
85
|
+
update.image = image;
|
|
86
|
+
}
|
|
87
|
+
return adapter.update({
|
|
88
|
+
model: 'user',
|
|
89
|
+
where: [
|
|
90
|
+
{
|
|
91
|
+
field: 'id',
|
|
92
|
+
value: userId
|
|
93
|
+
}
|
|
94
|
+
],
|
|
95
|
+
update
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Self-targeting exemption from the user-administration floor: the step
|
|
99
|
+
// interface layer (handleAuthStep) waives the auth.userAdminRole check when
|
|
100
|
+
// properties.userId equals the caller's own id - the self-service profile
|
|
101
|
+
// save is the core flow and profile is display/app data, not authorization
|
|
102
|
+
// inputs. Targeting another user still requires the role.
|
|
103
|
+
UpdateUserProfile.meta = {
|
|
104
|
+
selfTargetExempt: 'userId'
|
|
105
|
+
};
|
|
106
|
+
export default UpdateUserProfile;
|