/
identify_user_info.go
68 lines (52 loc) · 1.36 KB
/
identify_user_info.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
package analytics
import (
"errors"
"github.com/common-fate/analytics-go/acore"
)
func init() {
registerEvent(&UserInfo{})
}
type Role string
const (
RoleEndUser Role = "end_user"
RoleAdmin Role = "admin"
)
type UserInfo struct {
ID string `json:"-"`
GroupCount int `json:"group_count"`
IsAdmin bool `json:"-"`
AvailableRules int `json:"available_rules,omitempty"`
}
func (d *UserInfo) userID() string { return "" }
func (d *UserInfo) Type() string { return "cf:identify:user_info" }
func (d *UserInfo) EmittedWhen() string { return "Access Request created/updated" }
func (d *UserInfo) marshalEvent(ctx marshalContext) ([]acore.Message, error) {
// role is populated automatically based on IsAdmin.
// In future we might have additional role types rather than
// just 'admin' and 'end user'
role := RoleEndUser
if d.IsAdmin {
role = RoleAdmin
}
uid, ok := hash(d.ID, "usr")
if !ok {
return nil, errors.New("could not hash user ID")
}
id := acore.Identify{
DistinctId: uid,
Properties: eventToProperties(d).Set("role", role),
}
if ctx.DeploymentID != nil {
id.Groups = acore.NewGroups().Set("deployment", *ctx.DeploymentID)
}
m := []acore.Message{id}
return m, nil
}
func (d *UserInfo) fixture() {
*d = UserInfo{
ID: "usr_123",
IsAdmin: true,
GroupCount: 2,
AvailableRules: 1,
}
}