4ce93aee52
**Before** - any user with workpace_members permission was able to remove a user from their workspace. This triggered the deletion of workspaceMember + of userWorkspace, but did not delete the user (even if they had no workspace left) nor the roleTarget (acts as junction between role and userWorkspace) which was left with a userWorkspaceId pointing to nothing. This is because roleTarget points to userWorkspaceId but the foreign key constraint was not implemented - any user could delete their own account. This triggered the deletion of all their workspaceMembers, but not of their userWorkspace nor their user nor the roleTarget --> we have orphaned userWorkspace, not technically but product wise - a userWorkspace without a workspaceMember does not make sense So the problems are - we have some roleTargets pointing to non-existing userWorkspaceId (which caused https://github.com/twentyhq/twenty/issues/14608 ) - we have userWorkspaces that should not exist and that have no workspaceMember counterpart - it is not possible for a user to leave a workspace by themselves, they can only leave all workspaces at once, except if they are being removed from the workspace by another user **Now** - if a user has multiple workspaces, they are given the possibility to leave one workspace while remaining in the others (we show two buttons: Leave workspace and Delete account buttons). if a user has just one workspace, they only see Delete account - when a user leaves a workspace, we delete their workspaceMember, userWorkspace and roleTarget. If they don't belong to any other workspace we also soft-delete their user - soft-deleted users get hard deleted after 30 days thanks to a cron - we have two commands to clean the orphans roleTarget and userWorkspace (TODO: query db to see how many must be run) **Next** - once the commands have been run, we can implement and introduce the foreign key constraint on roleTarget Fixes https://github.com/twentyhq/twenty/issues/14608
106 lines
3.8 KiB
TypeScript
106 lines
3.8 KiB
TypeScript
import { deleteOneOperationFactory } from 'test/integration/graphql/utils/delete-one-operation-factory.util';
|
|
import { makeGraphqlAPIRequestWithMemberRole } from 'test/integration/graphql/utils/make-graphql-api-request-with-member-role.util';
|
|
import { updateOneOperationFactory } from 'test/integration/graphql/utils/update-one-operation-factory.util';
|
|
|
|
import { ErrorCode } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
|
import { PermissionsExceptionMessage } from 'src/engine/metadata-modules/permissions/permissions.exception';
|
|
import { WORKSPACE_MEMBER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/data/constants/workspace-member-data-seeds.constant';
|
|
|
|
const WORKSPACE_MEMBER_GQL_FIELDS = `
|
|
id
|
|
name {
|
|
firstName
|
|
}
|
|
`;
|
|
|
|
describe('workspace members permissions', () => {
|
|
it('should allow update when user is updating themself (member role)', async () => {
|
|
const graphqlOperation = updateOneOperationFactory({
|
|
objectMetadataSingularName: 'workspaceMember',
|
|
gqlFields: WORKSPACE_MEMBER_GQL_FIELDS,
|
|
recordId: WORKSPACE_MEMBER_DATA_SEED_IDS.JONY,
|
|
data: {
|
|
name: {
|
|
firstName: 'Jony',
|
|
},
|
|
},
|
|
});
|
|
|
|
const response =
|
|
await makeGraphqlAPIRequestWithMemberRole(graphqlOperation);
|
|
|
|
expect(response.body.errors).not.toBeDefined();
|
|
expect(response.body.data).toStrictEqual({
|
|
updateWorkspaceMember: {
|
|
id: WORKSPACE_MEMBER_DATA_SEED_IDS.JONY,
|
|
name: {
|
|
firstName: 'Jony',
|
|
},
|
|
},
|
|
});
|
|
expect(response.body.errors).toBeUndefined();
|
|
});
|
|
it('should throw when user does not have permission (member role)', async () => {
|
|
const graphqlOperation = updateOneOperationFactory({
|
|
objectMetadataSingularName: 'workspaceMember',
|
|
gqlFields: WORKSPACE_MEMBER_GQL_FIELDS,
|
|
recordId: WORKSPACE_MEMBER_DATA_SEED_IDS.TIM,
|
|
data: {
|
|
name: {
|
|
firstName: 'Not Tim',
|
|
},
|
|
},
|
|
});
|
|
|
|
const response =
|
|
await makeGraphqlAPIRequestWithMemberRole(graphqlOperation);
|
|
|
|
expect(response.body.data).toStrictEqual({ updateWorkspaceMember: null });
|
|
expect(response.body.errors).toBeDefined();
|
|
expect(response.body.errors[0].message).toBe(
|
|
PermissionsExceptionMessage.PERMISSION_DENIED,
|
|
);
|
|
expect(response.body.errors[0].extensions.code).toBe(ErrorCode.FORBIDDEN);
|
|
});
|
|
|
|
it('should throw when calling deleteOne ', async () => {
|
|
const graphqlOperation = deleteOneOperationFactory({
|
|
objectMetadataSingularName: 'workspaceMember',
|
|
gqlFields: WORKSPACE_MEMBER_GQL_FIELDS,
|
|
recordId: WORKSPACE_MEMBER_DATA_SEED_IDS.TIM,
|
|
});
|
|
|
|
const response =
|
|
await makeGraphqlAPIRequestWithMemberRole(graphqlOperation);
|
|
|
|
expect(response.body.data).toStrictEqual({ deleteWorkspaceMember: null });
|
|
expect(response.body.errors).toBeDefined();
|
|
expect(response.body.errors[0].message).toBe(
|
|
'Please use /deleteUserFromWorkspace to remove a workspace member.',
|
|
);
|
|
expect(response.body.errors[0].extensions.code).toBe(
|
|
ErrorCode.BAD_USER_INPUT,
|
|
);
|
|
});
|
|
|
|
it('should throw when calling deleteMany', async () => {
|
|
const graphqlOperation = deleteOneOperationFactory({
|
|
objectMetadataSingularName: 'workspaceMember',
|
|
gqlFields: WORKSPACE_MEMBER_GQL_FIELDS,
|
|
recordId: WORKSPACE_MEMBER_DATA_SEED_IDS.TIM,
|
|
});
|
|
|
|
const response =
|
|
await makeGraphqlAPIRequestWithMemberRole(graphqlOperation);
|
|
|
|
expect(response.body.data).toStrictEqual({ deleteWorkspaceMember: null });
|
|
expect(response.body.errors).toBeDefined();
|
|
expect(response.body.errors[0].message).toBe(
|
|
'Please use /deleteUserFromWorkspace to remove a workspace member.',
|
|
);
|
|
expect(response.body.errors[0].extensions.code).toBe(
|
|
ErrorCode.BAD_USER_INPUT,
|
|
);
|
|
});
|
|
});
|