Skip to content
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

cleanup: remove ListGroupRelations and its usage #756

Merged
merged 2 commits into from
Aug 29, 2024
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: 0 additions & 2 deletions core/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/raystack/frontier/internal/bootstrap/schema"

"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/pkg/metadata"
)

Expand All @@ -32,7 +31,6 @@ type Repository interface {
GetByIDs(ctx context.Context, groupIDs []string, flt Filter) ([]Group, error)
List(ctx context.Context, flt Filter) ([]Group, error)
UpdateByID(ctx context.Context, toUpdate Group) (Group, error)
ListGroupRelations(ctx context.Context, objectId, subjectType, role string) ([]relation.Relation, error)
SetState(ctx context.Context, id string, state State) error
Delete(ctx context.Context, id string) error
}
Expand Down
65 changes: 1 addition & 64 deletions core/group/mocks/repository.go

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

46 changes: 0 additions & 46 deletions internal/store/postgres/group_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import (
"fmt"
"strings"

"github.com/raystack/frontier/internal/bootstrap/schema"

"github.com/doug-martin/goqu/v9"
"github.com/raystack/frontier/core/group"
"github.com/raystack/frontier/core/organization"
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/pkg/db"
)

Expand Down Expand Up @@ -274,49 +271,6 @@ func (r GroupRepository) UpdateByID(ctx context.Context, grp group.Group) (group
return updated, nil
}

// TODO(kushsharma): no longer in use, delete if needed
func (r GroupRepository) ListGroupRelations(ctx context.Context, objectId string, subject_type string, role string) ([]relation.Relation, error) {
whereClauseExp := goqu.Ex{}
whereClauseExp["object_id"] = objectId
whereClauseExp["object_namespace_name"] = schema.GroupNamespace

if subject_type != "" {
if subject_type == "user" {
whereClauseExp["subject_namespace_name"] = schema.UserPrincipal
} else if subject_type == "group" {
whereClauseExp["subject_namespace_name"] = schema.GroupPrincipal
}
}

if role != "" {
like := "%:" + role
whereClauseExp["role_id"] = goqu.Op{"like": like}
}

query, params, err := dialect.Select(&relationCols{}).From(TABLE_RELATIONS).Where(whereClauseExp).ToSQL()
if err != nil {
return []relation.Relation{}, fmt.Errorf("%w: %s", queryErr, err)
}

var fetchedRelations []Relation
if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "ListGroupRelations", func(ctx context.Context) error {
return r.dbc.SelectContext(ctx, &fetchedRelations, query, params...)
}); err != nil {
// List should return empty list and no error instead
if errors.Is(err, sql.ErrNoRows) {
return []relation.Relation{}, nil
}
return []relation.Relation{}, fmt.Errorf("%w: %s", dbErr, err)
}

var transformedRelations []relation.Relation
for _, r := range fetchedRelations {
transformedRelations = append(transformedRelations, r.transformToRelationV2())
}

return transformedRelations, nil
}

func (r GroupRepository) SetState(ctx context.Context, id string, state group.State) error {
query, params, err := dialect.Update(TABLE_GROUPS).Set(
goqu.Record{
Expand Down
71 changes: 0 additions & 71 deletions internal/store/postgres/group_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,77 +534,6 @@ func (s *GroupRepositoryTestSuite) TestUpdateByID() {
}
}

func (s *GroupRepositoryTestSuite) TestListGroupRelations() {
type testCase struct {
Description string
ObjectID string
SubjectType string
Role string
ExpectedRelations []relation.Relation
ErrString string
}

var testCases = []testCase{
{
Description: "should get a list of relations",
ObjectID: s.groups[0].ID,
SubjectType: "user",
Role: "member",
ExpectedRelations: []relation.Relation{
{
Object: relation.Object{
ID: s.groups[0].ID,
Namespace: schema.GroupNamespace,
},
Subject: relation.Subject{
ID: s.users[0].ID,
Namespace: schema.UserPrincipal,
SubRelationName: "frontier/group:member",
},
},
{
Object: relation.Object{
ID: s.groups[0].ID,
Namespace: schema.GroupNamespace,
},
Subject: relation.Subject{
ID: s.users[1].ID,
Namespace: schema.UserPrincipal,
SubRelationName: "frontier/group:member",
},
},
},
},
}

for _, tc := range testCases {
s.Run(tc.Description, func() {
got, err := s.repository.ListGroupRelations(s.ctx, tc.ObjectID, tc.SubjectType, tc.Role)
if tc.ErrString != "" && err != nil {
if err.Error() != tc.ErrString {
s.T().Fatalf("got error %s, expected was %s", err.Error(), tc.ErrString)
}
}

for _, rel := range got {
found := false
for _, expectedRel := range tc.ExpectedRelations {
if cmp.Equal(rel, expectedRel, cmpopts.IgnoreFields(relation.Relation{},
"ID",
"CreatedAt",
"UpdatedAt")) {
found = true
break
}
}
if !found {
s.T().Fatalf("can't find relation %+v", rel)
}
}
})
}
}

func (s *GroupRepositoryTestSuite) TestSetState() {
type testCase struct {
Description string
Expand Down
Loading