This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch 4_2_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/4_2_X by this push:
     new ffada86bc5 [SYNCOPE-2008] Minimize manager Realms entitlements
ffada86bc5 is described below

commit ffada86bc5efab242e870d2b01aae4089d77381e
Author: Francesco Chicchiriccò <[email protected]>
AuthorDate: Thu Nov 5 13:54:34 2026 +0100

    [SYNCOPE-2008] Minimize manager Realms entitlements
---
 .../client/console/SyncopeConsoleSession.java      |  9 ++++--
 .../client/console/commons/RealmsUtils.java        |  8 ++---
 .../client/console/panels/RealmChoicePanel.java    |  7 ++---
 .../client/console/wizards/any/Details.java        | 22 +++++++-------
 .../apache/syncope/core/logic/UserSelfLogic.java   | 34 +++++++++++++++++++++-
 .../core/spring/security/AuthContextUtils.java     |  3 +-
 .../org/apache/syncope/fit/core/ManagerITCase.java | 18 +++++-------
 7 files changed, 64 insertions(+), 37 deletions(-)

diff --git 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleSession.java
 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleSession.java
index c9de08f95a..ab4dea7d2c 100644
--- 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleSession.java
+++ 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleSession.java
@@ -220,7 +220,7 @@ public class SyncopeConsoleSession extends 
AuthenticatedWebSession implements Ba
     public boolean isJWTExpiring() {
         return Optional.ofNullable(jwtExpiration).
                 map(je -> je.isBefore(Instant.now().
-                
plus(SyncopeWebApplication.get().getJwtExpirationMinutesThreshold(), 
ChronoUnit.MINUTES))).
+                        
plus(SyncopeWebApplication.get().getJwtExpirationMinutesThreshold(), 
ChronoUnit.MINUTES))).
                 orElse(false);
     }
 
