Search in sources :

Example 56 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class DaoNotificationWebSocketHandler method notify.

/**
 * @param action add, update or delete
 */
public void notify(String action, T vo, T originalVo, ApplicationEvent event) {
    for (WebSocketSession session : sessions) {
        PermissionHolder user = getUser(session);
        if (hasPermission(user, vo) && isSubscribed(session, action, vo, originalVo)) {
            this.runAs.runAs(user, () -> {
                Object userMessage = createNotification(action, vo, originalVo, event, user);
                if (userMessage != null) {
                    try {
                        ObjectWriter writer;
                        Class<?> view = this.viewForUser(user);
                        if (view != null) {
                            writer = this.jacksonMapper.writerWithView(view);
                        } else {
                            writer = this.jacksonMapper.writer();
                        }
                        String userJsonMessage = writer.writeValueAsString(userMessage);
                        notify(session, userJsonMessage);
                    } catch (JsonProcessingException e) {
                        log.warn("Failed to write object as JSON", e);
                    }
                }
            });
        }
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 57 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class TemporaryResourceManager method newTemporaryResource.

/**
 * Creates a new temporary resource, how the task is executed depends on the manager implementation.
 *
 * @param resourceType unique type string assigned to each resource type e.g. BULK_DATA_POINT
 * @param id if null will be assigned a UUID
 * @param expiration time after the resource completes that it will be removed (milliseconds). If null or less than zero, it will be set to the default DEFAULT_EXPIRATION_MILLISECONDS
 * @param timeout time after the resource starts that it will be timeout if not complete (milliseconds). If null or less than zero, it will be set to the default DEFAULT_TIMEOUT_MILLISECONDS
 * @param task the task to be run
 */
public final TemporaryResource<T, E> newTemporaryResource(String resourceType, String id, Long expiration, Long timeout, ResourceTask<T, E> task) {
    if (expiration == null || expiration < 0) {
        expiration = Common.getMillis(Common.TIME_PERIOD_CODES.getId(environment.getProperty("rest.temporaryResource.expirationPeriodType", "HOURS")), environment.getProperty("rest.temporaryResource.expirationPeriods", Integer.class, 4));
    }
    if (timeout == null || timeout < 0) {
        timeout = Common.getMillis(Common.TIME_PERIOD_CODES.getId(environment.getProperty("rest.temporaryResource.timeoutPeriodType", "HOURS")), environment.getProperty("rest.temporaryResource.timeoutPeriods", Integer.class, 3));
    }
    PermissionHolder user = Common.getUser();
    TemporaryResource<T, E> resource = new TemporaryResource<T, E>(resourceType, id, user, expiration, timeout, task, this);
    synchronized (resource) {
        this.add(resource);
        resource.schedule();
        return resource;
    }
}
Also used : PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 58 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class TemporaryResourceWebSocketHandler method notifySession.

private void notifySession(WebSocketSession session, CrudNotificationType type, TemporaryResource<?, ?> resource) throws JsonProcessingException, IOException {
    PermissionHolder user = this.getUser(session);
    TemporaryResourceSubscription subscription = (TemporaryResourceSubscription) session.getAttributes().get(SUBSCRIPTION_ATTRIBUTE);
    boolean hasAccess = permissionService.hasAccessToResource(user, resource);
    boolean isOwner = resource.isOwnedBy(user);
    if (hasAccess && (!subscription.isOwnResourcesOnly() || isOwner)) {
        Set<TemporaryResourceStatus> statuses = subscription.getStatuses();
        Set<String> resourceTypes = subscription.getResourceTypes();
        if ((subscription.isAnyStatus() || statuses.contains(resource.getStatus())) && (subscription.isAnyResourceType() || resourceTypes.contains(resource.getResourceType()))) {
            WebSocketNotification<TemporaryResource<?, ?>> notificationMessage = new WebSocketNotification<>(type, resource);
            boolean showResult = !resource.isComplete() && subscription.isShowResultWhenIncomplete() || resource.isComplete() && subscription.isShowResultWhenComplete();
            if (type == CrudNotificationType.DELETE) {
                showResult = false;
            }
            Class<?> view = showResult ? TemporaryResourceViews.ShowResult.class : Object.class;
            if (log.isTraceEnabled()) {
                log.trace("Notifying session " + session.getId() + " of change to resource " + resource);
            }
            try {
                this.sendRawMessageUsingView(session, notificationMessage, view);
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn("Error notifying session " + session.getId() + " of change to resource " + resource, e);
                }
            }
        }
    }
}
Also used : PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebSocketNotification(com.infiniteautomation.mango.rest.latest.websocket.WebSocketNotification) TemporaryResourceStatus(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResource.TemporaryResourceStatus)

Example 59 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class MangoWebSocketSessionTracker method afterConnectionEstablished.

public void afterConnectionEstablished(WebSocketSession session) {
    String httpSessionId = this.httpSessionIdForSession(session);
    if (httpSessionId != null) {
        sessionsByHttpSessionId.put(httpSessionId, session);
    }
    PermissionHolder permissionHolder = this.userForSession(session);
    Authentication authentication = this.authenticationForSession(session);
    boolean isJwt = authentication instanceof JwtAuthentication;
    User user = permissionHolder.getUser();
    if (user != null) {
        int userId = user.getId();
        if (isJwt) {
            jwtSessionsByUserId.put(userId, session);
        } else {
            otherSessionsByUserId.put(userId, session);
        }
    }
    if (isJwt) {
        JwtAuthentication jwtAuthentication = (JwtAuthentication) authentication;
        Date expiration = jwtAuthentication.getToken().getBody().getExpiration();
        TimeoutTask closeTask = new TimeoutTask(expiration, new CloseSessionTask(session));
        session.getAttributes().put(CLOSE_TIMEOUT_TASK_ATTR, closeTask);
        jwtSessions.add(session);
    }
}
Also used : User(com.serotonin.m2m2.vo.User) JwtAuthentication(com.serotonin.m2m2.web.mvc.spring.security.authentication.JwtAuthentication) Authentication(org.springframework.security.core.Authentication) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) JwtAuthentication(com.serotonin.m2m2.web.mvc.spring.security.authentication.JwtAuthentication) Date(java.util.Date) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 60 with PermissionHolder

