From f695304318571751485ca4d8e846ecc6021017b1 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 9 Jun 2022 18:53:34 +0800 Subject: [PATCH 1/2] fix: throw err if user is already a project user --- .../src/lib/meta/api/projectUserApis.ts | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/lib/meta/api/projectUserApis.ts b/packages/nocodb/src/lib/meta/api/projectUserApis.ts index 42e6f273aa..c10cb81988 100644 --- a/packages/nocodb/src/lib/meta/api/projectUserApis.ts +++ b/packages/nocodb/src/lib/meta/api/projectUserApis.ts @@ -52,24 +52,32 @@ async function userInvite(req, res, next): Promise { for (const email of emails) { // add user to project if user already exist const user = await User.getByEmail(email); + if (user) { + // check if this user has been added to this project + const projectUser = await ProjectUser.get(req.params.projectId, user.id); + if (projectUser) { + NcError.badRequest( + `${user.email} has been added to this project already` + ); + } + // todo : provide a different role await User.update(user.id, { roles: 'user' }); - if (!(await ProjectUser.get(req.params.projectId, user.id))) { - await ProjectUser.insert({ - project_id: req.params.projectId, - fk_user_id: user.id, - roles: req.body.roles || 'editor' - }); - } + await ProjectUser.insert({ + project_id: req.params.projectId, + fk_user_id: user.id, + roles: req.body.roles || 'editor' + }); const cachedUser = await NocoCache.get( `${CacheScope.USER}:${email}___${req.params.projectId}`, CacheGetType.TYPE_OBJECT ); + if (cachedUser) { cachedUser.roles = req.body.roles || 'editor'; await NocoCache.set( From 98a99c6227bb2b1f1040284ff888c508c23edad1 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 9 Jun 2022 19:00:30 +0800 Subject: [PATCH 2/2] chore: revise error msg --- packages/nocodb/src/lib/meta/api/projectUserApis.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nocodb/src/lib/meta/api/projectUserApis.ts b/packages/nocodb/src/lib/meta/api/projectUserApis.ts index c10cb81988..f0f5c00965 100644 --- a/packages/nocodb/src/lib/meta/api/projectUserApis.ts +++ b/packages/nocodb/src/lib/meta/api/projectUserApis.ts @@ -58,7 +58,7 @@ async function userInvite(req, res, next): Promise { const projectUser = await ProjectUser.get(req.params.projectId, user.id); if (projectUser) { NcError.badRequest( - `${user.email} has been added to this project already` + `${user.email} with role ${projectUser.roles} already exists in this project` ); }