package main import ( "strconv" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v5/pgxpool" ) // ---------- 对象实例 ---------- func (a *app) listObjects(c *gin.Context) { typ := c.Query("type") q := c.Query("q") limit, _ := strconv.Atoi(c.Query("limit")) if limit <= 0 || limit > 500 { limit = 100 } sqlStr := `SELECT id, object_type_id, type_api_name, pk, properties_json, created_at, updated_at FROM objects WHERE 1=1` args := []interface{}{} if typ != "" { args = append(args, typ) sqlStr += ` AND type_api_name=$` + strconv.Itoa(len(args)) } if q != "" { args = append(args, "%"+q+"%") sqlStr += ` AND properties_json::text ILIKE $` + strconv.Itoa(len(args)) } args = append(args, limit) sqlStr += ` ORDER BY id DESC LIMIT $` + strconv.Itoa(len(args)) rows, err := a.pool.Query(c, sqlStr, args...) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } defer rows.Close() authz, principal := a.authzOf(c) var out []Object for rows.Next() { var o Object var props []byte if err := rows.Scan(&o.ID, &o.ObjectID, &o.TypeAPIName, &o.PK, &props, &o.CreatedAt, &o.UpdatedAt); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } mks := a.loadObjectMarkings(c, o.TypeAPIName, o.PK) if !authz.CanRead(o.TypeAPIName, o.PK, mks) { continue } o.Properties = decodeProps(props) authz.ApplyFieldPolicy(o.TypeAPIName, o.Properties) out = append(out, o) } c.JSON(200, gin.H{"items": out, "count": len(out), "principal": principal}) } func (a *app) getObject(c *gin.Context) { typ := c.Param("type") pk := c.Param("pk") authz, principal := a.authzOf(c) o, err := loadObject(c, a.pool, typ, pk) if err != nil { c.JSON(404, gin.H{"error": "对象实例不存在"}) return } mks := a.loadObjectMarkings(c, typ, pk) if !authz.CanRead(typ, pk, mks) { c.JSON(404, gin.H{"error": "对象实例不存在", "principal": principal}) return } if o.Properties != nil { authz.ApplyFieldPolicy(typ, o.Properties) } c.JSON(200, o) } type objectInput struct { Type string `json:"type"` PK string `json:"pk"` Properties map[string]interface{} `json:"properties"` } func (a *app) createObject(c *gin.Context) { var in objectInput if err := c.ShouldBindJSON(&in); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } if in.Type == "" || in.PK == "" { c.JSON(400, gin.H{"error": "type 与 pk 必填"}) return } authz, _ := a.authzOf(c) if !authz.CanWrite("write", in.Type, in.PK) { a.abortForbidden(c, "无 create 权限") return } var oid int64 if err := a.pool.QueryRow(c, `SELECT id FROM object_types WHERE api_name=$1`, in.Type).Scan(&oid); err != nil { c.JSON(400, gin.H{"error": "对象类型不存在: " + in.Type}) return } props := "{}" if len(in.Properties) > 0 { props = mustJSON(in.Properties) } nameVal, _ := in.Properties["name"].(string) _, err := a.pool.Exec(c, `INSERT INTO objects(object_type_id,type_api_name,pk,name,properties_json) VALUES($1,$2,$3,$4,$5::jsonb) ON CONFLICT(object_type_id,pk) DO UPDATE SET name=EXCLUDED.name, properties_json=EXCLUDED.properties_json, updated_at=now()`, oid, in.Type, in.PK, nameVal, props) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(201, gin.H{"ok": true, "type": in.Type, "pk": in.PK}) } func (a *app) updateObject(c *gin.Context) { typ := c.Param("type") pk := c.Param("pk") var in objectInput if err := c.ShouldBindJSON(&in); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } authz, _ := a.authzOf(c) if !authz.CanWrite("write", typ, pk) { a.abortForbidden(c, "无 write 权限") return } props := "{}" if len(in.Properties) > 0 { props = mustJSON(in.Properties) } nameVal, _ := in.Properties["name"].(string) tag, err := a.pool.Exec(c, `UPDATE objects SET name=$1, properties_json=$2::jsonb, updated_at=now() WHERE type_api_name=$3 AND pk=$4`, nameVal, props, typ, pk) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } if tag.RowsAffected() == 0 { c.JSON(404, gin.H{"error": "对象实例不存在"}) return } c.JSON(200, gin.H{"ok": true}) } func (a *app) deleteObject(c *gin.Context) { typ := c.Param("type") pk := c.Param("pk") authz, _ := a.authzOf(c) if !authz.CanWrite("delete", typ, pk) { a.abortForbidden(c, "无 delete 权限") return } tag, err := a.pool.Exec(c, `DELETE FROM objects WHERE type_api_name=$1 AND pk=$2`, typ, pk) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } if tag.RowsAffected() == 0 { c.JSON(404, gin.H{"error": "对象实例不存在"}) return } c.JSON(200, gin.H{"ok": true}) } func loadObject(c *gin.Context, p *pgxpool.Pool, typ, pk string) (*Object, error) { var o Object var props []byte err := p.QueryRow(c, `SELECT id, object_type_id, type_api_name, pk, properties_json, created_at, updated_at FROM objects WHERE type_api_name=$1 AND pk=$2`, typ, pk). Scan(&o.ID, &o.ObjectID, &o.TypeAPIName, &o.PK, &props, &o.CreatedAt, &o.UpdatedAt) if err != nil { return nil, err } o.Properties = decodeProps(props) return &o, nil } // ---------- 链接实例 ---------- type linkInput struct { Type string `json:"type"` SourceType string `json:"source_type"` SourcePK string `json:"source_pk"` TargetType string `json:"target_type"` TargetPK string `json:"target_pk"` } func (a *app) createLink(c *gin.Context) { var in linkInput if err := c.ShouldBindJSON(&in); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } if in.Type == "" || in.SourceType == "" || in.SourcePK == "" || in.TargetType == "" || in.TargetPK == "" { c.JSON(400, gin.H{"error": "type/source/target 必填"}) return } authz, _ := a.authzOf(c) if !authz.CanWrite("write", in.SourceType, in.SourcePK) || !authz.CanWrite("write", in.TargetType, in.TargetPK) { a.abortForbidden(c, "无 write 权限创建此链接") return } // 校验类型存在 var cnt int if err := a.pool.QueryRow(c, `SELECT count(*) FROM link_types WHERE api_name=$1`, in.Type).Scan(&cnt); err != nil || cnt == 0 { c.JSON(400, gin.H{"error": "链接类型不存在: " + in.Type}) return } _, err := a.pool.Exec(c, `INSERT INTO links(link_type,source_type,source_pk,target_type,target_pk) VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING`, in.Type, in.SourceType, in.SourcePK, in.TargetType, in.TargetPK) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(201, gin.H{"ok": true}) } func (a *app) deleteLink(c *gin.Context) { authz, _ := a.authzOf(c) id, _ := strconv.ParseInt(c.Param("id"), 10, 64) // 先查链接属于哪些对象 var st, spk, tt, tpk string if err := a.pool.QueryRow(c, `SELECT source_type, source_pk, target_type, target_pk FROM links WHERE id=$1`, id).Scan(&st, &spk, &tt, &tpk); err == nil { if !authz.CanWrite("delete", st, spk) || !authz.CanWrite("delete", tt, tpk) { a.abortForbidden(c, "无 delete 权限") return } } tag, err := a.pool.Exec(c, `DELETE FROM links WHERE id=$1`, id) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } if tag.RowsAffected() == 0 { c.JSON(404, gin.H{"error": "链接不存在"}) return } c.JSON(200, gin.H{"ok": true}) }