Skip to content

Commit

Permalink
Add support for user's manager (#111)
Browse files Browse the repository at this point in the history
* Add support for user's manager

* doc update

* fix imports

* Whitespace

Co-authored-by: Tom Bamford <[email protected]>
  • Loading branch information
michaljirman and manicminer authored Oct 5, 2021
1 parent ccd2d0b commit 81791c3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
50 changes: 50 additions & 0 deletions msgraph/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,53 @@ func (c *UsersClient) Sendmail(ctx context.Context, id string, message MailMessa

return status, nil
}

// GetManager retrieves an user or organizational contact assigned as the user's manager.
func (c *UsersClient) GetManager(ctx context.Context, id string) (*User, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/users/%s/manager", id),
},
})
if err != nil {
return nil, status, err
}

defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
var manager User
if err := json.Unmarshal(respBody, &manager); err != nil {
return nil, status, err
}

return &manager, status, nil
}

// AssignManager assigns a user's manager.
func (c *UsersClient) AssignManager(ctx context.Context, id string, manager User) (int, error) {
var status int

body, err := json.Marshal(struct {
Manager odata.Id `json:"@odata.id"`
}{
Manager: *manager.ODataId,
})
if err != nil {
return status, fmt.Errorf("json.Marshal(): %v", err)
}

_, status, _, err = c.BaseClient.Put(ctx, PutHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/users/%s/manager/$ref", id),
HasTenantId: true,
},
})
if err != nil {
return status, fmt.Errorf("UsersClient.BaseClient.Post(): %v", err)
}

return status, nil
}
39 changes: 39 additions & 0 deletions msgraph/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func TestUsersClient(t *testing.T) {
testUsersClient_Update(t, c, *user)
testUsersClient_List(t, c)

manager := testUsersClient_Create(t, c, msgraph.User{
AccountEnabled: utils.BoolPtr(true),
DisplayName: utils.StringPtr("test-user-manager"),
MailNickname: utils.StringPtr(fmt.Sprintf("test-user-manager-%s", c.randomString)),
UserPrincipalName: utils.StringPtr(fmt.Sprintf("test-user-manager-%s@%s", c.randomString, c.connection.DomainName)),
PasswordProfile: &msgraph.UserPasswordProfile{
Password: utils.StringPtr(fmt.Sprintf("IrPa55w0rd%s", c.randomString)),
},
})
testUsersClient_Get(t, c, *manager.ID)

g := GroupsClientTest{
connection: test.NewConnection(auth.MsGraph, auth.TokenVersion2),
randomString: rs,
Expand Down Expand Up @@ -71,6 +82,10 @@ func TestUsersClient(t *testing.T) {
testGroupsClient_Delete(t, g, *groupParent.ID)
testGroupsClient_Delete(t, g, *groupChild.ID)

testUsersClient_AssignManager(t, c, *user.ID, *manager)
testUsersClient_GetManager(t, c, *user.ID)
testUsersClient_Delete(t, c, *manager.ID)

testUsersClient_Delete(t, c, *user.ID)
testUsersClient_ListDeleted(t, c, *user.ID)
testUsersClient_GetDeleted(t, c, *user.ID)
Expand Down Expand Up @@ -230,3 +245,27 @@ func testUsersClient_RestoreDeleted(t *testing.T, c UsersClientTest, id string)
t.Fatal("UsersClient.RestoreDeleted(): user ids do not match")
}
}

func testUsersClient_AssignManager(t *testing.T, c UsersClientTest, id string, manager msgraph.User) {
status, err := c.client.AssignManager(c.connection.Context, id, manager)
if err != nil {
t.Fatalf("UsersClient.AssignManager(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("UsersClient.AssignManager(): invalid status: %d", status)
}
}

func testUsersClient_GetManager(t *testing.T, c UsersClientTest, id string) (user *msgraph.User) {
user, status, err := c.client.GetManager(c.connection.Context, id)
if err != nil {
t.Fatalf("UsersClient.GetManager(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("UsersClient.GetManager(): invalid status: %d", status)
}
if user == nil {
t.Fatal("UsersClient.GetManager(): user was nil")
}
return
}

0 comments on commit 81791c3

Please sign in to comment.