feat: private materials with per-user shares

A material may be private: only its author and the users it is shared
with see it. Every student read (list, detail, course detail and
counters, the checklist linked material, the token twins) goes through
one visibility fragment. Existing materials stay public with no author.

GET/PUT /_bm/api/v1/materials/{id}/access let the author, a material
manager, toggle the flag and pick the users; the first change of an
author-less material makes the caller its author.
This commit is contained in:
Jakub Zych
2026-11-08 11:52:47 +01:00
parent 5ed02ed9f7
commit 418f8240a4
14 changed files with 688 additions and 65 deletions

View File

@@ -0,0 +1,36 @@
package updates
import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// AddPrivacyToMaterials returns the
// 20261108120000_add_privacy_to_jz_bm_materials gormigrate entry: a material
// may be private, and then only its author and the users it is shared with
// see it. Existing materials stay public and have no author. Deleting the
// author keeps the material and clears the author; deleting a material or a
// user drops their shares.
func AddPrivacyToMaterials() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20261108120000_add_privacy_to_jz_bm_materials",
Migrate: func(tx *gorm.DB) error {
return execAll(tx, `
ALTER TABLE jz_bm_materials ADD COLUMN is_private BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE jz_bm_materials ADD COLUMN author_user_id INTEGER NULL REFERENCES users (id) ON DELETE SET NULL;
CREATE INDEX jz_bm_materials_author_user_id_index ON jz_bm_materials (author_user_id);
CREATE TABLE jz_bm_material_shares (
material_id BIGINT NOT NULL REFERENCES jz_bm_materials (id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
PRIMARY KEY (material_id, user_id)
);
CREATE INDEX jz_bm_material_shares_user_id_index ON jz_bm_material_shares (user_id)`)
},
Rollback: func(tx *gorm.DB) error {
return execAll(tx, `
DROP TABLE IF EXISTS jz_bm_material_shares;
ALTER TABLE jz_bm_materials DROP COLUMN IF EXISTS author_user_id;
ALTER TABLE jz_bm_materials DROP COLUMN IF EXISTS is_private`)
},
}
}