-
Notifications
You must be signed in to change notification settings - Fork 5
/
admin.go
272 lines (231 loc) · 7.39 KB
/
admin.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package impl
import (
"context"
"database/sql"
"time"
"git.containerum.net/ch/auth/proto"
"git.containerum.net/ch/user-manager/pkg/db"
"git.containerum.net/ch/user-manager/pkg/models"
cherry "git.containerum.net/ch/user-manager/pkg/umErrors"
"git.containerum.net/ch/user-manager/pkg/utils"
"git.containerum.net/ch/user-manager/pkg/validation"
"github.com/containerum/utils/httputil"
"github.com/lib/pq"
)
func (u *serverImpl) AdminCreateUser(ctx context.Context, request models.UserLogin) (*models.UserLogin, error) {
u.log.WithField("login", request.Login).Info("creating user (admin)")
password, err := utils.SecureRandomString(10)
if err != nil {
u.log.WithError(err)
return nil, cherry.ErrUnableCreateUser()
}
errs := validation.ValidateUserLogin(request)
if errs != nil {
return nil, cherry.ErrRequestValidationFailed().AddDetailsErr(errs...)
}
user, err := u.svc.DB.GetAnyUserByLogin(ctx, request.Login)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return nil, cherry.ErrUnableCreateUser()
}
if user != nil {
return nil, cherry.ErrUserAlreadyExists()
}
salt := utils.GenSalt(request.Login, request.Login, request.Login) // compatibility with old client db
passwordHash := utils.GetKey(request.Login, password, salt)
newUser := &db.User{
Login: request.Login,
PasswordHash: passwordHash,
Salt: salt,
Role: "user",
IsActive: true,
IsDeleted: false,
}
err = u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
if createErr := tx.CreateUser(ctx, newUser); createErr != nil {
return createErr
}
if createErr := tx.CreateProfile(ctx, &db.Profile{
User: newUser,
Access: sql.NullString{String: "rw", Valid: true},
CreatedAt: pq.NullTime{Time: time.Now().UTC(), Valid: true},
}); createErr != nil {
return createErr
}
return nil
})
if err := u.handleDBError(err); err != nil {
return nil, cherry.ErrUnableCreateUser()
}
return &models.UserLogin{
ID: newUser.ID,
Login: newUser.Login,
Password: password,
}, nil
}
func (u *serverImpl) AdminActivateUser(ctx context.Context, request models.UserLogin) error {
u.log.Info("activating user (admin)")
user, err := u.svc.DB.GetAnyUserByLogin(ctx, request.Login)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableActivate()
}
if user.IsDeleted {
return cherry.ErrInvalidLogin()
}
if user.IsActive {
return cherry.ErrUserAlreadyActivated()
}
user.IsActive = true
err = u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.UpdateUser(ctx, user)
})
if err := u.handleDBError(err); err != nil {
return cherry.ErrUnableActivate()
}
return nil
}
func (u *serverImpl) AdminDeactivateUser(ctx context.Context, request models.UserLogin) error {
u.log.Info("deactivating user (admin)")
user, err := u.svc.DB.GetAnyUserByLogin(ctx, request.Login)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableDeleteUser()
}
if err := u.loginUserChecks(ctx, user); err != nil {
return err
}
if user.ID == httputil.MustGetUserID(ctx) {
return cherry.ErrUnableDeleteUser()
}
user.IsDeleted = true
user.IsActive = false
err = u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.UpdateUser(ctx, user)
})
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableDeleteUser()
}
_, authErr := u.svc.AuthClient.DeleteUserTokens(ctx, &authProto.DeleteUserTokensRequest{
UserId: user.ID,
})
return authErr
if err := u.svc.PermissionsClient.DeleteUserNamespaces(ctx, user); err != nil {
u.log.WithError(err)
}
return nil
}
func (u *serverImpl) AdminResetPassword(ctx context.Context, request models.UserLogin) (*models.UserLogin, error) {
u.log.Info("reseting user password (admin)")
user, err := u.svc.DB.GetUserByLogin(ctx, request.Login)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return nil, cherry.ErrUnableChangePassword()
}
if err := u.loginUserChecks(ctx, user); err != nil {
return nil, err
}
password, err := utils.SecureRandomString(10)
if err != nil {
u.log.WithError(err)
return nil, cherry.ErrUnableChangePassword()
}
user.PasswordHash = utils.GetKey(user.Login, password, user.Salt)
err = u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.UpdateUser(ctx, user)
})
if err = u.handleDBError(err); err != nil {
return nil, cherry.ErrUnableChangePassword()
}
if _, authErr := u.svc.AuthClient.DeleteUserTokens(ctx, &authProto.DeleteUserTokensRequest{
UserId: user.ID,
}); authErr != nil {
return nil, authErr
}
return &models.UserLogin{
ID: user.ID,
Login: user.Login,
Password: password,
}, nil
}
func (u *serverImpl) AdminSetAdmin(ctx context.Context, request models.UserLogin) error {
u.log.Info("giving admin permissions to user (admin)")
user, err := u.svc.DB.GetUserByLogin(ctx, request.Login)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableUpdateUserInfo()
}
if err := u.loginUserChecks(ctx, user); err != nil {
return err
}
user.Role = "admin"
err = u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.UpdateUser(ctx, user)
})
if err = u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableUpdateUserInfo()
}
return nil
}
func (u *serverImpl) AdminUnsetAdmin(ctx context.Context, request models.UserLogin) error {
u.log.Info("removing admin permissions from user (admin)")
user, err := u.svc.DB.GetUserByLogin(ctx, request.Login)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableUpdateUserInfo()
}
if err := u.loginUserChecks(ctx, user); err != nil {
return err
}
user.Role = "user"
err = u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.UpdateUser(ctx, user)
})
if err = u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableUpdateUserInfo()
}
return nil
}
func (u *serverImpl) CreateFirstAdmin(password string) error {
u.log.Info("creating first admin user")
user, err := u.svc.DB.GetAnyUserByLoginWOContext("admin@local.containerum.io")
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableCreateUser()
}
if user != nil {
u.log.Info("updating admin password")
user.PasswordHash = utils.GetKey("admin@local.containerum.io", password, user.Salt)
err = u.svc.DB.UpdateUserWOContext(user)
if err != nil {
return err
}
u.log.Info("admin password updated")
return nil
}
salt := utils.GenSalt("admin@local.containerum.io", "admin@local.containerum.io", "admin@local.containerum.io") // compatibility with old client db
passwordHash := utils.GetKey("admin@local.containerum.io", password, salt)
newUser := &db.User{
Login: "admin@local.containerum.io",
PasswordHash: passwordHash,
Salt: salt,
Role: "admin",
IsActive: true,
IsDeleted: false,
}
if createErr := u.svc.DB.CreateUserWOContext(newUser); createErr != nil {
return createErr
}
if createErr := u.svc.DB.CreateProfileWOContext(&db.Profile{
User: newUser,
Access: sql.NullString{String: "rw", Valid: true},
CreatedAt: pq.NullTime{Time: time.Now().UTC(), Valid: true},
}); createErr != nil {
return createErr
}
u.log.Info("admin created")
return nil
}