-
Notifications
You must be signed in to change notification settings - Fork 787
/
server.go
79 lines (70 loc) · 1.46 KB
/
server.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
package auth
import (
"fmt"
"sort"
"github.com/jenkins-x/jx/pkg/util"
)
func (s *AuthServer) Label() string {
if s.Name != "" {
return s.Name
}
return s.URL
}
func (s *AuthServer) Description() string {
if s.Name != "" {
return s.Name + " at " + s.URL
}
return s.URL
}
func (s *AuthServer) DeleteUser(username string) error {
idx := -1
for i, user := range s.Users {
if user.Username == username {
idx = i
break
}
}
if idx < 0 {
if len(s.Users) == 0 {
return fmt.Errorf("Cannot remote user %s as there are no users for this server", username)
}
return util.InvalidArg(username, s.GetUsernames())
}
s.Users = append(s.Users[0:idx], s.Users[idx+1:]...)
return nil
}
func (s *AuthServer) GetUsernames() []string {
answer := []string{}
for _, user := range s.Users {
name := user.Username
if name != "" {
answer = append(answer, name)
}
}
sort.Strings(answer)
return answer
}
//HasUserAuths checks if a server has any user auth configured
func (s *AuthServer) HasUserAuths() bool {
return len(s.Users) > 0
}
// CurrentAuth returns the current user auth, otherwise the first one
func (s *AuthServer) CurrentAuth() *UserAuth {
for _, user := range s.Users {
if user.Username == s.CurrentUser {
return user
}
}
if len(s.Users) > 0 {
return s.Users[0]
}
return nil
}
func (s *AuthServer) GetUserAuth(username string) *UserAuth {
for _, user := range s.Users {
if username == user.Username {
return user
}
}
return nil
}