/
service.go
95 lines (81 loc) · 2.4 KB
/
service.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
package customattrs
import (
"github.com/authgear/authgear-server/pkg/api/event"
"github.com/authgear/authgear-server/pkg/api/event/blocking"
"github.com/authgear/authgear-server/pkg/api/event/nonblocking"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/attrs"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/accesscontrol"
)
type EventService interface {
DispatchEventOnCommit(payload event.Payload) error
}
type Service struct {
Config *config.UserProfileConfig
ServiceNoEvent *ServiceNoEvent
Events EventService
}
func (s *Service) UpdateAllCustomAttributes(role accesscontrol.Role, userID string, reprForm map[string]interface{}) error {
err := s.ServiceNoEvent.UpdateAllCustomAttributes(role, userID, reprForm)
if err != nil {
return err
}
err = s.dispatchEvents(role, userID)
if err != nil {
return err
}
return nil
}
func (s *Service) UpdateCustomAttributesWithList(role accesscontrol.Role, userID string, attrs attrs.List) error {
err := s.ServiceNoEvent.UpdateCustomAttributesWithList(role, userID, attrs)
if err != nil {
return err
}
return nil
}
func (s *Service) UpdateCustomAttributesWithForm(role accesscontrol.Role, userID string, jsonPointerMap map[string]string) error {
err := s.ServiceNoEvent.UpdateCustomAttributesWithForm(role, userID, jsonPointerMap)
if err != nil {
return err
}
err = s.dispatchEvents(role, userID)
if err != nil {
return err
}
return nil
}
func (s *Service) dispatchEvents(role accesscontrol.Role, userID string) (err error) {
eventPayloads := []event.Payload{
&blocking.UserProfilePreUpdateBlockingEventPayload{
UserRef: model.UserRef{
Meta: model.Meta{
ID: userID,
},
},
AdminAPI: role == accesscontrol.RoleGreatest,
},
&nonblocking.UserProfileUpdatedEventPayload{
UserRef: model.UserRef{
Meta: model.Meta{
ID: userID,
},
},
AdminAPI: role == accesscontrol.RoleGreatest,
},
}
for _, eventPayload := range eventPayloads {
err = s.Events.DispatchEventOnCommit(eventPayload)
if err != nil {
return err
}
}
return nil
}
func (s *Service) ReadCustomAttributesInStorageForm(
role accesscontrol.Role,
userID string,
storageForm map[string]interface{},
) (map[string]interface{}, error) {
return s.ServiceNoEvent.ReadCustomAttributesInStorageForm(role, userID, storageForm)
}