瀏覽代碼

profile api refactoring

Jason Rivard 5 年之前
父節點
當前提交
c6cc54e6bf

+ 23 - 11
server/src/main/java/password/pwm/config/Configuration.java

@@ -93,6 +93,7 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeMap;
@@ -1077,26 +1078,37 @@ public class Configuration implements SettingReader
         final Map<String, Profile> returnMap = new LinkedHashMap<>();
         final Map<String, Profile> returnMap = new LinkedHashMap<>();
         for ( final String profileID : ProfileUtility.profileIDsForCategory( this, profileDefinition.getCategory() ) )
         for ( final String profileID : ProfileUtility.profileIDsForCategory( this, profileDefinition.getCategory() ) )
         {
         {
-            final Profile newProfile = newProfileForID( profileDefinition, profileID );
-            returnMap.put( profileID, newProfile );
+            if ( profileDefinition.getProfileFactoryClass().isPresent() )
+            {
+                final Profile newProfile = newProfileForID( profileDefinition, profileID );
+                returnMap.put( profileID, newProfile );
+            }
         }
         }
         return Collections.unmodifiableMap( returnMap );
         return Collections.unmodifiableMap( returnMap );
     }
     }
 
 
     private Profile newProfileForID( final ProfileDefinition profileDefinition, final String profileID )
     private Profile newProfileForID( final ProfileDefinition profileDefinition, final String profileID )
     {
     {
-        final Class<? extends Profile.ProfileFactory> profileFactoryClass = profileDefinition.getProfileFactoryClass();
+        Objects.requireNonNull( profileDefinition );
+        Objects.requireNonNull( profileID );
 
 
-        final Profile.ProfileFactory profileFactory;
-        try
-        {
-            profileFactory = profileFactoryClass.getDeclaredConstructor().newInstance();
-        }
-        catch ( final InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
+        final Optional<Class<? extends Profile.ProfileFactory>> optionalProfileFactoryClass = profileDefinition.getProfileFactoryClass();
+
+        if ( optionalProfileFactoryClass.isPresent() )
         {
         {
-            throw new IllegalStateException( "unable to create profile instance for " + profileDefinition );
+            final Profile.ProfileFactory profileFactory;
+            try
+            {
+                profileFactory = optionalProfileFactoryClass.get().getDeclaredConstructor().newInstance();
+                return profileFactory.makeFromStoredConfiguration( storedConfiguration, profileID );
+            }
+            catch ( final InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
+            {
+                throw new IllegalStateException( "unable to create profile instance for " + profileDefinition );
+            }
         }
         }
-        return profileFactory.makeFromStoredConfiguration( storedConfiguration, profileID );
+
+        throw new IllegalStateException( "unable to create profile instance for " + profileDefinition + " ( profile factory class not defined )" );
     }
     }
 
 
     public StoredConfiguration getStoredConfiguration( )
     public StoredConfiguration getStoredConfiguration( )

+ 7 - 1
server/src/main/java/password/pwm/config/profile/AbstractProfile.java

@@ -44,6 +44,7 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 
 
 public abstract class AbstractProfile implements Profile, SettingReader
 public abstract class AbstractProfile implements Profile, SettingReader
@@ -153,7 +154,12 @@ public abstract class AbstractProfile implements Profile, SettingReader
     @Override
     @Override
     public List<UserPermission> getPermissionMatches( )
     public List<UserPermission> getPermissionMatches( )
     {
     {
-        return readSettingAsUserPermission( profileType().getQueryMatch() );
+        final Optional<PwmSetting> optionalQueryMatchSetting = profileType().getQueryMatch();
+        if ( optionalQueryMatchSetting.isPresent() )
+        {
+            return readSettingAsUserPermission( optionalQueryMatchSetting.get() );
+        }
+        return Collections.emptyList();
     }
     }
 
 
     static Map<PwmSetting, StoredValue> makeValueMap(
     static Map<PwmSetting, StoredValue> makeValueMap(

+ 6 - 4
server/src/main/java/password/pwm/config/profile/ProfileDefinition.java

@@ -23,6 +23,8 @@ package password.pwm.config.profile;
 import password.pwm.config.PwmSetting;
 import password.pwm.config.PwmSetting;
 import password.pwm.config.PwmSettingCategory;
 import password.pwm.config.PwmSettingCategory;
 
 
+import java.util.Optional;
+
 public enum ProfileDefinition
 public enum ProfileDefinition
 {
 {
     Helpdesk(
     Helpdesk(
@@ -142,9 +144,9 @@ public enum ProfileDefinition
         return category;
         return category;
     }
     }
 
 
-    public PwmSetting getQueryMatch( )
+    public Optional<PwmSetting> getQueryMatch( )
     {
     {
-        return queryMatch;
+        return Optional.ofNullable( queryMatch );
     }
     }
 
 
     public Class<? extends Profile> getProfileImplClass()
     public Class<? extends Profile> getProfileImplClass()
@@ -152,8 +154,8 @@ public enum ProfileDefinition
         return profileImplClass;
         return profileImplClass;
     }
     }
 
 
-    public Class<? extends Profile.ProfileFactory> getProfileFactoryClass()
+    public Optional<Class<? extends Profile.ProfileFactory>> getProfileFactoryClass()
     {
     {
-        return profileFactoryClass;
+        return Optional.ofNullable( profileFactoryClass );
     }
     }
 }
 }

+ 6 - 7
server/src/main/java/password/pwm/config/profile/ProfileUtility.java

@@ -41,15 +41,14 @@ public class ProfileUtility
 {
 {
     private static final PwmLogger LOGGER = PwmLogger.forClass( ProfileUtility.class );
     private static final PwmLogger LOGGER = PwmLogger.forClass( ProfileUtility.class );
 
 
-    public static Optional<String> discoverProfileIDforUser(
+    public static Optional<String> discoverProfileIDForUser(
             final CommonValues commonValues,
             final CommonValues commonValues,
             final UserIdentity userIdentity,
             final UserIdentity userIdentity,
             final ProfileDefinition profileDefinition
             final ProfileDefinition profileDefinition
     )
     )
             throws PwmUnrecoverableException
             throws PwmUnrecoverableException
     {
     {
-        final String profileID = discoverProfileIDforUser( commonValues.getPwmApplication(), commonValues.getSessionLabel(), userIdentity, profileDefinition );
-        return Optional.ofNullable( profileID );
+        return discoverProfileIDForUser( commonValues.getPwmApplication(), commonValues.getSessionLabel(), userIdentity, profileDefinition );
     }
     }
 
 
     public static <T extends Profile> T profileForUser(
     public static <T extends Profile> T profileForUser(
@@ -60,7 +59,7 @@ public class ProfileUtility
     )
     )
             throws PwmUnrecoverableException
             throws PwmUnrecoverableException
     {
     {
-        final Optional<String> profileID = discoverProfileIDforUser( commonValues, userIdentity, profileDefinition );
+        final Optional<String> profileID = discoverProfileIDForUser( commonValues, userIdentity, profileDefinition );
         if ( !profileID.isPresent() )
         if ( !profileID.isPresent() )
         {
         {
             throw PwmUnrecoverableException.newException( PwmError.ERROR_NO_PROFILE_ASSIGNED, "profile of type " + profileDefinition + " is required but not assigned" );
             throw PwmUnrecoverableException.newException( PwmError.ERROR_NO_PROFILE_ASSIGNED, "profile of type " + profileDefinition + " is required but not assigned" );
@@ -70,7 +69,7 @@ public class ProfileUtility
     }
     }
 
 
 
 
-    public static String discoverProfileIDforUser(
+    public static Optional<String> discoverProfileIDForUser(
             final PwmApplication pwmApplication,
             final PwmApplication pwmApplication,
             final SessionLabel sessionLabel,
             final SessionLabel sessionLabel,
             final UserIdentity userIdentity,
             final UserIdentity userIdentity,
@@ -85,10 +84,10 @@ public class ProfileUtility
             final boolean match = LdapPermissionTester.testUserPermissions( pwmApplication, sessionLabel, userIdentity, queryMatches );
             final boolean match = LdapPermissionTester.testUserPermissions( pwmApplication, sessionLabel, userIdentity, queryMatches );
             if ( match )
             if ( match )
             {
             {
-                return profile.getIdentifier();
+                return Optional.of( profile.getIdentifier() );
             }
             }
         }
         }
-        return null;
+        return Optional.empty();
     }
     }
 
 
     public static List<String> profileIDsForCategory( final Configuration configuration, final PwmSettingCategory pwmSettingCategory )
     public static List<String> profileIDsForCategory( final Configuration configuration, final PwmSettingCategory pwmSettingCategory )

+ 1 - 1
server/src/main/java/password/pwm/http/servlet/activation/ActivateUserUtils.java

@@ -342,7 +342,7 @@ class ActivateUserUtils
         final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
         final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
         final ActivateUserBean activateUserBean = pwmApplication.getSessionStateService().getBean( pwmRequest, ActivateUserBean.class );
         final ActivateUserBean activateUserBean = pwmApplication.getSessionStateService().getBean( pwmRequest, ActivateUserBean.class );
 
 
-        final Optional<String> profileID = ProfileUtility.discoverProfileIDforUser( pwmRequest.commonValues(), userIdentity, ProfileDefinition.ActivateUser );
+        final Optional<String> profileID = ProfileUtility.discoverProfileIDForUser( pwmRequest.commonValues(), userIdentity, ProfileDefinition.ActivateUser );
 
 
         if ( !profileID.isPresent() || !pwmApplication.getConfig().getUserActivationProfiles().containsKey( profileID.get() ) )
         if ( !profileID.isPresent() || !pwmApplication.getConfig().getUserActivationProfiles().containsKey( profileID.get() ) )
         {
         {

+ 8 - 7
server/src/main/java/password/pwm/http/servlet/admin/UserDebugDataReader.java

@@ -43,6 +43,7 @@ import password.pwm.util.macro.MacroMachine;
 import password.pwm.util.password.PasswordUtility;
 import password.pwm.util.password.PasswordUtility;
 
 
 import java.util.Collections;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map;
@@ -138,23 +139,23 @@ public class UserDebugDataReader
             final PwmApplication pwmApplication,
             final PwmApplication pwmApplication,
             final SessionLabel sessionLabel,
             final SessionLabel sessionLabel,
             final UserIdentity userIdentity
             final UserIdentity userIdentity
-    ) throws PwmUnrecoverableException
+    )
+        throws PwmUnrecoverableException
     {
     {
-        final Map<ProfileDefinition, String> results = new TreeMap<>();
+        final Map<ProfileDefinition, String> results = new TreeMap<>( Comparator.comparing( Enum::name ) );
         for ( final ProfileDefinition profileDefinition : ProfileDefinition.values() )
         for ( final ProfileDefinition profileDefinition : ProfileDefinition.values() )
         {
         {
-            if ( profileDefinition.isAuthenticated() )
+            if ( profileDefinition.getQueryMatch().isPresent() && profileDefinition.getProfileFactoryClass().isPresent() )
             {
             {
-                final String id = ProfileUtility.discoverProfileIDforUser(
+                ProfileUtility.discoverProfileIDForUser(
                         pwmApplication,
                         pwmApplication,
                         sessionLabel,
                         sessionLabel,
                         userIdentity,
                         userIdentity,
                         profileDefinition
                         profileDefinition
-                );
-
-                results.put( profileDefinition, id );
+                ).ifPresent( ( id ) -> results.put( profileDefinition, id ) );
             }
             }
         }
         }
+
         return Collections.unmodifiableMap( results );
         return Collections.unmodifiableMap( results );
     }
     }
 
 

+ 10 - 9
server/src/main/java/password/pwm/http/servlet/forgottenpw/ForgottenPasswordUtil.java

@@ -83,6 +83,7 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 
 
 public class ForgottenPasswordUtil
 public class ForgottenPasswordUtil
@@ -630,20 +631,20 @@ public class ForgottenPasswordUtil
     )
     )
             throws PwmUnrecoverableException
             throws PwmUnrecoverableException
     {
     {
-        final String forgottenProfileID = ProfileUtility.discoverProfileIDforUser(
-                pwmApplication,
-                sessionLabel,
-                userIdentity,
-                ProfileDefinition.ForgottenPassword
+        final Optional<String> profileID = ProfileUtility.discoverProfileIDForUser(
+            pwmApplication,
+            sessionLabel,
+            userIdentity,
+            ProfileDefinition.ForgottenPassword
         );
         );
 
 
-        if ( StringUtil.isEmpty( forgottenProfileID ) )
+        if ( profileID.isPresent() )
         {
         {
-            final String msg = "user does not have a forgotten password profile assigned";
-            throw PwmUnrecoverableException.newException( PwmError.ERROR_NO_PROFILE_ASSIGNED, msg );
+            return pwmApplication.getConfig().getForgottenPasswordProfiles().get( profileID.get() );
         }
         }
 
 
-        return pwmApplication.getConfig().getForgottenPasswordProfiles().get( forgottenProfileID );
+        final String msg = "user does not have a forgotten password profile assigned";
+        throw PwmUnrecoverableException.newException( PwmError.ERROR_NO_PROFILE_ASSIGNED, msg );
     }
     }
 
 
     static ForgottenPasswordProfile forgottenPasswordProfile(
     static ForgottenPasswordProfile forgottenPasswordProfile(

+ 3 - 3
server/src/main/java/password/pwm/ldap/UserInfoReader.java

@@ -650,10 +650,10 @@ public class UserInfoReader implements UserInfo
         {
         {
             if ( profileDefinition.isAuthenticated() )
             if ( profileDefinition.isAuthenticated() )
             {
             {
-                final String profileID = ProfileUtility.discoverProfileIDforUser( pwmApplication, sessionLabel, userIdentity, profileDefinition );
-                returnMap.put( profileDefinition, profileID );
-                if ( profileID != null )
+                final Optional<String> profileID = ProfileUtility.discoverProfileIDForUser( pwmApplication, sessionLabel, userIdentity, profileDefinition );
+                if ( profileID.isPresent() )
                 {
                 {
+                    returnMap.put( profileDefinition, profileID.get() );
                     LOGGER.debug( sessionLabel, () -> "assigned " + profileDefinition.toString() + " profileID \"" + profileID + "\" to " + userIdentity.toDisplayString() );
                     LOGGER.debug( sessionLabel, () -> "assigned " + profileDefinition.toString() + " profileID \"" + profileID + "\" to " + userIdentity.toDisplayString() );
                 }
                 }
                 else
                 else

+ 8 - 7
server/src/main/java/password/pwm/util/password/PasswordUtility.java

@@ -99,6 +99,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map;
+import java.util.Optional;
 
 
 /**
 /**
  * @author Jason D. Rivard
  * @author Jason D. Rivard
@@ -597,20 +598,20 @@ public class PasswordUtility
         final boolean sendPassword = helpdeskProfile.readSettingAsBoolean( PwmSetting.HELPDESK_SEND_PASSWORD );
         final boolean sendPassword = helpdeskProfile.readSettingAsBoolean( PwmSetting.HELPDESK_SEND_PASSWORD );
         if ( sendPassword )
         if ( sendPassword )
         {
         {
-            final MessageSendMethod messageSendMethod;
+            final Optional<String> profileID = ProfileUtility.discoverProfileIDForUser( pwmApplication, sessionLabel, userIdentity, ProfileDefinition.ForgottenPassword );
+            if ( profileID.isPresent() )
             {
             {
-                final String profileID = ProfileUtility.discoverProfileIDforUser( pwmApplication, sessionLabel, userIdentity, ProfileDefinition.ForgottenPassword );
-                final ForgottenPasswordProfile forgottenPasswordProfile = pwmApplication.getConfig().getForgottenPasswordProfiles().get( profileID );
-                messageSendMethod = forgottenPasswordProfile.readSettingAsEnum( PwmSetting.RECOVERY_SENDNEWPW_METHOD, MessageSendMethod.class );
+                final ForgottenPasswordProfile forgottenPasswordProfile = pwmApplication.getConfig().getForgottenPasswordProfiles().get( profileID.get() );
+                final MessageSendMethod messageSendMethod = forgottenPasswordProfile.readSettingAsEnum( PwmSetting.RECOVERY_SENDNEWPW_METHOD, MessageSendMethod.class );
 
 
-            }
-            PasswordUtility.sendNewPassword(
+                PasswordUtility.sendNewPassword(
                     userInfo,
                     userInfo,
                     pwmApplication,
                     pwmApplication,
                     newPassword,
                     newPassword,
                     pwmRequest.getLocale(),
                     pwmRequest.getLocale(),
                     messageSendMethod
                     messageSendMethod
-            );
+                );
+            }
         }
         }
     }
     }
 
 

+ 21 - 27
server/src/main/java/password/pwm/ws/server/rest/RestProfileServer.java

@@ -46,7 +46,6 @@ import password.pwm.svc.stats.Statistic;
 import password.pwm.svc.stats.StatisticsManager;
 import password.pwm.svc.stats.StatisticsManager;
 import password.pwm.util.FormMap;
 import password.pwm.util.FormMap;
 import password.pwm.util.form.FormUtility;
 import password.pwm.util.form.FormUtility;
-import password.pwm.util.java.StringUtil;
 import password.pwm.util.macro.MacroMachine;
 import password.pwm.util.macro.MacroMachine;
 import password.pwm.ws.server.RestMethodHandler;
 import password.pwm.ws.server.RestMethodHandler;
 import password.pwm.ws.server.RestRequest;
 import password.pwm.ws.server.RestRequest;
@@ -62,6 +61,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 
 
 @WebServlet(
 @WebServlet(
@@ -122,19 +122,7 @@ public class RestProfileServer extends RestServlet
     {
     {
         final TargetUserIdentity targetUserIdentity = RestUtility.resolveRequestedUsername( restRequest, username );
         final TargetUserIdentity targetUserIdentity = RestUtility.resolveRequestedUsername( restRequest, username );
 
 
-        final String updateProfileID = ProfileUtility.discoverProfileIDforUser(
-                restRequest.getPwmApplication(),
-                restRequest.getSessionLabel(),
-                targetUserIdentity.getUserIdentity(),
-                ProfileDefinition.UpdateAttributes
-        );
-
-        if ( StringUtil.isEmpty( updateProfileID ) )
-        {
-            throw new PwmUnrecoverableException( PwmError.ERROR_NO_PROFILE_ASSIGNED );
-        }
-
-        final UpdateProfileProfile updateProfileProfile = restRequest.getPwmApplication().getConfig().getUpdateAttributesProfile().get( updateProfileID );
+        final UpdateProfileProfile updateProfileProfile = getProfile( restRequest, targetUserIdentity );
 
 
         final Map<String, String> profileData = new HashMap<>();
         final Map<String, String> profileData = new HashMap<>();
         {
         {
@@ -187,6 +175,24 @@ public class RestProfileServer extends RestServlet
         }
         }
     }
     }
 
 
+    private static UpdateProfileProfile getProfile( final RestRequest restRequest, final TargetUserIdentity targetUserIdentity )
+        throws PwmUnrecoverableException
+    {
+        final Optional<String> updateProfileID = ProfileUtility.discoverProfileIDForUser(
+            restRequest.getPwmApplication(),
+            restRequest.getSessionLabel(),
+            targetUserIdentity.getUserIdentity(),
+            ProfileDefinition.UpdateAttributes
+        );
+
+        if ( !updateProfileID.isPresent() )
+        {
+            throw new PwmUnrecoverableException( PwmError.ERROR_NO_PROFILE_ASSIGNED );
+        }
+
+        return restRequest.getPwmApplication().getConfig().getUpdateAttributesProfile().get( updateProfileID.get() );
+    }
+
     private static RestResultBean doPostProfileDataImpl(
     private static RestResultBean doPostProfileDataImpl(
             final RestRequest restRequest,
             final RestRequest restRequest,
             final JsonProfileData jsonInput
             final JsonProfileData jsonInput
@@ -201,19 +207,7 @@ public class RestProfileServer extends RestServlet
 
 
         final TargetUserIdentity targetUserIdentity = RestUtility.resolveRequestedUsername( restRequest, username );
         final TargetUserIdentity targetUserIdentity = RestUtility.resolveRequestedUsername( restRequest, username );
 
 
-        final String updateProfileID = ProfileUtility.discoverProfileIDforUser(
-                restRequest.getPwmApplication(),
-                restRequest.getSessionLabel(),
-                targetUserIdentity.getUserIdentity(),
-                ProfileDefinition.UpdateAttributes
-        );
-
-        if ( StringUtil.isEmpty( updateProfileID ) )
-        {
-            throw new PwmUnrecoverableException( PwmError.ERROR_NO_PROFILE_ASSIGNED );
-        }
-
-        final UpdateProfileProfile updateProfileProfile = restRequest.getPwmApplication().getConfig().getUpdateAttributesProfile().get( updateProfileID );
+        final UpdateProfileProfile updateProfileProfile = getProfile( restRequest, targetUserIdentity );
 
 
         {
         {
             final List<UserPermission> userPermission = updateProfileProfile.readSettingAsUserPermission( PwmSetting.UPDATE_PROFILE_QUERY_MATCH );
             final List<UserPermission> userPermission = updateProfileProfile.readSettingAsUserPermission( PwmSetting.UPDATE_PROFILE_QUERY_MATCH );