Showing
5 changed files
with
103 additions
and
12 deletions
application/src/main/java/org/thingsboard/server/controller/yunteng/YtMenuController.java
0 → 100644
1 | +package org.thingsboard.server.controller.yunteng; | |
2 | + | |
3 | +import lombok.RequiredArgsConstructor; | |
4 | +import org.springframework.http.HttpStatus; | |
5 | +import org.springframework.http.ResponseEntity; | |
6 | +import org.springframework.security.access.prepost.PreAuthorize; | |
7 | +import org.springframework.util.Assert; | |
8 | +import org.springframework.web.bind.annotation.*; | |
9 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | |
10 | +import org.thingsboard.server.common.data.exception.ThingsboardException; | |
11 | +import org.thingsboard.server.common.data.yunteng.core.exception.DataValidationException; | |
12 | +import org.thingsboard.server.common.data.yunteng.core.exception.FastIotException; | |
13 | +import org.thingsboard.server.common.data.yunteng.dto.MenuDTO; | |
14 | +import org.thingsboard.server.controller.BaseController; | |
15 | +import org.thingsboard.server.dao.yunteng.service.MenuService; | |
16 | + | |
17 | +import java.net.URI; | |
18 | +import java.util.List; | |
19 | +import java.util.Optional; | |
20 | +import java.util.Set; | |
21 | + | |
22 | +@RestController | |
23 | +@RequiredArgsConstructor | |
24 | +@RequestMapping("api/yt/menu") | |
25 | +public class YtMenuController extends BaseController { | |
26 | + | |
27 | + private final MenuService menuService; | |
28 | + | |
29 | + @GetMapping("/me/menus") | |
30 | + public List<MenuDTO> getMyMenus() throws ThingsboardException { | |
31 | + return menuService.getMyMenus( | |
32 | + getCurrentUser().getCurrentTenantId(), | |
33 | + getCurrentUser().getCurrentUserId(), | |
34 | + getCurrentUser().isPtSysadmin(), | |
35 | + getCurrentUser().isPtTenantAdmin()); | |
36 | + } | |
37 | + | |
38 | + @GetMapping("getMenuIdsByRoleId/{roleId}") | |
39 | + public Set<String> getMenuIdsByRoleId(@PathVariable("roleId") String roleId) { | |
40 | + return menuService.getMenuIdsByRoleId(roleId); | |
41 | + } | |
42 | + | |
43 | + @PutMapping("/manage/assign/{roleId}") | |
44 | + public void assignMenuToRole( | |
45 | + @RequestBody String[] menuIds, @PathVariable("roleId") String roleId) { | |
46 | + menuService.assignMenuToRole(menuIds, roleId); | |
47 | + } | |
48 | + | |
49 | + @GetMapping("{id}") | |
50 | + public ResponseEntity<MenuDTO> getMenus(@PathVariable("id") String id) { | |
51 | + return ResponseEntity.of(menuService.getMenu(id)); | |
52 | + } | |
53 | + | |
54 | + @PutMapping | |
55 | + // @PreAuthorize("@iot.check('menu:update')") | |
56 | + public MenuDTO updateMenu(@RequestBody MenuDTO menuDTO) throws ThingsboardException { | |
57 | + Assert.notNull(menuDTO.getId(), "menuId cannot be null"); | |
58 | + return menuService.updateMenu( | |
59 | + getCurrentUser().getCurrentTenantId(), getCurrentUser().isPtSysadmin(), menuDTO); | |
60 | + } | |
61 | + | |
62 | + @PostMapping | |
63 | + @PreAuthorize("hasAnyRole('SYS_ADMIN','TENANT_ADMIN')") | |
64 | + public ResponseEntity<MenuDTO> saveMenu(@RequestBody MenuDTO menuDTO) | |
65 | + throws FastIotException, ThingsboardException { | |
66 | + MenuDTO newMenuDTO = | |
67 | + menuService.saveMenu( | |
68 | + getCurrentUser().getCurrentTenantId(), getCurrentUser().isPtSysadmin(), menuDTO); | |
69 | + return Optional.ofNullable(newMenuDTO) | |
70 | + .map( | |
71 | + dto -> { | |
72 | + URI location = | |
73 | + ServletUriComponentsBuilder.fromCurrentRequest() | |
74 | + .path("/{id}") | |
75 | + .buildAndExpand(newMenuDTO.getId()) | |
76 | + .toUri(); | |
77 | + return ResponseEntity.created(location).body(newMenuDTO); | |
78 | + }) | |
79 | + .orElse(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build()); | |
80 | + } | |
81 | + | |
82 | + @DeleteMapping | |
83 | + @PreAuthorize("hasAnyRole('SYS_ADMIN','TENANT_ADMIN')") | |
84 | + public void deleteMenus(@RequestBody String[] ids) throws ThingsboardException { | |
85 | + if (ids.length == 0) { | |
86 | + throw new DataValidationException("please provide menu ids to delete"); | |
87 | + } | |
88 | + menuService.deleteMenus(getCurrentUser().getCurrentTenantId(), ids); | |
89 | + } | |
90 | +} | ... | ... |
... | ... | @@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils; |
8 | 8 | import org.springframework.beans.BeanUtils; |
9 | 9 | import org.springframework.stereotype.Service; |
10 | 10 | import org.springframework.transaction.annotation.Transactional; |
11 | +import org.thingsboard.server.common.data.id.EntityId; | |
11 | 12 | import org.thingsboard.server.common.data.yunteng.core.exception.DataValidationException; |
12 | 13 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
13 | 14 | import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; |
... | ... | @@ -54,7 +55,7 @@ public class YtTenantServiceImpl extends AbstractBaseService<TenantMapper, Tenan |
54 | 55 | public PageData<TenantDTO> page(Map<String, Object> queryMap) { |
55 | 56 | IPage<Tenant> tenantIPage = getPage(queryMap, "create_time", true); |
56 | 57 | IPage<TenantDTO> userPage = |
57 | - baseMapper.getTenantPage(tenantIPage, (String) queryMap.get("tenantName")); | |
58 | + baseMapper.getTenantPage(tenantIPage, (String) queryMap.get("tenantName"), EntityId.NULL_UUID.toString()); | |
58 | 59 | PageData<TenantDTO> pageData = getPageData(userPage, TenantDTO.class); |
59 | 60 | determineTenantStatus(pageData.getItems()); |
60 | 61 | return pageData; | ... | ... |
... | ... | @@ -14,5 +14,8 @@ import java.util.Set; |
14 | 14 | public interface TenantMapper extends BaseMapper<Tenant> { |
15 | 15 | Set<String> getTenantIdsByTenantIds(@Param("tenantIds") Collection<String> tenantIds); |
16 | 16 | |
17 | - IPage<TenantDTO> getTenantPage(IPage<?> page, @Param("tenantName") String tenantName); | |
17 | + IPage<TenantDTO> getTenantPage( | |
18 | + IPage<?> page, | |
19 | + @Param("tenantName") String tenantName, | |
20 | + @Param("defaultTenantId") String defaultTenantId); | |
18 | 21 | } | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | 2 | |
3 | - | |
4 | - | |
5 | 3 | import org.thingsboard.server.common.data.yunteng.dto.MenuDTO; |
6 | - | |
7 | 4 | import java.util.List; |
8 | 5 | import java.util.Optional; |
9 | 6 | import java.util.Set; |
10 | 7 | |
11 | 8 | public interface MenuService { |
12 | - | |
13 | 9 | /** |
14 | 10 | * sysadmin获取所有的menu,但是不获取租户自己创建的menu |
15 | 11 | * |
... | ... | @@ -22,17 +18,18 @@ public interface MenuService { |
22 | 18 | * |
23 | 19 | * @return list 树状menu |
24 | 20 | */ |
25 | - List<MenuDTO> getMyMenus(String tenantId,String userId,boolean isSysAdmin,boolean isTenantAdmin); | |
21 | + List<MenuDTO> getMyMenus( | |
22 | + String tenantId, String userId, boolean isPtSysAdmin, boolean isPtTenantAdmin); | |
26 | 23 | |
27 | - MenuDTO saveMenu(String tenantId,boolean isSysAdmin,MenuDTO menuDTO); | |
24 | + MenuDTO saveMenu(String tenantId, boolean isPtSysAdmin, MenuDTO menuDTO); | |
28 | 25 | |
29 | 26 | Optional<MenuDTO> getMenu(String id); |
30 | 27 | |
31 | - MenuDTO updateMenu(String tenantId,boolean isSysAdmin,MenuDTO menuDTO); | |
28 | + MenuDTO updateMenu(String tenantId, boolean isPtSysAdmin, MenuDTO menuDTO); | |
32 | 29 | |
33 | - boolean deleteMenus(String tenantId,String[] ids); | |
30 | + boolean deleteMenus(String tenantId, String[] ids); | |
34 | 31 | |
35 | - boolean assignMenuToTenant(String tenantId,String[] menuId); | |
32 | + boolean assignMenuToTenant(String tenantId, String[] menuId); | |
36 | 33 | |
37 | 34 | boolean assignMenuToRole(String[] menuId, String roleId); |
38 | 35 | ... | ... |
... | ... | @@ -24,7 +24,7 @@ |
24 | 24 | SELECT id, tenant_id,name, enabled, description, default_config, tenant_expire_time, create_time,icon |
25 | 25 | FROM sys_tenant |
26 | 26 | <where> |
27 | - tenant_id != 'DEFAULT_SYS_ADMIN_TENANT_CODE' | |
27 | + tenant_id != #{defaultTenantId} | |
28 | 28 | <if test="tenantName!=null"> |
29 | 29 | AND name LIKE CONCAT('%',#{tenantName}::TEXT,'%') |
30 | 30 | </if> | ... | ... |