<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to Apereo under one or more contributor license agreements. 
	See the NOTICE file distributed with this work for additional information 
	regarding copyright ownership. Apereo licenses this file to you under the 
	Apache License, Version 2.0 (the "License"); you may not use this file except 
	in compliance with the License. You may obtain a copy of the License at the 
	following location: http://www.apache.org/licenses/LICENSE-2.0 Unless required 
	by applicable law or agreed to in writing, software distributed under the 
	License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
	OF ANY KIND, either express or implied. See the License for the specific 
	language governing permissions and limitations under the License. -->
<!-- | deployerConfigContext.xml centralizes into one file some of the declarative 
	configuration that | all CAS deployers will need to modify. | | This file 
	declares some of the Spring-managed JavaBeans that make up a CAS deployment. 
	| The beans declared in this file are instantiated at context initialization 
	time by the Spring | ContextLoaderListener declared in web.xml. It finds 
	this file because this | file is among those declared in the context parameter 
	"contextConfigLocation". | | By far the most common change you will need 
	to make in this file is to change the last bean | declaration to replace 
	the default authentication handler with | one implementing your approach 
	for authenticating usernames and passwords. + -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- | The authentication manager defines security policy for authentication
        by specifying at a minimum | the authentication handlers that will be used
        to authenticate credential. While the AuthenticationManager | interface supports
        plugging in another implementation, the default PolicyBasedAuthenticationManager
        should | be sufficient in most cases. + -->
    <bean id="authenticationManager"
          class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"
          p:authenticationPolicy-ref="authenticationPolicy">
        <constructor-arg>
            <map>
                <entry key-ref="ldapAuthenticationHandler" value="#{null}"/>
                <entry key-ref="dbAuthenticationHandler" value="#{null}"/>
                <entry key-ref="propAuthenticationHandler" value="#{null}"/>
            </map>
        </constructor-arg>
    </bean>

    <bean id="authenticationPolicy" class="org.jasig.cas.authentication.AnyAuthenticationPolicy"/>

    <!-- | TODO: Replace this component with one suitable for your enviroment.
        | | This component provides authentication for the kind of credential used
        in your environment. In most cases | credential is a username/password pair
        that lives in a system of record like an LDAP directory. | The most common
        authentication handler beans: | | * org.jasig.cas.authentication.LdapAuthenticationHandler
        | * org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler | * org.jasig.cas.adaptors.x509.authentication.handler.support.X509CredentialsAuthenticationHandler
        | * org.jasig.cas.support.spnego.authentication.handler.support.JCIFSSpnegoAuthenticationHandler -->

    <!-- Normal login tanımları -->
    <bean id="propAuthenticationHandler"
          class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
        <property name="users">
            <map>
                <entry key="mstuser" value="etr"/>
            </map>
        </property>
    </bean>

    <!-- Database login tanımları -->
    <bean id="dbPasswordEncoder"
          class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"
          c:encodingAlgorithm="MD5" p:characterEncoding="UTF-8"/>

    <bean id="dbDataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.sybase.jdbc4.jdbc.SybDriver"/>
        <property name="url" value="jdbc:sybase:Tds:192.168.10.21:4901/PRD?ENCRYPT_PASSWORD=true"/>
        <property name="username" value="prd_atomic_user"/>
        <property name="password" value="A3vt4d28b9"/>
    </bean>

    <bean id="dbAuthenticationHandler"
          class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"
          p:passwordEncoder-ref="dbPasswordEncoder" p:dataSource-ref="dbDataSource"
          p:sql="SELECT USPS AS password FROM SAPSR3.[/ATOM/US00] WHERE LOWER(USID) = LOWER(?)"/>

    <!-- Ldap doğrulama tanımları -->
    <bean id="ldapAuthenticationHandler" class="org.jasig.cas.authentication.LdapAuthenticationHandler"
          p:principalIdAttribute="sAMAccountName" c:authenticator-ref="authenticator">
        <property name="principalAttributeMap">
            <map>
                <!-- | This map provides a simple attribute resolution mechanism. | Keys
                    are LDAP attribute names, values are CAS attribute names. | Use this facility
                    instead of a PrincipalResolver if LDAP is | the only attribute source. -->
                <entry key="givenName" value="givenName"/>
                <entry key="mail" value="mail"/>
                <entry key="sn" value="sn"/>
                <entry key="sAMAccountName" value="sAMAccountName"/>
            </map>
        </property>
    </bean>

    <bean id="authenticator" class="org.ldaptive.auth.Authenticator"
          c:resolver-ref="dnResolver" c:handler-ref="authHandler"
          p:entryResolver-ref="entryResolver">
        <property name="authenticationResponseHandlers">
            <list>
                <bean
                        class="org.ldaptive.auth.ext.ActiveDirectoryAuthenticationResponseHandler"/>
            </list>
        </property>
    </bean>

    <!-- Active Directory UPN format. -->
    <bean id="dnResolver" class="org.ldaptive.auth.FormatDnResolver"
          c:format="%s@${ldap.domain}"/>

    <bean id="authHandler" class="org.ldaptive.auth.PooledBindAuthenticationHandler"
          p:connectionFactory-ref="pooledLdapConnectionFactory"/>

    <bean id="pooledLdapConnectionFactory" class="org.ldaptive.pool.PooledConnectionFactory"
          p:connectionPool-ref="connectionPool"/>

    <bean id="connectionPool" class="org.ldaptive.pool.BlockingConnectionPool"
          init-method="initialize" p:poolConfig-ref="ldapPoolConfig"
          p:blockWaitTime="${ldap.pool.blockWaitTime}" p:validator-ref="searchValidator"
          p:pruneStrategy-ref="pruneStrategy" p:connectionFactory-ref="connectionFactory"/>

    <bean id="ldapPoolConfig" class="org.ldaptive.pool.PoolConfig"
          p:minPoolSize="${ldap.pool.minSize}" p:maxPoolSize="${ldap.pool.maxSize}"
          p:validateOnCheckOut="${ldap.pool.validateOnCheckout}"
          p:validatePeriodically="${ldap.pool.validatePeriodically}"
          p:validatePeriod="${ldap.pool.validatePeriod}"/>

    <bean id="connectionFactory" class="org.ldaptive.DefaultConnectionFactory"
          p:connectionConfig-ref="connectionConfig"/>

    <bean id="connectionConfig" class="org.ldaptive.ConnectionConfig"
          p:ldapUrl="${ldap.url}" p:connectTimeout="${ldap.connectTimeout}"
          p:useStartTLS="${ldap.useStartTLS}" p:sslConfig-ref="sslConfig"/>

    <bean id="sslConfig" class="org.ldaptive.ssl.SslConfig">
        <property name="credentialConfig">
            <bean class="org.ldaptive.ssl.X509CredentialConfig"
                  p:trustCertificates="${ldap.trustedCert}"/>
        </property>
    </bean>

    <bean id="pruneStrategy" class="org.ldaptive.pool.IdlePruneStrategy"
          p:prunePeriod="${ldap.pool.prunePeriod}" p:idleTime="${ldap.pool.idleTime}"/>

    <bean id="searchValidator" class="org.ldaptive.pool.SearchValidator"/>

    <!-- If you wish to search by user, rather than by dn, change {dn} to {user} -->
    <bean id="entryResolver" class="org.ldaptive.auth.SearchEntryResolver"
          p:baseDn="${ldap.authn.baseDn}" p:userFilter="userPrincipalName={dn}"
          p:subtreeSearch="true"/>


    <!-- Sample, in-memory data store for the ServiceRegistry. A real implementation
        would probably want to replace this with the JPA-backed ServiceRegistry DAO
        The name of this bean should remain "serviceRegistryDao". -->
    <!--<bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
        <property name="registeredServices">
            <list>
                <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                    <property name="id" value="0" />
                    <property name="name" value="HTTP" />
                    <property name="description" value="Detaysoft" />
                    <property name="serviceId" value="http://www.detaysoft.com" />
                    <property name="evaluationOrder" value="10000001" />
                </bean>
            </list>
        </property>
    </bean>-->

    <!-- JPA MySQL Configuration-->
    <bean
            id="dataSource"
            class="com.mchange.v2.c3p0.ComboPooledDataSource"
            p:driverClass="com.mysql.jdbc.Driver"
            p:jdbcUrl="jdbc:mysql://localhost:3306/cas"
            p:user="root"
            p:password="root"
            p:initialPoolSize="6"
            p:minPoolSize="6"
            p:maxPoolSize="18"
            p:maxIdleTimeExcessConnections="120"
            p:checkoutTimeout="10000"
            p:acquireIncrement="6"
            p:acquireRetryAttempts="5"
            p:acquireRetryDelay="2000"
            p:idleConnectionTestPeriod="30"
            p:preferredTestQuery="select 1"/>

    <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.JpaTicketRegistry"/>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <util:list id="packagesToScan">
        <value>org.jasig.cas.ticket</value>
        <value>org.jasig.cas.adaptors.jdbc</value>
        <value>org.jasig.cas.services</value>
        <value>org.jasig.cas.support.oauth.services</value>
        <value>org.jasig.cas.support.rest</value>
        <value>org.jasig.cas.support.pac4j</value>
    </util:list>

    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
          id="jpaVendorAdapter"
          p:generateDdl="true"
          p:showSql="true"/>

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="dataSource"
          p:jpaVendorAdapter-ref="jpaVendorAdapter"
          p:packagesToScan-ref="packagesToScan">
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>

    <!-- Ticket Registry -->
    <tx:advice id="txRegistryAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="deleteTicket" read-only="false"/>
            <tx:method name="addTicket" read-only="false"/>
            <tx:method name="updateTicket" read-only="false"/>
            <tx:method name="getTicket" read-only="true"/>
            <tx:method name="getTickets" read-only="true"/>
            <tx:method name="sessionCount" read-only="true"/>
            <tx:method name="serviceTicketCount" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <tx:advice id="txRegistryLockingAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="getOwner" read-only="true"/>
            <tx:method name="acquire" read-only="false"/>
            <tx:method name="release" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" read-only="false"/>
            <tx:method name="save*" read-only="false"/>
            <tx:method name="update*" read-only="false"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- End Ticket Registry + 4 AOP Field -->
    <aop:config>
        <aop:pointcut id="servicesManagerOperations"
                      expression="execution(* org.jasig.cas.services.JpaServiceRegistryDaoImpl.*(..))"/>
        <aop:pointcut id="ticketRegistryOperations"
                      expression="execution(* org.jasig.cas.ticket.registry.JpaTicketRegistry.*(..))"/>
        <aop:pointcut id="ticketRegistryLockingOperations"
                      expression="execution(* org.jasig.cas.ticket.registry.support.JpaLockingStrategy.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicesManagerOperations"/>
        <aop:advisor advice-ref="txRegistryAdvice" pointcut-ref="ticketRegistryOperations"/>
        <aop:advisor advice-ref="txRegistryLockingAdvice" pointcut-ref="ticketRegistryLockingOperations"/>
    </aop:config>

    <bean id="ticketRegistryCleaner"
          class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner"
          c:centralAuthenticationService-ref="centralAuthenticationService"
          c:ticketRegistry-ref="ticketRegistry"
          p:lock-ref="cleanerLock"/>

    <bean id="cleanerLock" class="org.jasig.cas.ticket.registry.support.JpaLockingStrategy"
          p:uniqueId="localhost"
          p:applicationId="cas-ticket-registry-cleaner"/>

    <bean id="jobDetailTicketRegistryCleaner"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
          p:targetObject-ref="ticketRegistryCleaner"
          p:targetMethod="clean"/>

    <bean id="triggerJobDetailTicketRegistryCleaner"
          class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"
          p:jobDetail-ref="jobDetailTicketRegistryCleaner"
          p:startDelay="20000"
          p:repeatInterval="5000000"/>

    <bean id="serviceRegistryDao" class="org.jasig.cas.services.JpaServiceRegistryDaoImpl">
    </bean>
    <!--  End JPA MySQL Configuration -->

    <bean id="auditTrailManager"
          class="org.jasig.inspektr.audit.support.Slf4jLoggingAuditTrailManager"/>
    <bean id="healthCheckMonitor" class="org.jasig.cas.monitor.HealthCheckMonitor"/>

</beans>
