Search in sources :

Example 76 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class CustomConditionFrame method initComponents.

@Override
public void initComponents() {
    super.initComponents();
    typeSelect.addValueChangeListener(e -> {
        boolean disableTypeCheckBox = ParamType.UNARY.equals(typeSelect.getValue()) || ParamType.BOOLEAN.equals(typeSelect.getValue());
        inExprCb.setEnabled(!disableTypeCheckBox);
        if (disableTypeCheckBox)
            inExprCb.setValue(false);
        boolean isEntity = ParamType.ENTITY.equals(typeSelect.getValue());
        boolean isEnum = ParamType.ENUM.equals(typeSelect.getValue());
        boolean isDate = ParamType.DATE.equals(typeSelect.getValue());
        entityLab.setEnabled(isEntity || isEnum);
        entitySelect.setEnabled(isEntity || isEnum);
        entitySelect.setRequired(entitySelect.isEnabled());
        paramWhereLab.setEnabled(isEntity);
        entityParamWhereField.setEnabled(isEntity);
        paramViewLab.setEnabled(isEntity);
        entityParamViewField.setEnabled(isEntity);
        useUserTimeZoneLab.setVisible(isDate);
        useUserTimeZone.setVisible(isDate);
        Param param = condition.getParam();
        fillEntitySelect(param);
        // recreate default value component based on param type
        if (!initializing && defaultValueLayout.isVisible()) {
            ParamType paramType = (ParamType) e.getValue();
            if ((isEntity || isEnum) && (entitySelect.getValue() == null)) {
                defaultValueLayout.remove(defaultValueComponent);
                param.setJavaClass(null);
            } else {
                Class paramJavaClass = getParamJavaClass(paramType);
                param.setJavaClass(paramJavaClass);
                param.setDefaultValue(null);
                createDefaultValueComponent();
            }
        }
    });
    inExprCb.addValueChangeListener(e -> {
        condition.getParam().setInExpr(BooleanUtils.isTrue((Boolean) e.getValue()));
        // recreate default value component based on "in list" checkbox value
        if (!initializing && defaultValueLayout.isVisible()) {
            condition.getParam().setDefaultValue(null);
            createDefaultValueComponent();
        }
    });
    useUserTimeZone.addValueChangeListener(e -> {
        if (defaultValueComponent != null) {
            if (defaultValueComponent instanceof DateField) {
                DateField dateField = (DateField) defaultValueComponent;
                if (Boolean.TRUE.equals(e.getValue())) {
                    UserSession userSession = userSessionSource.getUserSession();
                    if (userSession.getTimeZone() != null) {
                        dateField.setTimeZone(userSession.getTimeZone());
                    }
                    dateField.setValue(null);
                    dateField.setEditable(false);
                } else {
                    dateField.setTimeZone(TimeZone.getDefault());
                    dateField.setEditable(true);
                }
            }
        }
    });
    entitySelect.addValueChangeListener(e -> {
        if (initializing || !defaultValueLayout.isVisible()) {
            return;
        }
        if (e.getValue() == null) {
            defaultValueLayout.remove(defaultValueComponent);
            return;
        }
        Param param = condition.getParam();
        Class paramJavaClass = e.getValue() instanceof Class ? (Class) e.getValue() : ((MetaClass) e.getValue()).getJavaClass();
        param.setJavaClass(paramJavaClass);
        param.setDefaultValue(null);
        createDefaultValueComponent();
    });
    FilterHelper filterHelper = AppBeans.get(FilterHelper.class);
    filterHelper.setLookupNullSelectionAllowed(typeSelect, false);
    filterHelper.setLookupFieldPageLength(typeSelect, 12);
    joinField.setSuggester((source, text, cursorPosition) -> requestHint(joinField, text, cursorPosition));
    joinField.setHighlightActiveLine(false);
    joinField.setShowGutter(false);
    whereField.setSuggester((source, text, cursorPosition) -> requestHint(whereField, text, cursorPosition));
    whereField.setHighlightActiveLine(false);
    whereField.setShowGutter(false);
    entityParamWhereField.setSuggester((source, text, cursorPosition) -> requestHintParamWhere(entityParamWhereField, text, cursorPosition));
    entityParamWhereField.setHighlightActiveLine(false);
    entityParamWhereField.setShowGutter(false);
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 77 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class ListEditorHelper method getValueCaption.

public static String getValueCaption(Object v, ListEditor.ItemType itemType, TimeZone timeZone) {
    if (v == null)
        return null;
    switch(itemType) {
        case ENTITY:
            if (v instanceof Instance)
                return ((Instance) v).getInstanceName();
            else
                return v.toString();
        case STRING:
            return (String) v;
        case DATE:
            return Datatypes.getNN(java.sql.Date.class).format(v);
        case DATETIME:
            UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
            UserSession userSession = userSessionSource.getUserSession();
            if (timeZone != null) {
                return ((TimeZoneAwareDatatype) Datatypes.getNN(Date.class)).format(v, userSession.getLocale(), timeZone);
            } else {
                return Datatypes.getNN(Date.class).format(v, userSession.getLocale());
            }
        case INTEGER:
            return Datatypes.getNN(Integer.class).format(v);
        case LONG:
            return Datatypes.getNN(Long.class).format(v);
        case BIGDECIMAL:
            return Datatypes.getNN(BigDecimal.class).format(v);
        case DOUBLE:
            return Datatypes.getNN(Double.class).format(v);
        case ENUM:
            return AppBeans.get(Messages.class).getMessage((Enum) v);
        case UUID:
            return Datatypes.getNN(java.util.UUID.class).format(v);
        default:
            throw new IllegalStateException("Unknown item type");
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) TimeZoneAwareDatatype(com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype) Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) Date(java.util.Date) BigDecimal(java.math.BigDecimal) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 78 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class LogMdc method setup.

public static void setup(SecurityContext securityContext) {
    String userProp = AppContext.getProperty("cuba.logUserName");
    if (userProp == null || Boolean.valueOf(userProp)) {
        if (securityContext != null) {
            String user = securityContext.getUser();
            if (user == null) {
                UserSession session = securityContext.getSession();
                if (session != null)
                    user = session.getUser().getLogin();
                else if (securityContext.getSessionId() != null) {
                    ApplicationContext applicationContext = AppContext.getApplicationContext();
                    if (applicationContext.containsBean("cuba_UserSessions")) {
                        UserSessionFinder sessionFinder = (UserSessionFinder) applicationContext.getBean("cuba_UserSessions");
                        session = sessionFinder.get(securityContext.getSessionId());
                        if (session != null) {
                            user = session.getUser().getLogin();
                        }
                    }
                }
            }
            if (user != null) {
                MDC.put(USER, "/" + user);
            }
        } else {
            MDC.remove(USER);
        }
    }
    String applicationProp = AppContext.getProperty("cuba.logAppName");
    if (applicationProp == null || Boolean.valueOf(applicationProp)) {
        if (securityContext != null) {
            MDC.put(APPLICATION, "/" + AppContext.getProperty("cuba.webContextName"));
        } else {
            MDC.remove(APPLICATION);
        }
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) UserSession(com.haulmont.cuba.security.global.UserSession) UserSessionFinder(com.haulmont.cuba.core.sys.UserSessionFinder)

Example 79 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class FoldersServiceBean method checkImportPermissions.

protected void checkImportPermissions(Folder folder) {
    UserSession userSession = userSessionSource.getUserSession();
    if (folder instanceof SearchFolder) {
        SearchFolder searchFolder = (SearchFolder) folder;
        User currentUser = userSession.getCurrentOrSubstitutedUser();
        if (searchFolder.getUser() != null && !currentUser.equals(searchFolder.getUser())) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
        }
        if (searchFolder.getUser() == null && !userSession.isSpecificPermitted("cuba.gui.searchFolder.global")) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
        }
    }
    if (folder instanceof AppFolder) {
        if (!userSession.isSpecificPermitted("cuba.gui.appFolder.global")) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
        }
    }
}
Also used : AppFolder(com.haulmont.cuba.core.entity.AppFolder) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) AppFolder(com.haulmont.cuba.core.entity.AppFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Folder(com.haulmont.cuba.core.entity.Folder)

Example 80 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class ExecutionsImpl method endExecution.

public void endExecution() {
    try {
        Thread thread = Thread.currentThread();
        if (thread.isInterrupted()) {
            Thread.interrupted();
        }
        ExecutionContextImpl context = (ExecutionContextImpl) ExecutionContextHolder.getCurrentContext();
        if (context == null) {
            throw new IllegalStateException("No execution context found");
        }
        log.debug("End execution context: group={}, key={}", context.getGroup(), context.getKey());
        UserSession userSession = userSessionSource.getUserSession();
        removeExecutionContextFromUserSession(userSession, context);
        context.setState(ExecutionContextImpl.State.COMPLETED);
    } finally {
        ExecutionContextHolder.removeContext();
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession)

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