-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
139 lines (108 loc) · 4.28 KB
/
users.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
package functions
import (
"time"
"github.com/cygy/ginamite/common/mongo/session"
"github.com/cygy/ginamite/common/notifications"
"github.com/cygy/ginamite/plugins/mongodb/model"
"github.com/globalsign/mgo"
)
// DeleteUserByID : deletes an user and all its bound documents.
func DeleteUserByID(updateDocumentsReferencingToDeletedUser func(userID string, mongoSession *mgo.Database)) func(userID string) map[string]time.Time {
return func(userID string) map[string]time.Time {
session, db := session.Copy()
defer session.Close()
if updateDocumentsReferencingToDeletedUser != nil {
updateDocumentsReferencingToDeletedUser(userID, db)
}
model.DeleteDeletedUserByUserID(userID, db)
model.DeleteUserByUserID(userID, db)
authTokens, err := model.GetAuthTokens(userID, db)
revokedAuthTokens := make(map[string]time.Time, len(authTokens))
if err == nil {
for _, authToken := range authTokens {
revokedAuthTokens[authToken.ID.Hex()] = authToken.ExpiresAt
}
}
model.DeleteAuthTokensByUser(userID, db)
return revokedAuthTokens
}
}
// DisableUserByID : disables an user and all its bound documents.
func DisableUserByID(updateDocumentsReferencingToDisabledUser func(userID string, mongoSession *mgo.Database)) func(userID string) map[string]time.Time {
return func(userID string) map[string]time.Time {
session, db := session.Copy()
defer session.Close()
if updateDocumentsReferencingToDisabledUser != nil {
updateDocumentsReferencingToDisabledUser(userID, db)
}
model.DeleteUserByUserID(userID, db)
authTokens, err := model.GetAuthTokens(userID, db)
revokedAuthTokens := make(map[string]time.Time, len(authTokens))
if err == nil {
for _, authToken := range authTokens {
revokedAuthTokens[authToken.ID.Hex()] = authToken.ExpiresAt
}
}
model.DeleteAuthTokensByUser(userID, db)
return revokedAuthTokens
}
}
// EnableUserByID : enables an user and all its bound documents.
func EnableUserByID(updateDocumentsReferencingToEnabledUser func(userID string, mongoSession *mgo.Database)) func(userID string) {
return func(userID string) {
session, db := session.Copy()
defer session.Close()
if updateDocumentsReferencingToEnabledUser != nil {
updateDocumentsReferencingToEnabledUser(userID, db)
}
model.DeleteDisabledUserByUserID(userID, db)
}
}
// UpdateUserSocialNetworksByID : updates the user's social networks to its bound documents.
func UpdateUserSocialNetworksByID(UpdateDocumentsReferencingToUpdatedUserSocialNetworks func(userID string, mongoSession *mgo.Database)) func(userID string) {
return func(userID string) {
if UpdateDocumentsReferencingToUpdatedUserSocialNetworks != nil {
session, db := session.Copy()
defer session.Close()
UpdateDocumentsReferencingToUpdatedUserSocialNetworks(userID, db)
}
}
}
// GetNotificationTargetsByUserAndType : gets the targets of a notification sent to an user.
func GetNotificationTargetsByUserAndType(userID, notificationType string) []notifications.Target {
session, db := session.Copy()
defer session.Close()
targets := model.GetUserNotificationTargetsByType(userID, notificationType, db)
notificationsTargets := make([]notifications.Target, len(targets))
for i, target := range targets {
notificationsTargets[i] = notifications.Target{
Target: target.Type,
Value: target.Value,
}
}
return notificationsTargets
}
// IsEmailAddressValid : returns true if the email address is valid and messages can be sent it.
func IsEmailAddressValid(emailAddress string) bool {
session, db := session.Copy()
defer session.Close()
ok := model.IsUserWithValidEmailAddressExisting(emailAddress, db)
if !ok {
ok = model.IsDisabledUserWithValidEmailAddressExisting(emailAddress, db)
}
return ok
}
// GetNeverUsedAccounts : returns the count of unused accounts deleted (never logged in).
func GetNeverUsedAccounts(intervalInDays uint) []string {
session, db := session.Copy()
defer session.Close()
userIDs, _ := model.GetNeverUsedUserIDs(24*time.Hour*time.Duration(intervalInDays), db)
return userIDs
}
// GetInactiveAccounts : returns the count of inactive accounts deleted.
func GetInactiveAccounts(intervalInMonths uint) []string {
session, db := session.Copy()
defer session.Close()
userIDs, _ := model.GetInactiveUserIDs(30*24*time.Hour*time.Duration(intervalInMonths), db)
return userIDs
}