This is an automated email from the ASF dual-hosted git repository.
dengliming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 2e56cb7ea [ISSUE #3381] Fix change password error. (#3898)
2e56cb7ea is described below
commit 2e56cb7eadfdc167168cf9092f4e2b0cdc5e1ea2
Author: Kevin Clair <[email protected]>
AuthorDate: Tue Aug 30 22:45:49 2022 +0800
[ISSUE #3381] Fix change password error. (#3898)
---
.../admin/controller/DashboardUserController.java | 32 +++---
.../model/dto/DashboardUserModifyPasswordDTO.java | 108 +++++++++++++++++++++
.../shenyu/admin/model/entity/DashboardUserDO.java | 31 ++++--
.../shenyu/admin/service/DashboardUserService.java | 9 ++
.../service/impl/DashboardUserServiceImpl.java | 37 +++++--
5 files changed, 190 insertions(+), 27 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
index d769e8b75..5058163df 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
@@ -17,19 +17,12 @@
package org.apache.shenyu.admin.controller;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
-import javax.validation.Valid;
-import javax.validation.constraints.NotBlank;
-import javax.validation.constraints.NotEmpty;
-import javax.validation.constraints.NotNull;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.mapper.DashboardUserMapper;
import org.apache.shenyu.admin.model.custom.UserInfo;
import org.apache.shenyu.admin.model.dto.DashboardUserDTO;
+import org.apache.shenyu.admin.model.dto.DashboardUserModifyPasswordDTO;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.page.PageParameter;
import org.apache.shenyu.admin.model.query.DashboardUserQuery;
@@ -42,6 +35,17 @@ import org.apache.shenyu.admin.validation.annotation.Existed;
import org.apache.shenyu.common.utils.ShaUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import javax.validation.Valid;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@@ -147,8 +151,8 @@ public class DashboardUserController {
/**
* modify dashboard user password.
*
- * @param id primary key.
- * @param dashboardUserDTO dashboard user.
+ * @param id primary key.
+ * @param dashboardUserModifyPasswordDTO dashboard user.
* @return {@linkplain ShenyuAdminResult}
*/
@PutMapping("/modify-password/{id}")
@@ -156,15 +160,17 @@ public class DashboardUserController {
public ShenyuAdminResult modifyPassword(@PathVariable("id")
@Existed(provider =
DashboardUserMapper.class,
message = "user is not
found") final String id,
- @Valid @RequestBody final
DashboardUserDTO dashboardUserDTO) {
+ @Valid @RequestBody final
DashboardUserModifyPasswordDTO dashboardUserModifyPasswordDTO) {
UserInfo userInfo = (UserInfo)
SecurityUtils.getSubject().getPrincipal();
if (Objects.isNull(userInfo)) {
return
ShenyuAdminResult.error(ShenyuResultMessage.DASHBOARD_USER_LOGIN_ERROR);
}
- if (!userInfo.getUserId().equals(id) &&
!userInfo.getUserName().equals(dashboardUserDTO.getUserName())) {
+ dashboardUserModifyPasswordDTO.setId(id);
+ if (!userInfo.getUserId().equals(id) &&
!userInfo.getUserName().equals(dashboardUserModifyPasswordDTO.getUserName())) {
return
ShenyuAdminResult.error(ShenyuResultMessage.DASHBOARD_MODIFY_PASSWORD_ERROR);
}
- return updateDashboardUser(id, dashboardUserDTO);
+
dashboardUserModifyPasswordDTO.setPassword(ShaUtils.shaEncryption(dashboardUserModifyPasswordDTO.getPassword()));
+ return ShenyuAdminResult.success(ShenyuResultMessage.UPDATE_SUCCESS,
dashboardUserService.modifyPassword(dashboardUserModifyPasswordDTO));
}
/**
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/DashboardUserModifyPasswordDTO.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/DashboardUserModifyPasswordDTO.java
new file mode 100644
index 000000000..e8a240fe6
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/DashboardUserModifyPasswordDTO.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shenyu.admin.model.dto;
+
+import java.io.Serializable;
+
+import javax.validation.constraints.NotBlank;
+
+/**
+ * this is dashboard user from by web front.
+ */
+public class DashboardUserModifyPasswordDTO implements Serializable {
+
+ /**
+ * primary key.
+ */
+ private String id;
+
+ /**
+ * user name.
+ */
+ @NotBlank
+ private String userName;
+
+ /**
+ * user password.
+ */
+ @NotBlank
+ private String password;
+
+ public DashboardUserModifyPasswordDTO() {
+ }
+
+ public DashboardUserModifyPasswordDTO(final String id, final String
userName, final String password) {
+ this.id = id;
+ this.userName = userName;
+ this.password = password;
+ }
+
+ /**
+ * Gets the value of id.
+ *
+ * @return the value of id
+ */
+ public String getId() {
+ return id;
+ }
+
+ /**
+ * Sets the id.
+ *
+ * @param id id
+ */
+ public void setId(final String id) {
+ this.id = id;
+ }
+
+ /**
+ * Gets the value of userName.
+ *
+ * @return the value of userName
+ */
+ public String getUserName() {
+ return userName;
+ }
+
+ /**
+ * Sets the userName.
+ *
+ * @param userName userName
+ */
+ public void setUserName(final String userName) {
+ this.userName = userName;
+ }
+
+ /**
+ * Gets the value of password.
+ *
+ * @return the value of password
+ */
+ public String getPassword() {
+ return password;
+ }
+
+ /**
+ * Sets the password.
+ *
+ * @param password password
+ */
+ public void setPassword(final String password) {
+ this.password = password;
+ }
+}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
index 54783646a..349b1069a 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.admin.model.entity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.model.dto.DashboardUserDTO;
+import org.apache.shenyu.admin.model.dto.DashboardUserModifyPasswordDTO;
import org.apache.shenyu.common.utils.UUIDUtils;
import java.sql.Timestamp;
@@ -171,19 +172,37 @@ public final class DashboardUserDO extends BaseDO {
/**
* build dashboardUserDO.
*
- * @param dashboardUserDTO {@linkplain DashboardUserDTO}
+ * @param dashboardUserModifyPasswordDTO {@linkplain
DashboardUserModifyPasswordDTO}
* @return {@linkplain DashboardUserDO}
*/
- public static DashboardUserDO buildDashboardUserDO(final DashboardUserDTO
dashboardUserDTO) {
- return Optional.ofNullable(dashboardUserDTO).map(item -> {
+ public static DashboardUserDO buildDashboardUserDO(final
DashboardUserModifyPasswordDTO dashboardUserModifyPasswordDTO) {
+ return Optional.ofNullable(dashboardUserModifyPasswordDTO).map(item ->
{
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
DashboardUserDO dashboardUserDO = DashboardUserDO.builder()
- .userName(item.getUserName())
.password(item.getPassword())
- .role(item.getRole())
- .roles(item.getRoles())
.dateUpdated(currentTime)
+ .id(item.getId())
.build();
+ return dashboardUserDO;
+ }).orElse(null);
+ }
+
+ /**
+ * build dashboardUserDO.
+ *
+ * @param dashboardUserDTO {@linkplain DashboardUserDTO}
+ * @return {@linkplain DashboardUserDO}
+ */
+ public static DashboardUserDO buildDashboardUserDO(final DashboardUserDTO
dashboardUserDTO) {
+ return Optional.ofNullable(dashboardUserDTO).map(item -> {
+ Timestamp currentTime = new Timestamp(System.currentTimeMillis());
+ DashboardUserDO dashboardUserDO = DashboardUserDO.builder()
+ .userName(item.getUserName())
+ .password(item.getPassword())
+ .role(item.getRole())
+ .roles(item.getRoles())
+ .dateUpdated(currentTime)
+ .build();
if (StringUtils.isEmpty(item.getId())) {
dashboardUserDO.setId(UUIDUtils.getInstance().generateShortUuid());
dashboardUserDO.setEnabled(true);
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DashboardUserService.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DashboardUserService.java
index c2475adcb..67023b2cc 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DashboardUserService.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DashboardUserService.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.admin.service;
import org.apache.shenyu.admin.model.dto.DashboardUserDTO;
+import org.apache.shenyu.admin.model.dto.DashboardUserModifyPasswordDTO;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.query.DashboardUserQuery;
import org.apache.shenyu.admin.model.vo.DashboardUserEditVO;
@@ -104,4 +105,12 @@ public interface DashboardUserService {
* @return {@linkplain LoginDashboardUserVO}
*/
LoginDashboardUserVO login(String userName, String password);
+
+ /**
+ * modify password.
+ *
+ * @param dashboardUserModifyPasswordDTO {@linkplain
DashboardUserModifyPasswordDTO}
+ * @return rows
+ */
+ int modifyPassword(DashboardUserModifyPasswordDTO
dashboardUserModifyPasswordDTO);
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
index 23655548d..96a04aa3a 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
@@ -17,7 +17,6 @@
package org.apache.shenyu.admin.service.impl;
-import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.config.properties.JwtProperties;
@@ -26,6 +25,7 @@ import org.apache.shenyu.admin.mapper.DashboardUserMapper;
import org.apache.shenyu.admin.mapper.RoleMapper;
import org.apache.shenyu.admin.mapper.UserRoleMapper;
import org.apache.shenyu.admin.model.dto.DashboardUserDTO;
+import org.apache.shenyu.admin.model.dto.DashboardUserModifyPasswordDTO;
import org.apache.shenyu.admin.model.dto.UserRoleDTO;
import org.apache.shenyu.admin.model.entity.DashboardUserDO;
import org.apache.shenyu.admin.model.entity.RoleDO;
@@ -45,6 +45,15 @@ import org.apache.shenyu.admin.utils.JwtUtils;
import org.apache.shenyu.admin.utils.ListUtil;
import org.apache.shenyu.common.constant.AdminConstants;
import org.apache.shenyu.common.utils.ShaUtils;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nullable;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ldap.NameNotFoundException;
@@ -53,12 +62,7 @@ import org.springframework.ldap.support.LdapEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import javax.annotation.Nullable;
-import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
+import com.google.common.collect.Lists;
/**
* Implementation of the {@link
org.apache.shenyu.admin.service.DashboardUserService}.
@@ -257,7 +261,24 @@ public class DashboardUserServiceImpl implements
DashboardUserService {
jwtProperties.getExpiredSeconds())).setExpiredTime(jwtProperties.getExpiredSeconds());
}).orElse(null);
}
-
+
+ /**
+ * modify password.
+ *
+ * @param dashboardUserModifyPasswordDTO {@linkplain
DashboardUserModifyPasswordDTO}
+ * @return rows
+ */
+ @Override
+ public int modifyPassword(final DashboardUserModifyPasswordDTO
dashboardUserModifyPasswordDTO) {
+ DashboardUserDO dashboardUserDO =
DashboardUserDO.buildDashboardUserDO(dashboardUserModifyPasswordDTO);
+ DashboardUserDO before =
dashboardUserMapper.selectById(dashboardUserDO.getId());
+ int updateCount = dashboardUserMapper.updateSelective(dashboardUserDO);
+ if (updateCount > 0) {
+ publisher.onUpdated(dashboardUserDO, before);
+ }
+ return updateCount;
+ }
+
private DashboardUserVO loginByLdap(final String userName, final String
password) {
Assert.notNull(ldapProperties, "ldap config is not enable");
String searchBase = String.format("%s=%s,%s",
ldapProperties.getLoginField(), LdapEncoder.nameEncode(userName),
ldapProperties.getBaseDn());