【问题标题】:Neo4j and Ruby/Rails: How to only return nodes based on user permissionsNeo4j 和 Ruby/Rails:如何仅根据用户权限返回节点
【发布时间】:2017-01-07 17:27:35
【问题描述】:
  1. 如何仅返回用户有权限的项目的索引?
  2. 如何仅返回用户所在组具有权限的项目的索引?
  3. 如何仅在用户有权限的情况下返回单个项目?
  4. 如何仅在用户所在的组有权限的情况下返回单个项目?

注意:我知道可以查询 Item.all,然后遍历数组并仅提取 .has_permissions == User 所在的项目,但这完全忽略了将所有内容都放在图表中的好处,所以不是一个答案。


为了简单起见,假设有 3 个对象:

  • 一件物品
  • 一个用户
  • 一组

典型的图表情况:

(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

【问题讨论】:

  • 你能分享你的模型吗?
  • 当然;添加到问题中。

标签: ruby neo4j graph-databases neo4j.rb


【解决方案1】:

对于初学者,我建议查看this article(后半部分涉及非常相似的访问控制)

“如何只返回用户有权限的项目的索引?”

您可以通过多种方式做到这一点。更明确地说:

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

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多