OpenFGA模型报错修复:非直接关联无法与from结合的问题
修复OpenFGA模型的验证错误
问题原因
你遇到的错误是因为OpenFGA不允许将派生关系(通过and/or等逻辑组合定义的关系)作为from语法的引用源。entity类型中的can_read_relation是subject_relation and can_read_relation from defines组合出来的派生关系,并非直接关联到某个对象的关系,因此不能用actor_relation from can_read_relation这种写法。
修复方案
我们可以将派生关系的逻辑直接整合到can_read的定义中,绕过对中间派生关系的引用。以下是修复后的完整模型:
schema 1.1 type user type relation relations define actor_relation: [user] type relation_type relations define type: [relation] type entity_type relations define can_read_relation_type: [relation_type] define can_read_relation: type from can_read_relation_type type entity relations define subject_relation: [relation] define defines: [entity_type] # 保留原派生关系用于其他场景(如果需要) define can_read_relation: subject_relation and can_read_relation from defines # 直接整合逻辑,避免引用派生关系作为from源 define can_read: [user] or (actor_relation from subject_relation and can_read_relation from defines)
替代优化方案
如果不需要保留can_read_relation这个中间派生关系,也可以简化模型结构,直接将逻辑合并到can_read中,让模型更简洁:
schema 1.1 type user type relation relations define actor_relation: [user] type relation_type relations define type: [relation] type entity_type relations define allowed_read_relation_types: [relation_type] define allowed_read_relations: type from allowed_read_relation_types type entity relations define subject_relation: [relation] define entity_type: [entity_type] define can_read: [user] or (actor_relation from subject_relation and allowed_read_relations from entity_type)
核心逻辑说明
修复的关键是:from语法只能引用直接关联对象的关系(即定义为[type]的关系,比如subject_relation: [relation]、defines: [entity_type]这类),不能引用通过逻辑组合生成的派生关系。我们需要把派生关系里的条件直接写到最终的权限定义中,确保from引用的都是合法的直接关联关系。
内容的提问来源于stack exchange,提问作者Frederik D
相关产品推荐
相关产品推荐

