-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
243 lines (232 loc) · 6.26 KB
/
util.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package internal
import (
"sort"
"cloud.google.com/go/firestore"
"firebase.google.com/go/auth"
)
var (
// actions contains permission updates to be saved. key is the uid, value is a map of permission
// key and a bool value where true means adding the permission, false means removing it.
actions = map[string]map[string]bool{}
// savedUsers contains user id lists for all pages.
savedUsers = map[string][]string{}
)
// User is a representation of a user downloaded from firebase auth.
type User struct {
Name string
Email string
UID string
Claims map[string]bool
}
// fltrClaims removes all claims but permissions we're interested in.
func filterClaims(c map[string]any) map[string]bool {
perms := map[string]bool{}
for p, v := range c {
if _, ok := permsMap[p]; ok {
perms[p] = v.(bool)
}
}
return perms
}
// newUserFromAuth converts firebase auth user to User and saves it to local cache if needed.
// Claims are filtered to the permissions we're interested in. Returns the converted User and
// if it's the same as the previous version if any, otherwise true.
func newUserFromAuth(r *auth.UserRecord, saveToCache bool) (*User, bool) {
claims := filterClaims(r.CustomClaims)
u, ok := localUsers[r.UID]
if ok {
same := compareClaims(u.Claims, claims)
u.Claims = claims
u.Name = r.DisplayName
return u, same
}
u = &User{Name: r.DisplayName, Email: r.Email, Claims: claims, UID: r.UID}
if saveToCache || len(claims) > 0 {
localUsers[r.UID] = u
}
return u, true
}
// compareClaims compares two maps of claims if they have the same key-value pairs.
func compareClaims(a, b map[string]bool) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return false
}
}
return true
}
func copyClaims(src map[string]bool) map[string]bool {
dst := map[string]bool{}
for k, v := range src {
dst[k] = v
}
return dst
}
// saveListBody is the middle of the firestore transaction for listing, saving and refreshing.
// The input is a map of uid -> email users that come from the cached list of privileged users.
// It looks for new privileged users in case of refresh only by iterating through all auth users.
// On the other side, it checks all users from the cache list if they are still privileged for
// all three cases. In case of refresh and save it checks if the permissions are still the same.
// Not for listing, because it runs only at init when we download the list of permissions, so
// there's nothing to compare them with. In case of save we're merging actions into current
// permissions, updating Firebase auth claims and the cached firestore list of privileged users.
//
//nolint:all
func saveListBody(act int, res map[string]any) []firestore.Update {
crntMap := map[string]struct{}{}
var changed, newUsers, empty []string
var claimCb func(r *auth.UserRecord)
updates := make([]firestore.Update, 0, len(actions))
manualChanges := false
var uids map[string]struct{}
if act != actRefresh {
uids = map[string]struct{}{}
for uid, email := range res {
if _, ok := localUsers[uid]; !ok {
localUsers[uid] = &User{Email: email.(string), UID: uid}
}
uids[uid] = es
}
for uid := range actions {
uids[uid] = es
}
}
if fe.currentPage() == lst {
savedUsers[lst] = crntUsers
}
for _, uid := range savedUsers[lst] { // already downloaded users
crntMap[uid] = es
if act == actRefresh {
continue
}
if _, ok := res[uid]; !ok {
uids[uid] = es
}
}
// this is called in all cases to check consistency
preCb := func(r *auth.UserRecord) {
uid := r.UID
u, same := newUserFromAuth(r, act != actRefresh) // don't want to save all users locally
_, inRes := res[uid]
if len(u.Claims) == 0 {
if !inRes {
return
}
if _, ok := actions[uid]; !ok && act != actRefresh {
empty = append(empty, u.Email)
}
manualChanges = true
updates = append(updates, firestore.Update{Path: uid, Value: firestore.Delete})
delete(res, uid)
return
}
if !inRes {
updates = append(updates, firestore.Update{Path: uid, Value: u.Email})
res[uid] = es // no need to add email here
manualChanges = true
}
if act == actList {
return
}
if _, inCrnt := crntMap[uid]; !inCrnt {
newUsers = append(newUsers, u.Email)
}
if !same {
changed = append(changed, u.Email)
}
}
if act == actSave {
claimCb = func(r *auth.UserRecord) {
preCb(r)
uid := r.UID
u := localUsers[uid]
a, ok := actions[uid]
if !ok {
return
}
newClaims := copyClaims(u.Claims)
for p, ch := range a {
if ch { // add permission
if r.CustomClaims == nil {
r.CustomClaims = map[string]any{}
}
r.CustomClaims[p] = true
newClaims[p] = true
} else {
delete(r.CustomClaims, p)
delete(newClaims, p)
}
}
if !writeErrorIf(fb.setClaims(uid, r.CustomClaims)) {
return
}
u.Claims = newClaims
delete(actions, uid)
_, inRes := res[uid]
if inRes == (len(u.Claims) > 0) {
return
}
if inRes {
delete(res, uid)
updates = append(updates, firestore.Update{Path: uid, Value: firestore.Delete})
return
}
updates = append(updates, firestore.Update{Path: uid, Value: r.Email})
res[uid] = es
}
} else {
claimCb = preCb
}
if act == actRefresh {
fb.iterUsers(claimCb)
} else {
uidList := make([]auth.UserIdentifier, 0, len(uids))
for uid := range uids {
uidList = append(uidList, auth.UIDIdentifier{UID: uid})
}
if !fb.downloadClaims(uidList, claimCb) && manualChanges {
manualChanges = false // already written
}
}
savedUsers[lst] = savedUsers[lst][:0]
for uid := range res {
savedUsers[lst] = append(savedUsers[lst], uid)
}
sortByNameThenEmail(savedUsers[lst])
if fe.currentPage() == lst {
crntUsers = savedUsers[lst]
}
writeErrorList(errChanged, changed)
writeErrorList(errEmpty, empty)
writeErrorList(errNew, newUsers)
if manualChanges {
writeErrorStr(errManual)
if act != actRefresh {
writeErrorStr(warnMayRefresh)
}
}
return updates
}
func sortByNameThenEmail(x []string) {
sort.Slice(x, func(i, j int) bool {
ui, uj := localUsers[x[i]], localUsers[x[j]]
uiz := len(ui.Name) == 0
ujz := len(uj.Name) == 0
if uiz != ujz {
return !uiz
}
if ui.Name < uj.Name {
return true
}
if ui.Name > uj.Name {
return false
}
if ui.Email < uj.Email {
return true
}
return false
})
}