MCPcopy Index your code
hub / github.com/dgraph-io/dgraph / AddUserToGroup

Method AddUserToGroup

dgraphapi/acl.go:231–288  ·  view source on GitHub ↗
(userName, group string)

Source from the content-addressed store, hash-verified

229}
230
231func (hc *HTTPClient) AddUserToGroup(userName, group string) error {
232 const query = `mutation updateUser($name: String!, $group: String!) {
233 updateUser(input: {filter: {name: {eq: $name}},set: {groups: [{ name: $group }]}}) {
234 user {
235 name
236 groups {
237 name
238 }
239 }
240 }
241 }`
242 params := GraphQLParams{
243 Query: query,
244 Variables: map[string]interface{}{
245 "name": userName,
246 "group": group,
247 },
248 }
249 resp, err := hc.RunGraphqlQuery(params, true)
250 if err != nil {
251 return err
252 }
253 var result struct {
254 UpdateUser struct {
255 User []struct {
256 Name string
257 Groups []struct {
258 Name string
259 }
260 }
261 Name string
262 }
263 }
264 if err := json.Unmarshal(resp, &result); err != nil {
265 return errors.Wrap(err, "error unmarshalling response")
266 }
267
268 if len(result.UpdateUser.User) != 1 {
269 return errACLGroupCountMoreThanOne
270 }
271 if userName != result.UpdateUser.User[0].Name {
272 return errors.Errorf("username should be same, expected:%v, actual:%v", userName, result.UpdateUser.User[0].Name)
273 }
274
275 var foundGroup bool
276 for _, usr := range result.UpdateUser.User {
277 for _, grp := range usr.Groups {
278 if grp.Name == group {
279 foundGroup = true
280 break
281 }
282 }
283 }
284 if !foundGroup {
285 return errors.New("group name should be present in response")
286 }
287 return nil
288}

Calls 2

RunGraphqlQueryMethod · 0.95
ErrorfMethod · 0.45