-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
43 lines (36 loc) · 933 Bytes
/
user.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
package main
import (
"errors"
"os/user"
)
var (
// UserWhitelist contains the users whitelisted via the --users option.
UserWhitelist []*user.User
ErrNotWhitelisted = errors.New("not monitoring that users processes")
// users is a cache to prevent unnecessary calls to `LookupId`.
users = map[string]*user.User{}
)
// UserByUid returns a User for a particular Uid. An error will be returned
// if a User with that Uid does not exist or if the User is not whitelisted.
func UserByUid(uid string) (*user.User, error) {
if len(UserWhitelist) == 0 {
return userByUid(uid)
}
for _, user := range UserWhitelist {
if user.Uid == uid {
return userByUid(uid)
}
}
return nil, ErrNotWhitelisted
}
func userByUid(uid string) (*user.User, error) {
if user, ok := users[uid]; ok {
return user, nil
}
user, err := user.LookupId(uid)
if err != nil {
return nil, err
}
users[uid] = user
return user, nil
}