Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
VERSION := $(shell git describe --tags ${TAG})
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto admin-app compose-up-dev
.DEFAULT_GOAL := build
PROTON_COMMIT := "ee05c27600bd7d2782da4fde0997f84aa69e7eeb"
PROTON_COMMIT := "49c5649f4e8278df4c7ba9a07dfeb2827fdc3e15"

admin-app:
@echo " > generating admin build"
Expand Down
10 changes: 4 additions & 6 deletions cmd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func viewGroupCommand(cliConfig *Config) *cli.Command {
cmd := &cli.Command{
Use: "view",
Short: "View a group",
Args: cli.ExactArgs(2),
Args: cli.ExactArgs(1),
Example: heredoc.Doc(`
$ frontier group view <org-id> <group-id>
$ frontier group view <group-id>
`),
Annotations: map[string]string{
"group": "core",
Expand All @@ -170,11 +170,9 @@ func viewGroupCommand(cliConfig *Config) *cli.Command {
return err
}

orgID := args[0]
groupID := args[1]
groupID := args[0]
req, err := newRequest(&frontierv1beta1.GetGroupRequest{
Id: groupID,
OrgId: orgID,
Id: groupID,
}, header)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func TestClientGroup(t *testing.T) {
{
name: "`group` view without host should throw error host not found",
want: "",
subCommands: []string{"view", orgID, "123"},
subCommands: []string{"view", "123"},
err: cmd.ErrClientConfigHostNotFound,
},
{
name: "`group` view with host flag should return error",
want: "",
subCommands: []string{"view", orgID, "123", "-h", "test"},
subCommands: []string{"view", "123", "-h", "test"},
wantErr: true,
},
}
Expand Down
2 changes: 2 additions & 0 deletions core/group/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ type Filter struct {
State State
WithMemberCount bool

IncludeDisabled bool

GroupIDs []string
}
8 changes: 6 additions & 2 deletions core/group/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ func (s Service) Get(ctx context.Context, id string) (Group, error) {
return s.repository.GetByID(ctx, id)
}

func (s Service) GetByIDs(ctx context.Context, ids []string) ([]Group, error) {
return s.repository.GetByIDs(ctx, ids, Filter{})
func (s Service) GetByIDs(ctx context.Context, ids []string, flt ...Filter) ([]Group, error) {
f := Filter{}
if len(flt) > 0 {
f = flt[0]
Comment thread
whoAbhishekSah marked this conversation as resolved.
}
return s.repository.GetByIDs(ctx, ids, f)
}

func (s Service) List(ctx context.Context, flt Filter) ([]Group, error) {
Expand Down
156 changes: 59 additions & 97 deletions internal/api/v1beta1connect/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,38 @@ func (h *ConnectHandler) ListOrganizationGroups(ctx context.Context, request *co
return connect.NewResponse(&frontierv1beta1.ListOrganizationGroupsResponse{Groups: groups}), nil
}

// ensureOrgEnabled fails with FailedPrecondition when the org is disabled.
func (h *ConnectHandler) ensureOrgEnabled(ctx context.Context, orgID string) error {
Comment thread
whoAbhishekSah marked this conversation as resolved.
if _, err := h.orgService.Get(ctx, orgID); err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return connect.NewError(connect.CodeInternal, fmt.Errorf("ensureOrgEnabled: org_id=%s: %w", orgID, err))
}
}
return nil
}

// getGroupInEnabledOrg loads the group and makes sure its own org is enabled.
func (h *ConnectHandler) getGroupInEnabledOrg(ctx context.Context, groupID string) (group.Group, error) {
grp, err := h.groupService.Get(ctx, groupID)
if err != nil {
switch {
case errors.Is(err, group.ErrNotExist), errors.Is(err, group.ErrInvalidID), errors.Is(err, group.ErrInvalidUUID):
return group.Group{}, connect.NewError(connect.CodeNotFound, ErrGroupNotFound)
default:
return group.Group{}, connect.NewError(connect.CodeInternal, fmt.Errorf("getGroupInEnabledOrg: group_id=%s: %w", groupID, err))
}
}
if err := h.ensureOrgEnabled(ctx, grp.OrganizationID); err != nil {
Comment thread
whoAbhishekSah marked this conversation as resolved.
return group.Group{}, err
}
return grp, nil
}

// listGroupUsers returns the users that are members of the given group.
// When roleID is non-empty, only users with that role are returned.
func (h *ConnectHandler) listGroupUsers(ctx context.Context, groupID, roleID string) ([]user.User, error) {
Expand Down Expand Up @@ -174,26 +206,9 @@ func (h *ConnectHandler) CreateGroup(ctx context.Context, request *connect.Reque
}

func (h *ConnectHandler) GetGroup(ctx context.Context, request *connect.Request[frontierv1beta1.GetGroupRequest]) (*connect.Response[frontierv1beta1.GetGroupResponse], error) {
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
fetchedGroup, err := h.getGroupInEnabledOrg(ctx, request.Msg.GetId())
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("GetGroup.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
}

fetchedGroup, err := h.groupService.Get(ctx, request.Msg.GetId())
if err != nil {
switch {
case errors.Is(err, group.ErrNotExist), errors.Is(err, group.ErrInvalidID), errors.Is(err, group.ErrInvalidUUID):
return nil, connect.NewError(connect.CodeNotFound, ErrGroupNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("GetGroup.Get: group_id=%s: %w", request.Msg.GetId(), err))
}
return nil, err
}

groupPB, err := transformGroupToPB(fetchedGroup)
Expand Down Expand Up @@ -230,30 +245,21 @@ func (h *ConnectHandler) UpdateGroup(ctx context.Context, request *connect.Reque
return nil, connect.NewError(connect.CodeInvalidArgument, ErrBadRequest)
}

orgResp, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateGroup.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
}

metaDataMap := metadata.Build(request.Msg.GetBody().GetMetadata().AsMap())

if err := h.metaSchemaService.Validate(metaDataMap, groupMetaSchema); err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, ErrBadBodyMetaSchemaError)
}

if _, err := h.getGroupInEnabledOrg(ctx, request.Msg.GetId()); err != nil {
return nil, err
}

updatedGroup, err := h.groupService.Update(ctx, group.Group{
ID: request.Msg.GetId(),
Name: request.Msg.GetBody().GetName(),
Title: request.Msg.GetBody().GetTitle(),
OrganizationID: orgResp.ID,
Metadata: metaDataMap,
ID: request.Msg.GetId(),
Name: request.Msg.GetBody().GetName(),
Title: request.Msg.GetBody().GetTitle(),
Metadata: metaDataMap,
})
if err != nil {
switch {
Expand All @@ -273,21 +279,13 @@ func (h *ConnectHandler) UpdateGroup(ctx context.Context, request *connect.Reque
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateGroup: entity_id=%s: %w", updatedGroup.ID, err))
}

audit.GetAuditor(ctx, orgResp.ID).Log(audit.GroupUpdatedEvent, audit.GroupTarget(updatedGroup.ID))
audit.GetAuditor(ctx, updatedGroup.OrganizationID).Log(audit.GroupUpdatedEvent, audit.GroupTarget(updatedGroup.ID))
return connect.NewResponse(&frontierv1beta1.UpdateGroupResponse{Group: &groupPB}), nil
}

func (h *ConnectHandler) ListGroupUsers(ctx context.Context, request *connect.Request[frontierv1beta1.ListGroupUsersRequest]) (*connect.Response[frontierv1beta1.ListGroupUsersResponse], error) {
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("ListGroupUsers.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
if _, err := h.getGroupInEnabledOrg(ctx, request.Msg.GetId()); err != nil {
return nil, err
}

members, err := h.membershipService.ListPrincipalsByResource(ctx, request.Msg.GetId(), schema.GroupNamespace, membership.MemberFilter{
Expand Down Expand Up @@ -336,16 +334,8 @@ func (h *ConnectHandler) ListGroupUsers(ctx context.Context, request *connect.Re
}

func (h *ConnectHandler) RemoveGroupUser(ctx context.Context, request *connect.Request[frontierv1beta1.RemoveGroupUserRequest]) (*connect.Response[frontierv1beta1.RemoveGroupUserResponse], error) {
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("RemoveGroupUser.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
if _, err := h.getGroupInEnabledOrg(ctx, request.Msg.GetId()); err != nil {
return nil, err
}

if err := h.membershipService.RemoveGroupMember(ctx, request.Msg.GetId(), request.Msg.GetUserId(), schema.UserPrincipal); err != nil {
Expand All @@ -370,21 +360,13 @@ func (h *ConnectHandler) RemoveGroupUser(ctx context.Context, request *connect.R
}

func (h *ConnectHandler) SetGroupMemberRole(ctx context.Context, request *connect.Request[frontierv1beta1.SetGroupMemberRoleRequest]) (*connect.Response[frontierv1beta1.SetGroupMemberRoleResponse], error) {
orgID := request.Msg.GetOrgId()
groupID := request.Msg.GetGroupId()
principalID := request.Msg.GetPrincipalId()
principalType := request.Msg.GetPrincipalType()
roleID := request.Msg.GetRoleId()

if _, err := h.orgService.Get(ctx, orgID); err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SetGroupMemberRole.GetOrg: org_id=%s: %w", orgID, err))
}
if _, err := h.getGroupInEnabledOrg(ctx, groupID); err != nil {
return nil, err
}

if err := h.membershipService.SetGroupMemberRole(ctx, groupID, principalID, principalType, roleID); err != nil {
Expand Down Expand Up @@ -414,16 +396,15 @@ func (h *ConnectHandler) SetGroupMemberRole(ctx context.Context, request *connec
}

func (h *ConnectHandler) EnableGroup(ctx context.Context, request *connect.Request[frontierv1beta1.EnableGroupRequest]) (*connect.Response[frontierv1beta1.EnableGroupResponse], error) {
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
grps, err := h.groupService.GetByIDs(ctx, []string{request.Msg.GetId()}, group.Filter{IncludeDisabled: true})
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("EnableGroup.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("EnableGroup.GetByIDs: group_id=%s: %w", request.Msg.GetId(), err))
}
if len(grps) == 0 {
return nil, connect.NewError(connect.CodeNotFound, ErrGroupNotFound)
}
if err := h.ensureOrgEnabled(ctx, grps[0].OrganizationID); err != nil {
return nil, err
}
if err := h.groupService.Enable(ctx, request.Msg.GetId()); err != nil {
switch {
Expand All @@ -437,16 +418,8 @@ func (h *ConnectHandler) EnableGroup(ctx context.Context, request *connect.Reque
}

func (h *ConnectHandler) DisableGroup(ctx context.Context, request *connect.Request[frontierv1beta1.DisableGroupRequest]) (*connect.Response[frontierv1beta1.DisableGroupResponse], error) {
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("DisableGroup.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
if _, err := h.getGroupInEnabledOrg(ctx, request.Msg.GetId()); err != nil {
return nil, err
}
if err := h.groupService.Disable(ctx, request.Msg.GetId()); err != nil {
switch {
Expand All @@ -460,17 +433,6 @@ func (h *ConnectHandler) DisableGroup(ctx context.Context, request *connect.Requ
}

func (h *ConnectHandler) DeleteGroup(ctx context.Context, request *connect.Request[frontierv1beta1.DeleteGroupRequest]) (*connect.Response[frontierv1beta1.DeleteGroupResponse], error) {
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
if err != nil {
switch {
case errors.Is(err, organization.ErrDisabled):
return nil, connect.NewError(connect.CodeFailedPrecondition, ErrOrgDisabled)
case errors.Is(err, organization.ErrNotExist):
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("DeleteGroup.Get: org_id=%s: %w", request.Msg.GetOrgId(), err))
}
}
if err := h.deleterService.DeleteGroup(ctx, request.Msg.GetId()); err != nil {
switch {
case errors.Is(err, group.ErrNotExist):
Expand Down
Loading
Loading