| | |
| | | package com.ruoyi.account.service; |
| | | |
| | | import com.ruoyi.chargingPile.api.domain.SiteMenu; |
| | | import com.ruoyi.chargingPile.api.feignClient.PartnerClient; |
| | | import com.ruoyi.chargingPile.api.dto.GetSiteListDTO; |
| | | import com.ruoyi.chargingPile.api.feignClient.SiteClient; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | public class PartnerAppUserAccessService { |
| | | |
| | | private static final Integer PARTNER_ROLE_TYPE = 2; |
| | | private static final Integer COMPANY_ROLE_TYPE = 5; |
| | | private static final Long IMPOSSIBLE_USER_ID = -1L; |
| | | private static final String PARTNER_READ_ONLY_MESSAGE = "合作商账号仅支持查看用户信息"; |
| | | |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private SysUserClient sysUserClient; |
| | | @Resource |
| | | private PartnerClient partnerClient; |
| | | private SiteClient siteClient; |
| | | @Resource |
| | | private ChargingOrderClient chargingOrderClient; |
| | | |
| | |
| | | if (!isPartner(sysUser)) { |
| | | return null; |
| | | } |
| | | if (sysUser.getObjectId() == null) { |
| | | if (sysUser.getObjectId() == null || sysUser.getUserId() == null) { |
| | | return impossibleUserIds(); |
| | | } |
| | | R<Set<Integer>> siteResult = partnerClient.authSite(sysUser.getObjectId(), SiteMenu.SITE_LIST.getValue()); |
| | | R<List<GetSiteListDTO>> siteResult = siteClient.getSiteListByUserId(sysUser.getUserId()); |
| | | if (siteResult == null || !R.isSuccess(siteResult) || siteResult.getData() == null) { |
| | | return impossibleUserIds(); |
| | | } |
| | | List<Integer> siteIds = siteResult.getData().stream() |
| | | .filter(Objects::nonNull) |
| | | .map(GetSiteListDTO::getId) |
| | | .filter(Objects::nonNull) |
| | | .filter(siteId -> siteId > 0) |
| | | .distinct() |
| | |
| | | return userIds.isEmpty() ? impossibleUserIds() : userIds; |
| | | } |
| | | |
| | | public boolean isCurrentPartner() { |
| | | return isPartner(getCurrentSysUser()); |
| | | } |
| | | |
| | | public void assertUserVisible(Long userId) { |
| | | List<Long> visibleUserIds = getVisibleUserIdsForCurrentPartner(); |
| | | if (visibleUserIds != null && (userId == null || !visibleUserIds.contains(userId))) { |
| | |
| | | } |
| | | } |
| | | |
| | | public void assertWritable() { |
| | | if (isCurrentPartner()) { |
| | | throw new ServiceException(PARTNER_READ_ONLY_MESSAGE); |
| | | public void assertUsersVisible(Collection<Long> userIds) { |
| | | List<Long> visibleUserIds = getVisibleUserIdsForCurrentPartner(); |
| | | if (visibleUserIds == null) { |
| | | return; |
| | | } |
| | | if (userIds == null || userIds.isEmpty()) { |
| | | throw new ServiceException("无权操作所选用户"); |
| | | } |
| | | for (Long userId : userIds) { |
| | | if (userId == null || !visibleUserIds.contains(userId)) { |
| | | throw new ServiceException("无权操作所选用户"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 当前登录账号若为公司账号(roleType=5 且已绑定公司),返回其所属公司ID;非公司账号返回null。 |
| | | */ |
| | | public Integer getCurrentCompanyId() { |
| | | SysUser sysUser = getCurrentSysUser(); |
| | | if (!isCompany(sysUser)) { |
| | | return null; |
| | | } |
| | | return sysUser.getObjectId(); |
| | | } |
| | | |
| | | /** |
| | | * 数据范围强制:公司账号只允许查询本公司数据,覆盖前端传入的公司筛选条件;非公司账号保持原值。 |
| | | */ |
| | | public Integer resolveCompanyFilter(Integer requestCompanyId) { |
| | | Integer currentCompanyId = getCurrentCompanyId(); |
| | | return currentCompanyId != null ? currentCompanyId : requestCompanyId; |
| | | } |
| | | |
| | | /** |
| | | * 公司账号数据归属校验:记录不属于本公司时拒绝操作;非公司账号不做限制。 |
| | | */ |
| | | public void assertCompanyVisible(Integer recordCompanyId) { |
| | | Integer currentCompanyId = getCurrentCompanyId(); |
| | | if (currentCompanyId == null) { |
| | | return; |
| | | } |
| | | if (!Objects.equals(currentCompanyId, recordCompanyId)) { |
| | | throw new ServiceException("无权操作其他公司的数据"); |
| | | } |
| | | } |
| | | |
| | |
| | | return sysUser != null && Objects.equals(PARTNER_ROLE_TYPE, sysUser.getRoleType()); |
| | | } |
| | | |
| | | private boolean isCompany(SysUser sysUser) { |
| | | return sysUser != null && Objects.equals(COMPANY_ROLE_TYPE, sysUser.getRoleType()) |
| | | && sysUser.getObjectId() != null; |
| | | } |
| | | |
| | | private List<Long> impossibleUserIds() { |
| | | return Collections.singletonList(IMPOSSIBLE_USER_ID); |
| | | } |