Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 6x 22x 21x 2x 19x 19x 13x 13x 2x 17x 4x 13x 4x 12x 11x 11x 2x 2x 11x 3x 3x | /**
* Remove a member from a flow (or let them leave it)
* DELETE /v1/flows/{flowId}/members/{userId}
*
* @implements UC-APIKEY-006.1, UC-SPACE-006.1
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand, DeleteCommand } = require('@aws-sdk/lib-dynamodb')
const { permissionChecker } = require('../utils/PermissionChecker')
const { cascadeMemberRemovalReferences } = require('../utils/cascadeItemFieldValue')
const { deleteUserApiKeysInFlow } = require('../utils/apiKeyCleanup')
const { notifyPermissionsChanged } = require('../utils/permissionsChangedPush')
const { markFormerMember } = require('../utils/membershipItem')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
exports.handler = async (event) => {
try {
const flowId = event.pathParameters?.flowId
const targetUserId = event.pathParameters?.userId
const currentUserId = event.requestContext?.authorizer?.claims?.sub
if (!currentUserId || !flowId || !targetUserId) {
return {
statusCode: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Unauthorized' })
}
}
// Get flow info to check ownership
const flow = await docClient.send(
new GetCommand({
TableName: process.env.FLOWS_TABLE_NAME,
Key: { id: flowId }
})
)
if (!flow.Item) {
return {
statusCode: 404,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Flow not found' })
}
}
// Check authorization: user can remove themselves, or a role holding
// 'members:remove' can remove non-self members. The owner always holds the
// flow's system (admin) role, which grants this permission, so no separate
// owner bypass is needed here.
const isSelf = targetUserId === currentUserId
if (!isSelf) {
const permissionResult = await permissionChecker.hasPermission(
currentUserId,
flowId,
'members:remove'
)
if (!permissionResult.hasPermission) {
return {
statusCode: 403,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
error:
'Only the owner, the user themselves, or a member with members:remove can remove this membership'
})
}
}
}
// Prevent owner from removing themselves
if (flow.Item.ownerId === targetUserId) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Owner cannot be removed from flow' })
}
}
// Their API keys for this flow go first, while the membership still exists:
// if this fails the request fails and can simply be retried, instead of
// leaving a non-member whose keys keep working. A key acts with its own
// roles, independently of its creator's membership.
if (process.env.API_KEYS_TABLE) {
await deleteUserApiKeysInFlow(docClient, {
apiKeysTableName: process.env.API_KEYS_TABLE,
userId: targetUserId,
flowId
})
}
// Delete from MEMBERSHIPS_TABLE
await docClient.send(
new DeleteCommand({
TableName: process.env.FLOW_MEMBERSHIPS_TABLE_NAME,
Key: { userId: targetUserId, flowId }
})
)
// The removed member's open sessions drop the flow's permissions right away
await notifyPermissionsChanged([targetUserId], flowId)
// onDelete cascade: items in this flow that reference the removed member
// through a User field get SET_NULL (or a placeholder if the field is
// mandatory). Public entities can never have a User field, so this never
// needs to look outside the flow. With a former member state, the
// membership items move to it first and keep their member field
// (UC-SPACE-006.1).
if (process.env.ENTITIES_TABLE_NAME && process.env.ITEMS_TABLE_NAME) {
const { keep } = await markFormerMember({
docClient,
itemsTableName: process.env.ITEMS_TABLE_NAME,
flow: flow.Item,
userId: targetUserId
})
await cascadeMemberRemovalReferences({
docClient,
entitiesTableName: process.env.ENTITIES_TABLE_NAME,
itemsTableName: process.env.ITEMS_TABLE_NAME,
flowId,
removedUserId: targetUserId,
...(keep && { keep })
})
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ message: 'Membership removed successfully' })
}
} catch (error) {
console.error('Error removing member:', error)
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Failed to remove member' })
}
}
}
|