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

Add entity's collaborator removal filter to SearchAccounts #6926

Merged
merged 3 commits into from
Feb 20, 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
1 change: 1 addition & 0 deletions api/ttn/lorawan/v3/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10157,6 +10157,7 @@ Right is the enum that defines all the different rights to do something in the n
| `client_ids` | [`ClientIdentifiers`](#ttn.lorawan.v3.ClientIdentifiers) | | |
| `gateway_ids` | [`GatewayIdentifiers`](#ttn.lorawan.v3.GatewayIdentifiers) | | |
| `organization_ids` | [`OrganizationIdentifiers`](#ttn.lorawan.v3.OrganizationIdentifiers) | | |
| `complement_collaborators` | [`bool`](#bool) | | |

#### Field Rules

Expand Down
30 changes: 30 additions & 0 deletions api/ttn/lorawan/v3/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,12 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "complement_collaborators",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
Expand Down Expand Up @@ -4242,6 +4248,12 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "complement_collaborators",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
Expand Down Expand Up @@ -7484,6 +7496,12 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "complement_collaborators",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
Expand Down Expand Up @@ -10582,6 +10600,12 @@
"required": false,
"type": "string",
"format": "string"
},
{
"name": "complement_collaborators",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
Expand Down Expand Up @@ -11869,6 +11893,12 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "complement_collaborators",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
Expand Down
2 changes: 2 additions & 0 deletions api/ttn/lorawan/v3/search_services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ message SearchAccountsRequest {
OrganizationIdentifiers organization_ids = 6;
}

bool complement_collaborators = 7;

// NOTE: This request intentionally does not support pagination.
}

Expand Down
40 changes: 33 additions & 7 deletions pkg/identityserver/bunstore/entity_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ func (s *entitySearch) SearchAccounts(
defer span.End()

var selectQuery *bun.SelectQuery
var count int
var err error

if entityID := req.GetEntityIdentifiers(); entityID != nil {
entityType, entityUUID, err := s.getEntity(ctx, entityID)
Expand All @@ -388,23 +390,47 @@ func (s *entitySearch) SearchAccounts(
if q := req.GetQuery(); q != "" {
selectQuery = selectQuery.Where(ilike("account_friendly_id"), q)
}

count, err = selectQuery.Count(ctx)
if err != nil {
return nil, storeutil.WrapDriverError(err)
}
if req.ComplementCollaborators {
accountQuery := s.newSelectModel(ctx, &Account{}).
Column("account_type").
ColumnExpr("uid AS account_friendly_id")
if req.OnlyUsers {
accountQuery = accountQuery.Where(`account_type = 'user'`)
}
if q := req.GetQuery(); q != "" {
accountQuery = accountQuery.Where(ilike("uid"), q)
}
// Count the total number of results.
c, err := accountQuery.Count(ctx)
if err != nil {
return nil, storeutil.WrapDriverError(err)
}

count = c - count
selectQuery = accountQuery.Except(selectQuery)
}
} else {
selectQuery = s.newSelectModel(ctx, &Account{}).
ColumnExpr("account_type, uid AS account_friendly_id").
Order("uid")
Column("account_type").
ColumnExpr("uid AS account_friendly_id")
if req.OnlyUsers {
selectQuery = selectQuery.Where(`account_type = 'user'`)
}
if q := req.GetQuery(); q != "" {
selectQuery = selectQuery.Where(ilike("uid"), q)
}
// Count the total number of results.
count, err = selectQuery.Count(ctx)
if err != nil {
return nil, storeutil.WrapDriverError(err)
}
}

// Count the total number of results.
count, err := selectQuery.Count(ctx)
if err != nil {
return nil, storeutil.WrapDriverError(err)
}
store.SetTotal(ctx, uint64(count))

// Apply paging.
Expand Down
32 changes: 32 additions & 0 deletions pkg/identityserver/storetest/entity_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,14 @@ func (st *StoreTest) TestEntitySearch(t *T) {
if a.So(err, should.BeNil) && a.So(ids, should.NotBeNil) && a.So(ids, should.HaveLength, 1) {
a.So(ids[0], should.Resemble, usr1.GetIds().GetOrganizationOrUserIdentifiers())
}

// Exclude app1 collaborator.
ids, err = s.SearchAccounts(ctx, &ttnpb.SearchAccountsRequest{
CollaboratorOf: &ttnpb.SearchAccountsRequest_ApplicationIds{ApplicationIds: app1.GetIds()},
ComplementCollaborators: true,
})
a.So(err, should.BeNil)
a.So(ids, should.HaveLength, 3)
})

t.Run("Client", func(t *T) {
Expand All @@ -571,6 +579,14 @@ func (st *StoreTest) TestEntitySearch(t *T) {
if a.So(err, should.BeNil) && a.So(ids, should.NotBeNil) && a.So(ids, should.HaveLength, 1) {
a.So(ids[0], should.Resemble, usr1.GetIds().GetOrganizationOrUserIdentifiers())
}

// Exclude cli1 collaborator.
ids, err = s.SearchAccounts(ctx, &ttnpb.SearchAccountsRequest{
CollaboratorOf: &ttnpb.SearchAccountsRequest_ClientIds{ClientIds: cli1.GetIds()},
ComplementCollaborators: true,
})
a.So(err, should.BeNil)
a.So(ids, should.HaveLength, 3)
})

t.Run("Gateway", func(t *T) {
Expand All @@ -582,6 +598,14 @@ func (st *StoreTest) TestEntitySearch(t *T) {
if a.So(err, should.BeNil) && a.So(ids, should.NotBeNil) && a.So(ids, should.HaveLength, 1) {
a.So(ids[0], should.Resemble, usr1.GetIds().GetOrganizationOrUserIdentifiers())
}

// Exclude gtw1 collaborator.
ids, err = s.SearchAccounts(ctx, &ttnpb.SearchAccountsRequest{
CollaboratorOf: &ttnpb.SearchAccountsRequest_GatewayIds{GatewayIds: gtw1.GetIds()},
ComplementCollaborators: true,
})
a.So(err, should.BeNil)
a.So(ids, should.HaveLength, 3)
})

t.Run("Organization", func(t *T) {
Expand All @@ -593,6 +617,14 @@ func (st *StoreTest) TestEntitySearch(t *T) {
if a.So(err, should.BeNil) && a.So(ids, should.NotBeNil) && a.So(ids, should.HaveLength, 1) {
a.So(ids[0], should.Resemble, usr1.GetIds().GetOrganizationOrUserIdentifiers())
}

// Exclude org1 collaborator.
ids, err = s.SearchAccounts(ctx, &ttnpb.SearchAccountsRequest{
CollaboratorOf: &ttnpb.SearchAccountsRequest_OrganizationIds{OrganizationIds: org1.GetIds()},
ComplementCollaborators: true,
})
a.So(err, should.BeNil)
a.So(ids, should.HaveLength, 3)
})
})
}
Expand Down
Loading
Loading