diff --git a/.gitignore b/.gitignore index 668b97f4..8da0c3c1 100755 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ hs_err_pid* target *.iml *.yml +log diff --git a/dao/src/main/java/cc/iotkit/dao/AligenieDeviceRepository.java b/dao/src/main/java/cc/iotkit/dao/AligenieDeviceRepository.java index fb4a27da..c9b999c7 100755 --- a/dao/src/main/java/cc/iotkit/dao/AligenieDeviceRepository.java +++ b/dao/src/main/java/cc/iotkit/dao/AligenieDeviceRepository.java @@ -4,6 +4,13 @@ import cc.iotkit.model.aligenie.AligenieDevice; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AligenieDeviceRepository extends MongoRepository { + + void deleteByUid(String uid); + + List findByUid(String uid); + } diff --git a/dao/src/main/java/cc/iotkit/dao/AligenieProductRepository.java b/dao/src/main/java/cc/iotkit/dao/AligenieProductRepository.java index 426a0729..af13b620 100755 --- a/dao/src/main/java/cc/iotkit/dao/AligenieProductRepository.java +++ b/dao/src/main/java/cc/iotkit/dao/AligenieProductRepository.java @@ -4,6 +4,10 @@ import cc.iotkit.model.aligenie.AligenieProduct; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AligenieProductRepository extends MongoRepository { + + List findByUid(String uid); } diff --git a/manager/.DS_Store b/manager/.DS_Store index a3c148ab..ee771938 100755 Binary files a/manager/.DS_Store and b/manager/.DS_Store differ diff --git a/manager/src/main/java/cc/iotkit/manager/config/KeycloakSecurityConfig.java b/manager/src/main/java/cc/iotkit/manager/config/KeycloakSecurityConfig.java index a87de6b1..c6f1738c 100755 --- a/manager/src/main/java/cc/iotkit/manager/config/KeycloakSecurityConfig.java +++ b/manager/src/main/java/cc/iotkit/manager/config/KeycloakSecurityConfig.java @@ -54,12 +54,12 @@ public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter http .authorizeRequests() .antMatchers("/*.html", "/favicon.ico","/v2/api-docs", "/webjars/**", "/swagger-resources/**", "/*.js").permitAll() + .antMatchers("/api/**").hasAnyRole("iot_client_user") .antMatchers("/**/save*").hasRole("iot_write") .antMatchers("/**/del*").hasRole("iot_write") .antMatchers("/**/add*").hasRole("iot_write") .antMatchers("/**/clear*").hasRole("iot_write") .antMatchers("/**").hasAnyRole(systemRole) - .antMatchers("/api/**").hasAnyRole("iot_client_user") .and().csrf().disable(); } } \ No newline at end of file diff --git a/manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java b/manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java index f89e0ec8..751c3792 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java @@ -61,7 +61,7 @@ public class DeviceController { @PostMapping("/list") public PagingData getDevices(int page, - int limit, + int size, String pk, Boolean online, String dn) { @@ -79,7 +79,7 @@ public class DeviceController { condition.and("state.online").is(online); } return new PagingData<>(deviceDao.count(condition), - deviceDao.find(condition, (page - 1) * limit, limit, Sort.Order.desc("createAt"))); + deviceDao.find(condition, (page - 1) * size, size, Sort.Order.desc("createAt"))); } @GetMapping("/{deviceId}/children") diff --git a/manager/src/main/java/cc/iotkit/manager/controller/UserAccountController.java b/manager/src/main/java/cc/iotkit/manager/controller/UserAccountController.java deleted file mode 100755 index 72a04306..00000000 --- a/manager/src/main/java/cc/iotkit/manager/controller/UserAccountController.java +++ /dev/null @@ -1,18 +0,0 @@ -package cc.iotkit.manager.controller; - -import cc.iotkit.dao.UserAccountRepository; -import cc.iotkit.model.UserAccount; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/userAccount") -public class UserAccountController extends DbBaseController { - - @Autowired - public UserAccountController(UserAccountRepository userAccountRepository) { - super(userAccountRepository); - } - -} diff --git a/manager/src/main/java/cc/iotkit/manager/controller/UserInfoController.java b/manager/src/main/java/cc/iotkit/manager/controller/UserInfoController.java index 4cb24426..01f477b4 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/UserInfoController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/UserInfoController.java @@ -5,6 +5,7 @@ import cc.iotkit.common.exception.BizException; import cc.iotkit.common.utils.ReflectUtil; import cc.iotkit.dao.UserInfoRepository; import cc.iotkit.manager.service.AligenieService; +import cc.iotkit.manager.service.DataOwnerService; import cc.iotkit.manager.service.KeycloakAdminService; import cc.iotkit.manager.utils.AuthUtil; import cc.iotkit.model.UserInfo; @@ -14,31 +15,23 @@ import org.springframework.data.domain.Example; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.*; @RestController @RequestMapping("/user") -public class UserInfoController extends DbBaseController { +public class UserInfoController { @Value("${app.systemRole}") private String systemRole; - private final KeycloakAdminService keycloakAdminService; - private final UserInfoRepository userInfoRepository; - private final AligenieService aligenieService; - @Autowired - public UserInfoController(UserInfoRepository userInfoRepository, - KeycloakAdminService keycloakAdminService, - AligenieService aligenieService) { - super(userInfoRepository); - this.keycloakAdminService = keycloakAdminService; - this.userInfoRepository = userInfoRepository; - this.aligenieService = aligenieService; - } + private KeycloakAdminService keycloakAdminService; + @Autowired + private UserInfoRepository userInfoRepository; + @Autowired + private AligenieService aligenieService; + @Autowired + private DataOwnerService ownerService; /** * 平台用户列表 @@ -55,12 +48,12 @@ public class UserInfoController extends DbBaseController optUser = userInfoRepository.findById(id); + if (!optUser.isPresent()) { + throw new BizException("user does not exist"); + } + UserInfo user = optUser.get(); + ownerService.checkOwner(user); + userInfoRepository.deleteById(id); + } + @PostMapping("/client/user/save") public void saveClientUser(@RequestBody UserInfo user) { Optional userOpt = userInfoRepository.findById(user.getId()); @@ -102,11 +106,5 @@ public class UserInfoController extends DbBaseController { +public class AligenieDeviceController { @Autowired - public AligenieDeviceController(AligenieDeviceRepository aligenieDeviceRepository) { - super(aligenieDeviceRepository); + private AligenieDeviceRepository aligenieDeviceRepository; + @Autowired + private UserInfoRepository userInfoRepository; + @Autowired + private DataOwnerService ownerService; + @Autowired + private DeviceRepository deviceRepository; + + @GetMapping("/list/{uid}") + public List getDevices(@PathVariable("uid") String uid) { + UserInfo user = userInfoRepository.findById(uid).get(); + ownerService.checkOwner(user); + return aligenieDeviceRepository.findByUid(uid); + } + + @PostMapping("/bind/{uid}") + public void bind(@PathVariable("uid") String uid, + @RequestBody List devices) { + Optional optUser = userInfoRepository.findById(uid); + if (!optUser.isPresent()) { + throw new BizException("user does not exist"); + } + UserInfo user = optUser.get(); + ownerService.checkOwner(user); + + aligenieDeviceRepository.deleteByUid(uid); + for (Device device : devices) { + aligenieDeviceRepository.save(AligenieDevice.builder() + .uid(uid) + .deviceId(device.getDeviceId()) + .spaceName("客厅") + .name(device.getName()) + .build()); + } + + } + + @Data + public static class Device { + private String deviceId; + private String name; } } diff --git a/manager/src/main/java/cc/iotkit/manager/controller/aligenie/AligenieProductController.java b/manager/src/main/java/cc/iotkit/manager/controller/aligenie/AligenieProductController.java index c7cd9502..5bce08a9 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/aligenie/AligenieProductController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/aligenie/AligenieProductController.java @@ -1,14 +1,11 @@ package cc.iotkit.manager.controller.aligenie; -import cc.iotkit.dao.AligenieProductDao; import cc.iotkit.dao.AligenieProductRepository; -import cc.iotkit.dao.ProductDao; -import cc.iotkit.manager.controller.DbBaseController; import cc.iotkit.manager.model.aligenie.AligenieProductVo; import cc.iotkit.manager.service.DataOwnerService; import cc.iotkit.manager.utils.AuthUtil; -import cc.iotkit.model.product.Product; import cc.iotkit.model.aligenie.AligenieProduct; +import cc.iotkit.model.product.Product; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; import org.springframework.web.bind.annotation.GetMapping; @@ -21,38 +18,18 @@ import java.util.List; @RestController @RequestMapping("/aligenie/product") -public class AligenieProductController extends DbBaseController { - - private final ProductDao productDao; - private final AligenieProductDao aligenieProductDao; - private final DataOwnerService dataOwnerService; - +public class AligenieProductController { @Autowired - public AligenieProductController(AligenieProductRepository aligenieProductRepository, - ProductDao productDao, - AligenieProductDao aligenieProductDao, - DataOwnerService dataOwnerService) { - super(aligenieProductRepository); - this.productDao = productDao; - this.aligenieProductDao = aligenieProductDao; - this.dataOwnerService = dataOwnerService; - } + private DataOwnerService dataOwnerService; + + @Autowired + private AligenieProductRepository aligenieProductRepository; + @GetMapping("/products") - public List products() { - List productVos = new ArrayList<>(); - List aligenieProducts = repository - .findAll(Example - .of(AligenieProduct.builder() - .uid(AuthUtil.getUserId()) - .build())); - - for (AligenieProduct ap : aligenieProducts) { - Product product = productDao.get(ap.getProductKey()); - productVos.add(new AligenieProductVo(ap, product.getName())); - } - return productVos; + public List products() { + return aligenieProductRepository.findByUid(AuthUtil.getUserId()); } @PostMapping("/save") @@ -61,7 +38,7 @@ public class AligenieProductController extends DbBaseController= 300) { log.error("create userRepresentation response:{}", JsonUtil.toJsonString(response)); throw new BizException("create keycloak user failed"); } + + return newUid; } public void updateUser(UserInfo user) { diff --git a/model/src/main/java/cc/iotkit/model/UserInfo.java b/model/src/main/java/cc/iotkit/model/UserInfo.java index 2326af0d..e6083fec 100755 --- a/model/src/main/java/cc/iotkit/model/UserInfo.java +++ b/model/src/main/java/cc/iotkit/model/UserInfo.java @@ -16,7 +16,7 @@ import java.util.List; @NoArgsConstructor @AllArgsConstructor @Document -public class UserInfo { +public class UserInfo implements Owned { public static final int USER_TYPE_PLATFORM = 0; public static final int USER_TYPE_CLIENT = 1;