【问题标题】:Learning about constraints in Alloy了解合金中的约束
【发布时间】:2019-12-05 19:43:37
【问题描述】:

我正在为我的公司黑客马拉松探索 Alloy。我们有一个复杂的数据模型,我的目标是生成正确示例的图片,以便新员工可以看到它们并了解我们的应用程序。我设法偶然发现并生成了一些图片,但我无法表达以下想法:

角色可以授予对一个或多个字段的访问权限。 用户拥有一个或多个角色,并且可以访问零个或多个字段。当且仅当用户具有授予对该字段的访问权限的角色时,用户才能访问该字段。

这是语法错误的众多尝试之一;希望它能说明我对 Alloy 的工作原理有哪些误解。

sig Role { grantsAccess: some Field }
sig User { 
    has: some Role,
    canAccess: Field    
}{
    all u: User |
    f: Field in u.canAccess iff some r: Role in u.has | f in r.grantsAccess
}

谢谢!

【问题讨论】:

    标签: alloy


    【解决方案1】:
    -- A role can grant access to one or more fields. 
    
    sig Role { grant : some Field } 
    sig Field {}
    
    -- A user has one or more roles, 
    -- and can access zero or more fields. 
    
    sig User {
        roles : set Role
    }
    
    -- A user can access a field    
    
    pred User.canAccess[ field : Field ] {
    
        -- if and only if the user has a role 
        -- which grants access to that field.
    
        some role : this.roles | field in role.grant
    
        // alternative:
        // some ( this.roles & grant.field )
    }
    
    run ex1 { 
        some u : User, f : Field {
            u.canAccess[f]
        }
    }
    

    【讨论】:

    • 我认为用户具有“某些角色”,并且角色授予对“一组字段”的访问权限。但是我理解您为什么这样做,规范听起来有些不完整。为什么用户可以访问零个或多个字段而不是一个或多个字段,知道用户具有一个或多个角色并且角色授予对一个或多个字段的访问权限...
    • 我尽量按照他的规范literal。请注意,cmets 正是他指定的。我可能不会说一个角色有some 字段。在实践中,这使得系统初始化/设置非常尴尬。
    • 是的,我们同意。不过,根据规范,我会定义一个用户拥有some 角色。这意味着用户永远不能访问零字段,尽管规范另有说明......
    • 但这些是模型应该告知您的关于您的规格的见解:-)
    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多