Skip to content

Commit

Permalink
MID-8362 MID-8084 locale property added to MidPointPrincipal, now das…
Browse files Browse the repository at this point in the history
…hboards can be properly translated
  • Loading branch information
1azyman committed Dec 6, 2022
1 parent 42e9c1a commit d774dde
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 18 deletions.
Expand Up @@ -9,6 +9,8 @@

import java.util.Locale;

import com.evolveum.midpoint.authentication.api.util.AuthUtil;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.IModel;

Expand Down Expand Up @@ -67,6 +69,7 @@ protected LocaleDescriptor getSelectedLocaleDescriptor() {
protected void changeLocale(AjaxRequestTarget target, LocaleDescriptor descriptor) {
LOGGER.info("Changing locale to {}.", descriptor.getLocale());
getSession().setLocale(descriptor.getLocale());
AuthUtil.getPrincipalUser().setPreferredLocale(descriptor.getLocale());
WebComponentUtil.getCompiledGuiProfile().setLocale(descriptor.getLocale());

target.add(getPage());
Expand Down
Expand Up @@ -54,7 +54,7 @@
<script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.SingleLocalizableMessageType
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.up'))
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.up'), false)
</code>
</script>
</expression>
Expand Down Expand Up @@ -109,7 +109,7 @@
<script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.SingleLocalizableMessageType
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.failed'))
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.failed'), false)
</code>
</script>
</expression>
Expand Down Expand Up @@ -162,7 +162,7 @@
<script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.SingleLocalizableMessageType
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.modifications'))
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.modifications'), false)
</code>
</script>
</expression>
Expand Down Expand Up @@ -204,7 +204,7 @@
<script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c:ScriptExpressionEvaluatorType">
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.SingleLocalizableMessageType
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.runnable'))
return midpoint.translate(new SingleLocalizableMessageType().key('AdminDashboard.dataField.runnable'), false)
</code>
</script>
</expression>
Expand Down
Expand Up @@ -7,10 +7,7 @@

package com.evolveum.midpoint.model.api.expr;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

import javax.xml.namespace.QName;

Expand Down Expand Up @@ -1160,8 +1157,28 @@ TaskType executeChangesAsynchronously(Collection<ObjectDelta<?>> deltas, ModelEx

String translate(LocalizableMessage message);

/**
* Translates message parameter.
*
* @param message
* @param useDefaultLocale If true, default JVM locale will be used for translation - {@link Locale#getDefault()}.
* If false, Midpoint will check principal object for more appropriate login - {@link MidPointPrincipal#getLocale()}, if no locale available {@link Locale#getDefault()} will be used.
* @return translated string
*/
String translate(LocalizableMessage message, boolean useDefaultLocale);

String translate(LocalizableMessageType message);

/**
* Translates message parameter.
*
* @param message
* @param useDefaultLocale If true, default JVM locale will be used for translation - {@link Locale#getDefault()}.
* If false, Midpoint will check principal object for more appropriate login - {@link MidPointPrincipal#getLocale()}, if no locale available {@link Locale#getDefault()} will be used.
* @return translated string
*/
String translate(LocalizableMessageType message, boolean useDefaultLocale);

/**
* Counts accounts having `attributeValue` of `attributeName`.
*
Expand Down
Expand Up @@ -7,9 +7,14 @@
package com.evolveum.midpoint.model.api.interaction;

import com.evolveum.midpoint.common.LocalizationService;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.security.api.SecurityUtil;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DashboardWidgetType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DisplayType;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;

/**
Expand Down Expand Up @@ -56,12 +61,26 @@ public void setWidget(DashboardWidgetType widget) {

public String getLabel(LocalizationService localizationService) {
if(getDisplay() != null && getDisplay().getLabel() != null) {
return localizationService.translate(getDisplay().getLabel().toPolyString(), Locale.getDefault(), true);
return localizationService.translate(getDisplay().getLabel().toPolyString(), getLocale(), true);
} else {
return getWidget().getIdentifier();
}
}

@NotNull private Locale getLocale() {
Locale locale = null;
try {
MidPointPrincipal principal = SecurityUtil.getPrincipal();
if (principal != null) {
locale = principal.getLocale();
}
} catch (SecurityViolationException ex) {
// we can safely ignore this one, we only wanted locale if principal object is available
}

return locale != null ? locale : Locale.getDefault();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
Expand Up @@ -1727,12 +1727,35 @@ public TaskType submitTaskFromTemplate(String templateTaskOid, Map<QName, Object

@Override
public String translate(LocalizableMessage message) {
return localizationService.translate(message, Locale.getDefault());
return translate(message, true);
}

@Override
public String translate(LocalizableMessage message, boolean useDefaultLocale) {
Locale locale = findProperLocale(useDefaultLocale);

return localizationService.translate(message, locale);
}

@Override
public String translate(LocalizableMessageType message) {
return localizationService.translate(LocalizationUtil.toLocalizableMessage(message), Locale.getDefault());
return translate(message, true);
}

@Override
public String translate(LocalizableMessageType message, boolean useDefaultLocale) {
Locale locale = findProperLocale(useDefaultLocale);

return localizationService.translate(LocalizationUtil.toLocalizableMessage(message), locale);
}

@NotNull private Locale findProperLocale(boolean useDefaultLocale) {
if (useDefaultLocale) {
return Locale.getDefault();
}

MidPointPrincipal principal = SecurityUtil.getPrincipalSilent();
return principal != null ? principal.getLocale() : Locale.getDefault();
}

@Override
Expand Down
Expand Up @@ -8,18 +8,19 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;

import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.core.userdetails.UserDetails;

import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.ShortDumpable;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

/**
Expand All @@ -29,11 +30,12 @@
*
* @author Radovan Semancik
*/
public class MidPointPrincipal implements UserDetails, DebugDumpable, ShortDumpable {
public class MidPointPrincipal implements UserDetails, DebugDumpable, ShortDumpable {
private static final long serialVersionUID = 8299738301872077768L;

// Focus should not be final in case of session refresh, we need new focus object.
@NotNull private FocusType focus;
private Locale preferredLocale;
private Collection<Authorization> authorizations = new ArrayList<>();
private ActivationStatusType effectiveActivationStatus;
private SecurityPolicyType applicableSecurityPolicy;
Expand Down Expand Up @@ -111,7 +113,7 @@ public boolean isEnabled() {
} else {
effectiveActivationStatus = activation.getEffectiveStatus();
if (effectiveActivationStatus == null) {
throw new IllegalArgumentException("Null effective activation status in "+ focus);
throw new IllegalArgumentException("Null effective activation status in " + focus);
}
}
}
Expand Down Expand Up @@ -228,7 +230,7 @@ protected void debugDumpInternal(StringBuilder sb, int indent) {
DebugUtil.debugDumpWithLabelLn(sb, "Focus", focus.asPrismObject(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "Authorizations", authorizations, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "Delegators with other privilege limitations", delegatorWithOtherPrivilegesLimitationsCollection, indent + 1);
DebugUtil.debugDumpWithLabel(sb, "Attorney", attorney==null?null:attorney.asPrismObject(), indent + 1);
DebugUtil.debugDumpWithLabel(sb, "Attorney", attorney == null ? null : attorney.asPrismObject(), indent + 1);
}

@Override
Expand Down Expand Up @@ -259,4 +261,42 @@ public void shortDump(StringBuilder sb) {
sb.append("[").append(attorney).append("]");
}
}

/**
* Search for locale for this principal in multiple locations, returns first non-null item. Order of search:
*
* <ol>
* <li>{@link MidPointPrincipal#preferredLocale}</li>
* <li>{@link FocusType#getPreferredLanguage()}</li>
* <li>{@link FocusType#getLocale()}</li>
* <li>{@link Locale#getDefault()}</li>
* </ol>
*/
@NotNull
public Locale getLocale() {
Locale locale = getPreferredLocale();
if (locale != null) {
return locale;
}

locale = LocaleUtils.toLocale(focus.getPreferredLanguage());
if (locale != null) {
return locale;
}

locale = LocaleUtils.toLocale(focus.getLocale());
if (locale != null) {
return locale;
}

return Locale.getDefault();
}

public Locale getPreferredLocale() {
return preferredLocale;
}

public void setPreferredLocale(Locale preferredLocale) {
this.preferredLocale = preferredLocale;
}
}
Expand Up @@ -340,6 +340,14 @@ private static String getAddressFromHeader(String name, String value) {
return StringUtils.trim(split[0]);
}

public static MidPointPrincipal getPrincipalSilent() {
try {
return getPrincipal();
} catch (SecurityViolationException ex) {
return null;
}
}

/**
* Returns principal representing currently logged-in user. Returns null if the user is anonymous.
*/
Expand Down

0 comments on commit d774dde

Please sign in to comment.