MCPcopy Create free account
hub / github.com/Scalingo/cli / UpdateUserPassword

Function UpdateUserPassword

db/users/update_password.go:13–86  ·  view source on GitHub ↗
(ctx context.Context, app, addonUUID, username string)

Source from the content-addressed store, hash-verified

11)
12
13func UpdateUserPassword(ctx context.Context, app, addonUUID, username string) error {
14 isSupported, err := doesDatabaseHandleUserManagement(ctx, app, addonUUID)
15 if err != nil {
16 return errors.Wrap(ctx, err, "get user management information")
17 }
18
19 if !isSupported {
20 io.Error(ErrDatabaseNotSupportUserManagement)
21 return nil
22 }
23
24 if usernameValidation, ok := isUsernameValid(username); !ok {
25 io.Error(usernameValidation)
26 return nil
27 }
28
29 c, err := config.ScalingoClient(ctx)
30 if err != nil {
31 return errors.Wrap(ctx, err, "get Scalingo client")
32 }
33
34 l, err := c.DatabaseListUsers(ctx, app, addonUUID)
35 if err != nil {
36 return errors.Wrap(ctx, err, "list the database's users")
37 }
38 // Check if the userParam exists and is not protected
39 var user *scalingo.DatabaseUser
40 for _, u := range l {
41 if u.Name == username {
42 user = &u
43 break
44 }
45 }
46 if user == nil {
47 return errors.New(ctx, fmt.Sprintf("User \"%s\" does not exist", username))
48 }
49 if user.Protected {
50 return errors.New(ctx, fmt.Sprintf("User \"%s\" is protected", username))
51 }
52
53 password, confirmedPassword, err := askForPasswordWithRetry(ctx, 3)
54 if err != nil {
55 io.Error(err)
56 return nil
57 }
58
59 isPasswordGenerated := password == ""
60
61 var databaseUser scalingo.DatabaseUser
62
63 // We have two different API calls here to avoid breaking backwards compatibility of the CLI
64 if !isPasswordGenerated {
65 userUpdateParam := scalingo.DatabaseUpdateUserParam{
66 DatabaseID: addonUUID,
67 Password: password,
68 PasswordConfirmation: confirmedPassword,
69 }
70 databaseUser, err = c.DatabaseUpdateUser(ctx, app, addonUUID, username, userUpdateParam)

Callers

nothing calls this directly

Calls 5

ErrorFunction · 0.92
ScalingoClientFunction · 0.92
isUsernameValidFunction · 0.85
askForPasswordWithRetryFunction · 0.85

Tested by

no test coverage detected