Skip to content

Commit

Permalink
[Feature][UI] Add reset password feature (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
zixi0825 authored Jan 6, 2024
1 parent 53352fd commit d30dd3d
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public enum Status {
CREATE_VERIFICATION_IMAGE_ERROR(10020005, "create verification image error", "创建验证码错误"),
EXPIRED_VERIFICATION_CODE(10020006, "expired verification code", "验证码已过期,请重新刷新"),
INVALID_VERIFICATION_CODE(10020007, "invalid verification code", "错误的验证码,请重新输入"),
OLD_PASSWORD_IS_INCORRECT_ERROR(10020004, "Old Password is Incorrect", "旧密码错误"),
NEW_PASSWORD_CONFIRM_IS_INCORRECT_ERROR(10020004, "New Password Confirm is Incorrect", "新密码确认错误"),

WORKSPACE_EXIST_ERROR(11010001, "WorkSpace {0} is Exist error", "工作空间 {0} 已存在错误"),
CREATE_WORKSPACE_ERROR(11010002, "Create WorkSpace {0} Error", "创建工作空间 {0} 错误"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.datavines.engine.local.api.utils.LoggerFactory;
import org.slf4j.Logger;

import java.sql.Statement;

public class LocalRuntimeEnvironment implements RuntimeEnvironment {

protected Logger log = LoggerFactory.getLogger(LocalRuntimeEnvironment.class);
Expand All @@ -34,6 +36,8 @@ public class LocalRuntimeEnvironment implements RuntimeEnvironment {

private ConnectionHolder metadataConnection;

private Statement currentStatement;

@Override
public void prepare() {

Expand Down Expand Up @@ -84,6 +88,10 @@ public void setTargetConnection(ConnectionHolder targetConnection) {
}

public void close() throws Exception {
if (currentStatement != null) {
currentStatement.close();
}

if (sourceConnection != null) {
sourceConnection.close();
}
Expand All @@ -96,4 +104,8 @@ public void close() throws Exception {
metadataConnection.close();
}
}

public void setCurrentStatement(Statement statement) {
this.currentStatement = statement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BaseDataSinkExecutor(Config config, LocalRuntimeEnvironment env) {
this.env = env;
}

protected void innerExecute(Map<String, String> inputParameter) throws SQLException{
protected void innerExecute(Map<String, String> inputParameter) throws SQLException {
executeDataSink(env, inputParameter);
}

Expand All @@ -71,9 +71,16 @@ private void executeDataSink(LocalRuntimeEnvironment env, Map<String,String> inp
}

private void executeInsert(String sql, LocalRuntimeEnvironment env) throws SQLException {
Statement statement = env.getMetadataConnection().getConnection().createStatement();
statement.execute(sql);
statement.close();

Statement statement = null;
try {
statement = env.getMetadataConnection().getConnection().createStatement();
env.setCurrentStatement(statement);
statement.execute(sql);
} finally {
SqlUtils.closeStatement(statement);
env.setCurrentStatement(null);
}
}

private boolean checkTableExist(LocalRuntimeEnvironment env, String tableName) throws SQLException {
Expand All @@ -90,7 +97,7 @@ private boolean checkTableExist(LocalRuntimeEnvironment env, String tableName) t
return flag;
}

private void createTable(LocalRuntimeEnvironment env, String createTableSql) throws SQLException{
private void createTable(LocalRuntimeEnvironment env, String createTableSql) throws SQLException {
Statement statement = env.getMetadataConnection().getConnection().createStatement();
statement.execute(createTableSql);
statement.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private void sinkErrorData() {

try {
sourceConnectionStatement = env.getSourceConnection().getConnection().createStatement();
env.setCurrentStatement(sourceConnectionStatement);
String srcConnectorType = config.getString(SRC_CONNECTOR_TYPE);
ConnectorFactory connectorFactory = PluginLoader.getPluginLoader(ConnectorFactory.class).getOrCreatePlugin(srcConnectorType);

Expand Down Expand Up @@ -172,6 +173,7 @@ private void sinkErrorData() {
}

errorDataPreparedStatement = errorDataStorageConnection.prepareStatement(insertStatement);
env.setCurrentStatement(errorDataPreparedStatement);
for (int i=0; i<totalPage; i++) {
int start = i * pageSize;
int end = (i+1) * pageSize;
Expand Down Expand Up @@ -296,6 +298,7 @@ private void sinkErrorData() {
SqlUtils.closeResultSet(errorDataResultSet);
SqlUtils.closeStatement(errorDataPreparedStatement);
SqlUtils.closeConnection(errorDataStorageConnection);
env.setCurrentStatement(null);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.datavines.engine.local.transform.sql;

import io.datavines.common.config.Config;
import io.datavines.engine.local.api.LocalRuntimeEnvironment;
import io.datavines.engine.local.api.entity.ResultList;
import io.datavines.engine.local.api.utils.SqlUtils;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -32,7 +33,7 @@
public class ActualValueExecutor implements ITransformExecutor {

@Override
public ResultList execute(Connection connection, Config config) throws Exception {
public ResultList execute(Connection connection, Config config, LocalRuntimeEnvironment env) throws Exception {

Statement statement = null;
ResultSet resultSet = null;
Expand All @@ -41,6 +42,7 @@ public ResultList execute(Connection connection, Config config) throws Exception
String sql = config.getString(SQL);

statement = connection.createStatement();
env.setCurrentStatement(statement);
resultSet = statement.executeQuery(sql);
resultList = SqlUtils.getListFromResultSet(resultSet, SqlUtils.getQueryFromsAndJoins(sql));
if (CollectionUtils.isNotEmpty(resultList.getResultList())) {
Expand All @@ -61,6 +63,7 @@ public ResultList execute(Connection connection, Config config) throws Exception
} finally {
SqlUtils.closeResultSet(resultSet);
SqlUtils.closeStatement(statement);
env.setCurrentStatement(null);
}

return resultList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.datavines.engine.local.transform.sql;

import io.datavines.common.config.Config;
import io.datavines.engine.local.api.LocalRuntimeEnvironment;
import io.datavines.engine.local.api.entity.ResultList;
import io.datavines.engine.local.api.utils.SqlUtils;

Expand All @@ -29,14 +30,20 @@
public class ExpectedValueExecutor implements ITransformExecutor {

@Override
public ResultList execute(Connection connection, Config config) throws Exception {
public ResultList execute(Connection connection, Config config, LocalRuntimeEnvironment env) throws Exception {
String sql = config.getString(SQL);

Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultList resultList = SqlUtils.getListFromResultSet(resultSet, SqlUtils.getQueryFromsAndJoins(sql));
resultSet.close();
statement.close();
return resultList;
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
env.setCurrentStatement(statement);
resultSet = statement.executeQuery(sql);
ResultList resultList = SqlUtils.getListFromResultSet(resultSet, SqlUtils.getQueryFromsAndJoins(sql));
return resultList;
} finally {
SqlUtils.closeResultSet(resultSet);
SqlUtils.closeStatement(statement);
env.setCurrentStatement(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package io.datavines.engine.local.transform.sql;

import io.datavines.common.config.Config;
import io.datavines.engine.local.api.LocalRuntimeEnvironment;
import io.datavines.engine.local.api.entity.ResultList;

import java.sql.Connection;

public interface ITransformExecutor {
ResultList execute(Connection connection, Config config) throws Exception;
ResultList execute(Connection connection, Config config, LocalRuntimeEnvironment env) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.datavines.engine.local.transform.sql;

import io.datavines.common.config.Config;
import io.datavines.engine.local.api.LocalRuntimeEnvironment;
import io.datavines.engine.local.api.entity.ResultList;
import io.datavines.engine.local.api.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -30,15 +31,20 @@
public class InvalidateItemsExecutor implements ITransformExecutor {

@Override
public ResultList execute(Connection connection, Config config) throws Exception {
public ResultList execute(Connection connection, Config config, LocalRuntimeEnvironment env) throws Exception {

String outputTable = config.getString(INVALIDATE_ITEMS_TABLE);
String sql = config.getString(SQL);

Statement statement = connection.createStatement();
SqlUtils.dropView(outputTable, statement);
statement.execute("CREATE VIEW " + outputTable + " AS " + sql);
statement.close();
Statement statement = null;
try {
statement = connection.createStatement();
env.setCurrentStatement(statement);
SqlUtils.dropView(outputTable, statement);
statement.execute("CREATE VIEW " + outputTable + " AS " + sql);
} finally {
SqlUtils.closeStatement(statement);
env.setCurrentStatement(null);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ public ResultList process(LocalRuntimeEnvironment env) {
logger.info("transform sql is: {}, transform_type is : {}", sql, pluginType);
switch (TransformType.of(pluginType)){
case INVALIDATE_ITEMS :
resultList = new InvalidateItemsExecutor().execute(env.getSourceConnection().getConnection(), config);
resultList = new InvalidateItemsExecutor().execute(env.getSourceConnection().getConnection(), config, env);
break;
case ACTUAL_VALUE :
resultList = new ActualValueExecutor().execute(env.getSourceConnection().getConnection(), config);
resultList = new ActualValueExecutor().execute(env.getSourceConnection().getConnection(), config, env);
break;
case EXPECTED_VALUE_FROM_METADATA_SOURCE :
resultList = new ExpectedValueExecutor().execute(env.getMetadataConnection().getConnection(), config);
resultList = new ExpectedValueExecutor().execute(env.getMetadataConnection().getConnection(), config, env);
break;
case EXPECTED_VALUE_FROM_SOURCE :
resultList = new ExpectedValueExecutor().execute(env.getSourceConnection().getConnection(), config);
resultList = new ExpectedValueExecutor().execute(env.getSourceConnection().getConnection(), config, env);
break;
case EXPECTED_VALUE_FROM_TARGET_SOURCE:
resultList = new ExpectedValueExecutor().execute(env.getTargetConnection().getConnection(), config);
resultList = new ExpectedValueExecutor().execute(env.getTargetConnection().getConnection(), config, env);
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.datavines.common.exception.DataVinesException;
import io.datavines.core.constant.DataVinesConstants;
import io.datavines.core.aop.RefreshToken;
import io.datavines.server.api.dto.bo.user.UserResetPassword;
import io.datavines.server.repository.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand All @@ -47,8 +48,8 @@ public Object update(@Valid @RequestBody UserLogin userLogin) throws DataVinesEx

@ApiOperation(value = "reset password")
@PostMapping(value = "/resetPassword", consumes = MediaType.APPLICATION_JSON_VALUE)
public Object resetPassword(@Valid @RequestBody UserRegister userRegister) throws DataVinesException {
return null;
public Object resetPassword(@Valid @RequestBody UserResetPassword userResetPassword) throws DataVinesException {
return userService.resetPassword(userResetPassword);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public class UserResetPassword implements Serializable {
@NotBlank(message = "new password must not be null")
@Pattern(regexp = CommonConstants.REG_USER_PASSWORD, message = "password length must between 6-20")
private String newPassword;

@NotBlank(message = "new password comfirm must not be null")
@Pattern(regexp = CommonConstants.REG_USER_PASSWORD, message = "password length must between 6-20")
private String newPasswordConfirm;
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,25 @@ public Boolean updateUserInfo(UserUpdate userUpdate) {

@Override
public Boolean resetPassword(UserResetPassword userResetPassword) {
return null;
User user = getById(userResetPassword.getId());
if (user == null) {
log.info("User({}) not exist", userResetPassword.getId());
throw new DataVinesServerException(Status.USER_IS_NOT_EXIST_ERROR);
}

boolean checkPassword = BCrypt.checkpw(userResetPassword.getOldPassword(), user.getPassword());
if (checkPassword) {
if (!userResetPassword.getNewPassword().equals(userResetPassword.getNewPasswordConfirm())) {
throw new DataVinesServerException(Status.NEW_PASSWORD_CONFIRM_IS_INCORRECT_ERROR);
}

user.setPassword(BCrypt.hashpw(userResetPassword.getNewPassword(), BCrypt.gensalt()));
user.setUpdateTime(LocalDateTime.now());
updateById(user);
return true;
} else {
throw new DataVinesServerException(Status.OLD_PASSWORD_IS_INCORRECT_ERROR);
}
}

private boolean isUserExist(String username) {
Expand Down
Loading

0 comments on commit d30dd3d

Please sign in to comment.