Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(API): Add user management endpoints to the Projects Public API #12329

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

MarcL
Copy link
Contributor

@MarcL MarcL commented Dec 20, 2024

Summary

Adds the ability to add and delete users to and from projects using the public API endpoint:

POST /projects/<project-id>/users/
DELETE /projects/<project-id>/users/<user-id>

Also allow the ability to alter user roles in projects.

Note: There were problems getting the integration test server to work when I referenced the projectId.yml schema within the OpenAPI spec rather than inlined it. As we'll be looking to improve the public API at some point in the future, I've moved all the references to inline instead.

Related Linear tickets, Github issues, and Community forum posts

Partially fixes: PAY-1852

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Dec 20, 2024
Copy link

codecov bot commented Dec 20, 2024

Codecov Report

Attention: Patch coverage is 90.78947% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ublic-api/v1/handlers/projects/projects.handler.ts 78.78% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@MarcL MarcL marked this pull request as draft December 20, 2024 14:30
@MarcL MarcL changed the title feat(API): Allow deletion of user from projects feat(API): Allow user to be added to and removed from projects Dec 20, 2024
@netroy netroy force-pushed the pay-1852-public-api-delete-users-from-project branch from 3c650ef to e9d96f5 Compare January 9, 2025 19:45
@netroy netroy changed the title feat(API): Allow user to be added to and removed from projects feat(API): Add user management endpoints to the Projects Public API Jan 13, 2025
@netroy netroy marked this pull request as ready for review January 13, 2025 16:12
@@ -76,8 +81,8 @@ export class ProjectService {
}

const project = await this.getProjectWithScope(user, projectId, ['project:delete']);
if (!project) {
throw new NotFoundError(`Could not find project with ID: ${projectId}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-added the project ID. That should make it easier to debug this when a user reports this.

Comment on lines 97 to 103
// 0. check if this is a team project
if (project.type !== 'team') {
throw new ForbiddenError(
`Can't delete project. Project with ID "${projectId}" is not a team project.`,
);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to send back a ForbiddenError instead of a NotFound. The user has access to the project, they are just not allowed to delete it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the public API DTOs should be in it's own folder?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if the public API should define their own DTOs. We don't want to give the public API more power by accident when we add more power to the internal API.

@@ -20,7 +20,7 @@ export const projectRoleSchema = z.enum([
export type ProjectRole = z.infer<typeof projectRoleSchema>;

export const projectRelationSchema = z.object({
userId: z.string(),
userId: z.string().min(1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (non-blocking): Why the min(1)?

userId: z.string(),
role: projectRoleSchema,
userId: z.string().min(1),
role: projectRoleSchema.exclude(['project:personalOwner']),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've excluded the project:personalOwner here too. Personal projects are special, they can only have one owner with that role. There should not be any API endpoint that allows changing the relation. They are created when the user is created and deleted when the user is deleted.

@@ -211,28 +224,80 @@ export class ProjectService {

async syncProjectRelations(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add a test to make sure this throws when used with personal projects or the personalOwner role.

throw new ProjectNotFoundError(projectId);
}

// TODO: do we need to prevent project owner from being removed?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't, as long as this function only works with team projects.

TODO: add test to make sure this function throws when used on a personal project.

throw new ProjectNotFoundError(projectId);
}

// TODO: do we need to block any specific roles here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this function also should only work with team projects and should disallow the personalOwner role.

TODO: add test for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants