Search in sources :

Example 1 with ConstraintOperationType

use of com.haulmont.cuba.security.entity.ConstraintOperationType in project cuba by cuba-platform.

the class RowLevelSecurityExceptionHandler method doHandle.

@Override
protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) {
    try {
        Messages messages = AppBeans.get(Messages.NAME);
        String userCaption = null;
        String userMessage = null;
        if (throwable != null) {
            // noinspection ThrowableResultOfMethodCallIgnored
            Throwable rootCause = ExceptionUtils.getRootCause(throwable);
            RowLevelSecurityException exception = null;
            if (throwable instanceof RowLevelSecurityException) {
                exception = (RowLevelSecurityException) throwable;
            } else if (rootCause instanceof RowLevelSecurityException) {
                exception = (RowLevelSecurityException) rootCause;
            }
            if (exception != null) {
                String entity = exception.getEntity();
                MetaClass entityClass = AppBeans.get(Metadata.NAME, Metadata.class).getClassNN(entity);
                String entityCaption = messages.getTools().getEntityCaption(entityClass);
                ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.NAME);
                MetaClass mainMetaClass = extendedEntities.getOriginalOrThisMetaClass(entityClass);
                String originalEntity = mainMetaClass.getName();
                String entityName = originalEntity.split("\\$")[1];
                ConstraintOperationType operationType = exception.getOperationType();
                if (operationType != null) {
                    ConstraintLocalizationService service = AppBeans.get(ConstraintLocalizationService.NAME);
                    LocalizedConstraintMessage localizedMessage = service.findLocalizedConstraintMessage(originalEntity, operationType);
                    if (localizedMessage != null) {
                        UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                        Locale locale = userSessionSource.getLocale();
                        String localeCode = messages.getTools().localeToString(locale);
                        userCaption = localizedMessage.getLocalizedCaption(localeCode);
                        userMessage = localizedMessage.getLocalizedMessage(localeCode);
                    }
                    String operationId = operationType.getId();
                    if (StringUtils.isEmpty(userCaption)) {
                        String customCaptionKey = String.format("rowLevelSecurity.caption.%s.%s", entityName, operationId);
                        String customCaption = messages.getMainMessage(customCaptionKey);
                        if (!customCaptionKey.equals(customCaption)) {
                            userCaption = customCaption;
                        }
                    }
                    if (StringUtils.isEmpty(userMessage)) {
                        String customMessageKey = String.format("rowLevelSecurity.entityAndOperationMessage.%s.%s", entityName, operationId);
                        String customMessage = messages.getMainMessage(customMessageKey);
                        if (!customMessageKey.equals(customMessage)) {
                            userMessage = customMessage;
                        } else {
                            userMessage = messages.formatMainMessage("rowLevelSecurity.entityAndOperationMessage", messages.getMessage(operationType), entityCaption);
                        }
                    }
                } else {
                    String customCaptionKey = String.format("rowLevelSecurity.caption.%s", entityName);
                    String customCaption = messages.getMainMessage(customCaptionKey);
                    if (!customCaptionKey.equals(customCaption)) {
                        userCaption = customCaption;
                    }
                    String customMessageKey = String.format("rowLevelSecurity.entityMessage.%s", entityName);
                    String customMessage = messages.getMainMessage(customMessageKey);
                    if (!customMessageKey.equals(customMessage)) {
                        userMessage = customMessage;
                    } else {
                        userMessage = messages.formatMainMessage("rowLevelSecurity.entityMessage", entityCaption);
                    }
                }
            }
        }
        if (StringUtils.isEmpty(userCaption)) {
            userCaption = messages.getMainMessage("rowLevelSecurity.caption");
        }
        windowManager.showNotification(userCaption, userMessage, Frame.NotificationType.ERROR);
    } catch (Throwable th) {
        log.error("Unable to handle RowLevelSecurityException", throwable);
        log.error("Exception in RowLevelSecurityExceptionHandler", th);
    }
}
Also used : Locale(java.util.Locale) MetaClass(com.haulmont.chile.core.model.MetaClass) ConstraintOperationType(com.haulmont.cuba.security.entity.ConstraintOperationType) LocalizedConstraintMessage(com.haulmont.cuba.security.entity.LocalizedConstraintMessage) ConstraintLocalizationService(com.haulmont.cuba.core.app.ConstraintLocalizationService)

Example 2 with ConstraintOperationType

use of com.haulmont.cuba.security.entity.ConstraintOperationType in project cuba by cuba-platform.

the class AbstractComponentLoader method loadActionConstraint.

protected void loadActionConstraint(Action action, Element element) {
    if (action instanceof Action.HasSecurityConstraint) {
        Action.HasSecurityConstraint itemTrackingAction = (Action.HasSecurityConstraint) action;
        Attribute operationTypeAttribute = element.attribute("constraintOperationType");
        if (operationTypeAttribute != null) {
            ConstraintOperationType operationType = ConstraintOperationType.fromId(operationTypeAttribute.getValue());
            itemTrackingAction.setConstraintOperationType(operationType);
        }
        String constraintCode = element.attributeValue("constraintCode");
        itemTrackingAction.setConstraintCode(constraintCode);
    }
}
Also used : DeclarativeTrackingAction(com.haulmont.cuba.gui.xml.DeclarativeTrackingAction) DeclarativeAction(com.haulmont.cuba.gui.xml.DeclarativeAction) Attribute(org.dom4j.Attribute) ConstraintOperationType(com.haulmont.cuba.security.entity.ConstraintOperationType)

Aggregations

ConstraintOperationType (com.haulmont.cuba.security.entity.ConstraintOperationType)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 ConstraintLocalizationService (com.haulmont.cuba.core.app.ConstraintLocalizationService)1 DeclarativeAction (com.haulmont.cuba.gui.xml.DeclarativeAction)1 DeclarativeTrackingAction (com.haulmont.cuba.gui.xml.DeclarativeTrackingAction)1 LocalizedConstraintMessage (com.haulmont.cuba.security.entity.LocalizedConstraintMessage)1 Locale (java.util.Locale)1 Attribute (org.dom4j.Attribute)1