This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
user.go
154 lines (136 loc) · 3.51 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
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
package logic
import (
"crypto/md5"
"fmt"
"github.com/suzuki-shunsuke/go-ptr"
"github.com/suzuki-shunsuke/go-graylog"
"github.com/suzuki-shunsuke/go-graylog/validator"
)
func encryptPassword(password string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(password)))
}
// HasUser returns whether the user exists.
func (lgc *Logic) HasUser(username string) (bool, error) {
return lgc.store.HasUser(username)
}
// GetUser returns a user.
func (lgc *Logic) GetUser(username string) (*graylog.User, int, error) {
user, err := lgc.store.GetUser(username)
if err != nil {
return user, 500, err
}
if user == nil {
return user, 404, fmt.Errorf(`no user "%s" is found`, username)
}
return user, 200, nil
}
// GetUsers returns a list of users.
func (lgc *Logic) GetUsers() ([]graylog.User, int, error) {
users, err := lgc.store.GetUsers()
if err != nil {
return users, 500, err
}
return users, 200, nil
}
func (lgc *Logic) checkUserRoles(roles []string) (int, error) {
if len(roles) != 0 {
for _, roleName := range roles {
ok, err := lgc.HasRole(roleName)
if err != nil {
return 500, err
}
if !ok {
// unfortunately, graylog 2.4.3-1 returns 500 error
// https://github.com/Graylog2/graylog2-server/issues/4665
return 500, fmt.Errorf(`no role found with name "%s"`, roleName)
}
}
}
return 200, nil
}
// AddUser adds a user to the Server.
func (lgc *Logic) AddUser(user *graylog.User) (int, error) {
// client side validation
if err := validator.CreateValidator.Struct(user); err != nil {
return 400, err
}
// Check a given username has already used.
ok, err := lgc.HasUser(user.Username)
if err != nil {
return 500, err
}
if ok {
return 400, fmt.Errorf(
`the user "%s" has already existed`, user.Username)
}
// check role exists
if user.Roles != nil {
if sc, err := lgc.checkUserRoles(user.Roles.ToList()); err != nil {
return sc, err
}
}
user.SetDefaultValues()
user.Password = encryptPassword(user.Password)
// Add a user
if err := lgc.store.AddUser(user); err != nil {
return 500, err
}
return 201, nil
}
// UpdateUser updates a user of the Server.
// "email", "permissions", "full_name", "password"
func (lgc *Logic) UpdateUser(prms *graylog.UserUpdateParams) (int, error) {
if prms == nil {
return 400, fmt.Errorf("user is nil")
}
// Check updated user exists
ok, err := lgc.HasUser(prms.Username)
if err != nil {
return 500, err
}
if !ok {
return 404, fmt.Errorf(`the user "%s" is not found`, prms.Username)
}
// client side validation
if err := validator.UpdateValidator.Struct(prms); err != nil {
return 400, err
}
// check role exists
if prms.Roles != nil {
if sc, err := lgc.checkUserRoles(prms.Roles.ToList()); err != nil {
return sc, err
}
}
if prms.Password != nil {
prms.Password = ptr.PStr(encryptPassword(*prms.Password))
}
// update
if err := lgc.store.UpdateUser(prms); err != nil {
return 500, err
}
return 200, nil
}
// DeleteUser removes a user from the Server.
func (lgc *Logic) DeleteUser(name string) (int, error) {
// Check deleted user exists
ok, err := lgc.HasUser(name)
if err != nil {
return 500, err
}
if !ok {
return 404, fmt.Errorf(`the user "%s" is not found`, name)
}
if name == "admin" {
// graylog spec
return 404, fmt.Errorf(`the user "%s" is not found`, name)
}
// Delete a user
if err := lgc.store.DeleteUser(name); err != nil {
return 500, err
}
return 204, nil
}
// UserList returns a list of all users.
func (lgc *Logic) UserList() ([]graylog.User, error) {
return lgc.store.GetUsers()
}