Skip to content
AuthController.java 10 KiB
Newer Older
杨郁彬's avatar
杨郁彬 committed
package com.cesgroup.bdc.auth.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cesgroup.authen4.ws.CoreHttpClientWS;
import com.cesgroup.bdc.SelfDefinedGroup.entity.SelfDefinedGroup;
import com.cesgroup.bdc.SelfDefinedGroup.entity.SelfDefinedGroupUser;
import com.cesgroup.bdc.SelfDefinedGroup.service.ISelfDefinedGroupService;
import com.cesgroup.bdc.util.Constant;
import com.cesgroup.kingkong.core.security.model.IOrganization;
import com.cesgroup.kingkong.core.security.model.IUser;
import com.cesgroup.kingkong.core.security.service.AuthsystemService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/auth")
public class AuthController {
  @Autowired
  private AuthsystemService authsystemService;

  @Autowired
  private ISelfDefinedGroupService selfDefinedGroupService;

  @Value("${kingkong.authsystem.url}")
  private String host;

  private CoreHttpClientWS coreHttpClientWS;

  /**
   * 当前登录用户
   *
   * @return 用户信息
   * @author: shen.shaohua
   * @since: 2019/7/3 15:00
   */
  private IUser getUser() {
    IUser currentUser = (IUser) SecurityUtils.getSubject().getPrincipal();
    return currentUser;
  }

  /**
   * 获取部门信息
   *
   * @return 用户信息
   * @author: shen.shaohua
   * @since: 2019/7/3 15:00
   */
  private IOrganization getOrg(HttpSession session) {
    IOrganization org = (IOrganization) session.getAttribute(Constant.CURRENT_USER_ORG);
    if (org == null) {
      org = authsystemService.getOrganizationByUserId(getUser().getId());
      session.setAttribute(Constant.CURRENT_USER_ORG, org);
    }
    return org;
  }

  private CoreHttpClientWS getCoreHttpClientWS() {
    if (coreHttpClientWS == null) {
      System.out.println("host===" + host);
      coreHttpClientWS = CoreHttpClientWS.getCoreHttpClientWS(host);
    }
    return coreHttpClientWS;
  }

  @RequestMapping("/test")
  @ResponseBody
  public Map<String, Object> test() {
    Map<String, Object> mm = new HashMap<>();
    //IUser uu = (IUser) SecurityUtils.getSubject().getPrincipal();
    IUser uu = getUser();
    //mm.put("user", uu);
    List<IOrganization> list = authsystemService.findOrganizationsByTenantId(uu.getTenantId());
    mm.put("list", list);
    /*Object dd = uu.getUserDetail();
    System.out.println(dd.getClass());
		mm.put("dd", dd);
		boolean cc = getCoreHttpClientWS().checkPasswordByUserId(uu.getId(), "000000");
		mm.put("cc", cc);
		boolean aa = getCoreHttpClientWS().checkPasswordByUserId(uu.getId(), "010000");
		mm.put("aa", aa);*/
    return mm;
  }

