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