diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index ac6b38adcc..cfe6fb4e9f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -73,6 +73,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TimeZone; +import java.util.Arrays; import java.util.stream.Collectors; /** @@ -526,7 +527,6 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService { userMapper.queryTenantCodeByUserId(id); - accessTokenMapper.deleteAccessTokenByUserId(id); userMapper.deleteById(id); @@ -562,10 +562,7 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService { if (check(result, StringUtils.isEmpty(projectIds), Status.SUCCESS)) { return result; } - - String[] projectIdArr = projectIds.split(","); - - for (String projectId : projectIdArr) { + Arrays.stream(projectIds.split(",")).distinct().forEach(projectId -> { Date now = new Date(); ProjectUser projectUser = new ProjectUser(); projectUser.setUserId(userId); @@ -574,8 +571,7 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService { projectUser.setCreateTime(now); projectUser.setUpdateTime(now); projectUserMapper.insert(projectUser); - } - + }); putMsg(result, Status.SUCCESS); return result; @@ -614,15 +610,18 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService { return result; } - // 4. maintain the relationship between project and user - final Date today = new Date(); - ProjectUser projectUser = new ProjectUser(); - projectUser.setUserId(userId); - projectUser.setProjectId(project.getId()); - projectUser.setPerm(7); - projectUser.setCreateTime(today); - projectUser.setUpdateTime(today); - this.projectUserMapper.insert(projectUser); + // 4. maintain the relationship between project and user if not exists + ProjectUser projectUser = projectUserMapper.queryProjectRelation(project.getId(), userId); + if (projectUser == null) { + Date today = new Date(); + projectUser = new ProjectUser(); + projectUser.setUserId(userId); + projectUser.setProjectId(project.getId()); + projectUser.setPerm(7); + projectUser.setCreateTime(today); + projectUser.setUpdateTime(today); + this.projectUserMapper.insert(projectUser); + } this.putMsg(result, Status.SUCCESS); return result; diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql index f6cfae00e0..5623b40d99 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_h2.sql @@ -709,7 +709,8 @@ CREATE TABLE t_ds_relation_project_user perm int(11) DEFAULT '1', create_time datetime DEFAULT NULL, update_time datetime DEFAULT NULL, - PRIMARY KEY (id) + PRIMARY KEY (id), + UNIQUE KEY uniq_uid_pid(user_id,project_id) ); -- ---------------------------- diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql index 197173a1b8..40323aa75a 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_mysql.sql @@ -709,7 +709,7 @@ CREATE TABLE `t_ds_relation_project_user` ( `create_time` datetime DEFAULT NULL COMMENT 'create time', `update_time` datetime DEFAULT NULL COMMENT 'update time', PRIMARY KEY (`id`), - KEY `user_id_index` (`user_id`) USING BTREE + UNIQUE KEY uniq_uid_pid(user_id,project_id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- ---------------------------- diff --git a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql index 7e5526976f..3efee9a53b 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/dolphinscheduler_postgresql.sql @@ -625,7 +625,8 @@ CREATE TABLE t_ds_relation_project_user ( perm int DEFAULT '1' , create_time timestamp DEFAULT NULL , update_time timestamp DEFAULT NULL , - PRIMARY KEY (id) + PRIMARY KEY (id), + CONSTRAINT t_ds_relation_project_user_un UNIQUE (user_id, project_id) ) ; create index relation_project_user_id_index on t_ds_relation_project_user (user_id); diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql index 33dcab5031..b9623ad755 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/mysql/dolphinscheduler_ddl.sql @@ -57,3 +57,8 @@ d// delimiter ; CALL uc_dolphin_T_t_ds_alert_R_sign; DROP PROCEDURE uc_dolphin_T_t_ds_alert_R_sign; + +-- add unique key to t_ds_relation_project_user +ALTER TABLE t_ds_relation_project_user ADD UNIQUE KEY uniq_uid_pid(user_id,project_id); + + diff --git a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql index 71de54fe23..9f70bf422a 100644 --- a/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql +++ b/dolphinscheduler-dao/src/main/resources/sql/upgrade/2.0.6_schema/postgresql/dolphinscheduler_ddl.sql @@ -45,4 +45,8 @@ $BODY$; select dolphin_update_metadata(); -d// \ No newline at end of file +d// + +-- add unique key to t_ds_relation_project_user +CREATE UNIQUE INDEX t_ds_relation_project_user_un + on t_ds_relation_project_user (user_id, project_id); \ No newline at end of file