@@ -261,7 +261,7 @@ public class SyncopeConsoleSession extends 
AuthenticatedWebSession implements Ba
                     flatMap(r -> 
Optional.ofNullable(r.getHeaderString(RESTHeaders.OWNED_ENTITLEMENTS))).
                     filter(IdRepoEntitlement.MUST_CHANGE_PASSWORD::equals).
                     map(code -> clientFactory.setDomain(getDomain()).
-                    create(new BasicAuthenticationHandler(username, 
password))).
+                            create(new BasicAuthenticationHandler(username, 
password))).
                     orElse(null);
             if (client != null) {
                 authenticated = true;
@@ -355,7 +355,10 @@ public class SyncopeConsoleSession extends 
AuthenticatedWebSession implements Ba
     }
 
     public List<String> getAuthRealms() {
-        return 
self.entitlements().values().stream().flatMap(Set::stream).distinct().sorted().toList();
+        return self.entitlements().values().stream().
+                flatMap(Set::stream).
+                map(RealmsUtils::getFullPath).
+                distinct().sorted().toList();
     }
 
     public List<String> getSearchableRealms() {
diff --git 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/commons/RealmsUtils.java
 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/commons/RealmsUtils.java
index 6a4375fd94..d68017860d 100644
--- 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/commons/RealmsUtils.java
+++ 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/commons/RealmsUtils.java
@@ -26,12 +26,8 @@ import org.apache.syncope.common.rest.api.beans.RealmQuery;
 
 public final class RealmsUtils {
 
-    public static String getFullPath(final String fullpath) {
-        String display = fullpath;
-        if (display.indexOf('@') != -1) {
-            display = StringUtils.substringBefore(display, "@");
-        }
-        return display;
+    public static String getFullPath(final String path) {
+        return StringUtils.substringBefore(path, "@");
     }
 
     public static boolean checkInput(final String input) {
diff --git 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/RealmChoicePanel.java
 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/RealmChoicePanel.java
index fb923999ff..443fe1b4c3 100644
--- 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/RealmChoicePanel.java
+++ 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/RealmChoicePanel.java
@@ -127,11 +127,10 @@ public class RealmChoicePanel extends Panel {
                             sorted(Comparator.comparing(Pair::getLeft));
                 } else {
                     full = map.entrySet().stream().
-                            map(el -> Pair.of(el.getKey(), 
el.getValue().getLeft()));
+                            map(pair -> Pair.of(pair.getKey(), 
pair.getValue().getLeft()));
                 }
-                return full.filter(realm -> 
SyncopeConsoleSession.get().getSearchableRealms().stream().anyMatch(
-                        r -> realm.getValue().getFullPath().startsWith(r))).
-                        collect(Collectors.toList());
+                return full.filter(realm -> 
SyncopeConsoleSession.get().getSearchableRealms().stream().
+                        anyMatch(r -> 
realm.getValue().getFullPath().startsWith(r))).toList();
             }
         };
 
diff --git 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/wizards/any/Details.java
 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/wizards/any/Details.java
index 89b09757ea..723b7e284e 100644
--- 
a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/wizards/any/Details.java
+++ 
b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/wizards/any/Details.java
@@ -70,23 +70,24 @@ public class Details<T extends AnyTO> extends WizardStep {
             final PageReference pageRef) {
 
         this.pageRef = pageRef;
-        final List<String> authRealms = 
SyncopeConsoleSession.get().getAuthRealms();
-        final T inner = wrapper.getInnerObject();
-        final Fragment fragment;
 
+        Fragment fragment;
         if (templateMode) {
             realm = new AjaxTextFieldPanel(
-                    "destinationRealm", "destinationRealm", new 
PropertyModel<>(inner, "realm"), false);
+                    "destinationRealm", "destinationRealm",
+                    new PropertyModel<>(wrapper.getInnerObject(), "realm"), 
false);
             AjaxTextFieldPanel.class.cast(realm).enableJexlHelp();
+
             fragment = new Fragment("realmsFragment", 
"realmsTemplateFragment", this);
         } else {
             boolean fullRealmsTree = 
SyncopeWebApplication.get().fullRealmsTree(realmRestClient);
-            AutoCompleteSettings settings = new AutoCompleteSettings();
-            settings.setShowCompleteListOnFocusGain(fullRealmsTree);
-            settings.setShowListOnEmptyInput(fullRealmsTree);
+            List<String> authRealms = 
SyncopeConsoleSession.get().getAuthRealms();
 
             realm = new AjaxSearchFieldPanel("destinationRealm", 
"destinationRealm",
-                    new PropertyModel<>(inner, "realm"), settings) {
+                    new PropertyModel<>(wrapper.getInnerObject(), "realm"),
+                    new AutoCompleteSettings().
+                            setShowCompleteListOnFocusGain(fullRealmsTree).
+                            setShowListOnEmptyInput(fullRealmsTree)) {
 
                 private static final long serialVersionUID = 
-6390474600233486704L;
 
@@ -99,7 +100,7 @@ public class Details<T extends AnyTO> extends WizardStep {
                                     : 
realmRestClient.search(RealmsUtils.buildNameQuery(input))).getResult()).
                             stream().map(RealmTO::getFullPath).
                             filter(fullPath -> authRealms.stream().anyMatch(
-                            authRealm -> 
fullPath.startsWith(authRealm))).iterator();
+                                    authRealm -> 
fullPath.startsWith(authRealm))).iterator();
                 }
             };
 
@@ -107,9 +108,8 @@ public class Details<T extends AnyTO> extends WizardStep {
         }
         fragment.addOrReplace(realm);
         addOrReplace(fragment);
