Search in sources :

Example 16 with User

use of org.thingsboard.server.common.data.User in project thingsboard by thingsboard.

the class RefreshTokenAuthenticationProvider method authenticateByUserId.

private SecurityUser authenticateByUserId(UserId userId) {
    User user = userService.findUserById(userId);
    if (user == null) {
        throw new UsernameNotFoundException("User not found by refresh token");
    }
    UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getId());
    if (userCredentials == null) {
        throw new UsernameNotFoundException("User credentials not found");
    }
    if (!userCredentials.isEnabled()) {
        throw new DisabledException("User is not active");
    }
    if (user.getAuthority() == null)
        throw new InsufficientAuthenticationException("User has no authority assigned");
    UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.USER_NAME, user.getEmail());
    SecurityUser securityUser = new SecurityUser(user, userCredentials.isEnabled(), 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) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Example 17 with User

use of org.thingsboard.server.common.data.User 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 18 with User

use of org.thingsboard.server.common.data.User 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 19 with User

use of org.thingsboard.server.common.data.User in project thingsboard by thingsboard.

the class BaseUserServiceTest method testFindUserById.

@Test
public void testFindUserById() {
    User user = userService.findUserByEmail("sysadmin@thingsboard.org");
    Assert.assertNotNull(user);
    User foundUser = userService.findUserById(user.getId());
    Assert.assertNotNull(foundUser);
    Assert.assertEquals(user, foundUser);
}
Also used : User(org.thingsboard.server.common.data.User) Test(org.junit.Test)

Example 20 with User

use of org.thingsboard.server.common.data.User in project thingsboard by thingsboard.

the class BaseUserServiceTest method testSaveUserWithSameEmail.

@Test(expected = DataValidationException.class)
public void testSaveUserWithSameEmail() {
    User tenantAdminUser = userService.findUserByEmail("tenant@thingsboard.org");
    tenantAdminUser.setEmail("sysadmin@thingsboard.org");
    userService.saveUser(tenantAdminUser);
}
Also used : User(org.thingsboard.server.common.data.User) Test(org.junit.Test)

Aggregations

User (org.thingsboard.server.common.data.User)63 Test (org.junit.Test)37 Tenant (org.thingsboard.server.common.data.Tenant)33 Customer (org.thingsboard.server.common.data.Customer)16 TenantId (org.thingsboard.server.common.data.id.TenantId)14 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)12 UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)12 ArrayList (java.util.ArrayList)10 CustomerId (org.thingsboard.server.common.data.id.CustomerId)10 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9 Before (org.junit.Before)9 UserId (org.thingsboard.server.common.data.id.UserId)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)4