use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.

the class MangoWebSocketSessionTracker method afterConnectionClosed.

public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
    String httpSessionId = this.httpSessionIdForSession(session);
    if (httpSessionId != null) {
        sessionsByHttpSessionId.remove(httpSessionId, session);
    }
    PermissionHolder permissionHolder = this.userForSession(session);
    Authentication authentication = this.authenticationForSession(session);
    boolean isJwt = authentication instanceof JwtAuthentication;
    User user = permissionHolder.getUser();
    if (user != null) {
        int userId = user.getId();
        if (isJwt) {
            jwtSessionsByUserId.remove(userId, session);
        } else {
            otherSessionsByUserId.remove(userId, session);
        }
    }
    if (isJwt) {
        TimeoutTask closeTask = (TimeoutTask) session.getAttributes().get(CLOSE_TIMEOUT_TASK_ATTR);
        if (closeTask != null) {
            closeTask.cancel();
        }
        jwtSessions.remove(session);
    }
}
Also used : User(com.serotonin.m2m2.vo.User) JwtAuthentication(com.serotonin.m2m2.web.mvc.spring.security.authentication.JwtAuthentication) Authentication(org.springframework.security.core.Authentication) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) JwtAuthentication(com.serotonin.m2m2.web.mvc.spring.security.authentication.JwtAuthentication) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Aggregations

PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)205 ApiOperation (io.swagger.annotations.ApiOperation)98 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)98 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)86 ResponseEntity (org.springframework.http.ResponseEntity)53 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)50 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)50 ArrayList (java.util.ArrayList)50 HttpHeaders (org.springframework.http.HttpHeaders)50 URI (java.net.URI)48 List (java.util.List)43 User (com.serotonin.m2m2.vo.User)37 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)35 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)32 Common (com.serotonin.m2m2.Common)31 Collectors (java.util.stream.Collectors)30 Role (com.serotonin.m2m2.vo.role.Role)29 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)28 Autowired (org.springframework.beans.factory.annotation.Autowired)25 HashMap (java.util.HashMap)24