MobileLoginUserDetailService.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.zoniot.ccrc.security.mobile;
  2. import com.zoniot.ccrc.commom.utils.RedisUtil;
  3. import com.zoniot.ccrc.dao.SiteMapper;
  4. import com.zoniot.ccrc.dao.SiteUserMapper;
  5. import com.zoniot.ccrc.dao.UserMapper;
  6. import com.zoniot.ccrc.dto.LoginUser;
  7. import com.zoniot.ccrc.entity.Permission;
  8. import com.zoniot.ccrc.entity.Site;
  9. import com.zoniot.ccrc.entity.SiteUser;
  10. import com.zoniot.ccrc.entity.User;
  11. import com.zoniot.ccrc.service.IotService;
  12. import com.zoniot.ccrc.service.PermissionService;
  13. import org.springframework.beans.BeanUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.security.authentication.DisabledException;
  16. import org.springframework.security.core.userdetails.UserDetails;
  17. import org.springframework.security.core.userdetails.UserDetailsService;
  18. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  19. import org.springframework.stereotype.Component;
  20. import javax.annotation.Resource;
  21. import java.util.List;
  22. @Component
  23. public class MobileLoginUserDetailService implements UserDetailsService {
  24. @Resource
  25. private UserMapper userMapper;
  26. @Resource
  27. private PermissionService permissionService;
  28. @Resource
  29. private SiteUserMapper siteUserMapper;
  30. @Resource
  31. private SiteMapper siteMapper;
  32. @Autowired
  33. private RedisUtil redisUtil;
  34. @Autowired
  35. private IotService iotService;
  36. @Override
  37. public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
  38. User user = userMapper.findUserByMobile(mobile);
  39. if (user == null) {
  40. throw new UsernameNotFoundException("该用户不存在");
  41. }
  42. //判断是否禁用
  43. if (user.getEnable() == 0) {
  44. throw new DisabledException("账号已被禁用");
  45. }
  46. LoginUser loginUser = new LoginUser();
  47. BeanUtils.copyProperties(user, loginUser);
  48. //Map<Integer, List<Permission>> permissionMap = new HashMap<>();
  49. //Map<Integer, List<ProgramItem>> datePermissionMap = new HashMap<>();
  50. if (user.getIsSuperAdmin() == 1) {
  51. //超级管理员的权限
  52. List<Permission> permissions = permissionService.getPermissionBySuperAdmin();
  53. loginUser.setType(1);
  54. loginUser.setPermissionList(permissions);
  55. //查询物联网token
  56. String iotToken = iotService.getLoginIotToken("18800000001");
  57. if (iotToken != null) {
  58. loginUser.setIotToken(iotToken);
  59. }else {
  60. throw new UsernameNotFoundException("物联网平台无该账号");
  61. }
  62. } else {
  63. //站点管理员(多站点)
  64. Site site = siteUserMapper.getSiteByUserId(user.getId());
  65. SiteUser siteUser = siteUserMapper.findBySiteIdAndUserId(site.getId(),user.getId());
  66. if(siteUser.getType() == 2){
  67. List<Permission> permissions = permissionService.getPermissionBySiteAdminV2(site.getId(),site.getType());;
  68. loginUser.setPermissionList(permissions);
  69. }else {
  70. loginUser.setPermissionList(permissionService.getPermissionByUserId(user.getId(), site.getId()));
  71. }
  72. loginUser.setType(siteUser.getType());
  73. loginUser.setRoleType(siteUser.getRoleType());
  74. loginUser.setOrgId(siteUser.getOrganId());
  75. loginUser.setSiteId(site.getId());
  76. loginUser.setIsSiteAdmin(siteUser.getIsAdmin());//站点管理员
  77. loginUser.setSite(site);
  78. loginUser.setCustomerId(site.getCustomerId());
  79. loginUser.setCustomerName(site.getCustomerName());
  80. loginUser.setOrgId(siteUser.getOrganId());
  81. }
  82. return loginUser;
  83. }
  84. //判断siteList中是否包含siteId
  85. private boolean checkSite(List<Site> siteList, Integer siteId) {
  86. return siteList.parallelStream().anyMatch(site -> site.getId().equals(siteId));
  87. }
  88. private Integer checkSiteType(List<Site> siteList, Integer siteId) {
  89. Integer siteType = null;
  90. for (Site site : siteList) {
  91. if(site.getId().equals(siteId)){
  92. siteType = site.getType();
  93. break;
  94. }
  95. }
  96. return siteType;
  97. }
  98. }