Skip to content

Commit 8c815df

Browse files
feat: update org user info
1 parent c050d63 commit 8c815df

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

server/src/main/java/datart/server/base/params/UserAddParam.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class UserAddParam {
1616

1717
private String email;
1818

19+
private String name;
20+
21+
private String description;
22+
1923
private Set<String> roleIds = new HashSet<>();
2024

2125
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Datart
3+
* <p>
4+
* Copyright 2021
5+
* <p>
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package datart.server.base.params;
20+
21+
import lombok.Data;
22+
import lombok.EqualsAndHashCode;
23+
24+
import java.util.HashSet;
25+
import java.util.Set;
26+
27+
@EqualsAndHashCode(callSuper = true)
28+
@Data
29+
public class UserUpdateByIdParam extends UserUpdateParam {
30+
31+
private String password;
32+
33+
private Set<String> roleIds = new HashSet<>();
34+
35+
}

server/src/main/java/datart/server/controller/UserController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ public ResponseData<Boolean> addUser(@PathVariable String orgId, @Validated @Req
140140
return ResponseData.success(userService.addUserToOrg(userAddParam, orgId));
141141
}
142142

143+
@ApiOperation(value = "add User to organization")
144+
@GetMapping("/{orgId}/getUser/{userId}")
145+
public ResponseData<UserUpdateByIdParam> selectUserByIdFromOrg(@PathVariable String orgId, @PathVariable String userId) throws MessagingException, UnsupportedEncodingException {
146+
return ResponseData.success(userService.selectUserById(userId, orgId));
147+
}
148+
149+
@ApiOperation(value = "update user from organization")
150+
@PutMapping(value = "/{orgId}/updateUser")
151+
public ResponseData<Boolean> updateUserFromOrg(@PathVariable String orgId, @Validated @RequestBody UserUpdateByIdParam userUpdateParam) {
152+
return ResponseData.success(userService.updateUserFromOrg(userUpdateParam, orgId));
153+
}
154+
143155
@ApiOperation(value = "User Delete from organization")
144156
@DeleteMapping(value = "/{orgId}/deleteUser")
145157
public ResponseData<Boolean> deleteUserFromOrg(@PathVariable String orgId, @RequestParam String userId) {

server/src/main/java/datart/server/service/UserService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public interface UserService extends BaseCRUDService<User, UserMapperExt> {
6464
boolean addUserToOrg(UserAddParam userAddParam, String orgId) throws MessagingException, UnsupportedEncodingException;
6565

6666
boolean deleteUserFromOrg(String orgId, String userId);
67+
68+
boolean updateUserFromOrg(UserUpdateByIdParam userUpdateParam, String orgId);
69+
70+
UserUpdateByIdParam selectUserById(String userId, String orgId);
6771
}

server/src/main/java/datart/server/service/impl/UserServiceImpl.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import datart.core.common.Application;
3131
import datart.core.common.UUIDGenerator;
3232
import datart.core.entity.Organization;
33+
import datart.core.entity.Role;
3334
import datart.core.entity.User;
3435
import datart.core.entity.ext.UserBaseInfo;
3536
import datart.core.mappers.ext.OrganizationMapperExt;
@@ -39,10 +40,7 @@
3940
import datart.security.util.SecurityUtils;
4041
import datart.server.base.dto.OrganizationBaseInfo;
4142
import datart.server.base.dto.UserProfile;
42-
import datart.server.base.params.ChangeUserPasswordParam;
43-
import datart.server.base.params.UserAddParam;
44-
import datart.server.base.params.UserRegisterParam;
45-
import datart.server.base.params.UserResetPasswordParam;
43+
import datart.server.base.params.*;
4644
import datart.server.service.*;
4745
import lombok.extern.slf4j.Slf4j;
4846
import org.apache.commons.codec.digest.DigestUtils;
@@ -61,6 +59,7 @@
6159
import java.time.LocalDate;
6260
import java.util.Date;
6361
import java.util.List;
62+
import java.util.Set;
6463
import java.util.stream.Collectors;
6564

6665
import static datart.core.common.Application.getProperty;
@@ -378,6 +377,9 @@ public boolean addUserToOrg(UserAddParam userAddParam, String orgId) throws Mess
378377
BeanUtils.copyProperties(userAddParam, userRegisterParam);
379378
register(userRegisterParam, false);
380379
User user = this.userMapper.selectByUsername(userAddParam.getUsername());
380+
user.setName(userAddParam.getName());
381+
user.setDescription(userAddParam.getDescription());
382+
this.userMapper.updateByPrimaryKeySelective(user);
381383
orgService.addUserToOrg(user.getId(), orgId);
382384
roleService.updateRolesForUser(user.getId(), orgId, userAddParam.getRoleIds());
383385
return true;
@@ -392,6 +394,34 @@ public boolean deleteUserFromOrg(String orgId, String userId) {
392394
return true;
393395
}
394396

397+
@Override
398+
public boolean updateUserFromOrg(UserUpdateByIdParam userUpdateParam, String orgId) {
399+
securityManager.requireOrgOwner(orgId);
400+
User user = retrieve(userUpdateParam.getId());
401+
if (!user.getEmail().equals(userUpdateParam.getEmail()) && !checkEmail(userUpdateParam.getEmail())) {
402+
log.error("The email({}) has been registered", userUpdateParam.getEmail());
403+
Exceptions.tr(ParamException.class, "error.param.occupied", "resource.user.email");
404+
}
405+
if (StringUtils.isBlank(userUpdateParam.getPassword())) {
406+
userUpdateParam.setPassword(user.getPassword());
407+
} else if (!userUpdateParam.getPassword().equals(user.getPassword())){
408+
userUpdateParam.setPassword(BCrypt.hashpw(userUpdateParam.getPassword(), BCrypt.gensalt()));
409+
}
410+
roleService.updateRolesForUser(user.getId(), orgId, userUpdateParam.getRoleIds());
411+
return update(userUpdateParam);
412+
}
413+
414+
@Override
415+
public UserUpdateByIdParam selectUserById(String userId, String orgId) {
416+
securityManager.requireOrgOwner(orgId);
417+
UserUpdateByIdParam res = new UserUpdateByIdParam();
418+
User user = this.userMapper.selectByPrimaryKey(userId);
419+
BeanUtils.copyProperties(user, res);
420+
Set<String> roleIds = roleService.listUserRoles(orgId, userId).stream().map(Role::getId).collect(Collectors.toSet());
421+
res.setRoleIds(roleIds);
422+
return res;
423+
}
424+
395425
@Override
396426
public void requirePermission(User entity, int permission) {
397427

0 commit comments

Comments
 (0)