diff --git a/server/src/main/java/io/edurt/datacap/server/body/UserPasswordBody.java b/server/src/main/java/io/edurt/datacap/server/body/UserPasswordBody.java new file mode 100644 index 00000000..9a2626a5 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/body/UserPasswordBody.java @@ -0,0 +1,22 @@ +package io.edurt.datacap.server.body; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import javax.validation.constraints.NotBlank; + +@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class UserPasswordBody +{ + @NotBlank + private String oldPassword; + @NotBlank + private String newPassword; + @NotBlank + private String confirmPassword; +} diff --git a/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java b/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java index ee67a534..8506794d 100644 --- a/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java +++ b/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java @@ -13,6 +13,8 @@ public enum ServiceState USER_UNAUTHORIZED(4003, "Insufficient current user permissions"), USER_EXISTS(4004, "User exists"), USER_BAD_CREDENTIALS(4005, "The account or password is incorrect"), + USER_PASSWORD_INCORRECT(4006, "The user password is incorrect"), + USER_PASSWORD_DIFFERENT(4007, "Two passwords are different"), REQUEST_EXCEPTION(5000, "The request is abnormal"); private Integer code; diff --git a/server/src/main/java/io/edurt/datacap/server/controller/user/UserController.java b/server/src/main/java/io/edurt/datacap/server/controller/user/UserController.java index 17cd96b0..38ce57d3 100644 --- a/server/src/main/java/io/edurt/datacap/server/controller/user/UserController.java +++ b/server/src/main/java/io/edurt/datacap/server/controller/user/UserController.java @@ -1,10 +1,14 @@ package io.edurt.datacap.server.controller.user; +import io.edurt.datacap.server.body.UserPasswordBody; import io.edurt.datacap.server.common.Response; import io.edurt.datacap.server.entity.UserEntity; import io.edurt.datacap.server.service.UserService; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -24,4 +28,10 @@ public class UserController { return this.userService.info(id); } + + @PutMapping(value = "changePassword") + public Response changePassword(@Validated @RequestBody UserPasswordBody configure) + { + return this.userService.changePassword(configure); + } } diff --git a/server/src/main/java/io/edurt/datacap/server/service/UserService.java b/server/src/main/java/io/edurt/datacap/server/service/UserService.java index 6585f76a..ecf99cc0 100644 --- a/server/src/main/java/io/edurt/datacap/server/service/UserService.java +++ b/server/src/main/java/io/edurt/datacap/server/service/UserService.java @@ -1,5 +1,6 @@ package io.edurt.datacap.server.service; +import io.edurt.datacap.server.body.UserPasswordBody; import io.edurt.datacap.server.common.JwtResponse; import io.edurt.datacap.server.common.Response; import io.edurt.datacap.server.entity.UserEntity; @@ -11,4 +12,6 @@ public interface UserService Response authenticate(UserEntity configure); Response info(Long userId); + + Response changePassword(UserPasswordBody configure); } diff --git a/server/src/main/java/io/edurt/datacap/server/service/impl/UserServiceImpl.java b/server/src/main/java/io/edurt/datacap/server/service/impl/UserServiceImpl.java index d39dfcf8..df4f19ec 100644 --- a/server/src/main/java/io/edurt/datacap/server/service/impl/UserServiceImpl.java +++ b/server/src/main/java/io/edurt/datacap/server/service/impl/UserServiceImpl.java @@ -1,5 +1,6 @@ package io.edurt.datacap.server.service.impl; +import io.edurt.datacap.server.body.UserPasswordBody; import io.edurt.datacap.server.common.JwtResponse; import io.edurt.datacap.server.common.Response; import io.edurt.datacap.server.common.ServiceState; @@ -92,4 +93,23 @@ public class UserServiceImpl } return Response.success(this.userRepository.findById(userId).get()); } + + @Override + public Response changePassword(UserPasswordBody configure) + { + Optional userOptional = this.userRepository.findById(UserDetailsService.getUser().getId()); + if (!userOptional.isPresent()) { + return Response.failure(ServiceState.USER_NOT_FOUND); + } + UserEntity user = userOptional.get(); + if (!encoder.matches(configure.getOldPassword(), user.getPassword())) { + return Response.failure(ServiceState.USER_PASSWORD_INCORRECT); + } + if (!configure.getNewPassword().equals(configure.getConfirmPassword())) { + return Response.failure(ServiceState.USER_PASSWORD_DIFFERENT); + } + user.setPassword(encoder.encode(configure.getNewPassword())); + this.userRepository.save(user); + return Response.success(user.getId()); + } } diff --git a/web/console-fe/src/i18n/langs/en/settings.ts b/web/console-fe/src/i18n/langs/en/settings.ts index 66eb7d98..c23ebf41 100644 --- a/web/console-fe/src/i18n/langs/en/settings.ts +++ b/web/console-fe/src/i18n/langs/en/settings.ts @@ -1,3 +1,8 @@ export default { - profile: 'Public profile' + profile: 'Public profile', + security: 'Security', + changePassword: 'Change the password', + oldPassword: 'Old password', + newPassword: 'New password', + confirmPassword: 'Confirm password' } diff --git a/web/console-fe/src/i18n/langs/zhCn/settings.ts b/web/console-fe/src/i18n/langs/zhCn/settings.ts index d2d04220..4b05422f 100644 --- a/web/console-fe/src/i18n/langs/zhCn/settings.ts +++ b/web/console-fe/src/i18n/langs/zhCn/settings.ts @@ -1,3 +1,8 @@ export default { - profile: '个人资料' + profile: '个人资料', + security: '安全设置', + changePassword: '修改密码', + oldPassword: '旧密码', + newPassword: '新密码', + confirmPassword: '确认密码' } diff --git a/web/console-fe/src/model/UserPassword.ts b/web/console-fe/src/model/UserPassword.ts new file mode 100644 index 00000000..4ee14667 --- /dev/null +++ b/web/console-fe/src/model/UserPassword.ts @@ -0,0 +1,6 @@ +export interface UserPassword +{ + oldPassword: string; + newPassword: string; + confirmPassword: string; +} diff --git a/web/console-fe/src/router/index.ts b/web/console-fe/src/router/index.ts index 05c68be7..d9ea240e 100644 --- a/web/console-fe/src/router/index.ts +++ b/web/console-fe/src/router/index.ts @@ -95,6 +95,10 @@ const routes: Array = [ { path: 'public', component: () => import("../views/pages/profile/ProfilePublic.vue") + }, + { + path: 'security', + component: () => import("../views/pages/profile/ProfileSecurity.vue") } ] } diff --git a/web/console-fe/src/services/UserService.ts b/web/console-fe/src/services/UserService.ts index 5d7eac9d..eb2ab595 100644 --- a/web/console-fe/src/services/UserService.ts +++ b/web/console-fe/src/services/UserService.ts @@ -1,5 +1,6 @@ import {HttpCommon} from "@/common/HttpCommon"; import {ResponseModel} from "@/model/ResponseModel"; +import {UserPassword} from "@/model/UserPassword"; const baseUrl = "/api/v1/user"; @@ -9,6 +10,11 @@ class UserService { return new HttpCommon().get(baseUrl); } + + changePassword(configure: UserPassword): Promise + { + return new HttpCommon().put(baseUrl + '/changePassword', configure); + } } export default new UserService(); diff --git a/web/console-fe/src/views/pages/profile/ProfilePublic.vue b/web/console-fe/src/views/pages/profile/ProfilePublic.vue index b2d16a86..ab4e90fa 100644 --- a/web/console-fe/src/views/pages/profile/ProfilePublic.vue +++ b/web/console-fe/src/views/pages/profile/ProfilePublic.vue @@ -7,7 +7,7 @@ - + {{ formState.username }} diff --git a/web/console-fe/src/views/pages/profile/ProfileSecurity.vue b/web/console-fe/src/views/pages/profile/ProfileSecurity.vue new file mode 100644 index 00000000..09b55156 --- /dev/null +++ b/web/console-fe/src/views/pages/profile/ProfileSecurity.vue @@ -0,0 +1,111 @@ + + diff --git a/web/console-fe/src/views/pages/profile/layout/components/ProfileLayoutSidebar.vue b/web/console-fe/src/views/pages/profile/layout/components/ProfileLayoutSidebar.vue index 4322c0ec..ae9d44ec 100644 --- a/web/console-fe/src/views/pages/profile/layout/components/ProfileLayoutSidebar.vue +++ b/web/console-fe/src/views/pages/profile/layout/components/ProfileLayoutSidebar.vue @@ -4,7 +4,9 @@ @@ -18,6 +20,14 @@ {{ $t('setting.profile') }} + + + + {{ $t('setting.security') }} + +