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

feat: client query negation #3805

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ func (h *Handler) listOAuth2Clients(w http.ResponseWriter, r *http.Request, ps h
filters := Filter{
Limit: itemsPerPage,
Offset: page * itemsPerPage,
Name: r.URL.Query().Get("client_name"),
Owner: r.URL.Query().Get("owner"),
Name: field(r.URL.Query().Get("client_name")),
Owner: field(r.URL.Query().Get("owner")),
}

c, err := h.r.ClientManager().GetClients(r.Context(), filters)
Expand Down
18 changes: 16 additions & 2 deletions client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client

import (
"context"
"strings"

"github.com/ory/fosite"
)
Expand All @@ -21,11 +22,24 @@ type Filter struct {

// The name of the clients to filter by.
// in: query
Name string `json:"client_name"`
Name field `json:"client_name"`

// The owner of the clients to filter by.
// in: query
Owner string `json:"owner"`
Owner field `json:"owner"`
}

type field string

func (f field) Value() string {
if f.IsNegated() {
return string(f[2:])
}
return string(f)
}

func (f field) IsNegated() bool {
return strings.HasPrefix(string(f), "!=")
}

type Manager interface {
Expand Down
12 changes: 12 additions & 0 deletions client/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ func TestHelperCreateGetUpdateDeleteClient(k string, connection *pop.Connection,
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].Name, "name")

// get by name negated prefix
ds, err = t1.GetClients(ctx, Filter{Limit: 100, Offset: 0, Name: "!=name"})
assert.NoError(t, err)
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].GetID(), "2-1234")

// get by name not exist
ds, err = t1.GetClients(ctx, Filter{Limit: 100, Offset: 0, Name: "bad name"})
assert.NoError(t, err)
Expand All @@ -309,6 +315,12 @@ func TestHelperCreateGetUpdateDeleteClient(k string, connection *pop.Connection,
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].Owner, "aeneas")

// get by owner negated prefix
ds, err = t1.GetClients(ctx, Filter{Limit: 100, Offset: 0, Owner: "!=aeneas"})
assert.NoError(t, err)
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].GetID(), "2-1234")

testHelperUpdateClient(t, ctx, t1, k)
testHelperUpdateClient(t, ctx, t2, k)

Expand Down
4 changes: 2 additions & 2 deletions internal/httpclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ paths:
minimum: 1
type: string
style: form
- description: The name of the clients to filter by.
- description: The name of the clients to filter by. Prefix with "!=" to filter with negative matching.
explode: true
in: query
name: client_name
required: false
schema:
type: string
style: form
- description: The owner of the clients to filter by.
- description: The owner of the clients to filter by. Prefix with "!=" to filter with negative matching.
explode: true
in: query
name: owner
Expand Down
4 changes: 2 additions & 2 deletions internal/httpclient/docs/OAuth2API.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,8 @@ Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**pageSize** | **int64** | Items per Page This is the number of items per page to return. For details on pagination please head over to the [pagination documentation](https://www.ory.sh/docs/ecosystem/api-design#pagination). | [default to 250]
**pageToken** | **string** | Next Page Token The next page token. For details on pagination please head over to the [pagination documentation](https://www.ory.sh/docs/ecosystem/api-design#pagination). | [default to "1"]
**clientName** | **string** | The name of the clients to filter by. |
**owner** | **string** | The owner of the clients to filter by. |
**clientName** | **string** | The name of the clients to filter by. Prefix with "!=" to filter with negative matching. |
**owner** | **string** | The owner of the clients to filter by. Prefix with "!=" to filter with negative matching. |

### Return type

Expand Down
12 changes: 10 additions & 2 deletions persistence/sql/persister_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,18 @@ func (p *Persister) GetClients(ctx context.Context, filters client.Filter) (_ []
Order("id")

if filters.Name != "" {
query.Where("client_name = ?", filters.Name)
stmt := "client_name = ?"
if filters.Name.IsNegated() {
stmt = "client_name != ?"
}
query.Where(stmt, filters.Name.Value())
}
if filters.Owner != "" {
query.Where("owner = ?", filters.Owner)
stmt := "owner = ?"
if filters.Owner.IsNegated() {
stmt = "owner != ?"
}
query.Where(stmt, filters.Owner.Value())
}

if err := query.All(&cs); err != nil {
Expand Down
Loading