wisdomisite-java/src/main/java/com/zhgd/xmgl/security/PermissionEvaluator.java

89 lines
3.9 KiB
Java
Raw Normal View History

2025-09-10 20:26:17 +08:00
package com.zhgd.xmgl.security;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhgd.xmgl.modules.basicdata.entity.Company;
import com.zhgd.xmgl.modules.basicdata.enums.SystemUserAccountTypeEnum;
import com.zhgd.xmgl.modules.basicdata.service.ICompanyService;
import com.zhgd.xmgl.modules.project.service.IProjectService;
import com.zhgd.xmgl.security.entity.UserInfo;
import com.zhgd.xmgl.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component("perm")
public class PermissionEvaluator {
@Lazy
@Autowired
private ICompanyService companyService;
@Lazy
@Autowired
private IProjectService projectService;
/**
* 有企业sn的权限
*
* @param sn 企业sn
* @return
*/
public boolean hasCompanySnAccess(String sn) {
UserInfo user = SecurityUtils.getUser();
if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_ADMINISTRATOR_ACCOUNT.getValue())) {
return companyService.hasCompanySnAccessBy1(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_DISTRICT_ACCOUNT.getValue())) {
return companyService.hasCompanySnAccessBy2(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_CITY_ACCOUNT.getValue())) {
return companyService.hasCompanySnAccessBy3(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_SUB_ACCOUNT.getValue())) {
return companyService.hasCompanySnAccessBy4(user.getUserId(), sn);
}
return false;
}
/**
* 有企业id的权限
*
* @param companyId 企业id
* @return
*/
public boolean hasCompanyIdAccess(String companyId) {
Company company = companyService.getById(companyId);
if (company == null) {
return false;
}
return hasCompanySnAccess(company.getCompanySn());
}
/**
* 有sn企业或项目的权限
*
* @param sn 企业或项目sn
* @return
*/
public boolean hasSnAccess(String sn) {
UserInfo user = SecurityUtils.getUser();
int c = companyService.count(new LambdaQueryWrapper<Company>()
.eq(Company::getCompanySn, sn));
if (c > 0) {
return hasCompanySnAccess(sn);
} else {
if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_ADMINISTRATOR_ACCOUNT.getValue())) {
return companyService.hasProjectSnAccessBy1(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_DISTRICT_ACCOUNT.getValue())) {
return companyService.hasProjectSnAccessBy2(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_CITY_ACCOUNT.getValue())) {
return companyService.hasProjectSnAccessBy3(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.ENTERPRISE_SUB_ACCOUNT.getValue())) {
return companyService.hasProjectSnAccessBy4(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.PROJECT_ACCOUNT.getValue())) {
return projectService.hasProjectSnAccess(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.PROJECT_SUB_ACCOUNT.getValue())) {
return projectService.hasProjectSnAccess(user.getUserId(), sn);
} else if (user.getAccountType().equals(SystemUserAccountTypeEnum.NEW_USER.getValue())) {
return projectService.hasProjectSnAccessByNewUser(user.getUserId(), sn);
}
}
return true;
}
}