-        add(getGeneralStatusInformation("generalStatusInformation", inner).
+        add(getGeneralStatusInformation("generalStatusInformation", 
wrapper.getInnerObject()).
                 
setEnabled(includeStatusPanel).setVisible(includeStatusPanel).setRenderBodyOnly(true));
-
     }
 
     public Details<T> disableRealmSpecification() {
diff --git 
a/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/UserSelfLogic.java
 
b/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/UserSelfLogic.java
index 2a0e9475f5..b8f05c75b6 100644
--- 
a/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/UserSelfLogic.java
+++ 
b/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/UserSelfLogic.java
@@ -21,6 +21,7 @@ package org.apache.syncope.core.logic;
 import java.time.OffsetDateTime;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -55,6 +56,8 @@ import org.apache.syncope.core.persistence.api.entity.Realm;
 import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
 import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
 import org.apache.syncope.core.persistence.api.entity.user.User;
+import org.apache.syncope.core.persistence.api.utils.RealmUtils.ManagerRealm;
+import 
org.apache.syncope.core.persistence.api.utils.RealmUtils.NormalizedRealms;
 import org.apache.syncope.core.provisioning.api.UserProvisioningManager;
 import org.apache.syncope.core.provisioning.api.data.UserDataBinder;
 import org.apache.syncope.core.provisioning.api.jexl.TemplateUtils;
@@ -64,6 +67,7 @@ import 
org.apache.syncope.core.spring.policy.AccountPolicyException;
 import org.apache.syncope.core.spring.policy.PasswordPolicyException;
 import org.apache.syncope.core.spring.security.AuthContextUtils;
 import org.apache.syncope.core.spring.security.SecurityProperties;
+import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
 import 
org.apache.syncope.core.spring.security.throttle.PasswordResetRequestThrottler;
 import org.apache.syncope.core.spring.security.throttle.ThrottlerAttempts;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -81,6 +85,8 @@ public class UserSelfLogic extends AbstractUserLogic {
         throw sce;
     }
 
+    protected final SecurityProperties securityProperties;
+
     protected final DelegationDAO delegationDAO;
 
     protected final AccessTokenDAO accessTokenDAO;
@@ -115,6 +121,7 @@ public class UserSelfLogic extends AbstractUserLogic {
                 provisioningManager,
                 encryptorManager,
                 confParamOps);
+        this.securityProperties = securityProperties;
         this.delegationDAO = delegationDAO;
         this.accessTokenDAO = accessTokenDAO;
         this.resourceDAO = resourceDAO;
@@ -131,9 +138,34 @@ public class UserSelfLogic extends AbstractUserLogic {
     public Self read() {
         UserTO authenticated = binder.getAuthenticatedUserTO();
 
+        Map<String, Set<String>> authorizations;
+        if 
(securityProperties.getAnonymousUser().equals(AuthContextUtils.getUsername())) {
+            authorizations = AuthContextUtils.getAuthorizations();
+        } else {
+            // group manager Realms together, to avoid unwanted enumeration of 
managed anys
+            authorizations = AuthContextUtils.getAuthorities().stream().
+                    map(authority -> {
+                        NormalizedRealms normalized = 
NormalizedRealms.of(authority.getRealms());
+
+                        SyncopeGrantedAuthority updated = new 
SyncopeGrantedAuthority(authority.getAuthority());
+                        updated.addRealms(normalized.realms());
+
+                        normalized.managerRealms().stream().
+                                map(ManagerRealm::of).
+                                flatMap(Optional::stream).
+                                filter(r -> 
!updated.getRealms().contains(r.realmPath())).
+                                map(r -> r.realmPath() + '@').
+                                forEach(updated::addRealm);
+
+                        return updated;
+                    }).
+                    filter(authority -> !authority.getRealms().isEmpty()).
+                    
collect(Collectors.toMap(SyncopeGrantedAuthority::getAuthority, 
SyncopeGrantedAuthority::getRealms));
+        }
+
         return new Self(
                 authenticated,
-                POJOHelper.serialize(AuthContextUtils.getAuthorizations()),
+                POJOHelper.serialize(authorizations),
                 
POJOHelper.serialize(delegationDAO.findValidDelegating(authenticated.getKey(), 
OffsetDateTime.now())));
     }
 
diff --git 
a/core/spring/src/main/java/org/apache/syncope/core/spring/security/AuthContextUtils.java
 
b/core/spring/src/main/java/org/apache/syncope/core/spring/security/AuthContextUtils.java
index 3e6bd7b02b..444e24e6a7 100644
--- 
a/core/spring/src/main/java/org/apache/syncope/core/spring/security/AuthContextUtils.java
+++ 
b/core/spring/src/main/java/org/apache/syncope/core/spring/security/AuthContextUtils.java
@@ -92,7 +92,8 @@ public final class AuthContextUtils {
                 map(authentication -> authentication.getAuthorities().stream().
                         filter(SyncopeGrantedAuthority.class::isInstance).
                         map(SyncopeGrantedAuthority.class::cast).
-                        
collect(Collectors.toMap(SyncopeGrantedAuthority::getAuthority,
+                        collect(Collectors.toMap(
+                                SyncopeGrantedAuthority::getAuthority,
                                 SyncopeGrantedAuthority::getRealms))).
                 orElseGet(Map::of);
     }
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/ManagerITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/ManagerITCase.java
index 9faa8bd6e6..e3db19551d 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/ManagerITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/ManagerITCase.java
@@ -27,6 +27,7 @@ import jakarta.ws.rs.core.Response;
 import java.util.Optional;
 import java.util.Set;
 import java.util.function.Consumer;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.client.lib.SyncopeClient;
 import org.apache.syncope.client.lib.SyncopeClient.Self;
 import org.apache.syncope.common.lib.SyncopeClientException;
@@ -39,12 +40,10 @@ import org.apache.syncope.common.lib.to.GroupTO;
 import org.apache.syncope.common.lib.to.MembershipTO;
 import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.UserTO;
-import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
 import org.apache.syncope.common.rest.api.beans.AnyQuery;
 import org.apache.syncope.common.rest.api.service.UserService;
-import org.apache.syncope.core.persistence.api.utils.RealmUtils;
 import org.apache.syncope.fit.AbstractITCase;
 import org.junit.jupiter.api.Test;
 
@@ -75,10 +74,8 @@ public class ManagerITCase extends AbstractITCase {
         Self self = managerClient.self();
         Set<String> managerRealms = 
self.entitlements().get(IdRepoEntitlement.USER_SEARCH);
         assertEquals(1, managerRealms.size());
-        RealmUtils.ManagerRealm realm = 
RealmUtils.ManagerRealm.of(managerRealms.iterator().next()).orElseThrow();
-        assertEquals(SyncopeConstants.ROOT_REALM, realm.realmPath());
-        assertEquals(AnyTypeKind.USER, realm.kind());
-        assertEquals(managedKey, realm.anyKey());
+        assertTrue(managerRealms.iterator().next().endsWith("@"));
+        assertEquals(SyncopeConstants.ROOT_REALM, 
StringUtils.substringBefore(managerRealms.iterator().next(), "@"));
 
         selfCheck.ifPresent(checker -> checker.accept(self));
 
@@ -152,11 +149,10 @@ public class ManagerITCase extends AbstractITCase {
                 Optional.of(self -> {
                     Set<String> managerRealms = 
self.entitlements().get(IdRepoEntitlement.GROUP_READ);
                     assertEquals(1, managerRealms.size());
-                    RealmUtils.ManagerRealm realm = 
RealmUtils.ManagerRealm.of(managerRealms.iterator().next()).
-                            orElseThrow();
-                    assertEquals(SyncopeConstants.ROOT_REALM, 
realm.realmPath());
-                    assertEquals(AnyTypeKind.GROUP, realm.kind());
-                    assertEquals(group.getKey(), realm.anyKey());
+                    assertTrue(managerRealms.iterator().next().endsWith("@"));
+                    assertEquals(
+                            SyncopeConstants.ROOT_REALM,
+                            
StringUtils.substringBefore(managerRealms.iterator().next(), "@"));
                 }));
     }
 

Reply via email to