注意:我知道可以查询Item.all
__,然后遍历数组并只提取.has_permissions == User
__中的项,但这完全忽略了在图中包含所有内容的好处,因此不是答案。
为了保持这个简单,假设有三个对象:
典型图形情况:
(User)<-[:HAS_PERMISSIONS]-(Item)
(Group)<-[:HAS_PERMISSIONS]-(Item)
(Group)-[:HAS_MEMBERS]->(User)
与模型:
class Item
include Neo4j::ActiveNode
property :name, type: String
property :description, type: String
has_many :out, :user_permission_to, type: :PERMISSION_TO, model_class: :User
has_many :out, :group_permission_to, type: :PERMISSION_TO, model_class: :Group
end
class Identity
include Neo4j::ActiveNode
property :username, type: String
has_many :in, :permission_to, type: :PERMISSION_TO, model_class: :Item
has_many :in, :groups, type: :GROUP_MEMBER, model_class: :Group
end
class Group
include Neo4j::ActiveNode
property :group_name, type: String
has_many :in, :permission_to, type: :PERMISSION_TO, model_class: :Item
has_many :out, :members, type: :GROUP_MEMBER, model_class: :User
end
对于简单的控制器:
# GET /items
def index
@items = Item.all
render json: @items
end
# GET /item/1
def show
render json: @item
end
发布于 2017-01-08 21:53:50
首先,我建议查看这篇文章 (下半部分涉及非常类似的访问控制)。
“如何返回用户拥有权限的项目的索引?”
你可以用几种方法。更明确地:
identity.as(:id).query.match("(id)<-[PERMISSION_TO*1..2]-(item:Item)").pluck(:item)
或者,我认为这样做是可行的:
identity.permission_to(rel_length: 1..2)
“如何返回用户所在组具有权限的项目的索引?”
简单:
identity.groups.permission_to
“仅当用户拥有权限时,如何返回单个项?”
对于上述两种解决办法:
identity.as(:id).query.match("(id)<-[PERMISSION_TO*1..2]-(item:Item)").limit(1).pluck(:item)
# or
identity.permission_to(rel_length: 1..2).first
“仅当用户所在的组具有权限时,如何返回单个项?”
identity.groups.permission_to
另外,一些反馈意见:
使用“索引”一词的方式有点混乱,因为Neo4j有允许在标签上对属性进行性能查询的索引。
我可能会像这样做我的模特:
class Item
include Neo4j::ActiveNode
property :name, type: String
property :description, type: String
has_many :in, :users_with_permission, type: :CAN_ACCESS, model_class: :Identity
has_many :in, :groups_with_permission, type: :CAN_ACCESS, model_class: :Group
end
class Identity
include Neo4j::ActiveNode
property :username, type: String
has_many :out, :accessible_items, type: :CAN_ACCESS, model_class: :Item
has_many :out, :groups, type: :IN_GROUP # Don't need `model_class: :Group` here
end
class Group
include Neo4j::ActiveNode
property :group_name, type: String
has_many :out, :accessible_items, type: :CAN_ACCESS, model_class: :Item
has_many :in, :members, type: :IN_GROUP, model_class: :Identity
# You could also do:
# has_many :in, :members, model_class: :Identity, origin: :groups
end
https://stackoverflow.com/questions/41524340
复制相似问题