Exemplo n.º 1
0
 /** 初始化角色权限Map。 */
 private void initRoleRightsMap() {
   ApplicationContext ctx = Global._ctx;
   IRoleManager roleManager = (IRoleManager) ctx.getBean("roleManager");
   IModuleManager moduleManager = (IModuleManager) ctx.getBean("moduleManager");
   IFunctionManager functionManager = (IFunctionManager) ctx.getBean("functionManager");
   IMethodManager methodManager = (IMethodManager) ctx.getBean("methodManager");
   // 获得所有角色
   List allRolesList = roleManager.getRoles();
   Role rolePO = null;
   Module modulePO = null;
   List rights = null;
   for (int i = 0; i < allRolesList.size(); i++) {
     rolePO = (Role) allRolesList.get(i);
     rights = new ArrayList();
     // 获得角色对应的模块
     List modulesList = moduleManager.getModulesByRoleId(rolePO.getId());
     for (int j = 0; j < modulesList.size(); j++) {
       modulePO = (Module) modulesList.get(j);
       if (!modulePO.getActionUrl().equals("#")) {
         ModuleAndFunction maf = new ModuleAndFunction();
         maf.setModuleNameSpace(modulePO.getActionUrl());
         // 存放当前模块对应的所有方法并且也对应角色里的功能下的方法
         List methodArrayList = new ArrayList();
         List functionList =
             functionManager.getFunctionsByRoleIdAndModuleId(rolePO.getId(), modulePO.getId());
         for (Iterator iter = functionList.iterator(); iter.hasNext(); ) {
           Function function = (Function) iter.next();
           List methodList = methodManager.getMethodsByFunctionId(function.getId());
           for (int m = 0; m < methodList.size(); m++) {
             Method method = (Method) methodList.get(m);
             methodArrayList.add(method);
           }
         }
         maf.setMethodList(methodArrayList);
         rights.add(maf);
       }
     }
     RoleRightsMap.put(rolePO.getId(), rights);
   }
 }
  /*
   *
   * (non-Javadoc)
   *
   * @see com.opensymphony.xwork.ActionSupport#execute()
   */
  public String execute() {
    Module moduleVO = new Module();
    Module oldModuleVO = null;
    try {

      /** 验证请求的属性是否完整,防止知道URL非法请求----开始 */
      String name = this.getRequest().getParameter("name");
      String parentId = this.getRequest().getParameter("parentId");
      String actionUrl = this.getRequest().getParameter("actionUrl");
      String moduleLevel = this.getRequest().getParameter("moduleLevel");
      String viewType = this.getRequest().getParameter("viewType");
      String moduleSort = this.getRequest().getParameter("moduleSort");
      if (StringUtils.hasText(name)
          && StringUtils.hasText(parentId)
          && StringUtils.hasText(actionUrl)
          && StringUtils.hasText(moduleLevel)
          && StringUtils.hasText(viewType)
          && StringUtils.hasText(moduleSort)) {
        try {
          NumberUtils.parseNumber(parentId, Integer.class);
          NumberUtils.parseNumber(moduleLevel, Integer.class);
          NumberUtils.parseNumber(moduleSort, Integer.class);
        } catch (Exception e) {
          /** 记录日志 */
          LogsApplication appLog = new LogsApplication();
          appLog.setOperator(this.getUserSessionInfo().getName());
          appLog.setLogDate(new Date());
          appLog.setEventType("新增一级模块异常");
          appLog.setContent(
              "操作员["
                  + this.getUserSessionInfo().getName()
                  + "]"
                  + "新增一级模块出现异常,异常信息:["
                  + e.getMessage()
                  + "]!");
          appLog.setPriority(LogsApplication.EXCEPTION);
          appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
          this.appLogManager.saveOrUpdateAppLog(appLog);

          /** 输出错误信息 */
          logger.error(e.getMessage());
          e.printStackTrace();

          return INPUT;
        }
        /** 每个属性都有内容表示正常 */
      } else {
        /** 记录日志 */
        LogsApplication appLog = new LogsApplication();
        appLog.setOperator(this.getUserSessionInfo().getName());
        appLog.setLogDate(new Date());
        appLog.setEventType("新增一级模块异常");
        appLog.setContent(
            "操作员["
                + this.getUserSessionInfo().getName()
                + "]"
                + "新增一级模块出现异常,异常信息:["
                + "必填属性值有空值"
                + "]!");
        appLog.setPriority(LogsApplication.EXCEPTION);
        appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
        this.appLogManager.saveOrUpdateAppLog(appLog);

        logger.error("必填属性值有空值");

        /** 有空值的属性,返回 */
        return INPUT;
      }

      /** 验证请求的属性是否完整----结束 */

      /** 验证模块是否存在 */
      oldModuleVO = this.getModuleManager().getModuleByModuleName(StringUtils.trimWhitespace(name));
      if (oldModuleVO != null) {
        this.getRequest()
            .setAttribute(
                "actionName",
                this.getWebApplitionUrl() + "/webmanager/module/toAddFirstModule.action");
        return "existModule";
      }

      /** 添加模块 */
      // BeanUtils.copyProperties(moduleVO, moduleFormBean);
      // BeanUtil会将null的属性赋值为0||""。
      moduleVO.setId(null);
      moduleVO.setName(StringUtils.trimWhitespace(name));
      moduleVO.setParentId(
          (Integer) NumberUtils.parseNumber(StringUtils.trimWhitespace(parentId), Integer.class));
      moduleVO.setActionUrl(StringUtils.trimWhitespace(actionUrl));
      moduleVO.setModuleLevel(
          (Integer)
              NumberUtils.parseNumber(StringUtils.trimWhitespace(moduleLevel), Integer.class));
      moduleVO.setViewType(StringUtils.trimWhitespace(viewType));
      moduleVO.setModuleSort(
          (Integer) NumberUtils.parseNumber(StringUtils.trimWhitespace(moduleSort), Integer.class));

      /** 添加 */
      this.getModuleManager().saveOrUpdateModule(moduleVO);

      /** 记录日志 */
      LogsApplication appLog = new LogsApplication();
      appLog.setOperator(this.getUserSessionInfo().getName());
      appLog.setLogDate(new Date());
      appLog.setEventType("新增一级模块");
      appLog.setContent(
          "操作员["
              + this.getUserSessionInfo().getName()
              + "]"
              + "新增一级模块,新增一级模块是["
              + moduleVO.getName()
              + "]!");
      appLog.setPriority(LogsApplication.NORMAL);
      appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
      this.appLogManager.saveOrUpdateAppLog(appLog);

      return SUCCESS;

    } catch (Exception e) {
      /** 记录日志 */
      LogsApplication appLog = new LogsApplication();
      appLog.setOperator(this.getUserSessionInfo().getName());
      appLog.setLogDate(new Date());
      appLog.setEventType("新增一级模块异常");
      appLog.setContent(
          "操作员["
              + this.getUserSessionInfo().getName()
              + "]"
              + "新增一级模块出现异常,异常信息:["
              + e.getMessage()
              + "]!");
      appLog.setPriority(LogsApplication.EXCEPTION);
      appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
      this.appLogManager.saveOrUpdateAppLog(appLog);

      /** 输出错误信息 */
      logger.error(e.getMessage());
      e.printStackTrace();

      return ERROR;
    }
  }