package me.chyxion.tigon.shiro;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Mar 12, 2015 1:18:00 PM
 */
public class DefaultWebSecurityManagerExt extends DefaultWebSecurityManager {
    @Autowired(required = false)
    private AuthCallback authCallback;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onSuccessfulLogin(AuthenticationToken token,
            AuthenticationInfo info, Subject subject) {
        if (authCallback != null) {
            authCallback.onSuccessfulLogin(token, info, subject);
        }
        super.onSuccessfulLogin(token, info, subject);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onFailedLogin(AuthenticationToken token, 
            AuthenticationException ae, Subject subject) {
        if (authCallback != null) {
            authCallback.onFailedLogin(token, ae, subject);
        }
        super.onFailedLogin(token, ae, subject);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void beforeLogout(Subject subject) {
        if (authCallback != null) {
            authCallback.beforeLogout(subject);
        }
        super.beforeLogout(subject);
    }
}
