use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.
the class DeviceController method saveDeviceCredentials.
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/device/credentials", method = RequestMethod.POST)
@ResponseBody
public DeviceCredentials saveDeviceCredentials(@RequestBody DeviceCredentials deviceCredentials) throws ThingsboardException {
checkNotNull(deviceCredentials);
try {
Device device = checkDeviceId(deviceCredentials.getDeviceId());
DeviceCredentials result = checkNotNull(deviceCredentialsService.updateDeviceCredentials(deviceCredentials));
actorService.onCredentialsUpdate(getCurrentUser().getTenantId(), deviceCredentials.getDeviceId());
logEntityAction(device.getId(), device, device.getCustomerId(), ActionType.CREDENTIALS_UPDATED, null, deviceCredentials);
return result;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.CREDENTIALS_UPDATED, e, deviceCredentials);
throw handleException(e);
}
}
use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.
the class DeviceController method getDeviceCredentialsByDeviceId.
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/device/{deviceId}/credentials", method = RequestMethod.GET)
@ResponseBody
public DeviceCredentials getDeviceCredentialsByDeviceId(@PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
checkParameter(DEVICE_ID, strDeviceId);
try {
DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
Device device = checkDeviceId(deviceId);
DeviceCredentials deviceCredentials = checkNotNull(deviceCredentialsService.findDeviceCredentialsByDeviceId(deviceId));
logEntityAction(deviceId, device, device.getCustomerId(), ActionType.CREDENTIALS_READ, null, strDeviceId);
return deviceCredentials;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.CREDENTIALS_READ, e, strDeviceId);
throw handleException(e);
}
}
use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.
the class DeviceServiceImpl method deleteDevice.
@Override
public void deleteDevice(DeviceId deviceId) {
log.trace("Executing deleteDevice [{}]", deviceId);
Cache cache = cacheManager.getCache(DEVICE_CACHE);
validateId(deviceId, INCORRECT_DEVICE_ID + deviceId);
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(deviceId);
if (deviceCredentials != null) {
deviceCredentialsService.deleteDeviceCredentials(deviceCredentials);
}
deleteEntityRelations(deviceId);
Device device = deviceDao.findById(deviceId.getId());
List<Object> list = new ArrayList<>();
list.add(device.getTenantId());
list.add(device.getName());
cache.evict(list);
deviceDao.removeById(deviceId.getId());
}
use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.
the class DeviceServiceImpl method saveDevice.
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}")
@Override
public Device saveDevice(Device device) {
log.trace("Executing saveDevice [{}]", device);
deviceValidator.validate(device);
Device savedDevice = deviceDao.save(device);
if (device.getId() == null) {
DeviceCredentials deviceCredentials = new DeviceCredentials();
deviceCredentials.setDeviceId(new DeviceId(savedDevice.getUuidId()));
deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
deviceCredentials.setCredentialsId(RandomStringUtils.randomAlphanumeric(20));
deviceCredentialsService.createDeviceCredentials(deviceCredentials);
}
return savedDevice;
}
use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.
the class AuditLogServiceImpl method constructActionData.
private <E extends BaseData<I> & HasName, I extends UUIDBased & EntityId> JsonNode constructActionData(I entityId, E entity, ActionType actionType, Object... additionalInfo) {
ObjectNode actionData = objectMapper.createObjectNode();
switch(actionType) {
case ADDED:
case UPDATED:
ObjectNode entityNode = objectMapper.valueToTree(entity);
if (entityId.getEntityType() == EntityType.DASHBOARD) {
entityNode.put("configuration", "");
}
actionData.set("entity", entityNode);
break;
case DELETED:
case ACTIVATED:
case SUSPENDED:
case CREDENTIALS_READ:
String strEntityId = extractParameter(String.class, additionalInfo);
actionData.put("entityId", strEntityId);
break;
case ATTRIBUTES_UPDATED:
actionData.put("entityId", entityId.toString());
String scope = extractParameter(String.class, 0, additionalInfo);
List<AttributeKvEntry> attributes = extractParameter(List.class, 1, additionalInfo);
actionData.put("scope", scope);
ObjectNode attrsNode = objectMapper.createObjectNode();
if (attributes != null) {
for (AttributeKvEntry attr : attributes) {
attrsNode.put(attr.getKey(), attr.getValueAsString());
}
}
actionData.set("attributes", attrsNode);
break;
case ATTRIBUTES_DELETED:
case ATTRIBUTES_READ:
actionData.put("entityId", entityId.toString());
scope = extractParameter(String.class, 0, additionalInfo);
actionData.put("scope", scope);
List<String> keys = extractParameter(List.class, 1, additionalInfo);
ArrayNode attrsArrayNode = actionData.putArray("attributes");
if (keys != null) {
keys.forEach(attrsArrayNode::add);
}
break;
case RPC_CALL:
actionData.put("entityId", entityId.toString());
Boolean oneWay = extractParameter(Boolean.class, 1, additionalInfo);
String method = extractParameter(String.class, 2, additionalInfo);
String params = extractParameter(String.class, 3, additionalInfo);
actionData.put("oneWay", oneWay);
actionData.put("method", method);
actionData.put("params", params);
break;
case CREDENTIALS_UPDATED:
actionData.put("entityId", entityId.toString());
DeviceCredentials deviceCredentials = extractParameter(DeviceCredentials.class, additionalInfo);
actionData.set("credentials", objectMapper.valueToTree(deviceCredentials));
break;
case ASSIGNED_TO_CUSTOMER:
strEntityId = extractParameter(String.class, 0, additionalInfo);
String strCustomerId = extractParameter(String.class, 1, additionalInfo);
String strCustomerName = extractParameter(String.class, 2, additionalInfo);
actionData.put("entityId", strEntityId);
actionData.put("assignedCustomerId", strCustomerId);
actionData.put("assignedCustomerName", strCustomerName);
break;
case UNASSIGNED_FROM_CUSTOMER:
strEntityId = extractParameter(String.class, 0, additionalInfo);
strCustomerId = extractParameter(String.class, 1, additionalInfo);
strCustomerName = extractParameter(String.class, 2, additionalInfo);
actionData.put("entityId", strEntityId);
actionData.put("unassignedCustomerId", strCustomerId);
actionData.put("unassignedCustomerName", strCustomerName);
break;
}
return actionData;
}
Aggregations