Skip to content

Commit

Permalink
Move UpdateUser from GraphClient.go to User.go
Browse files Browse the repository at this point in the history
  • Loading branch information
TerraTalpi committed Aug 18, 2021
1 parent af7bec9 commit a4f6593
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
17 changes: 0 additions & 17 deletions GraphClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,23 +295,6 @@ func (g *GraphClient) CreateUser(userInput *User, opts ...CreateQueryOption) (Us
return user, err
}

// Patches a user given a user object. Note, only set the fields that should be changed
// Reference: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user-update
func (g *GraphClient) PatchUser(identifier string, userInput *User, opts ...PatchQueryOption) error {
resource := fmt.Sprintf("/users/%v", identifier)

bodyBytes, err := json.Marshal(userInput)
if err != nil {
return err
}

reader := bytes.NewReader(bodyBytes)
// TODO: check return body, maybe there is some potential success or error message hidden in it?
err = g.makePATCHAPICall(resource, compilePatchQueryOptions(opts), reader, nil)

return err
}

// UnmarshalJSON implements the json unmarshal to be used by the json-library.
// This method additionally to loading the TenantID, ApplicationID and ClientSecret
// immediately gets a Token from msgraph (hence initialize this GraphAPI instance)
Expand Down
24 changes: 24 additions & 0 deletions User.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package msgraph

import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -143,6 +145,28 @@ func (u User) GetFullName() string {
return fmt.Sprintf("%v %v", u.GivenName, u.Surname)
}

// UpdateUser patches this user object. Note, only set the fields that should be changed.
// Furthermore, the user cannot be disabled (field AccountEnabled) this way, because the
// default value of a boolean is false - and hence will not be posted via json.
//
// Reference: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user-update
func (u User) UpdateUser(userInput User, opts ...PatchQueryOption) error {
if u.graphClient == nil {
return ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/users/%v", u.ID)

bodyBytes, err := json.Marshal(userInput)
if err != nil {
return err
}

reader := bytes.NewReader(bodyBytes)
// TODO: check return body, maybe there is some potential success or error message hidden in it?
err = u.graphClient.makePATCHAPICall(resource, compilePatchQueryOptions(opts), reader, nil)
return err
}

// PrettySimpleString returns the User-instance simply formatted for logging purposes: {FullName (email) (activePhone)}
func (u User) PrettySimpleString() string {
return fmt.Sprintf("{ %v (%v) (%v) }", u.GetFullName(), u.Mail, u.GetActivePhone())
Expand Down

0 comments on commit a4f6593

Please sign in to comment.