Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 36 additions & 2 deletions pkg/connector/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import (
)

var licences = []string{creator, explorer, viewer, unlicensed}
var licensesMap = map[string]string{
"creator": creator,
"explorer": explorer,
"viewer": viewer,
"unlicensed": unlicensed,
}
var RolesPerLicense = map[string][]string{
creator: {siteAdministratorCreator, creator},
explorer: {siteAdministratorExplorer, explorerCanPublish, explorer, readOnly, siteAdministrator},
creator: {creator, siteAdministratorCreator},
explorer: {explorer, siteAdministratorExplorer, explorerCanPublish, readOnly, siteAdministrator},
viewer: {viewer},
unlicensed: {unlicensed},
}
Expand Down Expand Up @@ -105,6 +111,34 @@ func (l *licenseResourceType) Grants(ctx context.Context, resource *v2.Resource,
return rv, token, nil, nil
}

func (l *licenseResourceType) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) {
licenseName, ok := licensesMap[entitlement.Resource.Id.Resource]
if !ok {
return nil, fmt.Errorf("unknown license %s", entitlement.Resource.Id.Resource)
}
principalID := principal.Id.Resource

outputAnnotations := annotations.New()
err := l.client.UpdateUserSiteRole(ctx, principalID, licenseName)
if err != nil {
return outputAnnotations, fmt.Errorf("failed to grant %s license to user %s: %w", licenseName, principalID, err)
}

return outputAnnotations, nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: License Assignment Error and Missing Rate Limiting

The Grant method uses entitlement.Resource.Id.Resource for license name lookup, which is an editable slug and may cause incorrect license assignments. It should parse the entitlement.Id instead, similar to the site.go's Grant method. Additionally, the outputAnnotations in Grant are not populated with rate limiting information.

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

func (l *licenseResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotations.Annotations, error) {
principalID := grant.Principal.Id.Resource

outputAnnotations := annotations.New()
err := l.client.UpdateUserSiteRole(ctx, principalID, unlicensed)
if err != nil {
return outputAnnotations, fmt.Errorf("failed to revoke license from user %s: %w", principalID, err)
}

return outputAnnotations, nil
}

func licenseBuilder(client *tableau.Client) *licenseResourceType {
return &licenseResourceType{
resourceType: resourceTypeLicense,
Expand Down
37 changes: 37 additions & 0 deletions pkg/connector/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,43 @@ func (o *siteResourceType) Grants(ctx context.Context, resource *v2.Resource, pt
return rv, "", nil, nil
}

func (o *siteResourceType) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) {
roleName := entitlement.Slug
principalID := principal.Id.Resource

var apiRoleName string
for key, value := range roles {
if value == roleName {
apiRoleName = key
break
}
}

if apiRoleName == "" {
return nil, fmt.Errorf("unknown role: %s", roleName)
}

outputAnnotations := annotations.New()
err := o.client.UpdateUserSiteRole(ctx, principalID, apiRoleName)
if err != nil {
return outputAnnotations, fmt.Errorf("failed to grant %s role to user %s: %w", roleName, principalID, err)
}

return outputAnnotations, nil
}

func (o *siteResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotations.Annotations, error) {
principalID := grant.Principal.Id.Resource

outputAnnotations := annotations.New()
err := o.client.UpdateUserSiteRole(ctx, principalID, unlicensed)
if err != nil {
return outputAnnotations, fmt.Errorf("failed to revoke site role from user %s: %w", principalID, err)
}

return outputAnnotations, nil
}

func siteBuilder(client *tableau.Client) *siteResourceType {
return &siteResourceType{
resourceType: resourceTypeSite,
Expand Down
23 changes: 23 additions & 0 deletions pkg/tableau/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,26 @@ func (c *Client) RemoveUserFromSite(ctx context.Context, userId string) error {

return nil
}

func (c *Client) UpdateUserSiteRole(ctx context.Context, userId string, siteRole string) error {
url := fmt.Sprint(c.baseUrl, "/sites/", c.siteId, "/users/", userId)
var res struct {
User User `json:"user"`
}

requestBody, err := json.Marshal(map[string]interface{}{
"user": map[string]interface{}{
"siteRole": siteRole,
},
})

if err != nil {
return err
}

if err := c.doRequest(ctx, url, &res, nil, requestBody, http.MethodPut); err != nil {
return err
}

return nil
}
Loading