package cn.com.qmth.stmms.analysis.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.qmth.stmms.admin.interceptor.AdminInterceptor;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.hessian.HessianClientContext;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * Admin模块操作拦截器
 * 
 * @author LS
 * 
 */
public class AnalysisInterceptor extends AdminInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return validateAnalysisUser(request, response);
    }

    private boolean validateAnalysisUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = buildUser(RequestUtils.getWebUser(request));
        if (user != null) {
            if (user.getRoles().contains(Role.SUBJECT_TEACHER) || user.getRoles().contains(Role.HEAD_TEACHER)
                    || user.getRoles().contains(Role.MASTER) || user.getRoles().contains(Role.SCHOOL_ADMIN)) {
                HessianClientContext.setKey(user.getSchoolId() != null ? user.getSchoolId() : 0);
                return true;
            }
        }
        return sessionExpire(request, response, "/admin-login");
    }
}
