Showing
10 changed files
with
176 additions
and
145 deletions
... | ... | @@ -446,7 +446,7 @@ public abstract class BaseController { |
446 | 446 | } |
447 | 447 | } |
448 | 448 | |
449 | - User checkUserId(UserId userId, Operation operation) throws ThingsboardException { | |
449 | + protected User checkUserId(UserId userId, Operation operation) throws ThingsboardException { | |
450 | 450 | try { |
451 | 451 | validateId(userId, "Incorrect userId " + userId); |
452 | 452 | User user = userService.findUserById(getCurrentUser().getTenantId(), userId); | ... | ... |
... | ... | @@ -50,18 +50,21 @@ public class YtOrganizationController extends BaseController { |
50 | 50 | } |
51 | 51 | |
52 | 52 | @GetMapping("/me/organizations") |
53 | - public List<OrganizationDTO> getMyOrganizations() { | |
54 | - return organizationService.getMyOrganizations(); | |
53 | + public List<OrganizationDTO> getMyOrganizations() throws ThingsboardException { | |
54 | + return organizationService.getMyOrganizations( | |
55 | + getCurrentUser().isPtTenantAdmin(), | |
56 | + getCurrentUser().getCurrentTenantId(), | |
57 | + getCurrentUser().getCurrentUserId()); | |
55 | 58 | } |
56 | 59 | |
57 | 60 | @PutMapping |
58 | - public OrganizationDTO updateOrganization(@Validated(UpdateGroup.class) @RequestBody OrganizationDTO groupDTO) { | |
59 | - return organizationService.updateOrganization(groupDTO); | |
61 | + public OrganizationDTO updateOrganization(@Validated(UpdateGroup.class) @RequestBody OrganizationDTO groupDTO) throws ThingsboardException { | |
62 | + return organizationService.updateOrganization(groupDTO, getCurrentUser().getCurrentTenantId()); | |
60 | 63 | } |
61 | 64 | |
62 | 65 | @PutMapping("saveOrUpdateUserOrganizationMapping/{userId}") |
63 | 66 | public void bindUserToOrganization( |
64 | - @RequestBody String[] organizationIds, @PathVariable("userId") String userId) { | |
65 | - organizationService.bindUserToOrganization(userId, organizationIds); | |
67 | + @RequestBody String[] organizationIds, @PathVariable("userId") String userId) throws ThingsboardException { | |
68 | + organizationService.bindUserToOrganization(getCurrentUser().getCurrentTenantId(), userId, organizationIds); | |
66 | 69 | } |
67 | 70 | } | ... | ... |
... | ... | @@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor; |
8 | 8 | import org.apache.commons.lang3.StringUtils; |
9 | 9 | import org.springframework.http.ResponseEntity; |
10 | 10 | import org.springframework.security.access.prepost.PreAuthorize; |
11 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |
11 | 12 | import org.springframework.validation.annotation.Validated; |
12 | 13 | import org.springframework.web.bind.annotation.*; |
13 | 14 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
... | ... | @@ -16,9 +17,14 @@ import org.thingsboard.server.common.data.audit.ActionType; |
16 | 17 | import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
17 | 18 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
18 | 19 | import org.thingsboard.server.common.data.id.TenantId; |
20 | +import org.thingsboard.server.common.data.id.UserId; | |
21 | +import org.thingsboard.server.common.data.security.Authority; | |
22 | +import org.thingsboard.server.common.data.security.UserCredentials; | |
19 | 23 | import org.thingsboard.server.common.data.yunteng.common.AddGroup; |
20 | 24 | import org.thingsboard.server.common.data.yunteng.common.DeleteGroup; |
21 | 25 | import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; |
26 | +import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException; | |
27 | +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; | |
22 | 28 | import org.thingsboard.server.common.data.yunteng.utils.Demo; |
23 | 29 | import org.thingsboard.server.common.data.yunteng.utils.ExcelUtil; |
24 | 30 | import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO; |
... | ... | @@ -33,6 +39,9 @@ import org.thingsboard.server.common.data.yunteng.utils.tools.ResponseResult; |
33 | 39 | import org.thingsboard.server.controller.BaseController; |
34 | 40 | import org.thingsboard.server.dao.user.UserService; |
35 | 41 | import org.thingsboard.server.dao.yunteng.service.YtUserService; |
42 | +import org.thingsboard.server.service.security.model.SecurityUser; | |
43 | +import org.thingsboard.server.service.security.model.UserPrincipal; | |
44 | +import org.thingsboard.server.service.security.permission.Operation; | |
36 | 45 | |
37 | 46 | import javax.servlet.http.HttpServletRequest; |
38 | 47 | import javax.servlet.http.HttpServletResponse; |
... | ... | @@ -49,7 +58,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. |
49 | 58 | @RequiredArgsConstructor |
50 | 59 | @Api(value = "用户接口") |
51 | 60 | public class YtUserController extends BaseController { |
52 | - | |
61 | + private final BCryptPasswordEncoder passwordEncoder; | |
53 | 62 | private final YtUserService userService; |
54 | 63 | private final UserService tbUserService; |
55 | 64 | @GetMapping("{userId}") |
... | ... | @@ -140,17 +149,38 @@ public class YtUserController extends BaseController { |
140 | 149 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')") |
141 | 150 | @PostMapping("saveTenantAdmin") |
142 | 151 | public UserDTO saveTenantAdmin(@Validated(AddGroup.class)@RequestBody UserDTO userDTO) throws ThingsboardException { |
143 | - //创建TB的租户管理员 | |
144 | - User tbUser = new User(); | |
152 | + if(StringUtils.isAllBlank(userDTO.getTenantId())){ | |
153 | + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | |
154 | + } | |
145 | 155 | TenantId tenantId = new TenantId(UUID.fromString(userDTO.getTenantId())); |
146 | - tbUser.setTenantId(tenantId); | |
147 | - tbUser.setEmail(userDTO.getUsername()+ FastIotConstants.DEFAULT_EMAIL_SUFFIX_FOR_TB); | |
148 | - tbUser = tbUserService.saveUser(tbUser); | |
149 | - logEntityAction(tbUser.getId(), tbUser, | |
150 | - tbUser.getCustomerId(), | |
151 | - userDTO.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); | |
152 | - sendEntityNotificationMsg(tenantId, tbUser.getId(), | |
153 | - userDTO.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED); | |
156 | + try{ | |
157 | + //创建TB的租户管理员 | |
158 | + User tbUser = new User(); | |
159 | + tbUser.setAuthority(Authority.TENANT_ADMIN); | |
160 | + tbUser.setTenantId(tenantId); | |
161 | + tbUser.setEmail(userDTO.getUsername()+ FastIotConstants.DEFAULT_EMAIL_SUFFIX_FOR_TB); | |
162 | + tbUser = tbUserService.saveUser(tbUser); | |
163 | + userDTO.setTbUser(tbUser.getId().getId().toString()); | |
164 | + logEntityAction(tbUser.getId(), tbUser, | |
165 | + tbUser.getCustomerId(), | |
166 | + userDTO.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); | |
167 | + sendEntityNotificationMsg(tenantId, tbUser.getId(), | |
168 | + userDTO.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED); | |
169 | + | |
170 | + //激活租户管理员 | |
171 | + //1、获取UserCredentials 并获取activateToken | |
172 | + UserId userId = tbUser.getId(); | |
173 | + User user = checkUserId(userId, Operation.READ); | |
174 | + SecurityUser authUser = getCurrentUser(); | |
175 | + UserCredentials userCredentials = tbUserService.findUserCredentialsByUserId(authUser.getTenantId(), user.getId()); | |
176 | + //2、进行激活 | |
177 | + String encodedPassword = passwordEncoder.encode(FastIotConstants.DEFAULT_PWD); | |
178 | + UserCredentials credentials = tbUserService.activateUserCredentials(TenantId.SYS_TENANT_ID, userCredentials.getActivateToken(), encodedPassword); | |
179 | + User currentUser = tbUserService.findUserById(TenantId.SYS_TENANT_ID, credentials.getUserId()); | |
180 | + tbUserService.setUserCredentialsEnabled(currentUser.getTenantId(), currentUser.getId(), true); | |
181 | + }catch (Exception e){ | |
182 | + throw handleException(e); | |
183 | + } | |
154 | 184 | return userService.saveTenantAdmin( |
155 | 185 | userDTO, getCurrentUser().isPtSysadmin(), tenantId.getId().toString()); |
156 | 186 | } | ... | ... |
1 | 1 | package org.thingsboard.server.common.data.yunteng.dto; |
2 | 2 | |
3 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; |
4 | +import com.fasterxml.jackson.annotation.JsonIgnore; | |
4 | 5 | import com.fasterxml.jackson.annotation.JsonInclude; |
5 | 6 | import lombok.Data; |
6 | 7 | import lombok.EqualsAndHashCode; |
... | ... | @@ -22,7 +23,6 @@ public class UserDTO extends BaseDTO { |
22 | 23 | private String realName; |
23 | 24 | |
24 | 25 | @JsonInclude(JsonInclude.Include.NON_NULL) |
25 | - @NotEmpty(message = "密码不能为空或空字符串", groups = AddGroup.class) | |
26 | 26 | private String password; |
27 | 27 | |
28 | 28 | @JsonInclude(JsonInclude.Include.NON_NULL) |
... | ... | @@ -37,6 +37,10 @@ public class UserDTO extends BaseDTO { |
37 | 37 | private String phoneNumber; |
38 | 38 | private String email; |
39 | 39 | private String avatar; |
40 | + /** TB的UserId 不序列化到前端*/ | |
41 | + @JsonIgnore | |
42 | + private String tbUser; | |
43 | + | |
40 | 44 | private boolean enabled; |
41 | 45 | |
42 | 46 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
... | ... | @@ -45,7 +49,6 @@ public class UserDTO extends BaseDTO { |
45 | 49 | private boolean hasPassword; |
46 | 50 | |
47 | 51 | @JsonInclude(JsonInclude.Include.NON_NULL) |
48 | - @NotEmpty(message = "租户ID不能为空或者空字符串", groups = AddGroup.class) | |
49 | 52 | private String tenantId; |
50 | 53 | |
51 | 54 | @JsonInclude(JsonInclude.Include.NON_NULL) | ... | ... |
... | ... | @@ -12,9 +12,12 @@ import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidatio |
12 | 12 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
13 | 13 | import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO; |
14 | 14 | import org.thingsboard.server.common.data.yunteng.dto.OrganizationDTO; |
15 | +import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils; | |
16 | +import org.thingsboard.server.common.data.yunteng.utils.tree.TreeUtils; | |
15 | 17 | import org.thingsboard.server.dao.yunteng.entities.UserOrganizationMapping; |
16 | 18 | import org.thingsboard.server.dao.yunteng.entities.Organization; |
17 | 19 | import org.thingsboard.server.dao.yunteng.entities.User; |
20 | +import org.thingsboard.server.dao.yunteng.entities.YtDevice; | |
18 | 21 | import org.thingsboard.server.dao.yunteng.mapper.DeviceMapper; |
19 | 22 | import org.thingsboard.server.dao.yunteng.mapper.OrganizationMapper; |
20 | 23 | import org.thingsboard.server.dao.yunteng.mapper.UserMapper; |
... | ... | @@ -36,16 +39,14 @@ public class YtOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
36 | 39 | |
37 | 40 | @Override |
38 | 41 | @Transactional |
39 | - public OrganizationDTO saveOrganization(OrganizationDTO organizationDTO,String tenantId) { | |
42 | + public OrganizationDTO saveOrganization(OrganizationDTO organizationDTO, String tenantId) { | |
40 | 43 | Assert.notNull(organizationDTO.getName(), "organization name cannot be null"); |
41 | 44 | if (StringUtils.isNotBlank(organizationDTO.getParentId())) { |
42 | 45 | Organization organization = baseMapper.selectById(organizationDTO.getParentId()); |
43 | 46 | if (organization == null) { |
44 | 47 | throw new YtDataValidationException("parent organization not exist!"); |
45 | 48 | } else { |
46 | - if (!organization | |
47 | - .getTenantId() | |
48 | - .equals(null)) {//TODO getCurrentUser().getTenantId() | |
49 | + if (!organization.getTenantId().equals(tenantId)) { | |
49 | 50 | throw new YtDataValidationException("parent organization not exist."); |
50 | 51 | } |
51 | 52 | } |
... | ... | @@ -60,16 +61,13 @@ public class YtOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
60 | 61 | |
61 | 62 | @Override |
62 | 63 | @Transactional |
63 | - public boolean deleteOrganizations(DeleteDTO deleteDTO,String tenantId) { | |
64 | - if(null == deleteDTO || deleteDTO.getIds().isEmpty()){ | |
64 | + public boolean deleteOrganizations(DeleteDTO deleteDTO, String tenantId) { | |
65 | + if (null == deleteDTO || deleteDTO.getIds().isEmpty()) { | |
65 | 66 | throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); |
66 | 67 | } |
67 | 68 | String[] ids = deleteDTO.getIds().toArray(new String[deleteDTO.getIds().size()]); |
68 | 69 | Set<String> idToDelete = |
69 | - baseMapper | |
70 | - .findOrganizationTreeList( | |
71 | - null, Arrays.asList(ids))//TODO getCurrentUser().getTenantId() | |
72 | - .stream() | |
70 | + baseMapper.findOrganizationTreeList(tenantId, Arrays.asList(ids)).stream() | |
73 | 71 | .map(OrganizationDTO::getId) |
74 | 72 | .collect(Collectors.toSet()); |
75 | 73 | if (idToDelete.size() != ids.length) { |
... | ... | @@ -84,16 +82,17 @@ public class YtOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
84 | 82 | throw new YtDataValidationException("待删除数据存在关联用户,不能删除!"); |
85 | 83 | } |
86 | 84 | // 查询是否有设备使用该组织 |
87 | -// List<Device> deviceList = deviceMapper.selectList( | |
88 | -// new QueryWrapper<Device>() | |
89 | -// .lambda() | |
90 | -// .eq(Device::getTenantId, getCurrentUser().getTenantId()) | |
91 | -// .eq(Device::getOrganizationId,ids)); | |
92 | -// if (!deviceList.isEmpty()) { | |
93 | -// throw new DataValidationException("待删除数据存在关联设备,不能删除!"); | |
94 | -// } | |
85 | + List<YtDevice> deviceList = | |
86 | + deviceMapper.selectList( | |
87 | + new QueryWrapper<YtDevice>() | |
88 | + .lambda() | |
89 | + .eq(YtDevice::getTenantId, tenantId) | |
90 | + .eq(YtDevice::getOrganizationId, ids)); | |
91 | + if (!deviceList.isEmpty()) { | |
92 | + throw new YtDataValidationException("待删除数据存在关联设备,不能删除!"); | |
93 | + } | |
95 | 94 | doDeleteUserOrganizationMapping(idToDelete); |
96 | - return baseMapper.deleteBatchIds(idToDelete)>0; | |
95 | + return baseMapper.deleteBatchIds(idToDelete) > 0; | |
97 | 96 | } |
98 | 97 | |
99 | 98 | private void doDeleteUserOrganizationMapping(Collection<String> organizationIds) { |
... | ... | @@ -105,63 +104,58 @@ public class YtOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
105 | 104 | |
106 | 105 | @Override |
107 | 106 | @Transactional |
108 | - public OrganizationDTO updateOrganization(OrganizationDTO organizationDTO) { | |
109 | -// Organization organization = baseMapper.selectById(organizationDTO.getId()); | |
110 | -// Organization parent = null; | |
111 | -// if (StringUtils.isNotBlank(organizationDTO.getParentId())) { | |
112 | -// Organization parentOrganization = baseMapper.selectById(organizationDTO.getParentId()); | |
113 | -// if (parentOrganization | |
114 | -// .getTenantId() | |
115 | -// .equals(getCurrentUser().getTenantId())) { | |
116 | -// parent = parentOrganization; | |
117 | -// } | |
118 | -// } | |
119 | -// if (organization == null) { | |
120 | -// return null; | |
121 | -// } | |
122 | -// if (organization.getName().equals(organizationDTO.getName()) | |
123 | -// && organization.getSort() == organizationDTO.getSort() | |
124 | -// && organization.getRemark().equals(organizationDTO.getRemark()) | |
125 | -// && Objects.equals(organization.getParentId(), organizationDTO.getParentId())) { | |
126 | -// return organizationDTO; | |
127 | -// } | |
128 | -// organization.setName(organizationDTO.getName()); | |
129 | -// organization.setRemark(organizationDTO.getRemark()); | |
130 | -// organization.setSort(organizationDTO.getSort()); | |
131 | -// if (parent != null) { | |
132 | -// organization.setParentId(parent.getId()); | |
133 | -// } | |
134 | -// baseMapper.updateById(organization); | |
135 | -// organization.copyToDTO(organizationDTO); | |
136 | -// return organizationDTO; | |
137 | - return null; | |
107 | + public OrganizationDTO updateOrganization(OrganizationDTO organizationDTO, String tenantId) { | |
108 | + Organization organization = baseMapper.selectById(organizationDTO.getId()); | |
109 | + Organization parent = null; | |
110 | + if (StringUtils.isNotBlank(organizationDTO.getParentId())) { | |
111 | + Organization parentOrganization = baseMapper.selectById(organizationDTO.getParentId()); | |
112 | + if (parentOrganization.getTenantId().equals(tenantId)) { | |
113 | + parent = parentOrganization; | |
114 | + } | |
115 | + } | |
116 | + if (organization == null) { | |
117 | + return null; | |
118 | + } | |
119 | + if (organization.getName().equals(organizationDTO.getName()) | |
120 | + && organization.getSort() == organizationDTO.getSort() | |
121 | + && organization.getRemark().equals(organizationDTO.getRemark()) | |
122 | + && Objects.equals(organization.getParentId(), organizationDTO.getParentId())) { | |
123 | + return organizationDTO; | |
124 | + } | |
125 | + organization.setName(organizationDTO.getName()); | |
126 | + organization.setRemark(organizationDTO.getRemark()); | |
127 | + organization.setSort(organizationDTO.getSort()); | |
128 | + if (parent != null) { | |
129 | + organization.setParentId(parent.getId()); | |
130 | + } | |
131 | + baseMapper.updateById(organization); | |
132 | + organization.copyToDTO(organizationDTO); | |
133 | + return organizationDTO; | |
138 | 134 | } |
139 | 135 | |
140 | 136 | @Override |
141 | - public List<OrganizationDTO> getMyOrganizations() { | |
142 | -// if (getCurrentUser().isTenantAdmin()) { | |
143 | -// return findOrganizationTree(getCurrentUser().getTenantId()); | |
144 | -// } else { | |
145 | -// Set<String> organizationIds = | |
146 | -// userOrganizationMappingMapper | |
147 | -// .selectList( | |
148 | -// new QueryWrapper<UserOrganizationMapping>() | |
149 | -// .lambda() | |
150 | -// .in( | |
151 | -// UserOrganizationMapping::getUserId, | |
152 | -// getCurrentUser().getUserId())) | |
153 | -// .stream() | |
154 | -// .map(UserOrganizationMapping::getOrganizationId) | |
155 | -// .collect(Collectors.toSet()); | |
156 | -// List<Organization> organizations = | |
157 | -// baseMapper.selectList( | |
158 | -// new QueryWrapper<Organization>() | |
159 | -// .lambda() | |
160 | -// .eq(Organization::getTenantId, getCurrentUser().getTenantId()) | |
161 | -// .in(organizationIds.size() > 0, Organization::getId, organizationIds)); | |
162 | -// return TreeUtils.buildTree(ReflectUtils.sourceToTarget(organizations, OrganizationDTO.class)); | |
163 | -// } | |
164 | - return null; | |
137 | + public List<OrganizationDTO> getMyOrganizations( | |
138 | + boolean isPtTenantAdmin, String tenantId, String currentUserId) { | |
139 | + if (isPtTenantAdmin) { | |
140 | + return findOrganizationTree(tenantId); | |
141 | + } else { | |
142 | + Set<String> organizationIds = | |
143 | + userOrganizationMappingMapper | |
144 | + .selectList( | |
145 | + new QueryWrapper<UserOrganizationMapping>() | |
146 | + .lambda() | |
147 | + .in(UserOrganizationMapping::getUserId, currentUserId)) | |
148 | + .stream() | |
149 | + .map(UserOrganizationMapping::getOrganizationId) | |
150 | + .collect(Collectors.toSet()); | |
151 | + List<Organization> organizations = | |
152 | + baseMapper.selectList( | |
153 | + new QueryWrapper<Organization>() | |
154 | + .lambda() | |
155 | + .eq(Organization::getTenantId, tenantId) | |
156 | + .in(organizationIds.size() > 0, Organization::getId, organizationIds)); | |
157 | + return TreeUtils.buildTree(ReflectUtils.sourceToTarget(organizations, OrganizationDTO.class)); | |
158 | + } | |
165 | 159 | } |
166 | 160 | |
167 | 161 | private List<OrganizationDTO> findOrganizationTree(String tenantId) { |
... | ... | @@ -193,46 +187,43 @@ public class YtOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
193 | 187 | |
194 | 188 | @Override |
195 | 189 | @Transactional |
196 | - public void bindUserToOrganization(String userId, String[] organizationIds) { | |
197 | -// User user = userMapper.selectById(userId); | |
198 | -// if (user == null) { | |
199 | -// throw new DataValidationException("所选用户不存在"); | |
200 | -// } | |
201 | -// if (!user.getTenantId().equals(getCurrentUser().getTenantId())) { | |
202 | -// throw new DataValidationException("所选用户不可用"); | |
203 | -// } | |
204 | -// if (null != organizationIds && organizationIds.length > 0) { | |
205 | -// Set<String> newBinding = | |
206 | -// baseMapper | |
207 | -// .findOrganizationTreeList( | |
208 | -// getCurrentUser().getTenantId(), Arrays.asList(organizationIds)) | |
209 | -// .stream() | |
210 | -// .map(OrganizationDTO::getId) | |
211 | -// .collect(Collectors.toSet()); | |
212 | -// Set<String> existBinding = | |
213 | -// userOrganizationMappingMapper | |
214 | -// .selectList( | |
215 | -// new QueryWrapper<UserOrganizationMapping>() | |
216 | -// .lambda() | |
217 | -// .eq(UserOrganizationMapping::getUserId, userId)) | |
218 | -// .stream() | |
219 | -// .map(UserOrganizationMapping::getOrganizationId) | |
220 | -// .collect(Collectors.toSet()); | |
221 | -// Set<String> toDel = Sets.difference(existBinding, newBinding); | |
222 | -// Set<String> toAdd = Sets.difference(newBinding, existBinding); | |
223 | -// if (!toDel.isEmpty()) { | |
224 | -// userOrganizationMappingMapper.delete( | |
225 | -// new QueryWrapper<UserOrganizationMapping>() | |
226 | -// .lambda() | |
227 | -// .in(UserOrganizationMapping::getUserId, userId) | |
228 | -// .in(UserOrganizationMapping::getOrganizationId, toDel)); | |
229 | -// } | |
230 | -// if (!toAdd.isEmpty()) { | |
231 | -// toAdd.stream() | |
232 | -// .map(organizationId -> new UserOrganizationMapping(userId, organizationId)) | |
233 | -// .forEach(userOrganizationMappingMapper::insert); | |
234 | -// } | |
235 | -// } | |
190 | + public void bindUserToOrganization(String tenantId, String userId, String[] organizationIds) { | |
191 | + User user = userMapper.selectById(userId); | |
192 | + if (user == null) { | |
193 | + throw new YtDataValidationException("所选用户不存在"); | |
194 | + } | |
195 | + if (!user.getTenantId().equals(tenantId)) { | |
196 | + throw new YtDataValidationException("所选用户不可用"); | |
197 | + } | |
198 | + if (null != organizationIds && organizationIds.length > 0) { | |
199 | + Set<String> newBinding = | |
200 | + baseMapper.findOrganizationTreeList(tenantId, Arrays.asList(organizationIds)).stream() | |
201 | + .map(OrganizationDTO::getId) | |
202 | + .collect(Collectors.toSet()); | |
203 | + Set<String> existBinding = | |
204 | + userOrganizationMappingMapper | |
205 | + .selectList( | |
206 | + new QueryWrapper<UserOrganizationMapping>() | |
207 | + .lambda() | |
208 | + .eq(UserOrganizationMapping::getUserId, userId)) | |
209 | + .stream() | |
210 | + .map(UserOrganizationMapping::getOrganizationId) | |
211 | + .collect(Collectors.toSet()); | |
212 | + Set<String> toDel = Sets.difference(existBinding, newBinding); | |
213 | + Set<String> toAdd = Sets.difference(newBinding, existBinding); | |
214 | + if (!toDel.isEmpty()) { | |
215 | + userOrganizationMappingMapper.delete( | |
216 | + new QueryWrapper<UserOrganizationMapping>() | |
217 | + .lambda() | |
218 | + .in(UserOrganizationMapping::getUserId, userId) | |
219 | + .in(UserOrganizationMapping::getOrganizationId, toDel)); | |
220 | + } | |
221 | + if (!toAdd.isEmpty()) { | |
222 | + toAdd.stream() | |
223 | + .map(organizationId -> new UserOrganizationMapping(userId, organizationId)) | |
224 | + .forEach(userOrganizationMappingMapper::insert); | |
225 | + } | |
226 | + } | |
236 | 227 | } |
237 | 228 | |
238 | 229 | @Override | ... | ... |
... | ... | @@ -63,8 +63,6 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> imp |
63 | 63 | |
64 | 64 | public static final String ACTIVATE_URL_PATTERN = "%s/api/noauth/activate?activateToken=%s"; |
65 | 65 | private final PasswordEncoder passwordEncoder; |
66 | - /** 默认密码 */ | |
67 | - private static final String DEFAULT_PWD = "123456"; | |
68 | 66 | |
69 | 67 | @Override |
70 | 68 | public List<UserDetailsDTO> findUserDetailsByUsername(String username) { |
... | ... | @@ -98,7 +96,7 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> imp |
98 | 96 | // 添加的租户管理员 |
99 | 97 | if (StringUtils.isNotBlank(userDTO.getTenantId())) { |
100 | 98 | user.setTenantId(userDTO.getTenantId()); |
101 | - user.setPassword(passwordEncoder.encode(DEFAULT_PWD)); | |
99 | + user.setPassword(passwordEncoder.encode(FastIotConstants.DEFAULT_PWD)); | |
102 | 100 | int tenantExist = |
103 | 101 | tenantMapper.selectCount( |
104 | 102 | new QueryWrapper<Tenant>() |
... | ... | @@ -142,7 +140,7 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> imp |
142 | 140 | userRoleMapper.insert(userRole); |
143 | 141 | } |
144 | 142 | // 绑定用户和组织的关系 |
145 | - groupService.bindUserToOrganization(user.getId(), userDTO.getOrganizationIds()); | |
143 | + groupService.bindUserToOrganization(tenantId,user.getId(), userDTO.getOrganizationIds()); | |
146 | 144 | user.copyToDTO(userDTO, PASSWORD, ACTIVATE_TOKEN); |
147 | 145 | return userDTO; |
148 | 146 | } |
... | ... | @@ -352,7 +350,7 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> imp |
352 | 350 | validatePhoneNumberAndEmail(userDTO); |
353 | 351 | User user = new User(); |
354 | 352 | userDTO.copyToEntity(user, ID, PASSWORD, CREATE_TIME, UPDATE_TIME, ACTIVATE_TOKEN); |
355 | - user.setPassword(passwordEncoder.encode(DEFAULT_PWD)); | |
353 | + user.setPassword(passwordEncoder.encode(FastIotConstants.DEFAULT_PWD)); | |
356 | 354 | List<User> users = |
357 | 355 | baseMapper.selectList( |
358 | 356 | new QueryWrapper<User>().lambda().eq(User::getUsername, userDTO.getUsername())); | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | - | |
3 | - | |
4 | - | |
5 | 2 | import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO; |
6 | 3 | import org.thingsboard.server.common.data.yunteng.dto.OrganizationDTO; |
7 | 4 | |
... | ... | @@ -13,11 +10,12 @@ public interface YtOrganizationService { |
13 | 10 | |
14 | 11 | boolean deleteOrganizations(DeleteDTO deleteDTO,String tenantId); |
15 | 12 | |
16 | - List<OrganizationDTO> getMyOrganizations(); | |
13 | + List<OrganizationDTO> getMyOrganizations( | |
14 | + boolean isPtTenantAdmin, String tenantId, String currentUserId); | |
17 | 15 | |
18 | - OrganizationDTO updateOrganization(OrganizationDTO organizationDTO); | |
16 | + OrganizationDTO updateOrganization(OrganizationDTO organizationDTO, String tenantId); | |
19 | 17 | |
20 | - void bindUserToOrganization(String userId, String[] organizationIds); | |
18 | + void bindUserToOrganization(String tenantId, String userId, String[] organizationIds); | |
21 | 19 | |
22 | 20 | void unBindUserToOrganization(Set<String> userIds); |
23 | 21 | } | ... | ... |
... | ... | @@ -34,6 +34,7 @@ |
34 | 34 | <result property="updateTime" column="update_time"/> |
35 | 35 | <result property="accountExpireTime" column="account_expire_time"/> |
36 | 36 | <result property="level" column="level"/> |
37 | + <result property="tbUser" column="tb_user"/> | |
37 | 38 | </resultMap> |
38 | 39 | |
39 | 40 | <sql id="columns"> |
... | ... | @@ -50,7 +51,8 @@ |
50 | 51 | su.update_time AS update_time, |
51 | 52 | su.real_name AS real_name, |
52 | 53 | su.dept_id AS dept_id, |
53 | - su.level AS level | |
54 | + su.level AS level, | |
55 | + su.tb_user AS tb_user | |
54 | 56 | </sql> |
55 | 57 | |
56 | 58 | <select id="findUserDetailsByUserName" resultMap="userDetailsMap"> | ... | ... |