Skip to content

Commit

Permalink
add AssignResourceRole method
Browse files Browse the repository at this point in the history
  • Loading branch information
RazcoDev committed Apr 3, 2024
1 parent 6b01ac6 commit d2725ef
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pkg/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,33 @@ func (u *Users) Delete(ctx context.Context, userKey string) error {
// AssignRole assigns a role to a user in your context's environment, by user key, role key and tenant key.
// Usage Example:
// `roleAssignment, err := PermitClient.Api.Users.AssignRole(ctx, "user-key", "role-key", "default", "document:mydoc")`
func (u *Users) AssignRole(ctx context.Context, userKey string, roleKey string, tenantKey string, resourceInstance *string) (*models.RoleAssignmentRead, error) {
func (u *Users) AssignRole(ctx context.Context, userKey string, roleKey string, tenantKey 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)
if resourceInstance != nil {
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
}

// AssignResourceRole assigns a role to a user in your context's environment, by user key, role key and tenant key.
// 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 {
Expand Down

0 comments on commit d2725ef

Please sign in to comment.