<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:sec="http://www.springframework.org/schema/security"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/app.properties"/>
    </bean>


    <bean id="encoder"
          class="com.wisemapping.security.CustomPasswordEncoder"/>

    <sec:http pattern="/css/**" security="none"/>
    <sec:http pattern="/js/**" security="none"/>
    <sec:http pattern="/images/**" security="none"/>
    <sec:http pattern="/icons/**" security="none"/>
    <sec:http pattern="/favicon.ico" security="none"/>

    <sec:http pattern="/c/login" security="none"/>
    <sec:http pattern="/c/user/registration" security="none"/>
    <sec:http pattern="/c/user/resetpassword" security="none"/>
    <sec:http pattern="/c/home" security="none"/>

    <sec:http pattern="/c/maps/*/embed" security="none"/>
    <sec:http pattern="/c/maps/*/public" security="none"/>
    <sec:http pattern="/c/GCFInstall" security="none"/>

    <sec:http pattern="/c/publicview.htm" security="none"/>
    <sec:http pattern="/c/embeddedview.htm" security="none"/>
    <sec:http pattern="/c/termsOfUse" security="none"/>
    <sec:http pattern="/c/keyboard" security="none"/>

    <sec:http pattern="/c/activation" security="none"/>
    <sec:http pattern="/c/try" security="none"/>


    <sec:http use-expressions="true" create-session="never" pattern="/service/**">
        <sec:intercept-url pattern="/service/admin/users/**" access="isAuthenticated() and hasRole('ROLE_ADMIN')"/>
        <sec:intercept-url pattern="/service/**" access="isAuthenticated() and hasRole('ROLE_USER')"/>
        <sec:http-basic/>
    </sec:http>

    <sec:http use-expressions="true" access-denied-page="/c/login">
        <sec:intercept-url pattern="/c/**/*" access="isAuthenticated() and hasRole('ROLE_USER')"/>
        <sec:form-login login-page="/c/login"
                        default-target-url='/c/maps/'
                        always-use-default-target='false'
                        authentication-failure-url="/c/login?login_error=2"
                        login-processing-url="/c/j_spring_security_check"/>
        <sec:remember-me key="wisemapping-hashed-key"/>
        <sec:logout logout-url="/c/logout" invalidate-session="true" logout-success-url="/c/login"/>
    </sec:http>

    <sec:authentication-manager alias="authenticationManager">
        <sec:authentication-provider ref="dbAuthenticationProvider"/>
        <sec:authentication-provider user-service-ref="userDetailsService"/>
    </sec:authentication-manager>

    <bean id="dbAuthenticationProvider" class="com.wisemapping.security.AuthenticationProvider">
        <property name="userDetailsService" ref="userDetailsService"/>
        <property name="encoder" ref="encoder"/>
    </bean>

    <bean id="userDetailsService" class="com.wisemapping.security.UserDetailsService">
        <property name="userService" ref="userService"/>
        <property name="adminUser" value="${admin.user}"/>
    </bean>

</beans>