用户U1与用户U2共享笔记N1,用户U2与用户U3共享笔记。 当用户U1从笔记N1中删除用户U2时,用户U3也应该也看不到笔记。如何实现这个? 什么是我的模型看起来像 先感谢您 这就是我的表格的样子: -
ActiveRecord::Schema.define(version: 20170909050804) do
create_table "note_permissions", force: :cascade do |t|
t.integer "note_id", null: false
t.integer "student_id", null: false
t.integer "granted_by_id"
t.index ["granted_by_id"], name: "index_note_permissions_on_granted_by_id"
t.index ["note_id", "student_id", "granted_by_id"], name: "my_index", unique: true
t.index ["note_id"], name: "index_note_permissions_on_note_id"
t.index ["student_id"], name: "index_note_permissions_on_student_id"
end
create_table "notes", force: :cascade do |t|
t.string "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "students", force: :cascade do |t|
t.string "name"
t.integer "rollnumber"
t.string "subject"
t.string "room_no"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
这是我的模特
class Note < ApplicationRecord
has_many :note_permissions
end
class NotePermission < ApplicationRecord
belongs_to :note
belongs_to :student
belongs_to :granted_by, class_name: "NotePermission", dependent: :destroy
end
class Student < ApplicationRecord
has_many :note_permissions
has_many :notes, through: :note_permissions
end
答案 0 :(得分:0)
所以你有一个权限树,从U1
授予他们(U2
)权限的访问权限U1
开始,U2
授予U3
反过来。
这可以通过对注释具有单个root权限(对于U1
),然后使用外键通过另一个权限授予所有其他权限
类似的东西:
create_table :notes do |t|
# fields
end
create_table :note_permissions do |t|
t.belongs_to :note, null: false, foreign_key: true, on_delete: :cascade
t.belongs_to :user, null: false, foreign_key: true, on_delete: :cascade
t.belongs_to :granted_by, null: true
t.foreign_key :note_permissions, column: :granted_by_id, on_delete: :cascade
t.index [:note_id, :user_id, :granted_by_id], unique: true
end
class Note < ApplicationRecord
has_many :note_permissions
end
class NotePermission < ApplicationRecord
belongs_to :note
belongs_to :user
belongs_to :granted_by, class_name: "NotePermission", dependent: :destroy
end
class User < ApplicationRecord
has_many :note_permissions
has_many :notes, through: :note_permissions
end
现在,当用户创建记事时,也会创建匹配的NotePermission
(granted_by: nil
,因此根目录。)
# Plus other fields you want, like its contents
# Create a note with user_1
note = Note.create(note_permissions: [NotePermission.new(user: user_1)])
会做类似的事情:
BEGIN
INSERT INTO "notes" DEFAULT VALUES RETURNING "id"
INSERT INTO "note_permissions" ("note_id", "user_id") VALUES ($1, $2) RETURNING "id" [["note_id", 2], ["user_id", 1]]
COMMIT
任何NotePermission
条记录与其用户ID匹配的用户都有权访问权限,当他们授予其他人权限时,请将NotePermission
记录用作granted_by
。
user_1.notes.find 1 # note
user_2.notes.find 1 # ActiveRecordNotFound
# user_1 grants user_2 permission
NotePermission.create!(note: note, user: user_2, granted_by: user_1.note_permissions.find_by(note: note))
# user_2 can now see
user_2.notes.find 1 # note
# user_2 grants user_3 permission
NotePermission.create!(note: note, user: user_3, granted_by: user_2.note_permissions.find_by(note: note))
您可能希望禁止两次授予用户权限(更改唯一约束),例如U1 -> U2 -> U4
和U1 -> U3 -> U4
),或者您需要在U4
授予U5
时处理多条路径(因为您现在有U1 -> U2 -> U4 -> U5
AND U1 -> U3 -> U4 -> U5
)
当您删除NotePermission
时,您将通过granted_by
对他们授予的任何内容失效。
# Now have 3 permissions
NotePermission.all
NotePermission id: 1, note_id: 1, user_id: 1, granted_by_id: nil
NotePermission id: 2, note_id: 1, user_id: 2, granted_by_id: 1
NotePermission id: 3, note_id: 1, user_id: 3, granted_by_id: 2
# user_1 removes user_2
NotePermission.where(note: note, user_id: 2, granted_by: user_1.note_permissions.find_by(note: note)).delete_all
# Executes `DELETE FROM "note_permissions" WHERE "note_permissions"."note_id" = 1 AND "note_permissions"."user_id" = 1 AND "note_permissions"."granted_by_id" = 1`
# Because of the cascade, now have
NotePermission.all
NotePermission id: 1, note_id: 1, user_id: 1, granted_by_id: nil
# So the deletion of record 2, also caused the deletion of record 3 because of the granted_by_id foreign key