Search in sources :

Example 6 with UserId

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

the class JpaUserDaoTest method saveUser.

private void saveUser(UUID tenantId, UUID customerId) {
    User user = new User();
    UUID id = UUIDs.timeBased();
    user.setId(new UserId(id));
    user.setTenantId(new TenantId(tenantId));
    user.setCustomerId(new CustomerId(customerId));
    if (customerId == NULL_UUID) {
        user.setAuthority(Authority.TENANT_ADMIN);
    } else {
        user.setAuthority(Authority.CUSTOMER_USER);
    }
    String idString = id.toString();
    String email = idString.substring(0, idString.indexOf('-')) + "@thingsboard.org";
    user.setEmail(email);
    userDao.save(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) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) UUID(java.util.UUID)

Example 7 with UserId

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

the class AuditLogController method getAuditLogsByUserId.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/audit/logs/user/{userId}", params = { "limit" }, method = RequestMethod.GET)
@ResponseBody
public TimePageData<AuditLog> getAuditLogsByUserId(@PathVariable("userId") String strUserId, @RequestParam int limit, @RequestParam(required = false) Long startTime, @RequestParam(required = false) Long endTime, @RequestParam(required = false, defaultValue = "false") boolean ascOrder, @RequestParam(required = false) String offset) throws ThingsboardException {
    try {
        checkParameter("UserId", strUserId);
        TenantId tenantId = getCurrentUser().getTenantId();
        TimePageLink pageLink = createPageLink(limit, startTime, endTime, ascOrder, offset);
        return checkNotNull(auditLogService.findAuditLogsByTenantIdAndUserId(tenantId, new UserId(UUID.fromString(strUserId)), pageLink));
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) UserId(org.thingsboard.server.common.data.id.UserId) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with UserId

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

the class PluginApiController method processRequest.

@SuppressWarnings("rawtypes")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/{pluginToken}/**")
@ResponseStatus(value = HttpStatus.OK)
public DeferredResult<ResponseEntity> processRequest(@PathVariable("pluginToken") String pluginToken, RequestEntity<byte[]> requestEntity, HttpServletRequest request) throws ThingsboardException {
    log.debug("[{}] Going to process requst uri: {}", pluginToken, requestEntity.getUrl());
    DeferredResult<ResponseEntity> result = new DeferredResult<ResponseEntity>();
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(pluginToken);
    if (pluginMd == null) {
        result.setErrorResult(new PluginNotFoundException("Plugin with token: " + pluginToken + " not found!"));
    } else {
        TenantId tenantId = getCurrentUser().getTenantId();
        CustomerId customerId = getCurrentUser().getCustomerId();
        if (validatePluginAccess(pluginMd, tenantId, customerId)) {
            if (tenantId != null && ModelConstants.NULL_UUID.equals(tenantId.getId())) {
                tenantId = null;
            }
            UserId userId = getCurrentUser().getId();
            String userName = getCurrentUser().getName();
            PluginApiCallSecurityContext securityCtx = new PluginApiCallSecurityContext(pluginMd.getTenantId(), pluginMd.getId(), tenantId, customerId, userId, userName);
            actorService.process(new BasicPluginRestMsg(securityCtx, new RestRequest(requestEntity, request), result));
        } else {
            result.setResult(new ResponseEntity<>(HttpStatus.FORBIDDEN));
        }
    }
    return result;
}
Also used : BasicPluginRestMsg(org.thingsboard.server.extensions.api.plugins.rest.BasicPluginRestMsg) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PluginApiCallSecurityContext(org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext) ResponseEntity(org.springframework.http.ResponseEntity) TenantId(org.thingsboard.server.common.data.id.TenantId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) UserId(org.thingsboard.server.common.data.id.UserId) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with UserId

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

the class PluginWebSocketHandler method toRef.

private PluginWebsocketSessionRef toRef(WebSocketSession session) throws IOException {
    URI sessionUri = session.getUri();
    String path = sessionUri.getPath();
    path = path.substring(WebSocketConfiguration.WS_PLUGIN_PREFIX.length());
    if (path.length() == 0) {
        throw new IllegalArgumentException("URL should contain plugin token!");
    }
    String[] pathElements = path.split("/");
    String pluginToken = pathElements[0];
    // TODO: cache
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(pluginToken);
    if (pluginMd == null) {
        throw new InvalidParameterException("Can't find plugin with specified token!");
    } else {
        SecurityUser currentUser = (SecurityUser) session.getAttributes().get(WebSocketConfiguration.WS_SECURITY_USER_ATTRIBUTE);
        TenantId tenantId = currentUser.getTenantId();
        CustomerId customerId = currentUser.getCustomerId();
        if (PluginApiController.validatePluginAccess(pluginMd, tenantId, customerId)) {
            UserId userId = currentUser.getId();
            String userName = currentUser.getName();
            PluginApiCallSecurityContext securityCtx = new PluginApiCallSecurityContext(pluginMd.getTenantId(), pluginMd.getId(), tenantId, currentUser.getCustomerId(), userId, userName);
            return new BasicPluginWebsocketSessionRef(UUID.randomUUID().toString(), securityCtx, session.getUri(), session.getAttributes(), session.getLocalAddress(), session.getRemoteAddress());
        } else {
            throw new SecurityException("Current user is not allowed to use this plugin!");
        }
    }
}
Also used : BasicPluginWebsocketSessionRef(org.thingsboard.server.extensions.api.plugins.ws.BasicPluginWebsocketSessionRef) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) URI(java.net.URI) PluginApiCallSecurityContext(org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext) InvalidParameterException(java.security.InvalidParameterException) TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) UserId(org.thingsboard.server.common.data.id.UserId)

Example 10 with UserId

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

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