Search in sources :

Example 11 with UserId

use of org.thingsboard.server.common.data.id.UserId in project thingsboard by thingsboard.

the class UserController method deleteUser.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteUser(@PathVariable(USER_ID) String strUserId) throws ThingsboardException {
    checkParameter(USER_ID, strUserId);
    try {
        UserId userId = new UserId(toUUID(strUserId));
        User user = checkUserId(userId);
        userService.deleteUser(userId);
        logEntityAction(userId, user, user.getCustomerId(), ActionType.DELETED, null, strUserId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.USER), null, null, ActionType.DELETED, e, strUserId);
        throw handleException(e);
    }
}
Also used : 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) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 12 with UserId

use of org.thingsboard.server.common.data.id.UserId 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 13 with UserId

use of org.thingsboard.server.common.data.id.UserId 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 14 with UserId

use of org.thingsboard.server.common.data.id.UserId 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 15 with UserId

use of org.thingsboard.server.common.data.id.UserId 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)

Aggregations

UserId (org.thingsboard.server.common.data.id.UserId)17 User (org.thingsboard.server.common.data.User)9 CustomerId (org.thingsboard.server.common.data.id.CustomerId)9 TenantId (org.thingsboard.server.common.data.id.TenantId)8 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)4 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)4 UserPrincipal (org.thingsboard.server.service.security.model.UserPrincipal)3 Claims (io.jsonwebtoken.Claims)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 Customer (org.thingsboard.server.common.data.Customer)2 UserCredentialsId (org.thingsboard.server.common.data.id.UserCredentialsId)2 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)2 PluginApiCallSecurityContext (org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)1 URI (java.net.URI)1