/
user_profile.go
103 lines (89 loc) · 2.84 KB
/
user_profile.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
package facade
import (
"time"
"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/util/accesscontrol"
)
type StandardAttributesService interface {
UpdateStandardAttributes(role accesscontrol.Role, userID string, stdAttrs map[string]interface{}) error
DeriveStandardAttributes(role accesscontrol.Role, userID string, updatedAt time.Time, attrs map[string]interface{}) (map[string]interface{}, error)
}
type CustomAttributesService interface {
ReadCustomAttributesInStorageForm(role accesscontrol.Role, userID string, storageForm map[string]interface{}) (map[string]interface{}, error)
UpdateAllCustomAttributes(role accesscontrol.Role, userID string, customAttrs map[string]interface{}) error
}
type EventService interface {
DispatchEventOnCommit(payload event.Payload) error
}
type UserProfileFacade struct {
User UserService
StandardAttributes StandardAttributesService
CustomAttributes CustomAttributesService
Events EventService
}
func (f *UserProfileFacade) DeriveStandardAttributes(role accesscontrol.Role, userID string, updatedAt time.Time, attrs map[string]interface{}) (map[string]interface{}, error) {
return f.StandardAttributes.DeriveStandardAttributes(role, userID, updatedAt, attrs)
}
func (f *UserProfileFacade) ReadCustomAttributesInStorageForm(
role accesscontrol.Role,
userID string,
storageForm map[string]interface{},
) (map[string]interface{}, error) {
return f.CustomAttributes.ReadCustomAttributesInStorageForm(role, userID, storageForm)
}
func (f *UserProfileFacade) UpdateUserProfile(
role accesscontrol.Role,
userID string,
stdAttrs map[string]interface{},
customAttrs map[string]interface{},
) (err error) {
updated := false
err = f.User.CheckUserAnonymized(userID)
if err != nil {
return err
}
if stdAttrs != nil {
updated = true
err = f.StandardAttributes.UpdateStandardAttributes(role, userID, stdAttrs)
if err != nil {
return
}
}
if customAttrs != nil {
updated = true
err = f.CustomAttributes.UpdateAllCustomAttributes(role, userID, customAttrs)
if err != nil {
return
}
}
if updated {
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 = f.Events.DispatchEventOnCommit(eventPayload)
if err != nil {
return
}
}
}
return
}