Search in sources :

Example 36 with ThingsboardException

use of org.thingsboard.server.exception.ThingsboardException in project thingsboard by thingsboard.

the class RuleController method activateRuleById.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/rule/{ruleId}/activate", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void activateRuleById(@PathVariable(RULE_ID) String strRuleId) throws ThingsboardException {
    checkParameter(RULE_ID, strRuleId);
    try {
        RuleId ruleId = new RuleId(toUUID(strRuleId));
        RuleMetaData rule = checkRule(ruleService.findRuleById(ruleId));
        ruleService.activateRuleById(ruleId);
        actorService.onRuleStateChange(rule.getTenantId(), rule.getId(), ComponentLifecycleEvent.ACTIVATED);
        logEntityAction(rule.getId(), rule, null, ActionType.ACTIVATED, null, strRuleId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE), null, null, ActionType.ACTIVATED, e, strRuleId);
        throw handleException(e);
    }
}
Also used : RuleId(org.thingsboard.server.common.data.id.RuleId) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 37 with ThingsboardException

use of org.thingsboard.server.exception.ThingsboardException in project thingsboard by thingsboard.

the class TenantController method getTenantById.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/tenant/{tenantId}", method = RequestMethod.GET)
@ResponseBody
public Tenant getTenantById(@PathVariable("tenantId") String strTenantId) throws ThingsboardException {
    checkParameter("tenantId", strTenantId);
    try {
        TenantId tenantId = new TenantId(toUUID(strTenantId));
        checkTenantId(tenantId);
        return checkNotNull(tenantService.findTenantById(tenantId));
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 38 with ThingsboardException

use of org.thingsboard.server.exception.ThingsboardException in project thingsboard by thingsboard.

the class UserController method saveUser.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/user", method = RequestMethod.POST)
@ResponseBody
public User saveUser(@RequestBody User user, @RequestParam(required = false, defaultValue = "true") boolean sendActivationMail, HttpServletRequest request) throws ThingsboardException {
    try {
        SecurityUser authUser = getCurrentUser();
        if (authUser.getAuthority() == Authority.CUSTOMER_USER && !authUser.getId().equals(user.getId())) {
            throw new ThingsboardException(YOU_DON_T_HAVE_PERMISSION_TO_PERFORM_THIS_OPERATION, ThingsboardErrorCode.PERMISSION_DENIED);
        }
        boolean sendEmail = user.getId() == null && sendActivationMail;
        if (getCurrentUser().getAuthority() == Authority.TENANT_ADMIN) {
            user.setTenantId(getCurrentUser().getTenantId());
        }
        User savedUser = checkNotNull(userService.saveUser(user));
        if (sendEmail) {
            UserCredentials userCredentials = userService.findUserCredentialsByUserId(savedUser.getId());
            String baseUrl = constructBaseUrl(request);
            String activateUrl = String.format(ACTIVATE_URL_PATTERN, baseUrl, userCredentials.getActivateToken());
            String email = savedUser.getEmail();
            try {
                mailService.sendActivationEmail(activateUrl, email);
            } catch (ThingsboardException e) {
                userService.deleteUser(savedUser.getId());
                throw e;
            }
        }
        logEntityAction(savedUser.getId(), savedUser, savedUser.getCustomerId(), user.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
        return savedUser;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.USER), user, null, user.getId() == null ? ActionType.ADDED : ActionType.UPDATED, 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) 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 39 with ThingsboardException

use of org.thingsboard.server.exception.ThingsboardException in project thingsboard by thingsboard.

the class UserController method sendActivationEmail.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/user/sendActivationMail", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void sendActivationEmail(@RequestParam(value = "email") String email, HttpServletRequest request) throws ThingsboardException {
    try {
        User user = checkNotNull(userService.findUserByEmail(email));
        UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getId());
        if (!userCredentials.isEnabled()) {
            String baseUrl = constructBaseUrl(request);
            String activateUrl = String.format(ACTIVATE_URL_PATTERN, baseUrl, userCredentials.getActivateToken());
            mailService.sendActivationEmail(activateUrl, email);
        } 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) User(org.thingsboard.server.common.data.User) 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 40 with ThingsboardException

use of org.thingsboard.server.exception.ThingsboardException in project thingsboard by thingsboard.

the class UserController method getUserById.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
@ResponseBody
public User getUserById(@PathVariable(USER_ID) String strUserId) 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);
        }
        return checkUserId(userId);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) UserId(org.thingsboard.server.common.data.id.UserId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ThingsboardException (org.thingsboard.server.exception.ThingsboardException)88 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)72 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)39 TenantId (org.thingsboard.server.common.data.id.TenantId)23 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)18 CustomerId (org.thingsboard.server.common.data.id.CustomerId)17 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)11 Customer (org.thingsboard.server.common.data.Customer)10 MessagingException (javax.mail.MessagingException)8 DashboardId (org.thingsboard.server.common.data.id.DashboardId)8 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)8 Device (org.thingsboard.server.common.data.Device)7 EntityId (org.thingsboard.server.common.data.id.EntityId)7 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)7 User (org.thingsboard.server.common.data.User)6 Asset (org.thingsboard.server.common.data.asset.Asset)6 DeviceId (org.thingsboard.server.common.data.id.DeviceId)6 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)6 UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)6 List (java.util.List)5