Skip to content

Commit

Permalink
add jupyter as userHook
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Aug 28, 2024
1 parent fa836fd commit af2edc6
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 81 deletions.
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ project(":wedpr-components-transport").projectDir=file("wedpr-components/transpo
include ":wedpr-components-jupyter-intergration"
project(":wedpr-components-jupyter-intergration").projectDir=file("wedpr-components/env-integration/jupyter")

include "wedpr-components-hook"
project(":wedpr-components-hook").projectDir=file("wedpr-components/hook")

include "wedpr-components-user"
project(":wedpr-components-user").projectDir=file("wedpr-components/user")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void init() {
* @param agency the agency of the person
* @return success or failed
*/
@Override
public String allocate(String user, String agency) throws Exception {
return this.jupyterManager.allocateJupyter(user, agency);
}
Expand All @@ -56,6 +57,7 @@ public String allocate(String user, String agency) throws Exception {
* @param condition
* @return
*/
@Override
public List<JupyterInfoDO> queryJupyters(
boolean admin, String queryUser, JupyterInfoDO condition) throws Exception {
if (!admin) {
Expand All @@ -71,6 +73,7 @@ public List<JupyterInfoDO> queryJupyters(
* @param id specify the jupyter to destory
* @return success/failed
*/
@Override
public boolean destroy(boolean admin, String currentUser, String id) {
// the admin can delete all jupyter
if (admin) {
Expand All @@ -85,6 +88,7 @@ public boolean destroy(boolean admin, String currentUser, String id) {
* @param id the jupyter id
* @return success/failed
*/
@Override
public JupyterInfoDO open(String currentUser, String id) throws Exception {
return this.jupyterManager.openJupyter(currentUser, id);
}
Expand All @@ -95,6 +99,7 @@ public JupyterInfoDO open(String currentUser, String id) throws Exception {
* @param id specify the jupyter to close
* @return success/failed
*/
@Override
public JupyterInfoDO close(String currentUser, String id) throws Exception {
return this.jupyterManager.closeJupyter(currentUser, id);
}
Expand Down
7 changes: 7 additions & 0 deletions wedpr-components/hook/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Apply the java-library plugin to add support for Java Library
plugins {
id 'java'
}
dependencies{
compile project(":wedpr-core-utils")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.hook;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UserHook {
private static final Logger logger = LoggerFactory.getLogger(UserHook.class);

public interface UserCallback {
boolean interruptOnException();

void onCreated(String user) throws Exception;

void onUpdated(String user) throws Exception;

void onDeleted(String user) throws Exception;
}

public enum Action {
CREATE_USER,
UPDATE_USER,
DELETE_USER
}

private Map<String, UserCallback> callbacks;

public synchronized void registerUserCallback(String module, UserCallback callback) {
callbacks.put(module, callback);
}

private synchronized void triggerCallback(Action action, String user) throws Exception {
if (callbacks.isEmpty()) {
return;
}
for (String module : callbacks.keySet()) {
UserCallback callback = callbacks.get(module);
try {
switch (action) {
case CREATE_USER:
{
callback.onCreated(user);
continue;
}
case UPDATE_USER:
{
callback.onUpdated(user);
continue;
}
case DELETE_USER:
{
callback.onDeleted(user);
continue;
}
default:
continue;
}
} catch (Exception e) {
logger.warn("Trigger callback for module {} failed, reason: ", module, e);
if (callback.interruptOnException()) {
throw e;
}
}
}
}

public synchronized void onUserCreated(String user) throws Exception {
triggerCallback(Action.CREATE_USER, user);
}

public synchronized void onUserUpdated(String user) throws Exception {
triggerCallback(Action.UPDATE_USER, user);
}

public synchronized void onUserDeleted(String user) throws Exception {
triggerCallback(Action.DELETE_USER, user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@
<if test="condition.name != null and condition.name !=''">
and `name` like concat(#{condition.name}, '%')
</if>
<if test="condition.owner != null and condition.owner !=''">
<if test="condition.type != null and condition.type !=''">
and `type` = condition.type
</if>
<if test="condition.owner != null and condition.owner !=''">
and `owner` = #{condition.owner}
</if>
<if test="condition.agency != null and condition.agency !=''">
Expand Down
1 change: 1 addition & 0 deletions wedpr-components/user/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dependencies {
compile project(":wedpr-core-protocol")
compile project(":wedpr-components-uuid")
compile project(":wedpr-components-mybatis")
compile project(":wedpr-components-hook")
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package com.webank.wedpr.components.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.webank.wedpr.components.user.config.UserJwtConfig;
import com.webank.wedpr.components.user.entity.WedprGroupDetail;
import com.webank.wedpr.components.user.entity.WedprUser;
import com.webank.wedpr.components.user.entity.WedprUserRole;
import com.webank.wedpr.components.user.helper.PasswordHelper;
import com.webank.wedpr.components.user.helper.TokenImageHelper;
import com.webank.wedpr.components.user.requests.UserRegisterRequest;
import com.webank.wedpr.components.user.response.WedpRegisterResponse;
import com.webank.wedpr.components.user.response.WedprImageCodeResponse;
import com.webank.wedpr.components.user.response.WedprPublicKeyResponse;
import com.webank.wedpr.components.user.service.WedprGroupDetailService;
import com.webank.wedpr.components.user.service.WedprUserRoleService;
import com.webank.wedpr.components.user.service.WedprUserService;
import com.webank.wedpr.core.protocol.UserRoleEnum;
import com.webank.wedpr.core.utils.Constant;
import com.webank.wedpr.core.utils.WeDPRException;
import com.webank.wedpr.core.utils.WeDPRResponse;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -55,80 +45,13 @@ public class WedprUserRegisterController {
/**
* 用户注册
*
* @param userRegisterRequest
* @param userRegisterRequest the register request
* @return
*/
@PostMapping("/register")
@Transactional
public WeDPRResponse register(@Valid @RequestBody UserRegisterRequest userRegisterRequest) {
try {
// 检查用户名是否存在
LambdaQueryWrapper<WedprUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
String username = userRegisterRequest.getUsername();
lambdaQueryWrapper.eq(WedprUser::getUsername, username);
WedprUser queriedWedprUser = wedprUserService.getOne(lambdaQueryWrapper);
if (queriedWedprUser != null) {
return new WeDPRResponse(Constant.WEDPR_FAILED, "用户名已存在");
}
WedprUser wedprUser = getRegisterWedprUser(userRegisterRequest);
WedprUserRole wedprUserRole =
WedprUserRole.builder()
.username(username)
.roleId(UserRoleEnum.ORIGINAL_USER.getRoleId())
.createBy(username)
.updateBy(username)
.build();
wedprUserService.save(wedprUser);
wedprUserRoleService.save(wedprUserRole);

// 设置默认群组
WedprGroupDetail wedprGroupDetail = new WedprGroupDetail();
wedprGroupDetail.setGroupId(Constant.DEFAULT_INIT_GROUP_ID);
wedprGroupDetail.setUsername(username);
wedprGroupDetail.setCreateBy(username);
wedprGroupDetail.setUpdateBy(username);
wedprGroupDetailService.save(wedprGroupDetail);

WeDPRResponse wedprResponse =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
WedpRegisterResponse wedpRegisterResponse = new WedpRegisterResponse();
wedpRegisterResponse.setUsername(wedprUser.getUsername());
wedprResponse.setData(wedpRegisterResponse);
return wedprResponse;
} catch (Exception e) {
log.warn("注册用户失败", e);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return new WeDPRResponse(Constant.WEDPR_FAILED, "注册用户失败:" + e.getMessage());
}
}

private WedprUser getRegisterWedprUser(UserRegisterRequest userRegisterRequest)
throws WeDPRException {
WedprUser wedprUser = new WedprUser();
String username = userRegisterRequest.getUsername();
wedprUser.setUsername(username);
wedprUser.setTryCount(0);
wedprUser.setCreateBy(username);
wedprUser.setUpdateBy(username);
String password = userRegisterRequest.getPassword();
String plainPassword =
PasswordHelper.decryptPassword(password, userJwtConfig.getPrivateKey());
if (PasswordHelper.isStrongNonValid(plainPassword)) {
throw new WeDPRException("注册密码至少包含8个字符,并且至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符");
}
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String encodePassword =
Constant.ENCRYPT_PREFIX + bCryptPasswordEncoder.encode(plainPassword);
wedprUser.setPassword(encodePassword);
String phone = userRegisterRequest.getPhone();
if (StringUtils.hasLength(phone)) {
wedprUser.setPhone(phone);
}
String email = userRegisterRequest.getEmail();
if (StringUtils.hasLength(email)) {
wedprUser.setEmail(email);
}
return wedprUser;
return wedprUserService.register(userRegisterRequest);
}

@GetMapping("/pub")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.webank.wedpr.components.user.entity.WedprGroupDetail;
import com.webank.wedpr.components.user.response.WedprUserResponse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
Expand All @@ -13,6 +14,7 @@
* @author caryliao
* @since 2024-07-15
*/
@Mapper
public interface WedprGroupDetailMapper extends BaseMapper<WedprGroupDetail> {

IPage<WedprUserResponse> selectGroupUserPage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.webank.wedpr.components.user.entity.WedprUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

Expand All @@ -11,6 +12,7 @@
* @author caryliao
* @since 2024-07-15
*/
@Mapper
public interface WedprUserMapper extends BaseMapper<WedprUser> {

@Update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.webank.wedpr.components.user.entity.WedprUserRole;
import com.webank.wedpr.components.user.entity.result.WedprUserRoleResult;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

Expand All @@ -13,6 +14,7 @@
* @author caryliao
* @since 2024-07-15
*/
@Mapper
public interface WedprUserRoleMapper extends BaseMapper<WedprUserRole> {

@Select(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.webank.wedpr.components.user.config.UserJwtConfig;
import com.webank.wedpr.components.user.entity.WedprUser;
import com.webank.wedpr.components.user.requests.LoginRequest;
import com.webank.wedpr.components.user.requests.UserRegisterRequest;
import com.webank.wedpr.core.utils.WeDPRException;
import com.webank.wedpr.core.utils.WeDPRResponse;

/**
* 服务类
Expand All @@ -21,4 +23,6 @@ public interface WedprUserService extends IService<WedprUser> {
/** 检查用户登录 */
void checkWedprUserLoginReturn(LoginRequest request, UserJwtConfig userJwtConfig)
throws WeDPRException;

WeDPRResponse register(UserRegisterRequest userRegisterRequest);
}
Loading

0 comments on commit af2edc6

Please sign in to comment.