changePassword changes a user's password
(conf *viper.Viper, userId string)
| 268 | |
| 269 | // changePassword changes a user's password |
| 270 | func changePassword(conf *viper.Viper, userId string) error { |
| 271 | // 1. get the dgo client with appropriate access JWT |
| 272 | dc, cancel, err := getClientWithAdminCtx(conf) |
| 273 | if err != nil { |
| 274 | return fmt.Errorf("unable to get dgo client: %w", err) |
| 275 | } |
| 276 | defer cancel() |
| 277 | |
| 278 | // 2. get the new password |
| 279 | newPassword, err := x.AskUserPassword(userId, "New", 2) |
| 280 | if err != nil { |
| 281 | return err |
| 282 | } |
| 283 | |
| 284 | ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 285 | defer ctxCancel() |
| 286 | txn := dc.NewTxn() |
| 287 | defer func() { |
| 288 | if err := txn.Discard(ctx); err != nil { |
| 289 | glog.Errorf("Unable to discard transaction:%v", err) |
| 290 | } |
| 291 | }() |
| 292 | |
| 293 | // 3. query the user's current uid |
| 294 | user, err := queryUser(ctx, txn, userId) |
| 295 | if err != nil { |
| 296 | return fmt.Errorf("while querying user: %w", err) |
| 297 | } |
| 298 | if user == nil { |
| 299 | return fmt.Errorf("user %q does not exist", userId) |
| 300 | } |
| 301 | |
| 302 | // 4. mutate the user's password |
| 303 | chPdNQuads := []*api.NQuad{ |
| 304 | { |
| 305 | Subject: user.Uid, |
| 306 | Predicate: "dgraph.password", |
| 307 | ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: newPassword}}, |
| 308 | }} |
| 309 | mu := &api.Mutation{ |
| 310 | CommitNow: true, |
| 311 | Set: chPdNQuads, |
| 312 | } |
| 313 | if _, err := txn.Mutate(ctx, mu); err != nil { |
| 314 | return fmt.Errorf("unable to change password for user %v: %w", userId, err) |
| 315 | } |
| 316 | fmt.Printf("Successfully changed password for %v\n", userId) |
| 317 | return nil |
| 318 | } |
| 319 | |
| 320 | func userMod(conf *viper.Viper, userId string, groups string) error { |
| 321 | dc, cancel, err := getClientWithAdminCtx(conf) |
no test coverage detected