generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
groupmembership.go
111 lines (99 loc) · 2.65 KB
/
groupmembership.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package schema
import (
"context"
"entgo.io/contrib/entgql"
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
emixin "github.com/datumforge/entx/mixin"
"github.com/datumforge/fgax/entfga"
"github.com/datumforge/datum/internal/ent/enums"
"github.com/datumforge/datum/internal/ent/generated"
"github.com/datumforge/datum/internal/ent/generated/privacy"
"github.com/datumforge/datum/internal/ent/hooks"
"github.com/datumforge/datum/internal/ent/mixin"
)
// GroupMembership holds the schema definition for the GroupMembership entity
type GroupMembership struct {
ent.Schema
}
// Fields of the GroupMembership
func (GroupMembership) Fields() []ent.Field {
return []ent.Field{
field.Enum("role").
GoType(enums.Role("")).
Default(string(enums.RoleMember)),
field.String("group_id").Immutable(),
field.String("user_id").Immutable(),
}
}
// Edges of the GroupMembership
func (GroupMembership) Edges() []ent.Edge {
return []ent.Edge{
edge.To("group", Group.Type).
Field("group_id").
Required().
Unique().
Immutable(),
edge.To("user", User.Type).
Field("user_id").
Required().
Unique().
Immutable(),
edge.To("events", Event.Type),
}
}
// Annotations of the GroupMembership
func (GroupMembership) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.RelayConnection(),
entgql.QueryField(),
entgql.Mutations(entgql.MutationCreate(), (entgql.MutationUpdate())),
entfga.Annotations{
ObjectType: "group",
IncludeHooks: true,
IDField: "GroupID",
},
}
}
// Indexes of the GroupMembership
func (GroupMembership) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id", "group_id").
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")),
}
}
// Mixin of the GroupMembership
func (GroupMembership) Mixin() []ent.Mixin {
return []ent.Mixin{
emixin.AuditMixin{},
emixin.IDMixin{},
mixin.SoftDeleteMixin{},
}
}
// Hooks of the GroupMembership
func (GroupMembership) Hooks() []ent.Hook {
return []ent.Hook{
hooks.HookGroupMembers(),
}
}
// Policy of the GroupMembership
func (GroupMembership) Policy() ent.Policy {
return privacy.Policy{
Mutation: privacy.MutationPolicy{
privacy.GroupMembershipMutationRuleFunc(func(ctx context.Context, m *generated.GroupMembershipMutation) error {
return m.CheckAccessForEdit(ctx)
}),
privacy.AlwaysDenyRule(),
},
Query: privacy.QueryPolicy{
privacy.GroupMembershipQueryRuleFunc(func(ctx context.Context, q *generated.GroupMembershipQuery) error {
return q.CheckAccess(ctx)
}),
privacy.AlwaysDenyRule(),
},
}
}