MCPcopy
hub / github.com/dgraph-io/dgraph / changePassword

Function changePassword

acl/acl.go:270–318  ·  view source on GitHub ↗

changePassword changes a user's password

(conf *viper.Viper, userId string)

Source from the content-addressed store, hash-verified

268
269// changePassword changes a user's password
270func 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
320func userMod(conf *viper.Viper, userId string, groups string) error {
321 dc, cancel, err := getClientWithAdminCtx(conf)

Callers 1

modFunction · 0.85

Calls 5

AskUserPasswordFunction · 0.92
getClientWithAdminCtxFunction · 0.85
queryUserFunction · 0.85
MutateMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected