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

Function userAdd

acl/acl.go:77–122  ·  view source on GitHub ↗
(conf *viper.Viper, userid string, password string)

Source from the content-addressed store, hash-verified

75}
76
77func userAdd(conf *viper.Viper, userid string, password string) error {
78 dc, cancel, err := getClientWithAdminCtx(conf)
79 if err != nil {
80 return fmt.Errorf("unable to get admin context: %w", err)
81 }
82 defer cancel()
83
84 if len(password) == 0 {
85 var err error
86 password, err = x.AskUserPassword(userid, "New", 2)
87 if err != nil {
88 return err
89 }
90 }
91
92 ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
93 defer ctxCancel()
94 txn := dc.NewTxn()
95 defer func() {
96 if err := txn.Discard(ctx); err != nil {
97 glog.Errorf("Unable to discard transaction:%v", err)
98 }
99 }()
100
101 user, err := queryUser(ctx, txn, userid)
102 if err != nil {
103 return fmt.Errorf("while querying user: %w", err)
104 }
105 if user != nil {
106 return fmt.Errorf("unable to create user because of conflict: %v", userid)
107 }
108
109 createUserNQuads := CreateUserNQuads(userid, password)
110
111 mu := &api.Mutation{
112 CommitNow: true,
113 Set: createUserNQuads,
114 }
115
116 if _, err := txn.Mutate(ctx, mu); err != nil {
117 return fmt.Errorf("unable to create user: %w", err)
118 }
119
120 fmt.Printf("Created new user with id %v\n", userid)
121 return nil
122}
123
124func groupAdd(conf *viper.Viper, groupId string) error {
125 dc, cancel, err := getClientWithAdminCtx(conf)

Callers 1

addFunction · 0.85

Calls 6

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

Tested by

no test coverage detected