1
1
package io .kafbat .ui .service .rbac .extractor ;
2
2
3
- import io .kafbat .ui .config .auth .LdapProperties ;
4
3
import io .kafbat .ui .model .rbac .Role ;
5
4
import io .kafbat .ui .model .rbac .provider .Provider ;
6
5
import io .kafbat .ui .service .rbac .AccessControlService ;
7
- import java .util .List ;
8
- import java .util .Map ;
9
6
import java .util .Set ;
10
7
import java .util .stream .Collectors ;
11
8
import lombok .extern .slf4j .Slf4j ;
14
11
import org .springframework .ldap .core .support .BaseLdapPathContextSource ;
15
12
import org .springframework .security .core .GrantedAuthority ;
16
13
import org .springframework .security .core .authority .SimpleGrantedAuthority ;
17
- import org .springframework .security .ldap .userdetails .DefaultLdapAuthoritiesPopulator ;
18
- import org .springframework .util .Assert ;
14
+ import org .springframework .security .ldap .userdetails .NestedLdapAuthoritiesPopulator ;
19
15
20
16
@ Slf4j
21
- public class RbacLdapAuthoritiesExtractor extends DefaultLdapAuthoritiesPopulator {
17
+ public class RbacLdapAuthoritiesExtractor extends NestedLdapAuthoritiesPopulator {
22
18
23
19
private final AccessControlService acs ;
24
- private final LdapProperties props ;
25
20
26
21
public RbacLdapAuthoritiesExtractor (ApplicationContext context ,
27
22
BaseLdapPathContextSource contextSource , String groupFilterSearchBase ) {
28
23
super (contextSource , groupFilterSearchBase );
29
24
this .acs = context .getBean (AccessControlService .class );
30
- this .props = context .getBean (LdapProperties .class );
31
25
}
32
26
33
27
@ Override
34
28
protected Set <GrantedAuthority > getAdditionalRoles (DirContextOperations user , String username ) {
35
- var ldapGroups = getRoles (user .getNameInNamespace (), username );
29
+ var ldapGroups = super .getGroupMembershipRoles (user .getNameInNamespace (), username )
30
+ .stream ()
31
+ .map (GrantedAuthority ::getAuthority )
32
+ .peek (group -> log .trace ("Found LDAP group [{}] for user [{}]" , group , username ))
33
+ .collect (Collectors .toSet ());
36
34
37
35
return acs .getRoles ()
38
36
.stream ()
@@ -47,32 +45,4 @@ protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, St
47
45
.map (SimpleGrantedAuthority ::new )
48
46
.collect (Collectors .toSet ());
49
47
}
50
-
51
- private Set <String > getRoles (String userDn , String username ) {
52
- var groupSearchBase = props .getGroupFilterSearchBase ();
53
- Assert .notNull (groupSearchBase , "groupSearchBase is empty" );
54
-
55
- var groupRoleAttribute = props .getGroupRoleAttribute ();
56
- if (groupRoleAttribute == null ) {
57
-
58
- groupRoleAttribute = "cn" ;
59
- }
60
-
61
- log .trace (
62
- "Searching for roles for user [{}] with DN [{}], groupRoleAttribute [{}] and filter [{}] in search base [{}]" ,
63
- username , userDn , groupRoleAttribute , getGroupSearchFilter (), groupSearchBase );
64
-
65
- var ldapTemplate = getLdapTemplate ();
66
- ldapTemplate .setIgnoreNameNotFoundException (true );
67
-
68
- Set <Map <String , List <String >>> userRoles = ldapTemplate .searchForMultipleAttributeValues (
69
- groupSearchBase , getGroupSearchFilter (), new String [] {userDn , username },
70
- new String [] {groupRoleAttribute });
71
-
72
- return userRoles .stream ()
73
- .map (record -> record .get (getGroupRoleAttribute ()).get (0 ))
74
- .peek (group -> log .trace ("Found LDAP group [{}] for user [{}]" , group , username ))
75
- .collect (Collectors .toSet ());
76
- }
77
-
78
48
}
0 commit comments