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);
}
}
});
}
}
}
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;
}
}
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);
}
}
}
}
}
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);
}
}
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);
}
}
Aggregations