Search in sources :

Example 11 with UserSession

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);
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 12 with UserSession

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;
}
Also used : Role(com.haulmont.cuba.security.entity.Role) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 13 with UserSession

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());
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) PostConstruct(javax.annotation.PostConstruct)

Example 14 with UserSession

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;
}
Also used : ConstraintData(com.haulmont.cuba.security.global.ConstraintData) MetaClass(com.haulmont.chile.core.model.MetaClass) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 15 with UserSession

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);
    }
}
Also used : GlobalConfig(com.haulmont.cuba.core.global.GlobalConfig) GlobalConfig(com.haulmont.cuba.core.global.GlobalConfig) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) LoginException(com.haulmont.cuba.security.global.LoginException) TrustedClientCredentials(com.haulmont.cuba.security.auth.TrustedClientCredentials)

Aggregations

UserSession (com.haulmont.cuba.security.global.UserSession)127 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)29 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)25 TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)24 LoginException (com.haulmont.cuba.security.global.LoginException)23 Test (org.junit.Test)19 User (com.haulmont.cuba.security.entity.User)17 UUID (java.util.UUID)16 IOException (java.io.IOException)14 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)12 ArrayList (java.util.ArrayList)11 Locale (java.util.Locale)11 List (java.util.List)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)7 LogFileNotFoundException (com.haulmont.cuba.core.sys.logging.LogFileNotFoundException)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4