Commit 9ef3717d0e7061e35e429766e37b4bdbaabaa491

Authored by 云中非
2 parents 7be8b2df a69d02c1

Merge remote-tracking branch 'origin/master' into 20220507

@@ -43,7 +43,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. @@ -43,7 +43,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.
43 43
44 @RestController 44 @RestController
45 @RequestMapping("api/yt/admin") 45 @RequestMapping("api/yt/admin")
46 -@Api(tags = {"租户管理","菜单管理"}) 46 +@Api(tags = {"租户管理", "菜单管理"})
47 @RequiredArgsConstructor 47 @RequiredArgsConstructor
48 @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')") 48 @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')")
49 public class YtAdminController extends BaseController { 49 public class YtAdminController extends BaseController {
@@ -56,6 +56,7 @@ public class YtAdminController extends BaseController { @@ -56,6 +56,7 @@ public class YtAdminController extends BaseController {
56 private final UserService tbUserService; 56 private final UserService tbUserService;
57 57
58 @PostMapping("/tenant") 58 @PostMapping("/tenant")
  59 + @PreAuthorize("hasAnyAuthority('api:yt:admin:saveTenant')")
59 public ResponseEntity<TenantDTO> saveTenant(@RequestBody TenantReqDTO tenantReqDTO) { 60 public ResponseEntity<TenantDTO> saveTenant(@RequestBody TenantReqDTO tenantReqDTO) {
60 TenantDTO newTenant = ytTenantService.createNewTenant(tenantReqDTO); 61 TenantDTO newTenant = ytTenantService.createNewTenant(tenantReqDTO);
61 URI location = 62 URI location =
@@ -100,34 +101,44 @@ public class YtAdminController extends BaseController { @@ -100,34 +101,44 @@ public class YtAdminController extends BaseController {
100 } 101 }
101 102
102 @DeleteMapping("/tenant") 103 @DeleteMapping("/tenant")
103 - public void deleteTenant(@Validated(DeleteGroup.class) @RequestBody DeleteDTO deleteDTO) throws ThingsboardException{  
104 -  
105 - deleteDTO.getIds().forEach(id->{  
106 - ytTenantService.findById(id).ifPresent(tenantDTO -> {  
107 - try {  
108 - deleteTenant(tenantDTO.getTenantId());  
109 - } catch (ThingsboardException e) {  
110 - handleException(e);  
111 - }  
112 - });  
113 - }); 104 + public void deleteTenant(@Validated(DeleteGroup.class) @RequestBody DeleteDTO deleteDTO)
  105 + throws ThingsboardException {
  106 +
  107 + deleteDTO
  108 + .getIds()
  109 + .forEach(
  110 + id -> {
  111 + ytTenantService
  112 + .findById(id)
  113 + .ifPresent(
  114 + tenantDTO -> {
  115 + try {
  116 + deleteTenant(tenantDTO.getTenantId());
  117 + } catch (ThingsboardException e) {
  118 + handleException(e);
  119 + }
  120 + });
  121 + });
114 ytTenantService.deleteTenants( 122 ytTenantService.deleteTenants(
115 deleteDTO.getIds().toArray(new String[deleteDTO.getIds().size()])); 123 deleteDTO.getIds().toArray(new String[deleteDTO.getIds().size()]));
116 } 124 }
117 125
118 @DeleteMapping("/user/deleteTenantAdmin") 126 @DeleteMapping("/user/deleteTenantAdmin")
119 - public ResponseEntity<Boolean> deleteTenantAdmin(@Validated(DeleteGroup.class) @RequestBody DeleteDTO deleteDTO)  
120 - throws ThingsboardException { 127 + public ResponseEntity<Boolean> deleteTenantAdmin(
  128 + @Validated(DeleteGroup.class) @RequestBody DeleteDTO deleteDTO) throws ThingsboardException {
121 for (String strUserId : deleteDTO.getIds()) { 129 for (String strUserId : deleteDTO.getIds()) {
122 UserDTO userDTO = userService.findUserInfoById(strUserId); 130 UserDTO userDTO = userService.findUserInfoById(strUserId);
123 - if(null != userDTO && StringUtils.isNotBlank(userDTO.getTbUser())){ 131 + if (null != userDTO && StringUtils.isNotBlank(userDTO.getTbUser())) {
124 deleteTenantAdmin(userDTO.getTbUser()); 132 deleteTenantAdmin(userDTO.getTbUser());
125 - }else { 133 + } else {
126 return ResponseEntity.ok(false); 134 return ResponseEntity.ok(false);
127 } 135 }
128 } 136 }
129 - boolean result = userService.deleteUser(  
130 - deleteDTO.getIds(), getCurrentUser().isPtSysadmin(), getCurrentUser().getCurrentTenantId()); 137 + boolean result =
  138 + userService.deleteUser(
  139 + deleteDTO.getIds(),
  140 + getCurrentUser().isPtSysadmin(),
  141 + getCurrentUser().getCurrentTenantId());
131 return ResponseEntity.ok(result); 142 return ResponseEntity.ok(result);
132 } 143 }
133 144
@@ -164,13 +175,16 @@ public class YtAdminController extends BaseController { @@ -164,13 +175,16 @@ public class YtAdminController extends BaseController {
164 } 175 }
165 176
166 @GetMapping("/me/menus") 177 @GetMapping("/me/menus")
167 - public ResponseEntity<List<MenuDTO>> getMyMenus() throws ThingsboardException { 178 + public ResponseEntity<List<MenuDTO>> getMyMenus(
  179 + @RequestParam(value = "needButton", required = false, defaultValue = "true")
  180 + boolean needButton)
  181 + throws ThingsboardException {
168 return ResponseEntity.ok( 182 return ResponseEntity.ok(
169 menuService.getMyMenus( 183 menuService.getMyMenus(
170 getCurrentUser().getCurrentTenantId(), 184 getCurrentUser().getCurrentTenantId(),
171 getCurrentUser().getCurrentUserId(), 185 getCurrentUser().getCurrentUserId(),
172 getCurrentUser().isPtSysadmin(), 186 getCurrentUser().isPtSysadmin(),
173 - getCurrentUser().isPtTenantAdmin())); 187 + getCurrentUser().isPtTenantAdmin(),needButton));
174 } 188 }
175 189
176 @PutMapping("/menu/assign/{tenantId}") 190 @PutMapping("/menu/assign/{tenantId}")
@@ -207,10 +221,10 @@ public class YtAdminController extends BaseController { @@ -207,10 +221,10 @@ public class YtAdminController extends BaseController {
207 try { 221 try {
208 boolean isCreate = tenantReqDTO.getTenantId() == null; 222 boolean isCreate = tenantReqDTO.getTenantId() == null;
209 Tenant tbTenant; 223 Tenant tbTenant;
210 - if(isCreate){ 224 + if (isCreate) {
211 // 增加TB的租户创建 225 // 增加TB的租户创建
212 tbTenant = new Tenant(); 226 tbTenant = new Tenant();
213 - }else{ 227 + } else {
214 tbTenant = new Tenant(new TenantId(UUID.fromString(tenantReqDTO.getTenantId()))); 228 tbTenant = new Tenant(new TenantId(UUID.fromString(tenantReqDTO.getTenantId())));
215 } 229 }
216 tbTenant.setTenantProfileId(tenantReqDTO.getTenantProfileId()); 230 tbTenant.setTenantProfileId(tenantReqDTO.getTenantProfileId());
@@ -239,7 +253,8 @@ public class YtAdminController extends BaseController { @@ -239,7 +253,8 @@ public class YtAdminController extends BaseController {
239 tenantService.deleteTenant(tenantId); 253 tenantService.deleteTenant(tenantId);
240 tenantProfileCache.evict(tenantId); 254 tenantProfileCache.evict(tenantId);
241 tbClusterService.onTenantDelete(tenant, null); 255 tbClusterService.onTenantDelete(tenant, null);
242 - tbClusterService.broadcastEntityStateChangeEvent(tenantId, tenantId, ComponentLifecycleEvent.DELETED); 256 + tbClusterService.broadcastEntityStateChangeEvent(
  257 + tenantId, tenantId, ComponentLifecycleEvent.DELETED);
243 } catch (Exception e) { 258 } catch (Exception e) {
244 throw handleException(e); 259 throw handleException(e);
245 } 260 }
@@ -4,6 +4,7 @@ import io.swagger.annotations.Api; @@ -4,6 +4,7 @@ import io.swagger.annotations.Api;
4 import io.swagger.annotations.ApiOperation; 4 import io.swagger.annotations.ApiOperation;
5 import lombok.RequiredArgsConstructor; 5 import lombok.RequiredArgsConstructor;
6 import org.springframework.http.ResponseEntity; 6 import org.springframework.http.ResponseEntity;
  7 +import org.springframework.security.access.prepost.PreAuthorize;
7 import org.springframework.validation.annotation.Validated; 8 import org.springframework.validation.annotation.Validated;
8 import org.springframework.web.bind.annotation.*; 9 import org.springframework.web.bind.annotation.*;
9 import org.thingsboard.server.common.data.exception.ThingsboardException; 10 import org.thingsboard.server.common.data.exception.ThingsboardException;
@@ -27,6 +28,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. @@ -27,6 +28,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.
27 @RequestMapping("/api/yt/configuration/center") 28 @RequestMapping("/api/yt/configuration/center")
28 @RequiredArgsConstructor 29 @RequiredArgsConstructor
29 @Api(tags = "组态中心") 30 @Api(tags = "组态中心")
  31 +@PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
30 public class YtConfigurationCenterController extends BaseController { 32 public class YtConfigurationCenterController extends BaseController {
31 33
32 private final YtConfigurationCenterService ytConfigurationCenterService; 34 private final YtConfigurationCenterService ytConfigurationCenterService;
@@ -26,12 +26,14 @@ public class YtMenuController extends BaseController { @@ -26,12 +26,14 @@ public class YtMenuController extends BaseController {
26 private final MenuService menuService; 26 private final MenuService menuService;
27 27
28 @GetMapping("/me/menus") 28 @GetMapping("/me/menus")
29 - public List<MenuDTO> getMyMenus() throws ThingsboardException { 29 + public List<MenuDTO> getMyMenus(
  30 + @RequestParam(value = "needButton", required = false, defaultValue = "true") boolean needButton
  31 + ) throws ThingsboardException {
30 return menuService.getMyMenus( 32 return menuService.getMyMenus(
31 getCurrentUser().getCurrentTenantId(), 33 getCurrentUser().getCurrentTenantId(),
32 getCurrentUser().getCurrentUserId(), 34 getCurrentUser().getCurrentUserId(),
33 getCurrentUser().isPtSysadmin(), 35 getCurrentUser().isPtSysadmin(),
34 - getCurrentUser().isPtTenantAdmin()); 36 + getCurrentUser().isPtTenantAdmin(),needButton);
35 } 37 }
36 38
37 @GetMapping("getMenuIdsByRoleId/{roleId}") 39 @GetMapping("getMenuIdsByRoleId/{roleId}")
@@ -36,35 +36,42 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple @@ -36,35 +36,42 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple
36 public List<MenuDTO> getAllMenus(String tenantId) { 36 public List<MenuDTO> getAllMenus(String tenantId) {
37 37
38 List<Menu> menus = 38 List<Menu> menus =
39 - baseMapper.selectList(  
40 - new QueryWrapper<Menu>().lambda().eq(Menu::getTenantId, tenantId)); 39 + baseMapper.selectList(new QueryWrapper<Menu>().lambda().eq(Menu::getTenantId, tenantId));
41 Map<String, MenuDTO> menuDTOMap = new LinkedHashMap<>(menus.size()); 40 Map<String, MenuDTO> menuDTOMap = new LinkedHashMap<>(menus.size());
42 menus.forEach(menu -> menuDTOMap.put(menu.getId(), menu.getDTO(MenuDTO.class))); 41 menus.forEach(menu -> menuDTOMap.put(menu.getId(), menu.getDTO(MenuDTO.class)));
43 - return buildMenuDTOTree(menuDTOMap); 42 + return buildMenuDTOTree(menuDTOMap,true);
44 } 43 }
45 44
46 @Override 45 @Override
47 - public List<MenuDTO> getMyMenus(String tenantId,String userId,boolean isSysAdmin,boolean isTenantAdmin) { 46 + public List<MenuDTO> getMyMenus(
  47 + String tenantId,
  48 + String userId,
  49 + boolean isSysAdmin,
  50 + boolean isTenantAdmin,
  51 + boolean needButton) {
48 List<MenuDTO> menuDTOs; 52 List<MenuDTO> menuDTOs;
49 if (isSysAdmin) { 53 if (isSysAdmin) {
50 menuDTOs = baseMapper.selectSysAdminMenu(); 54 menuDTOs = baseMapper.selectSysAdminMenu();
51 - }  
52 - else if (isTenantAdmin) { 55 + } else if (isTenantAdmin) {
53 menuDTOs = baseMapper.selectTenantMenu(tenantId); 56 menuDTOs = baseMapper.selectTenantMenu(tenantId);
54 - }  
55 - else { 57 + } else {
56 menuDTOs = baseMapper.selectMyMenu(userId); 58 menuDTOs = baseMapper.selectMyMenu(userId);
57 } 59 }
58 Map<String, MenuDTO> menuDTOMap = new LinkedHashMap<>(menuDTOs.size()); 60 Map<String, MenuDTO> menuDTOMap = new LinkedHashMap<>(menuDTOs.size());
59 menuDTOs.forEach(menuDTO -> menuDTOMap.put(menuDTO.getId(), menuDTO)); 61 menuDTOs.forEach(menuDTO -> menuDTOMap.put(menuDTO.getId(), menuDTO));
60 - return buildMenuDTOTree(menuDTOMap); 62 + return buildMenuDTOTree(menuDTOMap, needButton);
61 } 63 }
62 64
63 - private List<MenuDTO> buildMenuDTOTree(Map<String, MenuDTO> menuDTOMap) { 65 + private List<MenuDTO> buildMenuDTOTree(Map<String, MenuDTO> menuDTOMap, boolean needButton) {
64 List<MenuDTO> result = Lists.newArrayList(); 66 List<MenuDTO> result = Lists.newArrayList();
65 for (MenuDTO node : menuDTOMap.values()) { 67 for (MenuDTO node : menuDTOMap.values()) {
66 MenuDTO parent = menuDTOMap.get(node.getParentId()); 68 MenuDTO parent = menuDTOMap.get(node.getParentId());
67 if (parent != null && !(node.getId().equals(parent.getId()))) { 69 if (parent != null && !(node.getId().equals(parent.getId()))) {
  70 + if (!needButton) {
  71 + if (node.getMeta().get("menuType").asText().equals("2")) {
  72 + continue;
  73 + }
  74 + }
68 parent.getChildren().add(node); 75 parent.getChildren().add(node);
69 continue; 76 continue;
70 } 77 }
@@ -77,12 +84,11 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple @@ -77,12 +84,11 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple
77 84
78 @Override 85 @Override
79 @Transactional 86 @Transactional
80 - public MenuDTO saveMenu(String tenantId,boolean isSysAdmin,MenuDTO menuDTO) { 87 + public MenuDTO saveMenu(String tenantId, boolean isSysAdmin, MenuDTO menuDTO) {
81 if (menuDTO.getType() == null) { 88 if (menuDTO.getType() == null) {
82 menuDTO.setType(MenuTypeEnum.CUSTOM); 89 menuDTO.setType(MenuTypeEnum.CUSTOM);
83 } 90 }
84 - if (!isSysAdmin  
85 - && menuDTO.getType().equals(MenuTypeEnum.SYSADMIN)) { 91 + if (!isSysAdmin && menuDTO.getType().equals(MenuTypeEnum.SYSADMIN)) {
86 throw new AccessDeniedException("Non-system admin cannot save system menu"); 92 throw new AccessDeniedException("Non-system admin cannot save system menu");
87 } 93 }
88 if (menuDTO.getType().equals(MenuTypeEnum.CUSTOM) 94 if (menuDTO.getType().equals(MenuTypeEnum.CUSTOM)
@@ -112,7 +118,7 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple @@ -112,7 +118,7 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple
112 118
113 @Override 119 @Override
114 @Transactional 120 @Transactional
115 - public MenuDTO updateMenu(String tenantId,boolean isSysAdmin,MenuDTO menuDTO) { 121 + public MenuDTO updateMenu(String tenantId, boolean isSysAdmin, MenuDTO menuDTO) {
116 Menu menu = baseMapper.selectById(menuDTO.getId()); 122 Menu menu = baseMapper.selectById(menuDTO.getId());
117 if (menu == null) { 123 if (menu == null) {
118 throw new YtDataValidationException("cannot find menu to update"); 124 throw new YtDataValidationException("cannot find menu to update");
@@ -134,7 +140,7 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple @@ -134,7 +140,7 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple
134 140
135 @Override 141 @Override
136 @Transactional 142 @Transactional
137 - public boolean deleteMenus(String tenantId,String[] menuIds) { 143 + public boolean deleteMenus(String tenantId, String[] menuIds) {
138 Set<String> ids = Set.of(menuIds); 144 Set<String> ids = Set.of(menuIds);
139 // 2. 删除角色对应的menu 145 // 2. 删除角色对应的menu
140 roleMapper.deleteRoleMenuMappingByMenuIds(ids); 146 roleMapper.deleteRoleMenuMappingByMenuIds(ids);
@@ -148,7 +154,7 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple @@ -148,7 +154,7 @@ public class MenuServiceImpl extends AbstractBaseService<MenuMapper, Menu> imple
148 154
149 @Override 155 @Override
150 @Transactional 156 @Transactional
151 - public boolean assignMenuToTenant(String tenantId,String[] menuId) { 157 + public boolean assignMenuToTenant(String tenantId, String[] menuId) {
152 Set<String> ids = Set.of(menuId); 158 Set<String> ids = Set.of(menuId);
153 int menuCountInDB = 159 int menuCountInDB =
154 baseMapper.selectCount(new QueryWrapper<Menu>().lambda().in(Menu::getId, ids)); 160 baseMapper.selectCount(new QueryWrapper<Menu>().lambda().in(Menu::getId, ids));
@@ -19,7 +19,7 @@ public interface MenuService { @@ -19,7 +19,7 @@ public interface MenuService {
19 * @return list 树状menu 19 * @return list 树状menu
20 */ 20 */
21 List<MenuDTO> getMyMenus( 21 List<MenuDTO> getMyMenus(
22 - String tenantId, String userId, boolean isPtSysAdmin, boolean isPtTenantAdmin); 22 + String tenantId, String userId, boolean isPtSysAdmin, boolean isPtTenantAdmin,boolean needButton);
23 23
24 MenuDTO saveMenu(String tenantId, boolean isPtSysAdmin, MenuDTO menuDTO); 24 MenuDTO saveMenu(String tenantId, boolean isPtSysAdmin, MenuDTO menuDTO);
25 25