@ -18,6 +18,8 @@ package org.thingsboard.server.controller;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import com.fasterxml.jackson.databind.node.ObjectNode ;
import com.fasterxml.jackson.databind.node.ObjectNode ;
import io.swagger.annotations.ApiOperation ;
import io.swagger.annotations.ApiParam ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.HttpStatus ;
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.PathVariable ;
@ -52,16 +54,22 @@ public class CustomerController extends BaseController {
public static final String CUSTOMER_ID = "customerId" ;
public static final String CUSTOMER_ID = "customerId" ;
public static final String IS_PUBLIC = "isPublic" ;
public static final String IS_PUBLIC = "isPublic" ;
public static final String CUSTOMER_SECURITY_CHECK = "If the user has the authority of 'Tenant Administrator', the server checks that the customer is owned by the same tenant. " +
"If the user has the authority of 'Customer User', the server checks that the user belongs to the customer." ;
@ApiOperation ( value = "Get Customer (getCustomerById)" ,
notes = "Get the Customer object based on the provided Customer Id. " + CUSTOMER_SECURITY_CHECK )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/customer/{customerId}" , method = RequestMethod . GET )
@RequestMapping ( value = "/customer/{customerId}" , method = RequestMethod . GET )
@ResponseBody
@ResponseBody
public Customer getCustomerById ( @PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
public Customer getCustomerById (
@ApiParam ( value = CUSTOMER_ID_PARAM_DESCRIPTION )
@PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
try {
try {
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
Customer customer = checkCustomerId ( customerId , Operation . READ ) ;
Customer customer = checkCustomerId ( customerId , Operation . READ ) ;
if ( ! customer . getAdditionalInfo ( ) . isNull ( ) ) {
if ( ! customer . getAdditionalInfo ( ) . isNull ( ) ) {
processDashboardIdFromAdditionalInfo ( ( ObjectNode ) customer . getAdditionalInfo ( ) , HOME_DASHBOARD ) ;
processDashboardIdFromAdditionalInfo ( ( ObjectNode ) customer . getAdditionalInfo ( ) , HOME_DASHBOARD ) ;
}
}
return customer ;
return customer ;
@ -70,10 +78,15 @@ public class CustomerController extends BaseController {
}
}
}
}
@ApiOperation ( value = "Get short Customer info (getShortCustomerInfoById)" ,
notes = "Get the short customer object that contains only the title and 'isPublic' flag. " + CUSTOMER_SECURITY_CHECK )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/customer/{customerId}/shortInfo" , method = RequestMethod . GET )
@RequestMapping ( value = "/customer/{customerId}/shortInfo" , method = RequestMethod . GET )
@ResponseBody
@ResponseBody
public JsonNode getShortCustomerInfoById ( @PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
public JsonNode getShortCustomerInfoById (
@ApiParam ( value = CUSTOMER_ID_PARAM_DESCRIPTION )
@PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
try {
try {
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
@ -88,10 +101,14 @@ public class CustomerController extends BaseController {
}
}
}
}
@ApiOperation ( value = "Get Customer Title (getCustomerTitleById)" ,
notes = "Get the title of the customer. " + CUSTOMER_SECURITY_CHECK )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/customer/{customerId}/title" , method = RequestMethod . GET , produces = "application/text" )
@RequestMapping ( value = "/customer/{customerId}/title" , method = RequestMethod . GET , produces = "application/text" )
@ResponseBody
@ResponseBody
public String getCustomerTitleById ( @PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
public String getCustomerTitleById (
@ApiParam ( value = CUSTOMER_ID_PARAM_DESCRIPTION )
@PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
try {
try {
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
@ -102,10 +119,14 @@ public class CustomerController extends BaseController {
}
}
}
}
@ApiOperation ( value = "Create or update Customer (saveCustomer)" ,
notes = "Creates or Updates the Customer. Platform generates random Customer Id during device creation. " +
"The Customer Id will be present in the response. Specify the Customer Id when you would like to update the Customer. " +
"Referencing non-existing Customer Id will cause an error." )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@RequestMapping ( value = "/customer" , method = RequestMethod . POST )
@RequestMapping ( value = "/customer" , method = RequestMethod . POST )
@ResponseBody
@ResponseBody
public Customer saveCustomer ( @RequestBody Customer customer ) throws ThingsboardException {
public Customer saveCustomer ( @ApiParam ( value = "A JSON value representing the customer." ) @ RequestBody Customer customer ) throws ThingsboardException {
try {
try {
customer . setTenantId ( getCurrentUser ( ) . getTenantId ( ) ) ;
customer . setTenantId ( getCurrentUser ( ) . getTenantId ( ) ) ;
@ -131,10 +152,13 @@ public class CustomerController extends BaseController {
}
}
}
}
@ApiOperation ( value = "Delete Customer (deleteCustomer)" ,
notes = "Deletes the Customer and all customer Users. All assigned Dashboards, Assets, Devices, etc. will be unassigned but not deleted. Referencing non-existing Customer Id will cause an error." )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@RequestMapping ( value = "/customer/{customerId}" , method = RequestMethod . DELETE )
@RequestMapping ( value = "/customer/{customerId}" , method = RequestMethod . DELETE )
@ResponseStatus ( value = HttpStatus . OK )
@ResponseStatus ( value = HttpStatus . OK )
public void deleteCustomer ( @PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
public void deleteCustomer ( @ApiParam ( value = CUSTOMER_ID_PARAM_DESCRIPTION )
@PathVariable ( CUSTOMER_ID ) String strCustomerId ) throws ThingsboardException {
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
checkParameter ( CUSTOMER_ID , strCustomerId ) ;
try {
try {
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
@ -161,14 +185,23 @@ public class CustomerController extends BaseController {
}
}
}
}
@ApiOperation ( value = "Get Tenant Customers (getCustomers)" ,
notes = "Returns a page of customers owned by tenant. " +
PAGE_DATA_PARAMETERS )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@RequestMapping ( value = "/customers" , params = { "pageSize" , "page" } , method = RequestMethod . GET )
@RequestMapping ( value = "/customers" , params = { "pageSize" , "page" } , method = RequestMethod . GET )
@ResponseBody
@ResponseBody
public PageData < Customer > getCustomers ( @RequestParam int pageSize ,
public PageData < Customer > getCustomers (
@RequestParam int page ,
@ApiParam ( value = PAGE_SIZE_DESCRIPTION )
@RequestParam ( required = false ) String textSearch ,
@RequestParam int pageSize ,
@RequestParam ( required = false ) String sortProperty ,
@ApiParam ( value = PAGE_NUMBER_DESCRIPTION )
@RequestParam ( required = false ) String sortOrder ) throws ThingsboardException {
@RequestParam int page ,
@ApiParam ( value = CUSTOMER_TEXT_SEARCH_DESCRIPTION )
@RequestParam ( required = false ) String textSearch ,
@ApiParam ( value = SORT_PROPERTY_DESCRIPTION , allowableValues = SORT_PROPERTY_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortProperty ,
@ApiParam ( value = SORT_ORDER_DESCRIPTION , allowableValues = SORT_ORDER_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortOrder ) throws ThingsboardException {
try {
try {
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
@ -178,10 +211,13 @@ public class CustomerController extends BaseController {
}
}
}
}
@ApiOperation ( value = "Get Tenant Customer by Customer title (getTenantCustomer)" ,
notes = "Get the Customer using Customer Title. Available for users with 'Tenant Administrator' authority only." )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@PreAuthorize ( "hasAuthority('TENANT_ADMIN')" )
@RequestMapping ( value = "/tenant/customers" , params = { "customerTitle" } , method = RequestMethod . GET )
@RequestMapping ( value = "/tenant/customers" , params = { "customerTitle" } , method = RequestMethod . GET )
@ResponseBody
@ResponseBody
public Customer getTenantCustomer (
public Customer getTenantCustomer (
@ApiParam ( value = "A string value representing the Customer title." )
@RequestParam String customerTitle ) throws ThingsboardException {
@RequestParam String customerTitle ) throws ThingsboardException {
try {
try {
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;