use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.
the class CubaRemoteInvocationExecutor method invoke.
@Override
public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (invocation instanceof CubaRemoteInvocation) {
CubaRemoteInvocation cubaInvocation = (CubaRemoteInvocation) invocation;
UUID sessionId = cubaInvocation.getSessionId();
if (sessionId != null) {
UserSession session = userSessions.getAndRefresh(sessionId);
if (session == null) {
ServerConfig serverConfig = configuration.getConfig(ServerConfig.class);
String sessionProviderUrl = serverConfig.getUserSessionProviderUrl();
if (StringUtils.isNotBlank(sessionProviderUrl)) {
log.debug("User session {} not found, trying to get it from {}", sessionId, sessionProviderUrl);
try {
HttpServiceProxy proxyFactory = new HttpServiceProxy(getServerSelector(sessionProviderUrl));
proxyFactory.setServiceUrl("cuba_TrustedClientService");
proxyFactory.setServiceInterface(TrustedClientService.class);
proxyFactory.afterPropertiesSet();
TrustedClientService trustedClientService = (TrustedClientService) proxyFactory.getObject();
if (trustedClientService != null) {
UserSession userSession = trustedClientService.findSession(serverConfig.getTrustedClientPassword(), sessionId);
if (userSession != null) {
userSessions.add(userSession);
} else {
log.debug("User session {} not found on {}", sessionId, sessionProviderUrl);
}
}
} catch (Exception e) {
log.error("Error getting user session from {}", sessionProviderUrl, e);
}
}
}
AppContext.setSecurityContext(new SecurityContext(sessionId));
}
if (cubaInvocation.getLocale() != null) {
Locale requestLocale = Locale.forLanguageTag(cubaInvocation.getLocale());
if (!globalConfig.getAvailableLocales().containsValue(requestLocale)) {
requestLocale = null;
}
UserInvocationContext.setRequestScopeInfo(sessionId, requestLocale, cubaInvocation.getTimeZone(), cubaInvocation.getAddress(), cubaInvocation.getClientInfo());
}
}
Object result;
try {
result = invocation.invoke(targetObject);
} finally {
AppContext.setSecurityContext(null);
UserInvocationContext.clearRequestScopeInfo();
}
return result;
}
use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.
the class Authentication method withSystemUser.
/**
* Execute code on behalf of the user with login set in {@code cuba.jmxUserLogin} app property.
*
* @param operation code to execute
* @return result of the execution
*/
public <T> T withSystemUser(AuthenticatedOperation<T> operation) {
SecurityContext previousSecurityContext = getSecurityContext();
setSecurityContext(null);
try {
begin(null);
return operation.call();
} finally {
setSecurityContext(previousSecurityContext);
}
}
use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.
the class AuthenticationManagerBean method authenticate.
@Override
@Nonnull
public AuthenticationDetails authenticate(Credentials credentials) throws LoginException {
checkNotNullArgument(credentials, "credentials should not be null");
SecurityContext previousSecurityContext = AppContext.getSecurityContext();
AppContext.setSecurityContext(new SecurityContext(serverSession));
try (Transaction tx = persistence.createTransaction()) {
AuthenticationDetails authenticationDetails = authenticateInternal(credentials);
tx.commit();
userSessionManager.clearPermissionsOnUser(authenticationDetails.getSession());
return authenticationDetails;
} finally {
AppContext.setSecurityContext(previousSecurityContext);
}
}
use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.
the class AuthenticationManagerBean method substituteUser.
@Nonnull
@Override
public UserSession substituteUser(User substitutedUser) {
UserSession currentSession = userSessionSource.getUserSession();
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
User user;
if (currentSession.getUser().equals(substitutedUser)) {
user = em.find(User.class, substitutedUser.getId());
if (user == null) {
throw new NoResultException("User not found");
}
} else {
user = loadSubstitutedUser(substitutedUser, currentSession, em);
}
UserSession session = userSessionManager.createSession(currentSession, user);
withSecurityContext(new SecurityContext(serverSession), () -> publishUserSubstitutedEvent(currentSession, session));
tx.commit();
userSessions.remove(currentSession);
userSessionManager.clearPermissionsOnUser(session);
userSessions.add(session);
return session;
}
}
use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.
the class FileDownloadController method download.
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserSession userSession = getSession(request, response);
if (userSession == null)
return;
AppContext.setSecurityContext(new SecurityContext(userSession));
try {
File file = null;
FileDescriptor fd = null;
if (request.getParameter("p") != null)
file = getFile(request, response);
else
fd = getFileDescriptor(request, response);
if (fd == null && file == null)
return;
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setIntHeader("Expires", -1);
response.setHeader("Content-Type", FileTypesHelper.DEFAULT_MIME_TYPE);
InputStream is = null;
ServletOutputStream os = null;
try {
is = fd != null ? fileStorage.openStream(fd) : FileUtils.openInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (FileStorageException e) {
log.error("Unable to download file", e);
response.sendError(e.getType().getHttpStatus());
} catch (Exception ex) {
log.error("Unable to download file", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
} finally {
AppContext.setSecurityContext(null);
}
}
Aggregations