Commit c2334588b0daf19fec623be33f5e55c6ce8819df

Authored by 云中非
2 parents 2909b4ae c048dab4

Merge branch 'master' into ljl1216

... ... @@ -15,6 +15,7 @@ import org.thingsboard.server.common.data.User;
15 15 import org.thingsboard.server.common.data.audit.ActionType;
16 16 import org.thingsboard.server.common.data.exception.ThingsboardException;
17 17 import org.thingsboard.server.common.data.id.EdgeId;
  18 +import org.thingsboard.server.common.data.id.TenantId;
18 19 import org.thingsboard.server.common.data.id.UserId;
19 20 import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
20 21 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
... ... @@ -195,9 +196,14 @@ public class YtAdminController extends BaseController {
195 196
196 197 private void updateOrSaveTenant(TenantReqDTO tenantReqDTO) throws ThingsboardException {
197 198 try {
198   - boolean isCreate = tenantReqDTO.getId() == null;
199   - // 增加TB的租户创建
200   - Tenant tbTenant = new Tenant();
  199 + boolean isCreate = tenantReqDTO.getTenantId() == null;
  200 + Tenant tbTenant;
  201 + if(isCreate){
  202 + // 增加TB的租户创建
  203 + tbTenant = new Tenant();
  204 + }else{
  205 + tbTenant = new Tenant(new TenantId(UUID.fromString(tenantReqDTO.getTenantId())));
  206 + }
201 207 tbTenant.setTitle(tenantReqDTO.getName());
202 208 tbTenant = tenantService.saveTenant(tbTenant);
203 209 if (isCreate) {
... ...
... ... @@ -2,6 +2,7 @@ package org.thingsboard.server.controller.yunteng;
2 2
3 3
4 4 import lombok.RequiredArgsConstructor;
  5 +import org.springframework.security.access.prepost.PreAuthorize;
5 6 import org.springframework.util.Assert;
6 7 import org.springframework.web.bind.annotation.*;
7 8 import org.thingsboard.server.common.data.exception.ThingsboardException;
... ... @@ -28,6 +29,7 @@ public class YtRoleController extends BaseController {
28 29 private final RoleService roleService;
29 30
30 31 @GetMapping(params = {PAGE_SIZE, PAGE})
  32 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN')")
31 33 public YtPageData<RoleDTO> pageRole(
32 34 @RequestParam(PAGE_SIZE) int pageSize,
33 35 @RequestParam(PAGE) int page,
... ... @@ -50,7 +52,7 @@ public class YtRoleController extends BaseController {
50 52 if (orderType != null) {
51 53 queryMap.put(ORDER_TYPE, orderType.name());
52 54 }
53   - return roleService.page(getCurrentUser().isPtSysadmin(), getCurrentUser().isPtAdmin(), getCurrentUser().getCurrentTenantId(),queryMap);
  55 + return roleService.page(getCurrentUser().getCurrentTenantId(),queryMap);
54 56 }
55 57
56 58 @DeleteMapping
... ...
... ... @@ -33,10 +33,12 @@ import org.thingsboard.server.common.data.security.Authority;
33 33 import org.thingsboard.server.common.data.security.UserCredentials;
34 34 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
35 35 import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils;
  36 +import org.thingsboard.server.common.data.yunteng.dto.UserDetailRoleDTO;
36 37 import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO;
37 38 import org.thingsboard.server.common.data.yunteng.dto.request.CodeTTL;
38 39 import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum;
39 40 import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum;
  41 +import org.thingsboard.server.common.data.yunteng.enums.RoleEnum;
40 42 import org.thingsboard.server.dao.audit.AuditLogService;
41 43 import org.thingsboard.server.dao.customer.CustomerService;
42 44 import org.thingsboard.server.service.security.model.SecurityUser;
... ... @@ -47,10 +49,8 @@ import org.thingsboard.server.dao.yunteng.service.YtUserService;
47 49 import ua_parser.Client;
48 50
49 51 import java.time.LocalDateTime;
50   -import java.util.List;
51   -import java.util.Objects;
52   -import java.util.Optional;
53   -import java.util.UUID;
  52 +import java.util.*;
  53 +import java.util.stream.Collectors;
54 54
55 55 import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.CacheConfigKey.MOBILE_LOGIN_SMS_CODE;
56 56 import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.DEFAULT_DELIMITER;
... ... @@ -94,6 +94,10 @@ public class RestAuthenticationProvider implements AuthenticationProvider {
94 94 if (!FastIotConstants.EMAIL_PATTERN.matcher(username).matches()) {
95 95 username += FastIotConstants.DEFAULT_EMAIL_SUFFIX_FOR_TB;
96 96 ytDetailDTO = ytUserDetailsByUserName(ytUserName, password).get();
  97 + // 如果是平台用户单独处理
  98 + if (isPlatFormUser(ytDetailDTO)) {
  99 + return validateByUsernameAndPassword(ytDetailDTO, authentication, userPrincipal);
  100 + }
97 101 }
98 102 return authenticateByUsernameAndPassword(
99 103 ytDetailDTO, authentication, userPrincipal, username, password);
... ... @@ -109,6 +113,26 @@ public class RestAuthenticationProvider implements AuthenticationProvider {
109 113 }
110 114 }
111 115
  116 + private Authentication validateByUsernameAndPassword(
  117 + UserDetailsDTO ytDetailDTO, Authentication authentication, UserPrincipal userPrincipal) {
  118 + Object principal = authentication.getPrincipal();
  119 + if (!(principal instanceof UserPrincipal)) {
  120 + throw new BadCredentialsException("Authentication Failed. Bad user principal.");
  121 + }
  122 + User user = new User();
  123 + user.setUserDetailsDTO(ytDetailDTO);
  124 + user.setAuthority(Authority.PLATFORM_USER);
  125 + user.setTenantId(new TenantId(EntityId.NULL_UUID));
  126 + user.setId(new UserId(UUID.fromString(ytDetailDTO.getId())));
  127 + String email = ytDetailDTO.getUsername();
  128 + email += FastIotConstants.DEFAULT_EMAIL_SUFFIX_FOR_TB;
  129 + user.setEmail(email);
  130 + UserCredentials userCredentials = new UserCredentials();
  131 + SecurityUser securityUser = new SecurityUser(user, userCredentials.isEnabled(), userPrincipal);
  132 + return new UsernamePasswordAuthenticationToken(
  133 + securityUser, null, securityUser.getAuthorities());
  134 + }
  135 +
112 136 private Authentication authenticateByUsernameAndPassword(
113 137 UserDetailsDTO ytDetailDTO,
114 138 Authentication authentication,
... ... @@ -116,7 +140,6 @@ public class RestAuthenticationProvider implements AuthenticationProvider {
116 140 String username,
117 141 String password) {
118 142
119   - // TODO 先验证sys_user账号密码是否正确,正确后,在账号后面加上后缀验证TB用户是否正确
120 143 Object principal = authentication.getPrincipal();
121 144 if (!(principal instanceof UserPrincipal)) {
122 145 throw new BadCredentialsException("Authentication Failed. Bad user principal.");
... ... @@ -358,4 +381,19 @@ public class RestAuthenticationProvider implements AuthenticationProvider {
358 381 }
359 382 return true;
360 383 }
  384 +
  385 + /**
  386 + * 判断是否是平台用户
  387 + *
  388 + * @param ytDetailDTO 用户详情
  389 + * @return true是平台用户 false不是平台用户
  390 + */
  391 + private boolean isPlatFormUser(UserDetailsDTO ytDetailDTO) {
  392 + Set<String> roles =
  393 + ytDetailDTO.getRoles().stream()
  394 + .map(UserDetailRoleDTO::getRoleType)
  395 + .map(RoleEnum::name)
  396 + .collect(Collectors.toSet());
  397 + return roles.stream().anyMatch(role -> role.equals(RoleEnum.PLATFORM_ADMIN.name()));
  398 + }
361 399 }
... ...
... ... @@ -20,6 +20,7 @@ public enum Authority {
20 20 SYS_ADMIN(0),
21 21 TENANT_ADMIN(1),
22 22 CUSTOMER_USER(2),
  23 + PLATFORM_USER(3),
23 24 REFRESH_TOKEN(10);
24 25
25 26 private int code;
... ...
... ... @@ -46,8 +46,7 @@ public class RoleServiceImpl extends AbstractBaseService<RoleMapper, Role> imple
46 46 private final UserRoleMapper userRoleMapper;
47 47
48 48 @Override
49   - public YtPageData<RoleDTO> page(
50   - boolean isSysadmin, boolean isPlatformAdmin, String tenantId, Map<String, Object> queryMap) {
  49 + public YtPageData<RoleDTO> page(String tenantId, Map<String, Object> queryMap) {
51 50 IPage<Role> roleIPage =
52 51 baseMapper.selectPage(
53 52 getPage(queryMap, "create_time", false),
... ... @@ -55,12 +54,8 @@ public class RoleServiceImpl extends AbstractBaseService<RoleMapper, Role> imple
55 54 .lambda()
56 55 .eq(queryMap.get("status") != null, Role::isEnabled, queryMap.get("status"))
57 56 .eq(queryMap.get("roleType") != null, Role::getRoleType, queryMap.get("roleType"))
58   - .ne(queryMap.get("roleType") == null, Role::getRoleType, RoleEnum.TENANT_ADMIN)
59   - .ne(
60   - queryMap.get("roleType") == null && isPlatformAdmin,
61   - Role::getRoleType,
62   - RoleEnum.SYS_ADMIN)
63   - .eq(!isSysadmin, Role::getTenantId, tenantId)
  57 + .ne(queryMap.get("roleType") == null, Role::getRoleType, RoleEnum.TENANT_ADMIN.name())
  58 + .eq(Role::getTenantId, tenantId)
64 59 .like(
65 60 queryMap.get("roleName") != null,
66 61 Role::getName,
... ...
... ... @@ -10,7 +10,7 @@ import java.util.Map;
10 10 import java.util.Set;
11 11
12 12 public interface RoleService {
13   - YtPageData<RoleDTO> page(boolean isSysadmin, boolean isPlatformAdmin, String tenantId, Map<String, Object> queryMap);
  13 + YtPageData<RoleDTO> page(String tenantId, Map<String, Object> queryMap);
14 14
15 15 boolean deleteRole(String[] roleIds,String tenantId);
16 16
... ...