Search in sources :

Example 31 with User

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

the class UserController method getActivationLink.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/user/{userId}/activationLink", method = RequestMethod.GET, produces = "text/plain")
@ResponseBody
public String getActivationLink(@PathVariable(USER_ID) String strUserId, HttpServletRequest request) throws ThingsboardException {
    checkParameter(USER_ID, strUserId);
    try {
        UserId userId = new UserId(toUUID(strUserId));
        SecurityUser authUser = getCurrentUser();
        if (authUser.getAuthority() == Authority.CUSTOMER_USER && !authUser.getId().equals(userId)) {
            throw new ThingsboardException(YOU_DON_T_HAVE_PERMISSION_TO_PERFORM_THIS_OPERATION, ThingsboardErrorCode.PERMISSION_DENIED);
        }
        User user = checkUserId(userId);
        UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getId());
        if (!userCredentials.isEnabled()) {
            String baseUrl = constructBaseUrl(request);
            String activateUrl = String.format(ACTIVATE_URL_PATTERN, baseUrl, userCredentials.getActivateToken());
            return activateUrl;
        } else {
            throw new ThingsboardException("User is already active!", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) User(org.thingsboard.server.common.data.User) UserId(org.thingsboard.server.common.data.id.UserId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 32 with User

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

the class UserServiceImpl method saveUser.

@Override
public User saveUser(User user) {
    log.trace("Executing saveUser [{}]", user);
    userValidator.validate(user);
    User savedUser = userDao.save(user);
    if (user.getId() == null) {
        UserCredentials userCredentials = new UserCredentials();
        userCredentials.setEnabled(false);
        userCredentials.setActivateToken(RandomStringUtils.randomAlphanumeric(DEFAULT_TOKEN_LENGTH));
        userCredentials.setUserId(new UserId(savedUser.getUuidId()));
        userCredentialsDao.save(userCredentials);
    }
    return savedUser;
}
Also used : User(org.thingsboard.server.common.data.User) UserId(org.thingsboard.server.common.data.id.UserId) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials)

Example 33 with User

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

the class UserEntity method toData.

@Override
public User toData() {
    User user = new User(new UserId(id));
    user.setCreatedTime(UUIDs.unixTimestamp(id));
    user.setAuthority(authority);
    if (tenantId != null) {
        user.setTenantId(new TenantId(tenantId));
    }
    if (customerId != null) {
        user.setCustomerId(new CustomerId(customerId));
    }
    user.setEmail(email);
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setAdditionalInfo(additionalInfo);
    return user;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) User(org.thingsboard.server.common.data.User) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 34 with User

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

the class UserEntity method toData.

@Override
public User toData() {
    User user = new User(new UserId(getId()));
    user.setCreatedTime(UUIDs.unixTimestamp(getId()));
    user.setAuthority(authority);
    if (tenantId != null) {
        user.setTenantId(new TenantId(fromString(tenantId)));
    }
    if (customerId != null) {
        user.setCustomerId(new CustomerId(fromString(customerId)));
    }
    user.setEmail(email);
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setAdditionalInfo(additionalInfo);
    return user;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) User(org.thingsboard.server.common.data.User) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 35 with User

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

the class BaseUserServiceTest method testFindUserCredentials.

@Test
public void testFindUserCredentials() {
    User user = userService.findUserByEmail("sysadmin@thingsboard.org");
    Assert.assertNotNull(user);
    UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getId());
    Assert.assertNotNull(userCredentials);
}
Also used : User(org.thingsboard.server.common.data.User) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) 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