package com.mjlogin;

import com.data.util.ErrorCode;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.routes.Action;
import com.taurus.core.routes.IController;
import com.taurus.core.routes.Interceptor;
import com.taurus.core.util.StringUtil;
import com.taurus.web.Controller;
import com.taurus.web.WebException;

public class WebInterceptor implements Interceptor{
	/**
	 * 验证session
	 */
	public static final int V_SESSION = 1;
	@Override
	public void intercept(Action action, IController controller, Object... args) throws Exception {
		Controller ctr = (Controller)controller;
		int validate = action.getActionKeyObj().validate();
		if((validate&V_SESSION)!=0) {
			String session = ctr.getSession();
			String token = ctr.getTokens();
			if (!Redis.use("group1_db0").exists(session)) {
				throw new WebException(ErrorCode._NO_SESSION);
			}

			if (StringUtil.isNotEmpty(token) && StringUtil.isNotEmpty(session))
			{
				String token_session = Redis.use("group1_db0").hget(token, "user");
				if (StringUtil.isEmpty(token_session))
				{
					throw new WebException(ErrorCode._NO_SESSION);
				}
				else {
					if (!token_session.equals(session))
					{
						throw new WebException(ErrorCode._NO_SESSION);
					}
				}
			}
			else {
				throw new WebException(ErrorCode._NO_SESSION);
			}

			String opt = Redis.use("group1_db0").hget(session, "opt");
//			background  del  sign
			if (!StringUtil.isEmpty(opt) && Integer.parseInt(opt) == 1) {
				throw new WebException(ErrorCode._NO_SESSION);
			}
		}
		
	}


}
