From c56685805e9ff3299bcce0cc904fae1715fd7f73 Mon Sep 17 00:00:00 2001 From: Arthur Ming Date: Sun, 26 May 2024 16:39:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=9B=E5=BB=BA=E9=A6=96=E4=B8=AA?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E6=97=B6=E5=80=99=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E8=A7=92=E8=89=B2=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E6=98=AF=E7=AE=A1=E7=90=86=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/angular | 2 +- .../hoteler/service/impl/UserServiceImpl.java | 25 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/angular b/src/main/angular index 64f87481..d248a72a 160000 --- a/src/main/angular +++ b/src/main/angular @@ -1 +1 @@ -Subproject commit 64f87481d1366eb20e5f7d5630707a567364c201 +Subproject commit d248a72ab2a167d2a4d7970e5cf9081d1b79d265 diff --git a/src/main/java/org/daming/hoteler/service/impl/UserServiceImpl.java b/src/main/java/org/daming/hoteler/service/impl/UserServiceImpl.java index 59093ab8..24f5a99f 100644 --- a/src/main/java/org/daming/hoteler/service/impl/UserServiceImpl.java +++ b/src/main/java/org/daming/hoteler/service/impl/UserServiceImpl.java @@ -64,6 +64,10 @@ public User getUserByUsername(String username) { Assert.hasText("username", "params 'username' is required"); var user = this.userDao.getUserByUsername(username) .orElseThrow(() -> this.errorService.createHotelerException(600005)); + return getUser(user); + } + + private User getUser(User user) { var roles = this.roleService.getRolesByUserId(user.getId()); if (Objects.nonNull(roles) && !roles.isEmpty()) { user.setRoles(roles); @@ -80,15 +84,7 @@ public User get(long id) { Assert.isTrue(id > 0, "params 'id' is required"); var user = userDao.get(id) .orElseThrow(() -> this.errorService.createHotelerException(600005)); - var roles = this.roleService.getRolesByUserId(user.getId()); - if (Objects.nonNull(roles) && !roles.isEmpty()) { - user.setRoles(roles); - var roleIds = roles.stream().mapToLong(Role::getId).toArray(); - var permission = this.permissionService.listByRoleId(roleIds); - user.setPermissions(permission); - - } - return user; + return getUser(user); } @Override @@ -120,14 +116,17 @@ public User create(CreateUserRequest createUserRequest) { .setUsername(createUserRequest.getUsername()) .setPassword(createUserRequest.getPassword()) .setPasswordType(createUserRequest.getPasswordType()); - var existUser = this.userDao.getUserByUsername(user.getUsername()); - if (existUser.isPresent()) { - throw this.errorService.createHotelerException(600012); + var isFirstUser = this.userMapper.count() == 0; + if (!isFirstUser) { + var existUser = this.userDao.getUserByUsername(user.getUsername()); + if (existUser.isPresent()) { + throw this.errorService.createHotelerException(600012); + } } + var passwordType = CommonUtils.isNotEmpty(user.getPasswordType()) ? user.getPasswordType() : "noop"; var passwordService = this.getPasswordService(passwordType); this.userMapper.create(user.getId(), user.getUsername(), passwordService.encodePassword(user.getPassword()), passwordType); - var isFirstUser = this.userMapper.count() == 0; var roleNames = createUserRequest.getRoles(); if (isFirstUser && !roleNames.contains("admin")) { roleNames.add("admin");