use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.
the class RefreshTokenAuthenticationProvider method authenticateByPublicId.
private SecurityUser authenticateByPublicId(String publicId) {
CustomerId customerId;
try {
customerId = new CustomerId(UUID.fromString(publicId));
} catch (Exception e) {
throw new BadCredentialsException("Refresh token is not valid");
}
Customer publicCustomer = customerService.findCustomerById(customerId);
if (publicCustomer == null) {
throw new UsernameNotFoundException("Public entity not found by refresh token");
}
if (!publicCustomer.isPublic()) {
throw new BadCredentialsException("Refresh token is not valid");
}
User user = new User(new UserId(EntityId.NULL_UUID));
user.setTenantId(publicCustomer.getTenantId());
user.setCustomerId(publicCustomer.getId());
user.setEmail(publicId);
user.setAuthority(Authority.CUSTOMER_USER);
user.setFirstName("Public");
user.setLastName("Public");
UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.PUBLIC_ID, publicId);
SecurityUser securityUser = new SecurityUser(user, true, userPrincipal);
return securityUser;
}
use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.
the class RestAuthenticationProvider method authenticateByPublicId.
private Authentication authenticateByPublicId(UserPrincipal userPrincipal, String publicId) {
CustomerId customerId;
try {
customerId = new CustomerId(UUID.fromString(publicId));
} catch (Exception e) {
throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
}
Customer publicCustomer = customerService.findCustomerById(customerId);
if (publicCustomer == null) {
throw new UsernameNotFoundException("Public entity not found: " + publicId);
}
if (!publicCustomer.isPublic()) {
throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
}
User user = new User(new UserId(EntityId.NULL_UUID));
user.setTenantId(publicCustomer.getTenantId());
user.setCustomerId(publicCustomer.getId());
user.setEmail(publicId);
user.setAuthority(Authority.CUSTOMER_USER);
user.setFirstName("Public");
user.setLastName("Public");
SecurityUser securityUser = new SecurityUser(user, true, userPrincipal);
return new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getAuthorities());
}
use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.
the class JpaCustomerDaoTest method testFindByTenantId.
@Test
public void testFindByTenantId() {
UUID tenantId1 = UUIDs.timeBased();
UUID tenantId2 = UUIDs.timeBased();
for (int i = 0; i < 20; i++) {
createCustomer(tenantId1, i);
createCustomer(tenantId2, i * 2);
}
TextPageLink pageLink1 = new TextPageLink(15, "CUSTOMER");
List<Customer> customers1 = customerDao.findCustomersByTenantId(tenantId1, pageLink1);
assertEquals(15, customers1.size());
TextPageLink pageLink2 = new TextPageLink(15, "CUSTOMER", customers1.get(14).getId().getId(), null);
List<Customer> customers2 = customerDao.findCustomersByTenantId(tenantId1, pageLink2);
assertEquals(5, customers2.size());
}
use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.
the class JpaCustomerDaoTest method createCustomer.
private void createCustomer(UUID tenantId, int index) {
Customer customer = new Customer();
customer.setId(new CustomerId(UUIDs.timeBased()));
customer.setTenantId(new TenantId(tenantId));
customer.setTitle("CUSTOMER_" + index);
customerDao.save(customer);
}
use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.
the class BaseUserServiceTest method testFindCustomerUsers.
@Test
public void testFindCustomerUsers() {
User customerUser = userService.findUserByEmail("customer@thingsboard.org");
TextPageData<User> pageData = userService.findCustomerUsers(customerUser.getTenantId(), customerUser.getCustomerId(), new TextPageLink(10));
Assert.assertFalse(pageData.hasNext());
List<User> users = pageData.getData();
Assert.assertEquals(1, users.size());
Assert.assertEquals(customerUser, users.get(0));
Tenant tenant = new Tenant();
tenant.setTitle("Test tenant");
tenant = tenantService.saveTenant(tenant);
TenantId tenantId = tenant.getId();
Customer customer = new Customer();
customer.setTitle("Test customer");
customer.setTenantId(tenantId);
customer = customerService.saveCustomer(customer);
CustomerId customerId = customer.getId();
List<User> customerUsers = new ArrayList<>();
for (int i = 0; i < 156; i++) {
User user = new User();
user.setAuthority(Authority.CUSTOMER_USER);
user.setTenantId(tenantId);
user.setCustomerId(customerId);
user.setEmail("testCustomer" + i + "@thingsboard.org");
customerUsers.add(userService.saveUser(user));
}
List<User> loadedCustomerUsers = new ArrayList<>();
TextPageLink pageLink = new TextPageLink(33);
do {
pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
loadedCustomerUsers.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(customerUsers, idComparator);
Collections.sort(loadedCustomerUsers, idComparator);
Assert.assertEquals(customerUsers, loadedCustomerUsers);
tenantService.deleteTenant(tenantId);
pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertTrue(pageData.getData().isEmpty());
}
Aggregations