security_policy_keyword.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package schema
  2. import (
  3. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect/entsql"
  6. "entgo.io/ent/schema"
  7. "entgo.io/ent/schema/edge"
  8. "entgo.io/ent/schema/field"
  9. "entgo.io/ent/schema/index"
  10. )
  11. // SecurityPolicyKeyword 是分组安全策略的自定义关键词。
  12. // group_id 为 NULL 表示全局词(对所有开启安全策略的分组生效),
  13. // 非 NULL 表示仅对指定分组生效。关键词与内置 seed 词包合并后参与匹配。
  14. type SecurityPolicyKeyword struct {
  15. ent.Schema
  16. }
  17. func (SecurityPolicyKeyword) Annotations() []schema.Annotation {
  18. return []schema.Annotation{
  19. entsql.Annotation{Table: "security_policy_keywords"},
  20. }
  21. }
  22. func (SecurityPolicyKeyword) Mixin() []ent.Mixin {
  23. return []ent.Mixin{
  24. mixins.TimeMixin{},
  25. mixins.SoftDeleteMixin{},
  26. }
  27. }
  28. func (SecurityPolicyKeyword) Fields() []ent.Field {
  29. return []ent.Field{
  30. // group_id: NULL = 全局词,非 NULL = 仅指定分组生效。
  31. field.Int64("group_id").
  32. Optional().
  33. Nillable(),
  34. field.String("keyword").
  35. MaxLen(200).
  36. NotEmpty().
  37. Comment("关键词原文,匹配时大小写不敏感"),
  38. field.String("category").
  39. MaxLen(50).
  40. Default("").
  41. Comment("分类:crack/reverse/pentest/privesc/evasion/custom"),
  42. field.Bool("enabled").
  43. Default(true).
  44. Comment("是否参与匹配"),
  45. }
  46. }
  47. func (SecurityPolicyKeyword) Edges() []ent.Edge {
  48. return []ent.Edge{
  49. edge.To("group", Group.Type).
  50. Field("group_id").
  51. Unique(),
  52. }
  53. }
  54. func (SecurityPolicyKeyword) Indexes() []ent.Index {
  55. return []ent.Index{
  56. // 全局词按 keyword 唯一(未删除)。
  57. index.Fields("keyword").
  58. Unique().
  59. StorageKey("idx_secpol_kw_global_unique").
  60. Annotations(entsql.IndexWhere("group_id IS NULL AND deleted_at IS NULL")),
  61. // 分组词按 (group_id, keyword) 唯一(未删除)。
  62. index.Fields("group_id", "keyword").
  63. Unique().
  64. StorageKey("idx_secpol_kw_group_unique").
  65. Annotations(entsql.IndexWhere("group_id IS NOT NULL AND deleted_at IS NULL")),
  66. index.Fields("group_id"),
  67. index.Fields("enabled"),
  68. index.Fields("deleted_at"),
  69. }
  70. }