Search in sources :

Example 16 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials 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 17 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class UserServiceImpl method deleteUser.

@Override
public void deleteUser(UserId userId) {
    log.trace("Executing deleteUser [{}]", userId);
    validateId(userId, INCORRECT_USER_ID + userId);
    UserCredentials userCredentials = userCredentialsDao.findByUserId(userId.getId());
    userCredentialsDao.removeById(userCredentials.getUuidId());
    deleteEntityRelations(userId);
    userDao.removeById(userId.getId());
}
Also used : UserCredentials(org.thingsboard.server.common.data.security.UserCredentials)

Example 18 with UserCredentials

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

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

Example 20 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class BaseUserServiceTest method testDeleteUser.

@Test
public void testDeleteUser() {
    User tenantAdminUser = userService.findUserByEmail("tenant@thingsboard.org");
    User user = new User();
    user.setAuthority(Authority.TENANT_ADMIN);
    user.setTenantId(tenantAdminUser.getTenantId());
    user.setEmail("tenant2@thingsboard.org");
    User savedUser = userService.saveUser(user);
    Assert.assertNotNull(savedUser);
    Assert.assertNotNull(savedUser.getId());
    User foundUser = userService.findUserById(savedUser.getId());
    Assert.assertNotNull(foundUser);
    UserCredentials userCredentials = userService.findUserCredentialsByUserId(foundUser.getId());
    Assert.assertNotNull(userCredentials);
    userService.deleteUser(foundUser.getId());
    userCredentials = userService.findUserCredentialsByUserId(foundUser.getId());
    foundUser = userService.findUserById(foundUser.getId());
    Assert.assertNull(foundUser);
    Assert.assertNull(userCredentials);
}
Also used : User(org.thingsboard.server.common.data.User) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) Test(org.junit.Test)

Aggregations

UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)24 User (org.thingsboard.server.common.data.User)12 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)8 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.Test)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 UserId (org.thingsboard.server.common.data.id.UserId)4 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)3 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)3 UserPrincipal (org.thingsboard.server.service.security.model.UserPrincipal)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpStatus (org.springframework.http.HttpStatus)2 ResponseEntity (org.springframework.http.ResponseEntity)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 UserCredentialsId (org.thingsboard.server.common.data.id.UserCredentialsId)2 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)2