2626import  com .walmartlabs .concord .server .security .Roles ;
2727import  com .walmartlabs .concord .server .security .UnauthorizedException ;
2828import  com .walmartlabs .concord .server .security .UserPrincipal ;
29- import  com .walmartlabs .concord .server .security . ldap . LdapPrincipal ;
30- import  com .walmartlabs .concord .server .security . ldap . LdapUserInfoProvider ;
29+ import  com .walmartlabs .concord .server .user . UserInfoProvider ;
30+ import  com .walmartlabs .concord .server .user . UserManager ;
3131import  io .takari .bpm .form .Form ;
3232
3333import  javax .inject .Inject ;
3838import  java .util .Map ;
3939import  java .util .Optional ;
4040import  java .util .Set ;
41- import  java .util .function .Supplier ;
4241import  java .util .regex .Matcher ;
4342import  java .util .regex .Pattern ;
4443
@@ -49,12 +48,12 @@ public class FormAccessManager {
4948    private  static  final  Pattern  GROUP_PATTERN  = Pattern .compile ("CN=(.*?)," , Pattern .CASE_INSENSITIVE );
5049
5150    private  final  ProcessStateManager  stateManager ;
52-     private  final  LdapUserInfoProvider   ldapUserInfoProvider ;
51+     private  final  UserManager   userManager ;
5352
5453    @ Inject 
55-     public  FormAccessManager (ProcessStateManager  stateManager , LdapUserInfoProvider   ldapUserInfoProvider ) {
54+     public  FormAccessManager (ProcessStateManager  stateManager , UserManager   userManager ) {
5655        this .stateManager  = stateManager ;
57-         this .ldapUserInfoProvider  = ldapUserInfoProvider ;
56+         this .userManager  = userManager ;
5857    }
5958
6059    @ SuppressWarnings ("unchecked" )
@@ -93,37 +92,22 @@ public void assertFormAccess(String formName, Map<String, Serializable> runAsPar
9392                    "the necessary permissions to access the form." );
9493        }
9594
96-         Set <String > groups  = com .walmartlabs .concord .forms .FormUtils .getRunAsLdapGroups (formName , runAsParams );
97-         if  (!groups .isEmpty ()) {
98-             Set <String > userLdapGroups  = getLdapPrincipalGroups (p , ldapUserInfoProvider , LdapPrincipal ::getCurrent );
95+         Set <String > formRunAsGroups  = com .walmartlabs .concord .forms .FormUtils .getRunAsLdapGroups (formName , runAsParams );
96+         if  (!formRunAsGroups .isEmpty ()) {
97+             Set <String > userLdapGroups  = Optional .ofNullable (userManager .getCurrentUserInfo ())
98+                     .map (UserInfoProvider .UserInfo ::groups )
99+                     .orElseGet (Set ::of );
99100
100-             boolean  isGroupMatched  = groups .stream ()
101+             boolean  isGroupMatched  = formRunAsGroups .stream ()
101102                    .anyMatch (group  -> matchesLdapGroup (group , userLdapGroups ));
102103
103104            if  (!isGroupMatched ) {
104105                throw  new  UnauthorizedException ("The current user ("  + p .getUsername () + ") doesn't have "  +
105-                         "the necessary permissions to resume process. Expected LDAP group(s) '"  + groups  + "'" );
106+                         "the necessary permissions to resume process. Expected LDAP group(s) '"  + formRunAsGroups  + "'" );
106107            }
107108        }
108109    }
109110
110-     static  Set <String > getLdapPrincipalGroups (UserPrincipal  p ,
111-                                               LdapUserInfoProvider  ldapUserInfoProvider ,
112-                                               Supplier <LdapPrincipal > currentPrincipalSupplier ) {
113-         if  (p  == null ) {
114-             return  Set .of ();
115-         }
116- 
117-         if  (p .getRealm ().equals ("apikey" )) {
118-             // apikey realm doesn't look up groups by default, get them now 
119-             return  ldapUserInfoProvider .getInfo (null , p .getUsername (), p .getDomain ()).groups ();
120-         }
121- 
122-         return  Optional .ofNullable (currentPrincipalSupplier .get ())
123-                 .map (LdapPrincipal ::getGroups )
124-                 .orElseGet (Set ::of );
125-     }
126- 
127111    // TODO: move to the formManager 
128112    private  Form  getForm (ProcessKey  processKey , String  formName ) {
129113        String  resource  = path (Constants .Files .JOB_ATTACHMENTS_DIR_NAME ,
0 commit comments