Skip to content

Commit

Permalink
Merge pull request #35 from permitio/raz/per-9544-role-assignment-in-…
Browse files Browse the repository at this point in the history
…go-doesnt-support-instances

add resource instances to assign role
  • Loading branch information
RazcoDev authored Apr 3, 2024
2 parents 82ddd9a + 270a29b commit 37bf6ff
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 12 deletions.
20 changes: 20 additions & 0 deletions pkg/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ func (u *Users) AssignRole(ctx context.Context, userKey string, roleKey string,
return roleAssignmentRead, nil
}

// AssignResourceRole assigns a *resource* role to a user in your context's environment, by user key, role key, tenant key and a resource instance string (e.g. "document:mydoc").
// Usage Example:
// `roleAssignment, err := PermitClient.Api.Users.AssignRole(ctx, "user-key", "role-key", "default", "document:mydoc")`
func (u *Users) AssignResourceRole(ctx context.Context, userKey string, roleKey string, tenantKey string, resourceInstance string) (*models.RoleAssignmentRead, error) {
err := u.lazyLoadPermitContext(ctx)
if err != nil {
u.logger.Error("", zap.Error(err))
return nil, err
}
userRoleCreate := *models.NewUserRoleCreate(roleKey, tenantKey)
userRoleCreate.SetResourceInstance(resourceInstance)
roleAssignmentRead, httpRes, err := u.client.UsersApi.AssignRoleToUser(ctx, u.config.Context.GetProject(), u.config.Context.GetEnvironment(), userKey).UserRoleCreate(userRoleCreate).Execute()
err = errors.HttpErrorHandle(err, httpRes)
if err != nil {
u.logger.Error("error assigning role:"+roleKey+" to user:"+userKey, zap.Error(err))
return nil, err
}
return roleAssignmentRead, nil
}

// UnassignRole unassigns a role from a user in your context's environment, by user key, role key and tenant key.
// Usage Example:
// `err := PermitClient.Api.Users.UnassignRole(ctx, "user-key", "role-key", "default")`
Expand Down
100 changes: 93 additions & 7 deletions pkg/models/model_role_assignment_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 91 additions & 5 deletions pkg/models/model_user_role_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37bf6ff

Please sign in to comment.