forked from luyan/epmet-cloud-lingshan
38 changed files with 809 additions and 98 deletions
@ -0,0 +1,9 @@ |
|||||
|
package com.epmet.commons.mybatis.dto.form; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessSettingFormDTO { |
||||
|
private String roleId; |
||||
|
private String operationKey; |
||||
|
} |
@ -1,6 +1,9 @@ |
|||||
package com.epmet.commons.mybatis.constant; |
package com.epmet.commons.tools.constant; |
||||
|
|
||||
public class AccessSettingConstant { |
public class AccessSettingConstant { |
||||
|
|
||||
|
public static final String ON = "ON"; |
||||
|
|
||||
public static final String I_CREATED_KEY = "I_CREATED"; |
public static final String I_CREATED_KEY = "I_CREATED"; |
||||
public static final String I_CREATED_ON = "ON"; |
public static final String I_CREATED_ON = "ON"; |
||||
|
|
@ -0,0 +1,16 @@ |
|||||
|
package com.epmet.dto.form; |
||||
|
|
||||
|
import com.epmet.dto.result.AccessConfigOpesResultDTO; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigOpesFormDTO { |
||||
|
|
||||
|
@NotBlank(message = "角色ID不能为空") |
||||
|
private String roleId; |
||||
|
private List<AccessConfigOpesResultDTO> opes; |
||||
|
|
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package com.epmet.dto.form; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigSaveSettingDTO { |
||||
|
|
||||
|
@NotBlank(message = "角色ID不能为空") |
||||
|
private String roleId; |
||||
|
@NotBlank(message = "操作Key不能为空") |
||||
|
private String operationKey; |
||||
|
private Set<String> scopeKeys; |
||||
|
private Set<String> settingKeys; |
||||
|
|
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package com.epmet.dto.form; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigSettingFormDTO { |
||||
|
|
||||
|
@NotBlank(message = "角色ID不能为空") |
||||
|
private String roleId; |
||||
|
|
||||
|
@NotBlank(message = "操作的Key不能为空") |
||||
|
private String operationKey; |
||||
|
|
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
package com.epmet.dto.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigOpesResultDTO { |
||||
|
|
||||
|
private String operationKey; |
||||
|
private String operationName; |
||||
|
private String brief; |
||||
|
private Boolean assigned; |
||||
|
|
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.epmet.dto.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigOptionsResultDTO { |
||||
|
private List<AccessConfigScopeResultDTO> scopeOptions; |
||||
|
private List<AccessConfigSettingResultDTO> settingOptions; |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.epmet.dto.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigScopeResultDTO { |
||||
|
|
||||
|
private String scopeKey; |
||||
|
private String scopeName; |
||||
|
private String scopeIndex; |
||||
|
private String operationKey; |
||||
|
private String roleId; |
||||
|
private Boolean assigned; |
||||
|
|
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
package com.epmet.dto.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class AccessConfigSettingResultDTO { |
||||
|
|
||||
|
private String settingKey; |
||||
|
private String settingName; |
||||
|
private String roleId; |
||||
|
private Boolean assigned; |
||||
|
private String operationKey; |
||||
|
|
||||
|
} |
@ -0,0 +1,73 @@ |
|||||
|
package com.epmet.controller; |
||||
|
|
||||
|
import com.epmet.commons.tools.utils.Result; |
||||
|
import com.epmet.commons.tools.validator.ValidatorUtils; |
||||
|
import com.epmet.dto.form.AccessConfigOpesFormDTO; |
||||
|
import com.epmet.dto.form.AccessConfigSaveSettingDTO; |
||||
|
import com.epmet.dto.form.AccessConfigSettingFormDTO; |
||||
|
import com.epmet.dto.result.AccessConfigOpesResultDTO; |
||||
|
import com.epmet.dto.result.AccessConfigOptionsResultDTO; |
||||
|
import com.epmet.service.AccessConfigService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@RestController |
||||
|
@RequestMapping("config") |
||||
|
public class AccessConfigController { |
||||
|
|
||||
|
@Autowired |
||||
|
private AccessConfigService accessConfigService; |
||||
|
|
||||
|
/** |
||||
|
* 列出角色的操作列表(及该操作的scope范围) |
||||
|
* @param roleId |
||||
|
* @return |
||||
|
*/ |
||||
|
@PostMapping("roleopes/{roleId}") |
||||
|
public Result listRoleOperations(@PathVariable("roleId") String roleId) { |
||||
|
List<AccessConfigOpesResultDTO> opes = accessConfigService.listOpesByRole(roleId); |
||||
|
return new Result().ok(opes); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 保存角色的操作功能列表 |
||||
|
* @return |
||||
|
*/ |
||||
|
@PostMapping("saveroleopes") |
||||
|
public Result saveRoleOpes(@RequestBody AccessConfigOpesFormDTO formDTO) { |
||||
|
accessConfigService.saveRoleOpes(formDTO.getRoleId(), formDTO.getOpes()); |
||||
|
return new Result(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查询可配置项列表 |
||||
|
* @return |
||||
|
*/ |
||||
|
@PostMapping("settingoptions") |
||||
|
public Result listSettingoptions(@RequestBody AccessConfigSettingFormDTO settingFormDTO) { |
||||
|
ValidatorUtils.validateEntity(settingFormDTO); |
||||
|
AccessConfigOptionsResultDTO options = accessConfigService.listScopeItemsForAccessConfig(settingFormDTO.getRoleId(), settingFormDTO.getOperationKey()); |
||||
|
return new Result().ok(options); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 保存设置 |
||||
|
* @param settings |
||||
|
* @return |
||||
|
*/ |
||||
|
@PostMapping("savesettings") |
||||
|
public Result saveSettings(@RequestBody AccessConfigSaveSettingDTO settings) { |
||||
|
ValidatorUtils.validateEntity(settings); |
||||
|
String roleId = settings.getRoleId(); |
||||
|
String operationKey = settings.getOperationKey(); |
||||
|
Set<String> scopeKeys = settings.getScopeKeys(); |
||||
|
Set<String> settingKeys = settings.getSettingKeys(); |
||||
|
accessConfigService.saveSettings(roleId, operationKey, scopeKeys, settingKeys); |
||||
|
return new Result(); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
package com.epmet.service; |
||||
|
|
||||
|
import com.epmet.dto.result.AccessConfigOpesResultDTO; |
||||
|
import com.epmet.dto.result.AccessConfigOptionsResultDTO; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
public interface AccessConfigService { |
||||
|
List<AccessConfigOpesResultDTO> listOpesByRole(String roleId); |
||||
|
|
||||
|
void saveRoleOpes(String roleId, List<AccessConfigOpesResultDTO> opes); |
||||
|
|
||||
|
AccessConfigOptionsResultDTO listScopeItemsForAccessConfig(String roleId, String operationKey); |
||||
|
|
||||
|
void saveSettings(String roleId, String operationKey, Set<String> scopeKeys, Set<String> settingKeys); |
||||
|
} |
@ -0,0 +1,182 @@ |
|||||
|
package com.epmet.service.impl; |
||||
|
|
||||
|
import com.epmet.commons.tools.constant.AccessSettingConstant; |
||||
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
import com.epmet.dao.*; |
||||
|
import com.epmet.dto.result.*; |
||||
|
import com.epmet.entity.AccessSettingEntity; |
||||
|
import com.epmet.entity.RoleOperationEntity; |
||||
|
import com.epmet.entity.RoleScopeEntity; |
||||
|
import com.epmet.redis.RoleAccessSettingRedis; |
||||
|
import com.epmet.redis.RoleOpeScopeRedis; |
||||
|
import com.epmet.service.AccessConfigService; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.springframework.util.CollectionUtils; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Service |
||||
|
public class AccessConfigServiceImpl implements AccessConfigService { |
||||
|
|
||||
|
protected static final Logger logger = LoggerFactory.getLogger(AccessConfigServiceImpl.class); |
||||
|
|
||||
|
@Autowired |
||||
|
private RoleOpeScopeRedis roleOpeScopeRedis; |
||||
|
|
||||
|
@Autowired |
||||
|
private RoleOperationDao roleOperationDao; |
||||
|
|
||||
|
@Autowired |
||||
|
private RoleScopeDao roleScopeDao; |
||||
|
|
||||
|
@Autowired |
||||
|
private AccessSettingDao accessSettingDao; |
||||
|
|
||||
|
@Autowired |
||||
|
private RoleAccessSettingRedis roleAccessSettingRedis; |
||||
|
|
||||
|
@Override |
||||
|
public List<AccessConfigOpesResultDTO> listOpesByRole(String roleId) { |
||||
|
return roleOperationDao.listOpesForAccessConfig(roleId); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional(rollbackFor = Exception.class) |
||||
|
public void saveRoleOpes(String roleId, List<AccessConfigOpesResultDTO> opes) { |
||||
|
List<RoleOperationResultDTO> operationsDB = roleOperationDao.listOperationsByRoleId(roleId); |
||||
|
Set<String> opeKeysDB = operationsDB.stream().map(opeDB -> opeDB.getOperationKey()).collect(Collectors.toSet()); |
||||
|
Set<String> opeKeysForm = opes.stream().map(opeForm -> opeForm.getOperationKey()).collect(Collectors.toSet()); |
||||
|
|
||||
|
for (String s : opeKeysDB) { |
||||
|
if (!opeKeysForm.contains(s)) { |
||||
|
// 说明这个已经被取消
|
||||
|
roleOperationDao.deleteRoleOpe(roleId, s); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
for (String s : opeKeysForm) { |
||||
|
if (!opeKeysDB.contains(s)) { |
||||
|
// 说明这个是新勾选的
|
||||
|
if (roleOperationDao.getRoleOpe(roleId, s) != null) { |
||||
|
if (roleOperationDao.enableRoleOpe(roleId, s) == 0) { |
||||
|
logger.error("权限配置:启用权限失败,roleId:{}", roleId); |
||||
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
||||
|
} |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
RoleOperationEntity newRoleOpe = new RoleOperationEntity(); |
||||
|
newRoleOpe.setRoleId(roleId); |
||||
|
newRoleOpe.setOperationKey(s); |
||||
|
roleOperationDao.insert(newRoleOpe); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 失效Redis缓存
|
||||
|
roleOpeScopeRedis.delRoleAllOpeScopes(roleId); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public AccessConfigOptionsResultDTO listScopeItemsForAccessConfig(String roleId, String operationKey) { |
||||
|
List<AccessConfigScopeResultDTO> scopeOptions = roleScopeDao.listScopeOptionsForAccessConfig(roleId, operationKey); |
||||
|
List<AccessConfigSettingResultDTO > settingOptions = accessSettingDao.listSettingOptionsForAccessConfig(roleId, operationKey); |
||||
|
AccessConfigOptionsResultDTO options = new AccessConfigOptionsResultDTO(); |
||||
|
options.setScopeOptions(scopeOptions); |
||||
|
options.setSettingOptions(settingOptions); |
||||
|
return options; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional |
||||
|
public void saveSettings(String roleId, String operationKey, Set<String> scopeKeys, Set<String> settingKeys) { |
||||
|
saveScopeSettings(roleId, operationKey, scopeKeys); |
||||
|
saveAccessSettingSettings(roleId, operationKey, settingKeys); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 保存设置 |
||||
|
* 可以优化为:遍历时候直接删除或者新增,而不用新建settingKeys2Delete, settingKeys2Add变量 |
||||
|
* @param roleId |
||||
|
* @param operationKey |
||||
|
*/ |
||||
|
private void saveAccessSettingSettings(String roleId, String operationKey, Set<String> newSettingKeys) { |
||||
|
Set<String> settingKeysDB = accessSettingDao.listAccessSettingsByRoleId(roleId, operationKey) |
||||
|
.stream() |
||||
|
.map(setting -> setting.getSettingKey()) |
||||
|
.collect(Collectors.toSet()); |
||||
|
|
||||
|
Set<String> settingKeys2Delete = settingKeysDB.stream().filter(settingKeyDB -> !newSettingKeys.contains(settingKeyDB)).collect(Collectors.toSet()); |
||||
|
Set<String> settingKeys2Add = newSettingKeys.stream().filter(newSetting -> !settingKeysDB.contains(newSetting)).collect(Collectors.toSet()); |
||||
|
|
||||
|
// 删除
|
||||
|
if (!CollectionUtils.isEmpty(settingKeys2Delete)) { |
||||
|
accessSettingDao.delete(roleId, operationKey, settingKeys2Delete); |
||||
|
} |
||||
|
|
||||
|
// 新增
|
||||
|
if (!CollectionUtils.isEmpty(settingKeys2Add)) { |
||||
|
settingKeys2Add.forEach(settingKey -> { |
||||
|
if (accessSettingDao.get(roleId, operationKey, settingKey) != null) { |
||||
|
// 数据库中已有
|
||||
|
accessSettingDao.enable(roleId, operationKey, settingKey); |
||||
|
} else { |
||||
|
AccessSettingEntity newSetting = new AccessSettingEntity(); |
||||
|
newSetting.setRoleId(roleId); |
||||
|
newSetting.setOperationKey(operationKey); |
||||
|
newSetting.setSettingKey(settingKey); |
||||
|
newSetting.setSettingValue(AccessSettingConstant.ON); |
||||
|
accessSettingDao.insert(newSetting); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
// 清空redis缓存
|
||||
|
roleAccessSettingRedis.delete(roleId, operationKey); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 保存Scope设置 |
||||
|
* @param roleId |
||||
|
* @param operationKey |
||||
|
* @param scopeKeys |
||||
|
*/ |
||||
|
private void saveScopeSettings(String roleId, String operationKey, Set<String> scopeKeys) { |
||||
|
List<RoleScopeEntity> scopesDB = roleScopeDao.listScopeEntities(roleId, operationKey); |
||||
|
// 数据库中已有的scopeKey列表
|
||||
|
Set<String> scopeKeysDB = scopesDB.stream().map(scope -> scope.getScopeKey()).collect(Collectors.toSet()); |
||||
|
|
||||
|
Set<String> scopeKeys2Add = scopeKeys.stream().filter(scopeKey -> !scopeKeysDB.contains(scopeKey)).collect(Collectors.toSet()); |
||||
|
Set<String> scopeKeys2Remove = scopeKeysDB.stream().filter(scopeKeyDB -> !scopeKeys.contains(scopeKeyDB)).collect(Collectors.toSet()); |
||||
|
|
||||
|
// 添加/重新启用
|
||||
|
if (!CollectionUtils.isEmpty(scopeKeys2Add)) { |
||||
|
scopeKeys2Add.forEach(scopeKey -> { |
||||
|
RoleScopeEntity rsDB = roleScopeDao.getByRoleIdAndOpeKey(roleId, operationKey, scopeKey); |
||||
|
if (rsDB != null) { |
||||
|
roleScopeDao.enableByRoleIdAndOpeKey(roleId, operationKey, scopeKey); |
||||
|
} else { |
||||
|
RoleScopeEntity rs2Add = new RoleScopeEntity(); |
||||
|
rs2Add.setRoleId(roleId); |
||||
|
rs2Add.setOperationKey(operationKey); |
||||
|
rs2Add.setScopeKey(scopeKey); |
||||
|
roleScopeDao.insert(rs2Add); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
// 删除
|
||||
|
if (!CollectionUtils.isEmpty(scopeKeys2Remove)) { |
||||
|
roleScopeDao.deleteByRoleIdAndOpeKey(roleId, operationKey, scopeKeys2Remove); |
||||
|
} |
||||
|
|
||||
|
// 清空redis缓存
|
||||
|
roleOpeScopeRedis.delRoleAllOpeScopes(roleId); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue