forked from absmach/magistrala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
162 lines (126 loc) · 5.53 KB
/
groups.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package auth
import (
"context"
"errors"
"time"
)
const (
// MaxLevel represents the maximum group hierarchy level.
MaxLevel = uint64(5)
// MinLevel represents the minimum group hierarchy level.
MinLevel = uint64(1)
)
var (
// ErrAssignToGroup indicates failure to assign member to a group.
ErrAssignToGroup = errors.New("failed to assign member to a group")
// ErrUnassignFromGroup indicates failure to unassign member from a group.
ErrUnassignFromGroup = errors.New("failed to unassign member from a group")
// ErrMissingParent indicates that parent can't be found
ErrMissingParent = errors.New("failed to retrieve parent")
// ErrGroupNotEmpty indicates group is not empty, can't be deleted.
ErrGroupNotEmpty = errors.New("group is not empty")
// ErrMemberAlreadyAssigned indicates that members is already assigned.
ErrMemberAlreadyAssigned = errors.New("member is already assigned")
)
// GroupMetadata defines the Metadata type.
type GroupMetadata map[string]interface{}
// Member represents the member information.
type Member struct {
ID string
Type string
}
// Group represents the group information.
type Group struct {
ID string
OwnerID string
ParentID string
Name string
Description string
Metadata GroupMetadata
// Indicates a level in tree hierarchy.
// Root node is level 1.
Level int
// Path in a tree consisting of group ids
// parentID1.parentID2.childID1
// e.g. 01EXPM5Z8HRGFAEWTETR1X1441.01EXPKW2TVK74S5NWQ979VJ4PJ.01EXPKW2TVK74S5NWQ979VJ4PJ
Path string
Children []*Group
CreatedAt time.Time
UpdatedAt time.Time
}
// PageMetadata contains page metadata that helps navigation.
type PageMetadata struct {
Total uint64
Offset uint64
Limit uint64
Size uint64
Level uint64
Name string
Type string
Metadata GroupMetadata
}
// GroupPage contains page related metadata as well as list of groups that
// belong to this page.
type GroupPage struct {
PageMetadata
Groups []Group
}
// MemberPage contains page related metadata as well as list of members that
// belong to this page.
type MemberPage struct {
PageMetadata
Members []Member
}
// GroupService specifies an API that must be fullfiled by the domain service
// implementation, and all of its decorators (e.g. logging & metrics).
type GroupService interface {
// CreateGroup creates new group.
CreateGroup(ctx context.Context, token string, g Group) (Group, error)
// UpdateGroup updates the group identified by the provided ID.
UpdateGroup(ctx context.Context, token string, g Group) (Group, error)
// ViewGroup retrieves data about the group identified by ID.
ViewGroup(ctx context.Context, token, id string) (Group, error)
// ListGroups retrieves groups.
ListGroups(ctx context.Context, token string, pm PageMetadata) (GroupPage, error)
// ListChildren retrieves groups that are children to group identified by parentID
ListChildren(ctx context.Context, token, parentID string, pm PageMetadata) (GroupPage, error)
// ListParents retrieves groups that are parent to group identified by childID.
ListParents(ctx context.Context, token, childID string, pm PageMetadata) (GroupPage, error)
// ListMembers retrieves everything that is assigned to a group identified by groupID.
ListMembers(ctx context.Context, token, groupID, groupType string, pm PageMetadata) (MemberPage, error)
// ListMemberships retrieves all groups for member that is identified with memberID belongs to.
ListMemberships(ctx context.Context, token, memberID string, pm PageMetadata) (GroupPage, error)
// RemoveGroup removes the group identified with the provided ID.
RemoveGroup(ctx context.Context, token, id string) error
// Assign adds a member with memberID into the group identified by groupID.
Assign(ctx context.Context, token, groupID, groupType string, memberIDs ...string) error
// Unassign removes member with memberID from group identified by groupID.
Unassign(ctx context.Context, token, groupID string, memberIDs ...string) error
// AssignGroupAccessRights adds access rights on thing groups to user group.
AssignGroupAccessRights(ctx context.Context, token, thingGroupID, userGroupID string) error
}
// GroupRepository specifies a group persistence API.
type GroupRepository interface {
// Save group
Save(ctx context.Context, g Group) (Group, error)
// Update a group
Update(ctx context.Context, g Group) (Group, error)
// Delete a group
Delete(ctx context.Context, id string) error
// RetrieveByID retrieves group by its id
RetrieveByID(ctx context.Context, id string) (Group, error)
// RetrieveAll retrieves all groups.
RetrieveAll(ctx context.Context, pm PageMetadata) (GroupPage, error)
// RetrieveAllParents retrieves all groups that are ancestors to the group with given groupID.
RetrieveAllParents(ctx context.Context, groupID string, pm PageMetadata) (GroupPage, error)
// RetrieveAllChildren retrieves all children from group with given groupID up to the hierarchy level.
RetrieveAllChildren(ctx context.Context, groupID string, pm PageMetadata) (GroupPage, error)
// Retrieves list of groups that member belongs to
Memberships(ctx context.Context, memberID string, pm PageMetadata) (GroupPage, error)
// Members retrieves everything that is assigned to a group identified by groupID.
Members(ctx context.Context, groupID, groupType string, pm PageMetadata) (MemberPage, error)
// Assign adds a member to group.
Assign(ctx context.Context, groupID, groupType string, memberIDs ...string) error
// Unassign removes a member from a group
Unassign(ctx context.Context, groupID string, memberIDs ...string) error
}