package com.gxzc.zen.umps.util import com.gxzc.zen.umps.common.Permission import org.slf4j.LoggerFactory import org.springframework.util.Assert /** * * @author NorthLan * @date 2018/2/6 * @url https://noahlan.com */ object PermissionUtil { private val logger = LoggerFactory.getLogger(PermissionUtil::class.java) /** * 权限判定 * @param needPermString 需要的权限 * @param userPerm 用户权限列表 */ fun isPermitted(needPermString: String, userPerm: HashSet): Boolean { try { assertPermissionString(needPermString) val userPermSet = convertToPermissionSet(userPerm) val needPerm = convertToPermission(needPermString) // 验证逻辑 platformId=0 为通用(超级)权限 userPermSet .filter { it.perm == needPerm.perm && verifyCRUD(needPerm.crud, it.crud) } .forEach { // 验证平台 return if (it.platformId == 0) { true } else { it.platformId == needPerm.platformId } } } catch (e: RuntimeException) { logger.error("权限验证系统错误...", e) return false } return false } /** * 验证 CRUD 四项权限 */ private fun verifyCRUD(needCRUD: String, userCRUD: String): Boolean { needCRUD.forEach { if (it !in userCRUD) { return false } } return true } /** * 批量将权限串转换为权限实体 */ private fun convertToPermissionSet(perms: HashSet): HashSet { val result = hashSetOf() perms.forEach({ result.add(convertToPermission(it)) }) return result } /** * 将权限串转换为权限实体 */ private fun convertToPermission(str: String): Permission { return Permission(str) } private fun assertPermissionString(str: String) { Assert.notNull(str, "Permission string not be null.") val splitList = str.split(':') if (splitList.size < 3) { throw RuntimeException("permission字符串错误,必须为:分割且长度等于3") } } }