@@ -355,32 +355,11 @@ protected UserProfileMetadata decorateUserProfileForCache(UserProfileMetadata de
355355 }
356356
357357 if (UserModel .USERNAME .equals (attributeName )) {
358- required = new Predicate <AttributeContext >() {
359- @ Override
360- public boolean test (AttributeContext context ) {
361- RealmModel realm = context .getSession ().getContext ().getRealm ();
362- return !realm .isRegistrationEmailAsUsername ();
363- }
364- };
358+ required = new UsernameRequiredPredicate ();
365359 }
366360
367361 if (UserModel .EMAIL .equals (attributeName )) {
368- Predicate <AttributeContext > requiredFromConfig = required ;
369- required = new Predicate <AttributeContext >() {
370- @ Override
371- public boolean test (AttributeContext context ) {
372- UserModel user = context .getUser ();
373-
374- if (isServiceAccountUser (user )) {
375- return false ;
376- }
377-
378- if (requiredFromConfig .test (context )) return true ;
379-
380- RealmModel realm = context .getSession ().getContext ().getRealm ();
381- return realm .isRegistrationEmailAsUsername ();
382- }
383- };
362+ required = new EmailRequiredPredicate (required );
384363 }
385364
386365 List <AttributeMetadata > existingMetadata = decoratedMetadata .getAttribute (attributeName );
@@ -439,7 +418,7 @@ private Predicate<AttributeContext> createViewAllowedPredicate(Predicate<Attribu
439418 return ac -> ac .getContext ().isRoleForContext (viewRoles ) || canEdit .test (ac );
440419 }
441420
442- private boolean isServiceAccountUser (UserModel user ) {
421+ private static boolean isServiceAccountUser (UserModel user ) {
443422 return user != null && user .getServiceAccountClientLink () != null ;
444423 }
445424
@@ -552,4 +531,34 @@ private Function<UserProfileContext, UserProfileMetadata> createUserDefinedProfi
552531 return decorateUserProfileForCache (decoratedMetadata , parsedConfig );
553532 };
554533 }
534+
535+ private static class EmailRequiredPredicate implements Predicate <AttributeContext > {
536+ private final Predicate <AttributeContext > required ;
537+
538+ public EmailRequiredPredicate (Predicate <AttributeContext > required ) {
539+ this .required = required ;
540+ }
541+
542+ @ Override
543+ public boolean test (AttributeContext context ) {
544+ UserModel user = context .getUser ();
545+
546+ if (isServiceAccountUser (user )) {
547+ return false ;
548+ }
549+
550+ if (required .test (context )) return true ;
551+
552+ RealmModel realm = context .getSession ().getContext ().getRealm ();
553+ return realm .isRegistrationEmailAsUsername ();
554+ }
555+ }
556+
557+ private static class UsernameRequiredPredicate implements Predicate <AttributeContext > {
558+ @ Override
559+ public boolean test (AttributeContext context ) {
560+ RealmModel realm = context .getSession ().getContext ().getRealm ();
561+ return !realm .isRegistrationEmailAsUsername ();
562+ }
563+ }
555564}
0 commit comments