package com.persagy.dmp.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.auth.service.AuthService;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.helper.SpringHelper;

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

/**
 * 登录认证处理抽象类
 * @author Charlie Yu
 * @date 2021-11-02
 */
public abstract class AbstractAuthServiceImpl implements AuthService {

    @Override
    public boolean validateLogin(HttpServletRequest request, HttpServletResponse response) {
        // 登录认证通常在网关完成，无网关服务在此完成
        return true;
    }

    @Override
    public void loginSuccess(HttpServletRequest request, HttpServletResponse response) {

    }

    @Override
    public void loginFailure(HttpServletRequest request, HttpServletResponse response) {

    }

    @Override
    public void release(HttpServletRequest request, HttpServletResponse response) {
        // 释放上下文缓存
        AppContext.unload();
    }

    /**
     * 从请求中装载上下文
     * @param request
     */
    protected void loadContextByRequest(HttpServletRequest request) {
        AppContext context = AppContext.getContext();
        context.setGroupCode(request.getParameter(AppContext.GROUP_CODE));
        context.setProjectId(request.getParameter(AppContext.PROJECT_ID));
        context.setAppId(request.getParameter(AppContext.APP_ID));
        context.setAccountId(request.getParameter("userId"));
        // 无用户时，默认为默认系统用户
        if(StrUtil.isBlank(context.getAccountId())) {
            context.setAccountId(CommonConstant.DEFAULT_ID);
        }
    }
}
