Skip to content

Commit 4db0752

Browse files
committed
Me, review fixes: /me says which forges people can sign in with (an OAuth application with its ID and secret), as today's; staff who aren't in an organization can't change its billing email; the sign-ups heartbeat fails when the database can't answer
1 parent c867299 commit 4db0752

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎app/src/auth/me.rs‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,38 @@ pub fn summary(source: Option<&str>, detail: Option<&str>) -> Option<String> {
9898
}
9999

100100
/// `GET /api/ui/me`.
101-
pub async fn me(Authenticated(user): Authenticated<User>) -> laravel::Result<Json<Value>> {
101+
/// The forges a person can sign in with here: those whose OAuth application has its ID and secret.
102+
#[derive(Clone, laravel::config::Section)]
103+
pub struct SignInApps {
104+
#[env("GITHUB_CLIENT_ID")]
105+
pub github_id: Option<String>,
106+
#[env("GITHUB_CLIENT_SECRET")]
107+
pub github_secret: Option<String>,
108+
#[env("GITLAB_CLIENT_ID")]
109+
pub gitlab_id: Option<String>,
110+
#[env("GITLAB_CLIENT_SECRET")]
111+
pub gitlab_secret: Option<String>,
112+
}
113+
114+
impl SignInApps {
115+
#[must_use]
116+
pub fn forges(&self) -> Vec<&'static str> {
117+
let set = |v: &Option<String>| v.as_deref().is_some_and(|s| !s.trim().is_empty());
118+
let mut forges = Vec::new();
119+
if set(&self.github_id) && set(&self.github_secret) {
120+
forges.push("github");
121+
}
122+
if set(&self.gitlab_id) && set(&self.gitlab_secret) {
123+
forges.push("gitlab");
124+
}
125+
forges
126+
}
127+
}
128+
129+
pub async fn me(
130+
Authenticated(user): Authenticated<User>,
131+
apps: laravel::config::Section<SignInApps>,
132+
) -> laravel::Result<Json<Value>> {
102133
let user_id = user.id;
103134
let row = db::query!(
104135
"SELECT staff, heard_about_pending, email_low_credits, email_receipts, \
@@ -119,6 +150,7 @@ pub async fn me(Authenticated(user): Authenticated<User>) -> laravel::Result<Jso
119150
"name": user.name, "email": user.email, "avatar_url": user.avatar_url,
120151
"login": identities.get(0).and_then(|i| i.get("login")).cloned(), "identities": identities,
121152
},
153+
"forges": apps.forges(),
122154
"organizations": organizations,
123155
"invitations": invitations::pending_for(&user).await?,
124156
"staff": row.try_get::<bool>("staff")?,
@@ -218,7 +250,6 @@ pub async fn update_billing_email(
218250
match row.try_get::<Option<String>>("role")?.as_deref().and_then(Role::parse) {
219251
Some(Role::Admin) => {}
220252
Some(_) => return Err(laravel::abort_with(403, "only admins can change the billing email")),
221-
None if user.staff => {}
222253
None => return Err(laravel::abort(404)),
223254
}
224255
let email = p.billing_email.as_deref().unwrap_or("").trim();
@@ -354,16 +385,19 @@ impl NotifySignups {
354385
) -> laravel::Result<()> {
355386
let result = queue_signups(&team, &app.url).await;
356387
let stale = WAIT_MINUTES + STALE_MINUTES;
357-
let oldest: Option<i64> = db::query!(
388+
let waiting = db::query!(
358389
"SELECT count(*) FROM users WHERE signup_notified_at IS NULL \
359390
AND created_at < now() - make_interval(mins => {stale}::int)"
360391
)
361-
.scalar()
362-
.await
363-
.unwrap_or(None);
364-
let health = match (&result, oldest) {
392+
.scalar::<i64>()
393+
.await;
394+
// A database that can't answer fails the heartbeat too.
395+
let health = match (&result, waiting) {
365396
(Err(e), _) => Err(e.to_string()),
366-
(Ok(_), Some(n)) if n > 0 => Err(format!("{n} sign-ups waited over {stale} minutes for the team's email")),
397+
(Ok(_), Err(e)) => Err(e.to_string()),
398+
(Ok(_), Ok(Some(n))) if n > 0 => {
399+
Err(format!("{n} sign-ups waited over {stale} minutes for the team's email"))
400+
}
367401
_ => Ok(()),
368402
};
369403
crate::heartbeat::push("VISUALCI_SIGNUPS_HEARTBEAT", health).await;

‎app/tests/me.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async fn me_shows_their_organizations_and_email_choices(app: Application) -> lar
4040
.assert_json_path("organizations.0.slug", "acme")
4141
.assert_json_path("organizations.0.role", "admin")
4242
.assert_json_path("notifications.low_credits", true)
43-
.assert_json_path("heard_about_options.1.key", "ai_assistant");
43+
.assert_json_path("heard_about_options.1.key", "ai_assistant")
44+
.assert_json_path("forges", json!([]));
4445
app.get("/api/ui/me").await.assert_status(401);
4546

4647
app.patch("/api/ui/me/notifications")
@@ -73,6 +74,9 @@ async fn only_admins_set_the_billing_email(app: Application) -> laravel::Result<
7374
.json(json!({ "billing_email": email }))
7475
};
7576
put(&bob, "ap@example.com").await.assert_status(403);
77+
// Staff who aren't in it are nobody here.
78+
let sam = create!(User { pid: laravel::str::uuid7(), name: "Sam", email: "sam@example.com", staff: true }).await?;
79+
put(&sam, "ap@example.com").await.assert_status(404);
7680
put(&ada, "not an address").await.assert_status(422);
7781
put(&ada, " AP@Example.com ").await.assert_ok().assert_json_path("billing_email", "ap@example.com");
7882
put(&ada, "").await.assert_ok().assert_json_path("billing_email", json!(null));

0 commit comments

Comments
 (0)