Skip to content

Commit a7ae36e

Browse files
committed
Allow filtering by subject in the upstream OAuth links admin API
1 parent 0e3d10e commit a7ae36e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

crates/handlers/src/admin/v1/upstream_oauth_links/list.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ pub struct FilterParams {
4040
#[serde(rename = "filter[provider]")]
4141
#[schemars(with = "Option<crate::admin::schema::Ulid>")]
4242
provider: Option<Ulid>,
43+
44+
/// Retrieve the items with the given subject
45+
#[serde(rename = "filter[subject]")]
46+
subject: Option<String>,
4347
}
4448

4549
impl std::fmt::Display for FilterParams {
@@ -56,6 +60,11 @@ impl std::fmt::Display for FilterParams {
5660
sep = '&';
5761
}
5862

63+
if let Some(subject) = &self.subject {
64+
write!(f, "{sep}filter[subject]={subject}")?;
65+
sep = '&';
66+
}
67+
5968
let _ = sep;
6069
Ok(())
6170
}
@@ -166,6 +175,12 @@ pub async fn handler(
166175
filter
167176
};
168177

178+
let filter = if let Some(subject) = &params.subject {
179+
filter.for_subject(subject)
180+
} else {
181+
filter
182+
};
183+
169184
let page = repo.upstream_oauth_link().list(filter, pagination).await?;
170185
let count = repo.upstream_oauth_link().count(filter).await?;
171186

@@ -443,5 +458,45 @@ mod tests {
443458
}
444459
}
445460
"###);
461+
462+
// Filter by subject
463+
let request = Request::get(format!(
464+
"/api/admin/v1/upstream-oauth-links?filter[subject]={}",
465+
"subject1"
466+
))
467+
.bearer(&token)
468+
.empty();
469+
470+
let response = state.request(request).await;
471+
response.assert_status(StatusCode::OK);
472+
let body: serde_json::Value = response.json();
473+
assert_json_snapshot!(body, @r###"
474+
{
475+
"meta": {
476+
"count": 1
477+
},
478+
"data": [
479+
{
480+
"type": "upstream-oauth-link",
481+
"id": "01FSHN9AG0AQZQP8DX40GD59PW",
482+
"attributes": {
483+
"created_at": "2022-01-16T14:40:00Z",
484+
"provider_id": "01FSHN9AG09NMZYX8MFYH578R9",
485+
"subject": "subject1",
486+
"user_id": "01FSHN9AG0MZAA6S4AF7CTV32E",
487+
"human_account_name": "alice@acme"
488+
},
489+
"links": {
490+
"self": "/api/admin/v1/upstream-oauth-links/01FSHN9AG0AQZQP8DX40GD59PW"
491+
}
492+
}
493+
],
494+
"links": {
495+
"self": "/api/admin/v1/upstream-oauth-links?filter[subject]=subject1&page[first]=10",
496+
"first": "/api/admin/v1/upstream-oauth-links?filter[subject]=subject1&page[first]=10",
497+
"last": "/api/admin/v1/upstream-oauth-links?filter[subject]=subject1&page[last]=10"
498+
}
499+
}
500+
"###);
446501
}
447502
}

docs/api/spec.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,17 @@
18371837
"nullable": true
18381838
},
18391839
"style": "form"
1840+
},
1841+
{
1842+
"in": "query",
1843+
"name": "filter[subject]",
1844+
"description": "Retrieve the items with the given subject",
1845+
"schema": {
1846+
"description": "Retrieve the items with the given subject",
1847+
"type": "string",
1848+
"nullable": true
1849+
},
1850+
"style": "form"
18401851
}
18411852
],
18421853
"responses": {
@@ -2931,6 +2942,11 @@
29312942
"description": "Retrieve the items for the given provider",
29322943
"$ref": "#/components/schemas/ULID",
29332944
"nullable": true
2945+
},
2946+
"filter[subject]": {
2947+
"description": "Retrieve the items with the given subject",
2948+
"type": "string",
2949+
"nullable": true
29342950
}
29352951
}
29362952
},

0 commit comments

Comments
 (0)