Search in sources :

Example 21 with DeviceCredentials

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);
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 22 with DeviceCredentials

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);
    }
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 23 with DeviceCredentials

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());
}
Also used : DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Cache(org.springframework.cache.Cache)

Example 24 with DeviceCredentials

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;
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 25 with DeviceCredentials

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Aggregations

DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)39 Test (org.junit.Test)21 Device (org.thingsboard.server.common.data.Device)18 DeviceId (org.thingsboard.server.common.data.id.DeviceId)7 DeviceCredentialsId (org.thingsboard.server.common.data.id.DeviceCredentialsId)6 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)2 Before (org.junit.Before)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)2 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)2 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 UUID (java.util.UUID)1 Cache (org.springframework.cache.Cache)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 DeviceCredentialsService (org.thingsboard.server.dao.device.DeviceCredentialsService)1