diff --git a/client/handler.go b/client/handler.go index a3bcdc90985..5b023787f9d 100644 --- a/client/handler.go +++ b/client/handler.go @@ -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) diff --git a/client/manager.go b/client/manager.go index b4c54cb3b2a..8879fad3fa5 100644 --- a/client/manager.go +++ b/client/manager.go @@ -5,6 +5,7 @@ package client import ( "context" + "strings" "github.com/ory/fosite" ) @@ -21,11 +22,27 @@ 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:]) + } + if strings.HasPrefix(string(f), "=") { + return string(f[1:]) + } + return string(f) +} + +func (f field) IsNegated() bool { + return strings.HasPrefix(string(f), "!=") } type Manager interface { diff --git a/persistence/sql/persister_client.go b/persistence/sql/persister_client.go index c85893c1df8..e79d22de0f5 100644 --- a/persistence/sql/persister_client.go +++ b/persistence/sql/persister_client.go @@ -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.Name.IsNegated() { + stmt = "owner != ?" + } + query.Where(stmt, filters.Owner.Value()) } if err := query.All(&cs); err != nil {