-
Notifications
You must be signed in to change notification settings - Fork 0
[BB-1925] Add workspace role #69
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "maps" | ||
| "slices" | ||
|
|
||
| v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
| "github.com/conductorone/baton-sdk/pkg/types/entitlement" | ||
| resources "github.com/conductorone/baton-sdk/pkg/types/resource" | ||
|
|
||
| "github.com/conductorone/baton-slack/pkg" | ||
| "github.com/conductorone/baton-slack/pkg/connector/client" | ||
| ) | ||
|
|
||
| const ( | ||
| PrimaryOwnerRoleID = "primary_owner" | ||
| OwnerRoleID = "owner" | ||
| AdminRoleID = "admin" | ||
| MultiChannelGuestRoleID = "multi_channel_guest" | ||
| SingleChannelGuestRoleID = "single_channel_guest" | ||
| InvitedMemberRoleID = "invited_member" | ||
| BotRoleID = "bot" | ||
| MemberRoleID = "member" | ||
| RoleAssignmentEntitlement = "assigned" | ||
| ) | ||
|
|
||
| var roles = map[string]string{ | ||
| PrimaryOwnerRoleID: "Primary Owner", | ||
| OwnerRoleID: "Owner", | ||
| AdminRoleID: "Admin", | ||
| MultiChannelGuestRoleID: "Multi Channel Guest", | ||
| SingleChannelGuestRoleID: "Single Channel Guest", | ||
| InvitedMemberRoleID: "Invited member", | ||
| BotRoleID: "Bot", | ||
| MemberRoleID: "Member", | ||
| } | ||
|
|
||
| type workspaceRoleType struct { | ||
| resourceType *v2.ResourceType | ||
| businessPlusClient *client.Client | ||
| } | ||
|
|
||
| func (o *workspaceRoleType) ResourceType(_ context.Context) *v2.ResourceType { | ||
| return o.resourceType | ||
| } | ||
|
|
||
| func workspaceRoleBuilder(businessPlusClient *client.Client) *workspaceRoleType { | ||
| return &workspaceRoleType{ | ||
| resourceType: resourceTypeWorkspaceRole, | ||
| businessPlusClient: businessPlusClient, | ||
| } | ||
| } | ||
|
|
||
| func roleResource( | ||
| _ context.Context, | ||
| roleID string, | ||
| parentResourceID *v2.ResourceId, | ||
| ) (*v2.Resource, error) { | ||
| roleName, ok := roles[roleID] | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid roleID: %s", roleID) | ||
| } | ||
|
|
||
| roleId := fmt.Sprintf("%s:%s", parentResourceID.Resource, roleID) | ||
|
|
||
| r, err := resources.NewRoleResource( | ||
| roleName, | ||
| resourceTypeWorkspaceRole, | ||
| roleId, | ||
| nil, | ||
| resources.WithParentResourceID(parentResourceID)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return r, nil | ||
| } | ||
|
|
||
| func (o *workspaceRoleType) List( | ||
| ctx context.Context, | ||
| parentResourceID *v2.ResourceId, | ||
| _ resources.SyncOpAttrs, | ||
| ) ( | ||
| []*v2.Resource, | ||
| *resources.SyncOpResults, | ||
| error, | ||
| ) { | ||
| if parentResourceID == nil { | ||
| return nil, &resources.SyncOpResults{}, nil | ||
| } | ||
|
|
||
| output, err := pkg.MakeResourceList( | ||
| ctx, | ||
| slices.Collect(maps.Keys(roles)), | ||
| parentResourceID, | ||
| roleResource, | ||
| ) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| return output, &resources.SyncOpResults{}, nil | ||
| } | ||
|
|
||
| func (o *workspaceRoleType) Entitlements( | ||
| ctx context.Context, | ||
| resource *v2.Resource, | ||
| attrs resources.SyncOpAttrs, | ||
| ) ( | ||
| []*v2.Entitlement, | ||
| *resources.SyncOpResults, | ||
| error, | ||
| ) { | ||
| found, missing, err := client.GetWorkspaceNames(ctx, attrs.Session, []string{resource.ParentResourceId.Resource}) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("error getting workspace name for workspace id %s: %w", resource.ParentResourceId.Resource, err) | ||
| } | ||
| workspaceName, exists := found[resource.ParentResourceId.Resource] | ||
| if !exists { | ||
| return nil, nil, fmt.Errorf("workspace not found in cache: %s (missing: %v)", resource.ParentResourceId.Resource, missing) | ||
| } | ||
| return []*v2.Entitlement{ | ||
| entitlement.NewAssignmentEntitlement( | ||
| resource, | ||
| RoleAssignmentEntitlement, | ||
| entitlement.WithGrantableTo(resourceTypeUser), | ||
| entitlement.WithDescription( | ||
| fmt.Sprintf( | ||
| "Has the %s role in the Slack %s workspace", | ||
| resource.DisplayName, | ||
| workspaceName, | ||
| ), | ||
| ), | ||
| entitlement.WithDisplayName( | ||
| fmt.Sprintf( | ||
| "%s workspace %s role", | ||
| workspaceName, | ||
| resource.DisplayName, | ||
| ), | ||
| ), | ||
| ), | ||
| }, | ||
| &resources.SyncOpResults{}, | ||
| nil | ||
| } | ||
|
|
||
| // Grants would normally return the grants for each role resource. Due to how | ||
| // the Slack API works, it is more efficient to emit these roles while listing | ||
| // grants for each individual user. Instead of having to list users for each | ||
| // role we can divine which roles a user should be granted when calculating | ||
| // their grants. | ||
| // TLDR: workspaceRoles are set in the workspace.go's Grants method. | ||
| func (o *workspaceRoleType) Grants( | ||
| _ context.Context, | ||
| _ *v2.Resource, | ||
| _ resources.SyncOpAttrs, | ||
| ) ( | ||
| []*v2.Grant, | ||
| *resources.SyncOpResults, | ||
| error, | ||
| ) { | ||
| return nil, &resources.SyncOpResults{}, nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotations: annotations.New(&v2.SkipGrants{}), since grants is a noop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done