agtloginaction.java

来自「培训考试系统代码」· Java 代码 · 共 275 行

JAVA
275
字号





package com.huawei.icd30.agt.login;

import java.util.LinkedList;
import javax.servlet.http.*;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import com.huawei.icd30.agt.util.*;
import com.huawei.icd30.common.db.*;
import com.huawei.icd30.common.mutex.*;


/**
 * <p> 处理登录时逻辑处理的Action类,
 * 根据工号查询数据库得到该用户所拥有的权限,
 * 并定位到框架页面。</p>
 * <p> </p>
 * <p> </p>
 * @author 龙燕茜
 * @version 1.0
 */

public class AgtLoginAction extends Action
{

    public ActionForward perform(ActionMapping mapping,
           ActionForm form, HttpServletRequest req,
           HttpServletResponse res)
    {
        String method = req.getMethod();
        String workerNo = (String)req.getParameter("workNo");

        req.getSession().setAttribute("workerNo",workerNo);

        //从一期Session中获取工号
        String staffNo = (String)req.getSession().getAttribute("workerNo");

        //设置工号
        req.getSession().setAttribute("staffno",staffNo);

        //定义连接的实例
        SysDbConn aplcoms = null;
        try
        {
            //得到一个连接的实例
            aplcoms = SysConnPool.getInstance().getAplComs();

            //检验用户身份
            if("POST".equalsIgnoreCase(method))
            {//从用户统一登录界面进入的登录请求
               String pass = req.getParameter("password");
               aplcoms.preparedSP();
               aplcoms.setString(1,staffNo);
               aplcoms.setString(2,pass);

               SysRecord record = aplcoms.csCommonSP("P_Unify_Login").getParamSet();
               if(!(record != null && record.getInt(0) == 0))
               {//用户身分不合法
                    return (mapping.findForward("loginerror"));
               }

            }
            else
            {//从坐席进入的登录请求
               String pass = req.getParameter("randomInt");
               aplcoms.preparedSP();
               aplcoms.setString(1,staffNo);
               aplcoms.setString(2,pass);

               SysRecord record = aplcoms.csCommonSP("P_Agent_Login").getParamSet();
               if(!(record != null && record.getInt(0) == 0))
               {//用户身分不合法
                   return (mapping.findForward("loginerror"));
               }
            }


            //处理模块的互斥
            String staffCity = null;
            aplcoms.preparedQuery("");
            aplcoms.setString(1 ,staffNo);
            SysResultSet rssl = aplcoms.csCommonQuery("SQL_Agt_GetStaffCity" ,"1" ,"-1").getResultSet();
            if(rssl != null)
            {//地市
                if(rssl.next())
                {
                   staffCity = rssl.getString(0);
                }
            }

           try
           {
               req.getSession().setAttribute("StaffCityId",staffCity);
               MutexManager.registMutex(staffCity,staffNo,0,false);
           }
           catch(Exception e)
           {
               return (mapping.findForward("loginerror1"));
           }


            //获取该用户的所属地市
            LinkedList cityId =  new LinkedList();
            LinkedList cityName = new LinkedList();
            aplcoms.preparedQuery("");
            aplcoms.setString(1 ,staffNo);
            SysResultSet rs1 = aplcoms.csCommonQuery("SQL_Agt_LoginGetCity" ,"1" ,"-1").getResultSet();
            if(rs1 != null)
            {//数据库中存在该用户所属于地市
                while(rs1.next())
                {
                    cityId.add(rs1.getString(0));
                    cityName.add(rs1.getString(1));
                }
            }
            else
            {//数据库中不存在该用户所属于地市,定向到出错页面
                req.setAttribute("errorId",ErrorCode.LOGIN_CITYIDNULL);
                return (servlet.findForward("error"));
            }

            //把该用户的所属地市放到session中
            req.getSession().setAttribute("CITYID",cityId);
            req.getSession().setAttribute("CITYNAME",cityName);

            //获取该用户所拥有的权限
            LinkedList authList =  new LinkedList();
            aplcoms.preparedQuery("");
            aplcoms.setString(1 ,staffNo);
            SysResultSet rs2 = aplcoms.csCommonQuery("SQL_Agt_LoginGetAuth" ,"1" ,"-1").getResultSet();
            if(rs2 != null)
            {
                while(rs2.next())
                {
                    authList.addLast(rs2.getString(0));
                }
            }

            aplcoms.preparedQuery("");
            aplcoms.setString(1 ,staffNo);
            rs2 = aplcoms.csCommonQuery("SQL_Agt_LoginStaffAuth" ,"1" ,"-1").getResultSet();
            if(rs2 != null)
            {
                while(rs2.next())
                {
                    if(!(authList.contains(rs2.getString(0))))
                    {
                      authList.addLast(rs2.getString(0));
                    }
                }
            }

            //把该用户的权限存放在session中,??????????????
            req.getSession().setAttribute("AUTHLIST",authList);

            //对菜单树进行处理
            String menuStr = "";
            SysResultSet menuRs = LoginGetMenu.getMenuRs();
            if(menuRs != null)
            {
               for(int i=0;menuRs.setRecord(i)&&i<menuRs.getMetaData().getRecordCount();i++)
               {
                   String menuAuth = menuRs.getString(6);
                   if(isAuth(authList,menuAuth))
                   {//拥有权限
                      if(!menuStr.equalsIgnoreCase(""))
                      {
                        menuStr += "~";
                      }
                      menuStr += menuRs.getString(0) + "|";
                      menuStr += menuRs.getString(1) + "|";
                      menuStr += menuRs.getString(2) + "|";
                      menuStr += menuRs.getString(3) + "|";
                      menuStr += menuRs.getString(4) + "|";
                      menuStr += menuRs.getString(5);
                   }
               }
            }
            //菜单存放在session中,??????????

           req.getSession().setAttribute("AGTMENU",menuStr);

            //定向到主页面
            return (mapping.findForward("main"));
        }
        catch (SysDbException aple)
        {//捕获CommonService系统异常,定向到出错页面
            aple.printStackTrace(System.err);
            req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
            return (servlet.findForward("error"));
        }
        catch(java.sql.SQLException sqle)
        {//捕获调用aplcoms异常,定向到出错页面
            sqle.printStackTrace(System.err);
            req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
            return (servlet.findForward("error"));
        }
        catch(Exception e)
        {//捕获未知异常,定向到出错页面
            e.printStackTrace(System.err);
            req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
            return (servlet.findForward("error"));
        }
        finally
        {//关闭连接实例
            if(aplcoms != null)
            {
               aplcoms.close();
            }
        }

    }

    /**
     * 菜单权限的检查
     * @param authList
     * @param menuAuth
     * @return
     */
    private boolean isAuth(LinkedList authList,String menuAuth)
    {
        if("00".equalsIgnoreCase(menuAuth))
        {
           return true;
        }
        else if(authList.contains(menuAuth))
        {
           return true;
        }
        else
        {//对可能为父菜单权限的进行检查
            for(int i=1; i<6; i++)
            {
               String childMenuAuth = menuAuth + "0" + i;
               if(authList.contains(childMenuAuth))
               {
                  return true;
               }
            }

            //对考试菜单进行特殊处理
            if(menuAuth.equalsIgnoreCase("07"))
            {
              for(int i=1; i<6; i++)
              {
                 String childMenuAuth = menuAuth + "030" + i;
                 if(authList.contains(childMenuAuth))
                 {
                    return true;
                 }
              }
            }
            //对课程的菜单进行特殊处理
            if(menuAuth.equalsIgnoreCase("11"))
            {
              for(int i=1; i<6; i++)
              {
                 String childMenuAuth = menuAuth + "020" + i;
                 if(authList.contains(childMenuAuth))
                 {
                    return true;
                 }
              }
            }

        }
        return false;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?