  /**
   * 修改密码
   *
   * @param oldPassword
   * @param newPassword
   * @return
   * @author: shen.shaohua
   * @since: 2019/7/3 15:09
   */
  @RequestMapping("/modifyPassword")
  @ResponseBody
  public Map<String, Object> modifyPassword(String oldPassword, String newPassword) {
    Map<String, Object> mm = new HashMap<>();
    IUser uu = getUser();
    /*Account account = (Account) uu.getUserDetail();
    account.getPassword();*/
    try {
      boolean correct = getCoreHttpClientWS().checkPasswordByUserId(uu.getId(), oldPassword);
      if (correct) {
        authsystemService.modifyPassword(uu.getId(), oldPassword, newPassword);
        mm.put("code", "0");//成功
      } else {
        mm.put("code", "1");//原密码错误
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return mm;
  }

  /**
   * 单位选取页面
   *
   * @author: shen.shaohua
   * @since: 2019/7/8 22:37
   */
  @RequestMapping("/selectDept")
  public ModelAndView selectDept() {
    ModelAndView mv = new ModelAndView("auth/selectDept");
    return mv;
  }


  @RequestMapping("/selectStampDept")
  public ModelAndView selectStampDept() {
    ModelAndView mv = new ModelAndView("stamp/selectDept");
    return mv;
  }

  /**
   * 组织树
   *
   * @return
   */
  @RequestMapping("/orgTree")
  public ModelAndView orgTree() {
    ModelAndView mv = new ModelAndView("auth/orgTree");
    return mv;
  }

  /**
   * 组织树数据
   *
   * @return
   */
  @RequestMapping("/orgTreeData")
  @ResponseBody
  public List<IOrganization> orgTreeData() {
    IUser uu = getUser();
    List<IOrganization> organizationList = authsystemService.findOrganizationsByTenantId(uu.getTenantId());
    return organizationList;
  }

  /**
   * 组织及自定义组树数据
   *
   * @return
   */
  @RequestMapping("/orgAndSelfTreeData")
  @ResponseBody
  public List<Map<String, Object>> orgAndSelfTreeData(HttpServletRequest request, HttpSession session) {
    String searchName = request.getParameter("searchName");
    System.out.println(searchName);
    List<Map<String, Object>> list = new ArrayList<>();
    IUser uu = getUser();
    List<IOrganization> organizationList0 = authsystemService.findOrganizationsByTenantId(uu.getTenantId());
    List<IOrganization> organizationList = null;
    if (StrUtil.isNotBlank(searchName)) {
      organizationList = organizationList0.stream().filter(org -> org.getName().indexOf(searchName) >= 0).collect(Collectors.toList());
      addToTargetList(searchName, organizationList0, organizationList);
    } else {
      organizationList = organizationList0;
    }
    organizationList.forEach(org -> {
      Map<String, Object> m = BeanUtil.beanToMap(org);
      m.put("deptId", org.getId());
      if (StrUtil.isEmpty(org.getParentId())) {
        m.put("type", "orgRoot"); //根节点
        m.put("open", true);
      } else {
        m.put("type", "org");
      }
      list.add(m);
    });
    Map<String, Object> root2 = new HashMap<>();
    root2.put("id", "selfGroup");
    root2.put("name", "自定义组");
    root2.put("type", "groupRoot");
    //root2.put("isParent", true);
    root2.put("open", true);
    list.add(root2);
    //List<SelfDefinedGroup> selfDefinedGroupList = selfDefinedGroupService.getAll(uu.getId()); //修改自定义组查询权限
    List<SelfDefinedGroup> selfDefinedGroupList = selfDefinedGroupService.getAll(getOrg(session).getId()); //修改自定义组查询权限
    for (SelfDefinedGroup selfDefinedGroup : selfDefinedGroupList) {
      List<SelfDefinedGroupUser> selfDefinedGroupUserList = selfDefinedGroup.getDeptList();
      selfDefinedGroup.setDeptList(null);
      Map<String, Object> m1 = BeanUtil.beanToMap(selfDefinedGroup);
      m1.remove("deptList");
      m1.put("type", "group");
      m1.put("parentId", "selfGroup");
      m1.put("name", selfDefinedGroup.getGroupName());
      list.add(m1); //自定义组
      for (SelfDefinedGroupUser selfDefinedGroupUser : selfDefinedGroupUserList) {
        Map<String, Object> m2 = BeanUtil.beanToMap(selfDefinedGroupUser);
        m2.put("type", "groupElement");
        m2.put("parentId", selfDefinedGroupUser.getGroupId());
        m2.put("id", selfDefinedGroupUser.getId());
        m2.put("deptId", selfDefinedGroupUser.getDeptId());
        m2.put("name", this.getDeptName(organizationList0, selfDefinedGroupUser.getDeptId()));
        list.add(m2); //自定义组下的部门
      }
    }
    return list;
  }

  /**
   * 组织及自定义组树数据
   *
   * @return
   */
  @RequestMapping("/orgStampTreeData")
  @ResponseBody
  public List<Map<String, Object>> orgStampTreeData(HttpServletRequest request, HttpSession session) {
    String searchName = request.getParameter("searchName");
    System.out.println(searchName);
    List<Map<String, Object>> list = new ArrayList<>();
    IUser uu = getUser();
    List<IOrganization> organizationList0 = authsystemService.findOrganizationsByTenantId(uu.getTenantId());
    List<IOrganization> organizationList = null;
    if (StrUtil.isNotBlank(searchName)) {
      organizationList = organizationList0.stream().filter(org -> org.getName().indexOf(searchName) >= 0).collect(Collectors.toList());
      addToTargetList(searchName, organizationList0, organizationList);
    } else {
      organizationList = organizationList0;
    }
    //获取当前用户
    IUser user = (IUser) SecurityUtils.getSubject().getPrincipal();
    //获取当前用户组织
    IOrganization org1 = authsystemService.getOrganizationByUserId(user.getId());
    organizationList.forEach(org -> {
      if (!org.getId().equals(org1.getId())) {
        Map<String, Object> m = BeanUtil.beanToMap(org);
        m.put("deptId", org.getId());
        if (StrUtil.isEmpty(org.getParentId())) {
          m.put("type", "orgRoot"); //根节点
          m.put("open", true);
        } else {
          m.put("type", "org");
        }
        list.add(m);
      }

    });
    return list;
  }

  private void addToTargetList(String searchName, List<IOrganization> organizationList0, List<IOrganization> targetList) {
    List<IOrganization> targetList2 = CollUtil.newArrayList(targetList);
    for (IOrganization parentOrg : targetList2) {
      loopAddToTargetList(searchName, parentOrg, organizationList0, targetList);
    }
  }

  private void loopAddToTargetList(String searchName, IOrganization parentOrg, List<IOrganization> organizationList0, List<IOrganization> targetList) {
    for (IOrganization oo : organizationList0) {
      if (oo.getParentId() != null && oo.getParentId().equals(parentOrg.getId()) && oo.getName().indexOf(searchName) < 0) {
        targetList.add(oo);
        loopAddToTargetList(searchName, oo, organizationList0, targetList);
      }
    }
  }

  private String getDeptName(List<IOrganization> organizationList, String orgId) {
    String orgName = "";
    for (IOrganization org : organizationList) {
      if (orgId.equals(org.getId())) {
        orgName = org.getName();
        break;
      }
    }
    return orgName;
  }
}