package org.apereo.cas.config;

import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.web.security.CasJdbcUserDetailsManagerConfigurer;
import org.apereo.cas.web.security.CasLdapUserDetailsManagerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;

/**
 * This is {@link CasWebApplicationSecurityConfiguration}.
 *
 * @author Misagh Moayyed
 * @since 5.1.0
 */
@Configuration("casWebApplicationSecurityConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class CasWebApplicationSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private CasConfigurationProperties casProperties;

    @Override
    public void init(final AuthenticationManagerBuilder auth) throws Exception {
        if (StringUtils.isNotBlank(casProperties.getAdminPagesSecurity().getJdbc().getQuery())) {
            auth.apply(new CasJdbcUserDetailsManagerConfigurer(casProperties.getAdminPagesSecurity()));
        }
        if (StringUtils.isNotBlank(casProperties.getAdminPagesSecurity().getLdap().getBaseDn())
                && StringUtils.isNotBlank(casProperties.getAdminPagesSecurity().getLdap().getLdapUrl())
                && StringUtils.isNotBlank(casProperties.getAdminPagesSecurity().getLdap().getLdapAuthz().getRoleAttribute())
                && StringUtils.isNotBlank(casProperties.getAdminPagesSecurity().getLdap().getUserFilter())) {
            auth.apply(new CasLdapUserDetailsManagerConfigurer<>(casProperties.getAdminPagesSecurity()));
        }
    }

}

@@ -126,32 +126,206 @@
 tionProperties;%0A
+import org.apereo.cas.configuration.model.core.web.security.AdminPagesSecurityProperties;%0Aimport org.apereo.cas.configuration.model.support.ldap.LdapAuthorizationProperties;%0A
 import org.apere
@@ -1611,38 +1611,111 @@
     if (
-StringUtils.isNotBlank
+isLdapAuthorizationActive()) %7B%0A            auth.apply(new CasLdapUserDetailsManagerConfigurer%3C%3E
 (casProp
@@ -1748,31 +1748,11 @@
 ty()
-.getLdap().getBaseDn(
 ))
+;
 %0A   
@@ -1760,97 +1760,233 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-&& StringUtils.isNotBlank(casProperties.getAdminPagesSecurity().getLdap()
+private boolean isLdapAuthorizationActive() %7B%0A        final AdminPagesSecurityProperties.Ldap ldap = casProperties.getAdminPagesSecurity().getLdap();%0A        final LdapAuthorizationProperties authZ = ldap
 .getLdap
 Url(
@@ -1985,14 +1985,16 @@
 Ldap
-Url())
+Authz();
 %0A   
@@ -1994,34 +1994,30 @@
 ();%0A        
-        &&
+return
  StringUtils
@@ -2032,63 +2032,64 @@
 ank(
-casProperties.getAdminPagesSecurity().getL
+ldap.getBaseDn()) && StringUtils.isNotBlank(l
 dap
-()
 .getLdap
 Auth
@@ -2088,32 +2088,11 @@
 Ldap
-Authz().getRoleAttribute
+Url
 ())%0A
@@ -2137,55 +2137,12 @@
 ank(
-casProperties.getAdminPagesSecurity().getL
+l
 dap
-()
 .get
@@ -2154,20 +2154,18 @@
 ilter())
-) %7B
 %0A
+ 
         
@@ -2172,96 +2172,112 @@
     
-auth.apply(new CasLdapUserDetailsManagerConfigurer%3C%3E(casProperties.getAdminPagesSecurity
+   && (StringUtils.isNotBlank(authZ.getRoleAttribute()) %7C%7C StringUtils.isNotBlank(authZ.getRoleAttribute
 ()))
@@ -2282,23 +2282,12 @@
 ));%0A    
-    %7D%0A    %7D%0A
+%7D
 %0A%7D%0A

clean up ldap authZ config for webapp security