123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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<String>): 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<String>): HashSet<Permission> {
- val result = hashSetOf<Permission>()
- 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")
- }
- }
- }
|