diff --git a/wedpr-components/env-integration/jupyter/build.gradle b/wedpr-components/env-integration/jupyter/build.gradle index b2a0a892..310e81b0 100644 --- a/wedpr-components/env-integration/jupyter/build.gradle +++ b/wedpr-components/env-integration/jupyter/build.gradle @@ -6,4 +6,5 @@ dependencies{ compile project(":wedpr-components-mybatis") compile project(":wedpr-components-uuid") compile project(":wedpr-components-sys-config") + compile project(":wedpr-components-hook") } diff --git a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterConfig.java b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/core/JupyterConfig.java similarity index 86% rename from wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterConfig.java rename to wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/core/JupyterConfig.java index f4badfa9..92e0e258 100644 --- a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterConfig.java +++ b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/core/JupyterConfig.java @@ -13,7 +13,7 @@ * */ -package com.webank.wedpr.components.integration.jupyter.service.impl; +package com.webank.wedpr.components.integration.jupyter.core; import com.webank.wedpr.core.config.WeDPRConfig; @@ -24,6 +24,7 @@ public class JupyterConfig { private static String JUPYTER_HOST_CONFIGUATINON_KEY = WeDPRConfig.apply("wedpr.jupyter.host_configuration_key", "jupyter_entrypoints"); private static String JUPYTER_ENTRYPOINT_SPLITTER = ";"; + private static String JUPYTER_MODULE = "jupyter-integration"; public static String getJupyterHostConfiguatinonKey() { return JUPYTER_HOST_CONFIGUATINON_KEY; @@ -36,4 +37,8 @@ public static Integer getMaxJupyterPerHost() { public static String getJupyterEntrypointSplitter() { return JUPYTER_ENTRYPOINT_SPLITTER; } + + public static String getJupyterModule() { + return JUPYTER_MODULE; + } } diff --git a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterManager.java b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/core/JupyterManager.java similarity index 97% rename from wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterManager.java rename to wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/core/JupyterManager.java index f2041cd2..b0b74226 100644 --- a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterManager.java +++ b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/core/JupyterManager.java @@ -12,9 +12,8 @@ * the License. * */ -package com.webank.wedpr.components.integration.jupyter.service.impl; +package com.webank.wedpr.components.integration.jupyter.core; -import com.webank.wedpr.components.integration.jupyter.core.JupyterStatus; import com.webank.wedpr.components.integration.jupyter.dao.JupyterInfoDO; import com.webank.wedpr.components.integration.jupyter.dao.JupyterMapper; import com.webank.wedpr.components.meta.sys.config.dao.SysConfigMapper; diff --git a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/hook/JupyterUserCallback.java b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/hook/JupyterUserCallback.java new file mode 100644 index 00000000..8d6083c3 --- /dev/null +++ b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/hook/JupyterUserCallback.java @@ -0,0 +1,52 @@ +/* + * Copyright 2017-2025 [webank-wedpr] + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * + */ + +package com.webank.wedpr.components.integration.jupyter.hook; + +import com.webank.wedpr.components.hook.UserHook; +import com.webank.wedpr.components.integration.jupyter.core.JupyterManager; +import com.webank.wedpr.core.config.WeDPRCommonConfig; +import com.webank.wedpr.core.utils.WeDPRException; +import org.apache.commons.lang3.StringUtils; + +public class JupyterUserCallback implements UserHook.UserCallback { + private final JupyterManager jupyterManager; + + public JupyterUserCallback(JupyterManager jupyterManager) { + this.jupyterManager = jupyterManager; + } + + @Override + public boolean interruptOnException() { + return false; + } + + @Override + public void onCreated(String user) throws Exception { + this.jupyterManager.allocateJupyter(user, WeDPRCommonConfig.getAgency()); + } + + // do nothing + @Override + public void onUpdated(String user) throws Exception {} + + @Override + public void onDeleted(String user) throws Exception { + if (StringUtils.isBlank(user)) { + throw new WeDPRException("Delete jupyter failed, must specify the jupyter user"); + } + this.jupyterManager.deleteJupyter(user, null); + } +} diff --git a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterServiceImpl.java b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterServiceImpl.java index a76508a5..83deda2d 100644 --- a/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterServiceImpl.java +++ b/wedpr-components/env-integration/jupyter/src/main/java/com/webank/wedpr/components/integration/jupyter/service/impl/JupyterServiceImpl.java @@ -15,8 +15,12 @@ package com.webank.wedpr.components.integration.jupyter.service.impl; +import com.webank.wedpr.components.hook.UserHook; +import com.webank.wedpr.components.integration.jupyter.core.JupyterConfig; +import com.webank.wedpr.components.integration.jupyter.core.JupyterManager; import com.webank.wedpr.components.integration.jupyter.dao.JupyterInfoDO; import com.webank.wedpr.components.integration.jupyter.dao.JupyterMapper; +import com.webank.wedpr.components.integration.jupyter.hook.JupyterUserCallback; import com.webank.wedpr.components.integration.jupyter.service.JupyterService; import com.webank.wedpr.components.meta.sys.config.dao.SysConfigMapper; import com.webank.wedpr.core.config.WeDPRCommonConfig; @@ -33,11 +37,14 @@ public class JupyterServiceImpl implements JupyterService { private @Autowired SysConfigMapper sysConfigMapper; private @Autowired JupyterMapper jupyterMapper; + private @Autowired UserHook userHook; private JupyterManager jupyterManager; @PostConstruct public void init() { this.jupyterManager = new JupyterManager(sysConfigMapper, jupyterMapper); + userHook.registerUserCallback( + JupyterConfig.getJupyterModule(), new JupyterUserCallback(this.jupyterManager)); } /** * allocate the jupyter environment for given user diff --git a/wedpr-components/env-integration/jupyter/src/main/resources/mapper/JupyterMapper.xml b/wedpr-components/env-integration/jupyter/src/main/resources/mapper/JupyterMapper.xml index 432a1708..be3d950e 100644 --- a/wedpr-components/env-integration/jupyter/src/main/resources/mapper/JupyterMapper.xml +++ b/wedpr-components/env-integration/jupyter/src/main/resources/mapper/JupyterMapper.xml @@ -59,8 +59,10 @@ - public int deleteJupyterInfo(@Param("id")String id); - delete from `wedpr_jupyter_table` where `id` = #{id} + delete from `wedpr_jupyter_table` where 1 = 1 + + and `id` = #{id} + and `owner` = #{owner} diff --git a/wedpr-components/user/src/main/java/com/webank/wedpr/components/user/controller/WedprGroupController.java b/wedpr-components/user/src/main/java/com/webank/wedpr/components/user/controller/WedprGroupController.java index 0fec7a6a..e99ad951 100644 --- a/wedpr-components/user/src/main/java/com/webank/wedpr/components/user/controller/WedprGroupController.java +++ b/wedpr-components/user/src/main/java/com/webank/wedpr/components/user/controller/WedprGroupController.java @@ -3,6 +3,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.webank.wedpr.components.hook.UserHook; import com.webank.wedpr.components.token.auth.TokenUtils; import com.webank.wedpr.components.token.auth.model.GroupInfo; import com.webank.wedpr.components.token.auth.model.UserToken; @@ -55,6 +56,7 @@ public class WedprGroupController { @Autowired private WedprUserRoleService wedprUserRoleService; @Autowired private ApplicationEventPublisher applicationEventPublisher; + @Autowired private UserHook userHook; /** * 创建用户组,检查用户组名是否存在,创建用户组失败时记录警告日志 @@ -351,6 +353,10 @@ public WeDPRResponse deleteGroupUser( LambdaQueryWrapper lambdaQueryWrapper2 = new LambdaQueryWrapper<>(); lambdaQueryWrapper2.eq(WedprGroupDetail::getUsername, username); wedprGroupDetailService.remove(lambdaQueryWrapper2); + + // call userHook + userHook.onUserDeleted(username); + CreateWedprGroupResponse createWedprGroupResponse = new CreateWedprGroupResponse(); createWedprGroupResponse.setGroupId(groupId); wedprResponse.setData(createWedprGroupResponse);