Merge branch 'master' of github.com:thingsboard/thingsboard

This commit is contained in:
oleg 2017-12-29 14:50:29 +02:00
commit 86c8a1ad7d
7 changed files with 69 additions and 24 deletions

View File

@ -125,10 +125,6 @@
<groupId>io.jsonwebtoken</groupId> <groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId> <artifactId>jjwt</artifactId>
</dependency> </dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.velocity</groupId> <groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId> <artifactId>velocity</artifactId>

View File

@ -82,7 +82,7 @@ public class CustomerController extends BaseController {
@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(@RequestBody Customer customer) throws ThingsboardException {
try { try {
customer.setTenantId(getCurrentUser().getTenantId()); customer.setTenantId(getCurrentUser().getTenantId());
@ -107,7 +107,7 @@ public class CustomerController extends BaseController {
} }
@PreAuthorize("hasAuthority('TENANT_ADMIN')") @PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customers", params = { "limit" }, method = RequestMethod.GET) @RequestMapping(value = "/customers", params = {"limit"}, method = RequestMethod.GET)
@ResponseBody @ResponseBody
public TextPageData<Customer> getCustomers(@RequestParam int limit, public TextPageData<Customer> getCustomers(@RequestParam int limit,
@RequestParam(required = false) String textSearch, @RequestParam(required = false) String textSearch,
@ -122,4 +122,16 @@ public class CustomerController extends BaseController {
} }
} }
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/tenant/customers", params = {"customerTitle"}, method = RequestMethod.GET)
@ResponseBody
public Customer getTenantCustomer(
@RequestParam String customerTitle) throws ThingsboardException {
try {
TenantId tenantId = getCurrentUser().getTenantId();
return checkNotNull(customerService.findCustomerByTenantIdAndTitle(tenantId, customerTitle));
} catch (Exception e) {
throw handleException(e);
}
}
} }

View File

@ -20,7 +20,6 @@ import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
@ -31,7 +30,9 @@ import org.thingsboard.server.config.JwtSettings;
import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.security.model.UserPrincipal; import org.thingsboard.server.service.security.model.UserPrincipal;
import java.util.Arrays; import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -81,13 +82,13 @@ public class JwtTokenFactory {
claims.put(CUSTOMER_ID, securityUser.getCustomerId().getId().toString()); claims.put(CUSTOMER_ID, securityUser.getCustomerId().getId().toString());
} }
DateTime currentTime = new DateTime(); ZonedDateTime currentTime = ZonedDateTime.now();
String token = Jwts.builder() String token = Jwts.builder()
.setClaims(claims) .setClaims(claims)
.setIssuer(settings.getTokenIssuer()) .setIssuer(settings.getTokenIssuer())
.setIssuedAt(currentTime.toDate()) .setIssuedAt(Date.from(currentTime.toInstant()))
.setExpiration(currentTime.plusSeconds(settings.getTokenExpirationTime()).toDate()) .setExpiration(Date.from(currentTime.plusSeconds(settings.getTokenExpirationTime()).toInstant()))
.signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()) .signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey())
.compact(); .compact();
@ -129,11 +130,11 @@ public class JwtTokenFactory {
throw new IllegalArgumentException("Cannot create JWT Token without username/email"); throw new IllegalArgumentException("Cannot create JWT Token without username/email");
} }
DateTime currentTime = new DateTime(); ZonedDateTime currentTime = ZonedDateTime.now();
UserPrincipal principal = securityUser.getUserPrincipal(); UserPrincipal principal = securityUser.getUserPrincipal();
Claims claims = Jwts.claims().setSubject(principal.getValue()); Claims claims = Jwts.claims().setSubject(principal.getValue());
claims.put(SCOPES, Arrays.asList(Authority.REFRESH_TOKEN.name())); claims.put(SCOPES, Collections.singletonList(Authority.REFRESH_TOKEN.name()));
claims.put(USER_ID, securityUser.getId().getId().toString()); claims.put(USER_ID, securityUser.getId().getId().toString());
claims.put(IS_PUBLIC, principal.getType() == UserPrincipal.Type.PUBLIC_ID); claims.put(IS_PUBLIC, principal.getType() == UserPrincipal.Type.PUBLIC_ID);
@ -141,8 +142,8 @@ public class JwtTokenFactory {
.setClaims(claims) .setClaims(claims)
.setIssuer(settings.getTokenIssuer()) .setIssuer(settings.getTokenIssuer())
.setId(UUID.randomUUID().toString()) .setId(UUID.randomUUID().toString())
.setIssuedAt(currentTime.toDate()) .setIssuedAt(Date.from(currentTime.toInstant()))
.setExpiration(currentTime.plusSeconds(settings.getRefreshTokenExpTime()).toDate()) .setExpiration(Date.from(currentTime.plusSeconds(settings.getRefreshTokenExpTime()).toInstant()))
.signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()) .signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey())
.compact(); .compact();

