-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
106 lines (88 loc) · 2.56 KB
/
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
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
package gitdir
import (
"errors"
"github.com/rs/zerolog/log"
"github.com/belak/go-gitdir/models"
)
// User is the internal representation of a user. This data is copied from the
// loaded config file.
type User struct {
Username string
IsAnonymous bool
IsAdmin bool
}
// AnonymousUser is the user that is returned when no user is available.
var AnonymousUser = &User{
Username: "<anonymous>",
IsAnonymous: true,
IsAdmin: false,
}
// ErrUserNotFound is returned from LookupUser commands when the user is not
// found.
var ErrUserNotFound = errors.New("user not found")
// LookupUserFromUsername looks up a user objects given their username.
func (c *Config) LookupUserFromUsername(username string) (*User, error) {
userConfig, ok := c.Users[username]
if !ok {
log.Warn().Msg("username does not match a user")
return AnonymousUser, ErrUserNotFound
}
if userConfig.Disabled {
log.Warn().Msg("user is disabled")
return AnonymousUser, ErrUserNotFound
}
return &User{
Username: username,
IsAnonymous: false,
IsAdmin: userConfig.IsAdmin,
}, nil
}
// LookupUserFromKey looks up a user object given their PublicKey.
func (c *Config) LookupUserFromKey(pk models.PublicKey, remoteUser string) (*User, error) {
username, ok := c.publicKeys[pk.RawMarshalAuthorizedKey()]
if !ok {
log.Warn().Msg("key does not exist")
return AnonymousUser, ErrUserNotFound
}
userConfig, ok := c.Users[username]
if !ok {
log.Warn().Msg("key does not match a user")
return AnonymousUser, ErrUserNotFound
}
if userConfig.Disabled {
log.Warn().Msg("user is disabled")
return AnonymousUser, ErrUserNotFound
}
// If they weren't the git user make sure their username matches their key.
if remoteUser != c.Options.GitUser && remoteUser != username {
log.Warn().Msg("key belongs to different user")
return AnonymousUser, ErrUserNotFound
}
return &User{
Username: username,
IsAnonymous: false,
IsAdmin: userConfig.IsAdmin,
}, nil
}
// LookupUserFromInvite looks up a user object given an invite code.
func (c *Config) LookupUserFromInvite(invite string) (*User, error) {
username, ok := c.Invites[invite]
if !ok {
log.Warn().Msg("invite does not exist")
return AnonymousUser, ErrUserNotFound
}
userConfig, ok := c.Users[username]
if !ok {
log.Warn().Msg("invite does not match a user")
return AnonymousUser, ErrUserNotFound
}
if userConfig.Disabled {
log.Warn().Msg("user is disabled")
return AnonymousUser, ErrUserNotFound
}
return &User{
Username: username,
IsAnonymous: false,
IsAdmin: userConfig.IsAdmin,
}, nil
}