This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathuser_filter.go
92 lines (77 loc) · 2.97 KB
/
user_filter.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
package ldclient
type userFilter struct {
allAttributesPrivate bool
globalPrivateAttributes []string
}
func newUserFilter(config Config) userFilter {
return userFilter{
allAttributesPrivate: config.AllAttributesPrivate,
globalPrivateAttributes: config.PrivateAttributeNames,
}
}
func (uf userFilter) scrubUser(user User) *User {
if len(user.PrivateAttributeNames) == 0 && len(uf.globalPrivateAttributes) == 0 && !uf.allAttributesPrivate {
return &user
}
isPrivate := map[string]bool{}
for _, n := range uf.globalPrivateAttributes {
isPrivate[n] = true
}
for _, n := range user.PrivateAttributeNames {
isPrivate[n] = true
}
user.PrivateAttributeNames = nil // this property is not used in the output schema for events
user.PrivateAttributes = nil // see below
// Because we're only resetting these properties if we're going to proceed with the scrubbing logic, it is
// possible to pass an already-scrubbed user to this function (with, potentially, some attribute names in
// PrivateAttributes) and get back the same object, as long as the configuration does not have private
// attributes enabled. This allows us to reuse the event processor code in ld-relay, where we may have to
// reprocess events that have already been through the scrubbing process.
if user.Custom != nil {
var custom = map[string]interface{}{}
for k, v := range *user.Custom {
if uf.allAttributesPrivate || isPrivate[k] {
user.PrivateAttributes = append(user.PrivateAttributes, k)
} else {
custom[k] = v
}
}
user.Custom = &custom
}
if !isEmpty(user.Avatar) && (uf.allAttributesPrivate || isPrivate["avatar"]) {
user.Avatar = nil
user.PrivateAttributes = append(user.PrivateAttributes, "avatar")
}
if !isEmpty(user.Country) && (uf.allAttributesPrivate || isPrivate["country"]) {
user.Country = nil
user.PrivateAttributes = append(user.PrivateAttributes, "country")
}
if !isEmpty(user.Ip) && (uf.allAttributesPrivate || isPrivate["ip"]) {
user.Ip = nil
user.PrivateAttributes = append(user.PrivateAttributes, "ip")
}
if !isEmpty(user.FirstName) && (uf.allAttributesPrivate || isPrivate["firstName"]) {
user.FirstName = nil
user.PrivateAttributes = append(user.PrivateAttributes, "firstName")
}
if !isEmpty(user.LastName) && (uf.allAttributesPrivate || isPrivate["lastName"]) {
user.LastName = nil
user.PrivateAttributes = append(user.PrivateAttributes, "lastName")
}
if !isEmpty(user.Name) && (uf.allAttributesPrivate || isPrivate["name"]) {
user.Name = nil
user.PrivateAttributes = append(user.PrivateAttributes, "name")
}
if !isEmpty(user.Secondary) && (uf.allAttributesPrivate || isPrivate["secondary"]) {
user.Secondary = nil
user.PrivateAttributes = append(user.PrivateAttributes, "secondary")
}
if !isEmpty(user.Email) && (uf.allAttributesPrivate || isPrivate["email"]) {
user.Email = nil
user.PrivateAttributes = append(user.PrivateAttributes, "email")
}
return &user
}
func isEmpty(s *string) bool {
return s == nil || *s == ""
}