use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.
the class DynamicAttributesCacheStrategy method init.
@Override
public void init() {
clientCacheManager.getExecutorService().scheduleWithFixedDelay(() -> {
if (needToValidateCache) {
UserSession userSession = cacheUserSessionProvider.getUserSession();
if (userSession == null) {
// cache user session unavailable
return;
}
try {
AppContext.setSecurityContext(new SecurityContext(userSession));
loadObject();
} catch (NoUserSessionException e) {
log.warn("Cache user session expired", e);
} catch (Exception e) {
log.error("Unable to update dynamic attributes cache", e);
}
}
}, 0, 10, TimeUnit.SECONDS);
}
use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.
the class TestUserSessionSource method getUserSession.
@Override
public synchronized UserSession getUserSession() {
if (session == null) {
User user = new User();
user.setId(UUID.fromString(USER_ID));
user.setLogin("test_admin");
user.setName("Test Administrator");
user.setPassword(DigestUtils.md5Hex("test_admin"));
session = new UserSession(UUID.randomUUID(), user, Collections.<Role>emptyList(), Locale.ENGLISH, false);
}
return session;
}
use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.
the class ScreenHistoryEntity method init.
@PostConstruct
protected void init() {
UserSession userSession = AppBeans.get(UserSessionSource.class).getUserSession();
setUser(userSession.getUser());
setSubstitutedUser(userSession.getSubstitutedUser());
}
use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.
the class SecurityImpl method getConstraints.
protected List<ConstraintData> getConstraints(MetaClass metaClass) {
UserSession userSession = userSessionSource.getUserSession();
MetaClass mainMetaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
List<ConstraintData> constraints = new ArrayList<>();
constraints.addAll(userSession.getConstraints(mainMetaClass.getName()));
for (MetaClass parent : mainMetaClass.getAncestors()) {
constraints.addAll(userSession.getConstraints(parent.getName()));
}
return constraints;
}
use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.
the class ConfigStorageCommon method getConfigValue.
/**
* Method returns a result of config method invocation
* @param classFQN fully qualified configuration interface name
* @param methodName config getter method name
* @param userLogin parameter is used for authentication if there is no security context bound to the current thread
* and configuration method source is DATABASE
* @param userPassword see userLogin parameter description
* @return configuration method invocation result
*/
public String getConfigValue(String classFQN, String methodName, String userLogin, String userPassword) {
Class<?> aClass;
try {
aClass = Class.forName(classFQN);
} catch (ClassNotFoundException e) {
return String.format("Class %s not found.\nPlease ensure that you entered a fully qualified class name and " + "that you class is in a proper application module (core, web or portal).", classFQN);
}
if (Config.class.isAssignableFrom(aClass)) {
Config config = configuration.getConfig((Class<? extends Config>) aClass);
Method method;
boolean logoutRequired = false;
try {
method = aClass.getMethod(methodName);
// DATABASE, then login attempt with 'userLogin' and 'userPassword' will be made
if (AppContext.getSecurityContext() == null) {
SourceType sourceType;
Source methodSourceAnnotation = method.getAnnotation(Source.class);
if (methodSourceAnnotation != null) {
sourceType = methodSourceAnnotation.type();
} else {
Source classSourceAnnotation = aClass.getAnnotation(Source.class);
sourceType = classSourceAnnotation.type();
}
if (sourceType != null && sourceType == SourceType.DATABASE) {
if (Strings.isNullOrEmpty(userLogin)) {
return "No security context bound to the current thread. Please specify the user name.";
} else {
try {
Map<String, Locale> availableLocales = configuration.getConfig(GlobalConfig.class).getAvailableLocales();
Locale defaultLocale = availableLocales.values().iterator().next();
TrustedClientCredentials credentials = new TrustedClientCredentials(userLogin, userPassword, defaultLocale);
UserSession session = authenticationService.login(credentials).getSession();
AppContext.setSecurityContext(new SecurityContext(session));
logoutRequired = true;
} catch (LoginException e) {
log.error(ExceptionUtils.getStackTrace(e));
return "Login error: " + e.getMessage();
}
}
}
}
Object result = method.invoke(config);
return result == null ? null : result.toString();
} catch (NoSuchMethodException e) {
return String.format("Method %s() not found in class %s", methodName, classFQN);
} catch (InvocationTargetException | IllegalAccessException e) {
return ExceptionUtils.getStackTrace(e);
} finally {
if (logoutRequired) {
try {
authenticationService.logout();
} finally {
AppContext.setSecurityContext(null);
}
}
}
} else {
return String.format("Class %s is not an implementation of Config interface", classFQN);
}
}
Aggregations