Search in sources :

Example 41 with PermissionHolder

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

the class MangoWebSocketHandshakeInterceptor method beforeHandshake.

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    HttpSession session = getSession(request);
    if (session != null) {
        attributes.put(HTTP_SESSION_ID_ATTR, session.getId());
    }
    // get the user at the time of HTTP -> websocket upgrade
    Principal principal = request.getPrincipal();
    if (principal instanceof Authentication) {
        Authentication authentication = (Authentication) principal;
        attributes.put(AUTHENTICATION_ATTR, authentication);
        Object authenticationPrincipal = authentication.getPrincipal();
        if (authenticationPrincipal instanceof PermissionHolder) {
            attributes.put(USER_ATTR, authenticationPrincipal);
        }
    }
    return true;
}
Also used : HttpSession(javax.servlet.http.HttpSession) Authentication(org.springframework.security.core.Authentication) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Principal(java.security.Principal)

Example 42 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 43 with PermissionHolder

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

the class RoleModelMapping method map.

@Override
public RoleModel map(Object from, PermissionHolder user, RestModelMapper mapper) {
    RoleVO role = (RoleVO) from;
    RoleModel model = new RoleModel(role);
    if (role.getInherited() != null) {
        Set<String> inherited = new HashSet<>(role.getInherited().size());
        model.setInherited(inherited);
        for (Role inheritedRole : role.getInherited()) {
            inherited.add(inheritedRole.getXid());
        }
    }
    return model;
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) HashSet(java.util.HashSet)

Example 44 with PermissionHolder

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

the class MangoSessionDataModelMapping method map.

@Override
public MangoSessionDataModel map(Object from, PermissionHolder user, RestModelMapper mapper) {
    MangoSessionDataVO source = (MangoSessionDataVO) from;
    MangoSessionDataModel result = new MangoSessionDataModel();
    result.setSessionId(source.getSessionId());
    result.setContextPath(source.getContextPath());
    result.setVirtualHost(source.getVirtualHost());
    result.setLastNode(source.getLastNode());
    result.setAccessTime(new Date(source.getAccessTime()));
    result.setLastAccessTime(new Date(source.getLastAccessTime()));
    result.setCreateTime(new Date(source.getCreateTime()));
    result.setCookieTime(new Date(source.getCookieTime()));
    result.setLastSavedTime(source.getLastSavedTime());
    result.setExpiryTime(new Date(source.getExpiryTime()));
    result.setMaxInterval(source.getMaxInterval());
    User owner = dao.get(source.getUserId());
    if (user != null) {
        result.setUsername(owner.getUsername());
    }
    return result;
}
Also used : User(com.serotonin.m2m2.vo.User) MangoSessionDataVO(com.serotonin.m2m2.vo.MangoSessionDataVO) Date(java.util.Date)

Example 45 with PermissionHolder

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

the class MangoSessionDataModelMapping method unmap.

@Override
public MangoSessionDataVO unmap(Object from, PermissionHolder user, RestModelMapper mapper) {
    MangoSessionDataModel source = (MangoSessionDataModel) from;
    MangoSessionDataVO result = new MangoSessionDataVO();
    result.setSessionId(source.getSessionId());
    result.setContextPath(source.getContextPath());
    result.setVirtualHost(source.getVirtualHost());
    result.setLastNode(source.getLastNode());
    result.setAccessTime(source.getAccessTime() != null ? source.getAccessTime().getTime() : 0l);
    result.setLastAccessTime(source.getLastAccessTime() != null ? source.getLastAccessTime().getTime() : 0l);
    result.setCreateTime(source.getCreateTime() != null ? source.getCreateTime().getTime() : 0l);
    result.setCookieTime(source.getCookieTime() != null ? source.getCookieTime().getTime() : 0l);
    result.setLastSavedTime(source.getLastSavedTime());
    result.setExpiryTime(source.getExpiryTime() != null ? source.getExpiryTime().getTime() : 0l);
    result.setMaxInterval(source.getMaxInterval());
    User owner = dao.getByXid(source.getUsername());
    if (user != null) {
        result.setUserId(owner.getId());
    }
    return result;
}
Also used : User(com.serotonin.m2m2.vo.User) MangoSessionDataVO(com.serotonin.m2m2.vo.MangoSessionDataVO)

Aggregations

PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)120 ApiOperation (io.swagger.annotations.ApiOperation)97 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)97 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)64 ResponseEntity (org.springframework.http.ResponseEntity)53 HttpHeaders (org.springframework.http.HttpHeaders)50 URI (java.net.URI)48 ArrayList (java.util.ArrayList)37 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)34 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)29 List (java.util.List)27 User (com.serotonin.m2m2.vo.User)25 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)24 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)24 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)19 HashMap (java.util.HashMap)19 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)18 Common (com.serotonin.m2m2.Common)18 Collectors (java.util.stream.Collectors)17 Role (com.serotonin.m2m2.vo.role.Role)16