View File

@ -22,20 +22,24 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.TextPageData; import org.thingsboard.server.common.data.page.TextPageData;
import org.thingsboard.server.common.data.page.TextPageLink; import org.thingsboard.server.common.data.page.TextPageLink;
import java.util.Optional;
public interface CustomerService { public interface CustomerService {
Customer findCustomerById(CustomerId customerId); Customer findCustomerById(CustomerId customerId);
Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title);
ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId); ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId);
Customer saveCustomer(Customer customer); Customer saveCustomer(Customer customer);
void deleteCustomer(CustomerId customerId); void deleteCustomer(CustomerId customerId);
Customer findOrCreatePublicCustomer(TenantId tenantId); Customer findOrCreatePublicCustomer(TenantId tenantId);
TextPageData<Customer> findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink); TextPageData<Customer> findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink);
void deleteCustomersByTenantId(TenantId tenantId); void deleteCustomersByTenantId(TenantId tenantId);
} }

View File

@ -52,6 +52,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
private static final String PUBLIC_CUSTOMER_TITLE = "Public"; private static final String PUBLIC_CUSTOMER_TITLE = "Public";
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId ";
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
@Autowired @Autowired
private CustomerDao customerDao; private CustomerDao customerDao;
@ -78,6 +79,13 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
return customerDao.findById(customerId.getId()); return customerDao.findById(customerId.getId());
} }
@Override
public Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title) {
log.trace("Executing findCustomerByTenantIdAndTitle [{}] [{}]", tenantId, title);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
return customerDao.findCustomersByTenantIdAndTitle(tenantId.getId(), title);
}
@Override @Override
public ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId) { public ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId) {
log.trace("Executing findCustomerByIdAsync [{}]", customerId); log.trace("Executing findCustomerByIdAsync [{}]", customerId);

View File

@ -33,8 +33,7 @@
<spring.version>4.3.4.RELEASE</spring.version> <spring.version>4.3.4.RELEASE</spring.version>
<spring-security.version>4.2.0.RELEASE</spring-security.version> <spring-security.version>4.2.0.RELEASE</spring-security.version>
<jjwt.version>0.7.0</jjwt.version> <jjwt.version>0.7.0</jjwt.version>
<joda-time.version>2.4</joda-time.version> <json-path.version>2.2.0</json-path.version>
<json-path.version>2.2.0</json-path.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<slf4j.version>1.7.7</slf4j.version> <slf4j.version>1.7.7</slf4j.version>
<logback.version>1.2.3</logback.version> <logback.version>1.2.3</logback.version>
@ -483,11 +482,6 @@
<artifactId>jjwt</artifactId> <artifactId>jjwt</artifactId>
<version>${jjwt.version}</version> <version>${jjwt.version}</version>
</dependency> </dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.velocity</groupId> <groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId> <artifactId>velocity</artifactId>

View File

@ -77,6 +77,36 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public Optional<Customer> findCustomer(String title) {
Map<String, String> params = new HashMap<String, String>();
params.put("customerTitle", title);
try {
ResponseEntity<Customer> customerEntity = restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle={customerTitle}", Customer.class, params);
return Optional.of(customerEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Asset> findAsset(String name) {
Map<String, String> params = new HashMap<String, String>();
params.put("assetName", name);
try {
ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params);
return Optional.of(assetEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Customer createCustomer(String title) { public Customer createCustomer(String title) {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTitle(title); customer.setTitle(title);