/
update.go
53 lines (43 loc) · 1.41 KB
/
update.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
// SPDX-License-Identifier: Apache-2.0
//nolint:dupl // ignore similar code in create.go
package user
import (
"context"
"fmt"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// UpdateUser updates an existing user in the database.
func (e *engine) UpdateUser(ctx context.Context, u *library.User) (*library.User, error) {
e.logger.WithFields(logrus.Fields{
"user": u.GetName(),
}).Tracef("updating user %s in the database", u.GetName())
// cast the library type to database type
//
// https://pkg.go.dev/github.com/go-vela/types/database#UserFromLibrary
user := database.UserFromLibrary(u)
// validate the necessary fields are populated
//
// https://pkg.go.dev/github.com/go-vela/types/database#User.Validate
err := user.Validate()
if err != nil {
return nil, err
}
// encrypt the fields for the user
//
// https://pkg.go.dev/github.com/go-vela/types/database#User.Encrypt
err = user.Encrypt(e.config.EncryptionKey)
if err != nil {
return nil, fmt.Errorf("unable to encrypt user %s: %w", u.GetName(), err)
}
// send query to the database
result := e.client.Table(constants.TableUser).Save(user)
// decrypt fields to return user
err = user.Decrypt(e.config.EncryptionKey)
if err != nil {
return nil, fmt.Errorf("unable to decrypt user %s: %w", u.GetName(), err)
}
return user.ToLibrary(), result.Error
}