Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for app role assignments for users, groups and sp #39

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions msgraph/app_role_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,50 @@ import (
"net/http"
)

type appRoleAssignmentsResourceType string

const (
groupsAppRoleAssignmentsResource appRoleAssignmentsResourceType = "groups"
usersAppRoleAssignmentsResource appRoleAssignmentsResourceType = "users"
servicePrincipalsAppRoleAssignmentsResource appRoleAssignmentsResourceType = "servicePrincipals"
)

// AppRoleAssignmentsClient performs operations on AppRoleAssignments.
type AppRoleAssignmentsClient struct {
BaseClient Client
BaseClient Client
resourceType appRoleAssignmentsResourceType
}

// NewUsersAppRoleAssignmentsClient returns a new AppRoleAssignmentsClient for users assignments
func NewUsersAppRoleAssignmentsClient(tenantId string) *AppRoleAssignmentsClient {
return &AppRoleAssignmentsClient{
BaseClient: NewClient(Version10, tenantId),
resourceType: usersAppRoleAssignmentsResource,
}
}

// NewGroupsAppRoleAssignmentsClient returns a new AppRoleAssignmentsClient for groups assignments
func NewGroupsAppRoleAssignmentsClient(tenantId string) *AppRoleAssignmentsClient {
return &AppRoleAssignmentsClient{
BaseClient: NewClient(Version10, tenantId),
resourceType: groupsAppRoleAssignmentsResource,
}
}

// NewAppRoleAssignmentsClient returns a new AppRoleAssignmentsClient
func NewAppRoleAssignmentsClient(tenantId string) *AppRoleAssignmentsClient {
// NewServicePrincipalsAppRoleAssignmentsClient returns a new AppRoleAssignmentsClient for service principal assignments
func NewServicePrincipalsAppRoleAssignmentsClient(tenantId string) *AppRoleAssignmentsClient {
return &AppRoleAssignmentsClient{
BaseClient: NewClient(Version10, tenantId),
BaseClient: NewClient(Version10, tenantId),
resourceType: servicePrincipalsAppRoleAssignmentsResource,
}
}

// List returns a list of app role assignments.
func (c *AppRoleAssignmentsClient) List(ctx context.Context, groupId string) (*[]AppRoleAssignment, int, error) {
func (c *AppRoleAssignmentsClient) List(ctx context.Context, id string) (*[]AppRoleAssignment, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/groups/%s/appRoleAssignments", groupId),
Entity: fmt.Sprintf("/%s/%s/appRoleAssignments", c.resourceType, id),
HasTenantId: true,
},
})
Expand All @@ -47,11 +73,11 @@ func (c *AppRoleAssignmentsClient) List(ctx context.Context, groupId string) (*[
}

// Remove removes a app role assignment.
func (c *AppRoleAssignmentsClient) Remove(ctx context.Context, groupId, appRoleAssignmentId string) (int, error) {
func (c *AppRoleAssignmentsClient) Remove(ctx context.Context, id, appRoleAssignmentId string) (int, error) {
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/groups/%s/appRoleAssignments/%s", groupId, appRoleAssignmentId),
Entity: fmt.Sprintf("/%s/%s/appRoleAssignments/%s", c.resourceType, id, appRoleAssignmentId),
HasTenantId: true,
},
})
Expand All @@ -61,16 +87,16 @@ func (c *AppRoleAssignmentsClient) Remove(ctx context.Context, groupId, appRoleA
return status, nil
}

// Assign assigns an app role to a group.
func (c *AppRoleAssignmentsClient) Assign(ctx context.Context, groupId, resourceId, appRoleId string) (*AppRoleAssignment, int, error) {
// Assign assigns an app role to a user, group or service principal depending on client resource type.
func (c *AppRoleAssignmentsClient) Assign(ctx context.Context, clientServicePrincipalId, resourceServicePrincipalId, appRoleId string) (*AppRoleAssignment, int, error) {
var status int
data := struct {
PrincipalId string `json:"principalId"`
ResourceId string `json:"resourceId"`
AppRoleId string `json:"appRoleId"`
}{
PrincipalId: groupId,
ResourceId: resourceId,
PrincipalId: clientServicePrincipalId,
ResourceId: resourceServicePrincipalId,
AppRoleId: appRoleId,
}

Expand All @@ -82,7 +108,7 @@ func (c *AppRoleAssignmentsClient) Assign(ctx context.Context, groupId, resource
Body: body,
ValidStatusCodes: []int{http.StatusCreated},
Uri: Uri{
Entity: fmt.Sprintf("/groups/%s/appRoleAssignments", groupId),
Entity: fmt.Sprintf("/%s/%s/appRoleAssignments", c.resourceType, clientServicePrincipalId),
HasTenantId: true,
},
})
Expand Down
Loading