Files
suixinkan_uikit/suixinkan/Features/Home/Services/RolePermissionMatcher.swift

40 lines
1.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// RolePermissionMatcher.swift
// suixinkan
//
import Foundation
/// `role-permission`
enum RolePermissionMatcher {
/// role_code legacy id role_name
static func match(
in list: [RolePermissionResponse],
roleCode: String,
roleName: String,
legacyRoleId: Int = 0
) -> RolePermissionResponse? {
if let role = AppRoleCode.fromCode(roleCode),
let matched = list.first(where: { AppRoleCode.fromCode($0.role.roleCode) == role }) {
return matched
}
if legacyRoleId > 0,
let matched = list.first(where: { $0.role.id == legacyRoleId }) {
return matched
}
let trimmedName = roleName.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmedName.isEmpty {
return list.first { item in
let itemName = item.role.name.trimmingCharacters(in: .whitespacesAndNewlines)
guard !itemName.isEmpty else { return false }
return itemName.contains(trimmedName) || trimmedName.contains(itemName)
}
}
return nil
}
}