第三方接入修改

V0.5.x
xiwa 2022-03-23 05:07:37 +08:00
parent eaa2749663
commit bcaef397c0
12 changed files with 115 additions and 95 deletions

1
.gitignore vendored
View File

@ -25,3 +25,4 @@ hs_err_pid*
target
*.iml
*.yml
log

View File

@ -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<AligenieDevice, String> {
void deleteByUid(String uid);
List<AligenieDevice> findByUid(String uid);
}

View File

@ -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<AligenieProduct, String> {
List<AligenieProduct> findByUid(String uid);
}

BIN
manager/.DS_Store vendored

Binary file not shown.

View File

@ -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();
}
}

View File

@ -61,7 +61,7 @@ public class DeviceController {
@PostMapping("/list")
public PagingData<DeviceInfo> 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")

View File

@ -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<UserAccountRepository, UserAccount> {
@Autowired
public UserAccountController(UserAccountRepository userAccountRepository) {
super(userAccountRepository);
}
}

View File

@ -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<UserInfoRepository, UserInfo> {
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<UserInfoRepository, Use
*/
@PostMapping("/platform/user/add")
public void addPlatformUser(@RequestBody UserInfo user) {
user.setId(UUID.randomUUID().toString());
user.setType(UserInfo.USER_TYPE_PLATFORM);
user.setOwnerId(AuthUtil.getUserId());
user.setRoles(Arrays.asList(Constants.ROLE_SYSTEM));
user.setRoles(Collections.singletonList(Constants.ROLE_SYSTEM));
user.setCreateAt(System.currentTimeMillis());
keycloakAdminService.createUser(user, Constants.PWD_SYSTEM_USER);
String uid = keycloakAdminService.createUser(user, Constants.PWD_SYSTEM_USER);
user.setId(uid);
userInfoRepository.save(user);
}
@ -81,15 +74,26 @@ public class UserInfoController extends DbBaseController<UserInfoRepository, Use
*/
@PostMapping("/client/user/add")
public void addClientUser(@RequestBody UserInfo user) {
user.setId(UUID.randomUUID().toString());
user.setType(UserInfo.USER_TYPE_CLIENT);
user.setOwnerId(AuthUtil.getUserId());
user.setRoles(Arrays.asList(Constants.ROLE_CLIENT));
user.setRoles(Collections.singletonList(Constants.ROLE_CLIENT));
user.setCreateAt(System.currentTimeMillis());
keycloakAdminService.createUser(user, Constants.PWD_CLIENT_USER);
String uid = keycloakAdminService.createUser(user, Constants.PWD_CLIENT_USER);
user.setId(uid);
userInfoRepository.save(user);
}
@PostMapping("/client/user/{id}/delete")
public void deleteClientUser(@PathVariable("id") String id) {
Optional<UserInfo> 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<UserInfo> userOpt = userInfoRepository.findById(user.getId());
@ -102,11 +106,5 @@ public class UserInfoController extends DbBaseController<UserInfoRepository, Use
}
ReflectUtil.copyNoNulls(user, oldUser);
userInfoRepository.save(oldUser);
boolean isAligenie = user.getUsePlatforms().isAligenie();
//同步天猫精灵设备
if (oldUser.getUsePlatforms().isAligenie() != isAligenie) {
aligenieService.syncDevice(user);
}
}
}

View File

@ -1,19 +1,65 @@
package cc.iotkit.manager.controller.aligenie;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.dao.AligenieDeviceRepository;
import cc.iotkit.manager.controller.DbBaseController;
import cc.iotkit.dao.DeviceRepository;
import cc.iotkit.dao.UserInfoRepository;
import cc.iotkit.manager.service.DataOwnerService;
import cc.iotkit.model.UserInfo;
import cc.iotkit.model.aligenie.AligenieDevice;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/aligenieDevice")
public class AligenieDeviceController extends DbBaseController<AligenieDeviceRepository, AligenieDevice> {
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<AligenieDevice> 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<Device> devices) {
Optional<UserInfo> 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;
}
}

View File

@ -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<AligenieProductRepository, AligenieProduct> {
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<AligenieProductVo> products() {
List<AligenieProductVo> productVos = new ArrayList<>();
List<AligenieProduct> 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<AligenieProduct> products() {
return aligenieProductRepository.findByUid(AuthUtil.getUserId());
}
@PostMapping("/save")
@ -61,7 +38,7 @@ public class AligenieProductController extends DbBaseController<AligenieProductR
product.setCreateAt(System.currentTimeMillis());
}
dataOwnerService.checkOwnerSave(repository,product);
aligenieProductDao.save(product.getProductId(), product);
dataOwnerService.checkOwnerSave(aligenieProductRepository, product);
aligenieProductRepository.save(product);
}
}

View File

@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collections;
@Slf4j
@Service
@ -49,14 +50,13 @@ public class KeycloakAdminService {
return keycloak;
}
public void createUser(UserInfo user,String pwd) {
public String createUser(UserInfo user, String pwd) {
Keycloak keycloak = getKeycloak();
UsersResource usersResource = keycloak.realm(realm)
.users();
UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setId(user.getId());
userRepresentation.setUsername(user.getUid());
userRepresentation.setGroups(Arrays.asList(getGroup(user.getType())));
userRepresentation.setGroups(Collections.singletonList(getGroup(user.getType())));
userRepresentation.setRealmRoles(user.getRoles());
if (user.getEmail() != null) {
userRepresentation.setEmail(user.getEmail());
@ -68,12 +68,17 @@ public class KeycloakAdminService {
credentialRepresentation.setType(CredentialRepresentation.PASSWORD);
credentialRepresentation.setValue(pwd);
credentialRepresentation.setTemporary(false);
userRepresentation.setCredentials(Arrays.asList(credentialRepresentation));
userRepresentation.setCredentials(Collections.singletonList(credentialRepresentation));
javax.ws.rs.core.Response response = usersResource.create(userRepresentation);
String url = response.getLocation().getPath();
String newUid = url.substring(url.lastIndexOf("/") + 1);
if (response.getStatus() >= 300) {
log.error("create userRepresentation response:{}", JsonUtil.toJsonString(response));
throw new BizException("create keycloak user failed");
}
return newUid;
}
public void updateUser(UserInfo user) {

View File

@ -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;