Search in sources :

Example 16 with Customer

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;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) User(org.thingsboard.server.common.data.User) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) Customer(org.thingsboard.server.common.data.Customer) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Example 17 with Customer

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());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) User(org.thingsboard.server.common.data.User) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) Customer(org.thingsboard.server.common.data.Customer) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 18 with Customer

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());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 19 with Customer

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);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Customer(org.thingsboard.server.common.data.Customer) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 20 with 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());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) User(org.thingsboard.server.common.data.User) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Tenant(org.thingsboard.server.common.data.Tenant) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Aggregations

Customer (org.thingsboard.server.common.data.Customer)59 Test (org.junit.Test)37 CustomerId (org.thingsboard.server.common.data.id.CustomerId)24 Tenant (org.thingsboard.server.common.data.Tenant)21 ArrayList (java.util.ArrayList)18 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)18 User (org.thingsboard.server.common.data.User)16 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Asset (org.thingsboard.server.common.data.asset.Asset)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)10 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)8 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 Device (org.thingsboard.server.common.data.Device)7 Matchers.containsString (org.hamcrest.Matchers.containsString)4 AssetId (org.thingsboard.server.common.data.id.AssetId)3 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UUID (java.util.UUID)2 Before (org.junit.Before)2