Files
suixinkan_ios_new/suixinkan/App/State/PermissionContext.swift
汉秋 5692134efc Align iOS home common apps and all-functions page with Android.
Add unified HomeCommonMenu/HomeAllFunctions diagnostics, rebuild all-functions from top-level menuList permissions, and document Home and AllFunctions module logic.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-30 09:40:02 +08:00

159 lines
6.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.

//
// PermissionContext.swift
// suixinkan
//
// Created by Codex on 2026/6/22.
//
import Foundation
import Combine
@MainActor
/// URI
final class PermissionContext: ObservableObject {
@Published private(set) var rolePermissions: [RolePermissionResponse] = []
@Published private(set) var permissionURIs: Set<String> = []
@Published var currentRole: RoleInfo?
/// App role_code legacy id
var currentAppRole: AppRoleCode? {
if let code = AppRoleCode.fromCode(currentRole?.roleCode) { return code }
if let roleId = currentRole?.id, let code = AppRoleCode.fromLegacyRoleId(roleId) { return code }
return AppRoleCode.fromRoleName(currentRole?.name)
}
/// role_code
func replaceRolePermissions(
_ rolePermissions: [RolePermissionResponse],
currentRoleCode: String? = nil,
cachedLegacyRoleId: Int? = nil,
roleName: String? = nil
) {
self.rolePermissions = rolePermissions
let resolvedCode = RolePermissionMatching.resolveRoleCode(
roleCode: currentRoleCode,
roleId: cachedLegacyRoleId,
roleName: roleName,
permissions: rolePermissions
)?.rawValue ?? currentRoleCode
if let matched = RolePermissionMatching.match(
in: rolePermissions,
roleCode: resolvedCode,
roleId: cachedLegacyRoleId,
roleName: roleName ?? currentRole?.name
) {
currentRole = matched.role
} else if let currentRole,
let matched = rolePermissions.first(where: { $0.role.id == currentRole.id }) {
self.currentRole = matched.role
} else {
currentRole = rolePermissions.first?.role
}
permissionURIs = Set(Self.flattenPermissions(currentRole?.permission ?? []))
}
///
func selectRole(code: AppRoleCode, accountContext: AccountContext) {
guard let rolePermission = rolePermissions.first(where: {
AppRoleCode.fromCode($0.role.roleCode) == code
}) else { return }
applyRoleSelection(rolePermission, accountContext: accountContext)
}
/// legacy id使 API role id
func selectRole(id roleId: Int, accountContext: AccountContext) {
guard let rolePermission = rolePermissions.first(where: { $0.role.id == roleId }) else { return }
applyRoleSelection(rolePermission, accountContext: accountContext)
}
/// URI
func canAccess(_ uri: String) -> Bool {
permissionURIs.contains(uri.trimmingCharacters(in: .whitespacesAndNewlines))
}
/// URI API
func topLevelPermissionURIs(for roleCode: String?) -> [String] {
topLevelPermissions(for: roleCode).map(\.uri)
}
/// API
func topLevelPermissions(for roleCode: String?) -> [PermissionItem] {
guard let roleCode,
let rolePermission = matchedRolePermission(roleCode: roleCode) else {
return []
}
return rolePermission.role.permission.filter { item in
!item.uri.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}
///
func currentRoleScenicScopes() -> [BusinessScope] {
guard let currentRole else { return [] }
guard let rolePermission = matchedRolePermission(roleCode: currentRole.roleCode)
?? rolePermissions.first(where: { $0.role.id == currentRole.id }) else {
return []
}
return Self.uniqueScenicScopes(from: rolePermission.scenic)
}
/// 退
func reset() {
rolePermissions = []
permissionURIs = []
currentRole = nil
}
///
private func applyRoleSelection(_ rolePermission: RolePermissionResponse, accountContext: AccountContext) {
currentRole = rolePermission.role
permissionURIs = Set(Self.flattenPermissions(rolePermission.role.permission))
let scenicScopes = Self.uniqueScenicScopes(from: rolePermission.scenic)
accountContext.replaceScopes(
scenic: scenicScopes,
stores: accountContext.storeScopes,
currentScenicId: accountContext.currentScenic?.id,
currentStoreId: accountContext.currentStore?.id
)
}
/// role_code
private func matchedRolePermission(roleCode: String) -> RolePermissionResponse? {
RolePermissionMatching.match(
in: rolePermissions,
roleCode: roleCode,
roleId: nil,
roleName: nil
)
}
/// URI
private static func flattenPermissions(_ permissions: [PermissionItem]) -> [String] {
permissions.flatMap { item -> [String] in
let current = item.uri.trimmingCharacters(in: .whitespacesAndNewlines)
let children = flattenPermissions(item.children)
return current.isEmpty ? children : [current] + children
}
}
///
private static func uniqueScenicScopes(from scenics: [ScenicInfo]) -> [BusinessScope] {
var seen = Set<Int>()
return scenics.compactMap { scenic in
guard seen.insert(scenic.id).inserted else { return nil }
return BusinessScope(
id: scenic.id,
name: scenic.name,
kind: .scenic,
status: scenic.status,
address: scenic.location?.address,
latitude: scenic.location?.lat,
longitude: scenic.location?.lng,
coverURLString: scenic.coverImg
)
}
}
}