Promote/demote CLI for migrated users
Browse files at this commitbfb4aaf8d796573d454c947512da12f4c6a23f33Parent: f675a3d- feat
- promote/demote CLI for migrated users
- invites refuse existing accounts
3 changed files +108 -11
- crates/im-core/src/accounts.rs +47 -4
- crates/im-web/src/admin.rs +7 -3
- crates/im-web/src/main.rs +54 -4
crates/im-core/src/accounts.rsmodified+47 -4
| @@ -228,6 +228,12 @@ pub async fn create_invite( | |||
| 228 | invited_by: Option<UserId>, | 228 | invited_by: Option<UserId>, |
| 229 | admin: bool, | 229 | admin: bool, |
| 230 | ) -> std::result::Result<Token, AccountError> { | 230 | ) -> std::result::Result<Token, AccountError> { |
| 231 | + // Inviting an address that already has an account is refused here, not | ||
| 232 | + // at acceptance time — the admin learns immediately, and the account is | ||
| 233 | + // never shadowed by a pending invite. | ||
| 234 | + if user_by_email(store, email).await?.is_some() { | ||
| 235 | + return Err(AccountError::EmailTaken); | ||
| 236 | + } | ||
| 231 | let token = Token::mint(); | 237 | let token = Token::mint(); |
| 232 | let now = store::now(); | 238 | let now = store::now(); |
| 233 | let expires = now + time::Duration::days(INVITE_DAYS); | 239 | let expires = now + time::Duration::days(INVITE_DAYS); |
| @@ -479,6 +485,20 @@ pub async fn set_disabled(store: &Store, user: &UserId, disabled: bool) -> Resul | |||
| 479 | Ok(()) | 485 | Ok(()) |
| 480 | } | 486 | } |
| 481 | 487 | ||
| 488 | +/// Grants or revokes im's admin flag. Never crosses a token — this is im's | ||
| 489 | +/// own panel, not a claim apps ever see. | ||
| 490 | +pub async fn set_admin(store: &Store, user: &UserId, admin: bool) -> Result<()> { | ||
| 491 | + store | ||
| 492 | + .conn | ||
| 493 | + .execute( | ||
| 494 | + "UPDATE users SET admin = ?1 WHERE id = ?2", | ||
| 495 | + turso::params![admin as i64, user.to_string()], | ||
| 496 | + ) | ||
| 497 | + .await | ||
| 498 | + .map_err(backend)?; | ||
| 499 | + Ok(()) | ||
| 500 | +} | ||
| 501 | + | ||
| 482 | #[cfg(test)] | 502 | #[cfg(test)] |
| 483 | mod tests { | 503 | mod tests { |
| 484 | use super::*; | 504 | use super::*; |
| @@ -488,6 +508,30 @@ mod tests { | |||
| 488 | } | 508 | } |
| 489 | 509 | ||
| 490 | use std::path::Path; | 510 | use std::path::Path; |
| 511 | + #[tokio::test] | ||
| 512 | + async fn invite_refuses_existing_account_and_admin_toggles() { | ||
| 513 | + let store = fixture().await; | ||
| 514 | + let invite = create_invite(&store, "ann@example.com", None, false) | ||
| 515 | + .await | ||
| 516 | + .unwrap(); | ||
| 517 | + let user = create_user_from_invite(&store, invite.expose(), "Ann", "tDLr9!mZQ2xv") | ||
| 518 | + .await | ||
| 519 | + .unwrap(); | ||
| 520 | + assert!(!user.admin); | ||
| 521 | + | ||
| 522 | + // Inviting an address that already has an account is refused at | ||
| 523 | + // creation, not at acceptance. | ||
| 524 | + assert!(matches!( | ||
| 525 | + create_invite(&store, "ann@example.com", None, false).await, | ||
| 526 | + Err(AccountError::EmailTaken) | ||
| 527 | + )); | ||
| 528 | + | ||
| 529 | + set_admin(&store, &user.id, true).await.unwrap(); | ||
| 530 | + let promoted = user_by_id(&store, &user.id).await.unwrap().unwrap(); | ||
| 531 | + assert!(promoted.admin); | ||
| 532 | + set_admin(&store, &user.id, false).await.unwrap(); | ||
| 533 | + assert!(!user_by_id(&store, &user.id).await.unwrap().unwrap().admin); | ||
| 534 | + } | ||
| 491 | 535 | ||
| 492 | #[test] | 536 | #[test] |
| 493 | fn password_roundtrip() { | 537 | fn password_roundtrip() { |
| @@ -563,11 +607,10 @@ mod tests { | |||
| 563 | create_user_from_invite(&store, first.expose(), "Ann", "tDLr9!mZQ2xv") | 607 | create_user_from_invite(&store, first.expose(), "Ann", "tDLr9!mZQ2xv") |
| 564 | .await | 608 | .await |
| 565 | .unwrap(); | 609 | .unwrap(); |
| 566 | - let second = create_invite(&store, "ann@example.com", None, false) | 610 | + // The second invite never exists: the refusal moved from acceptance |
| 567 | - .await | 611 | + // time to creation time. |
| 568 | - .unwrap(); | ||
| 569 | assert!(matches!( | 612 | assert!(matches!( |
| 570 | - create_user_from_invite(&store, second.expose(), "Ann Two", "tDLr9!mZQ2xv").await, | 613 | + create_invite(&store, "ann@example.com", None, false).await, |
| 571 | Err(AccountError::EmailTaken) | 614 | Err(AccountError::EmailTaken) |
| 572 | )); | 615 | )); |
| 573 | } | 616 | } |
crates/im-web/src/admin.rsmodified+7 -3
| @@ -275,9 +275,13 @@ async fn invite(cx: &Cx, Form(input): Form<InviteForm>) -> Result<Response> { | |||
| 275 | let store = &app(cx).store; | 275 | let store = &app(cx).store; |
| 276 | let email = input.email.trim().to_string(); | 276 | let email = input.email.trim().to_string(); |
| 277 | let admin = input.admin.as_deref() == Some("yes"); | 277 | let admin = input.admin.as_deref() == Some("yes"); |
| 278 | - let token = accounts::create_invite(store, &email, Some(me.id.clone()), admin) | 278 | + let token = match accounts::create_invite(store, &email, Some(me.id.clone()), admin).await { |
| 279 | - .await | 279 | + Ok(token) => token, |
| 280 | - .map_err(|e| topcoat::Error::from(std::io::Error::other(e.to_string())))?; | 280 | + Err(accounts::AccountError::EmailTaken) => { |
| 281 | + return back(cx, "users", "&error=email_taken"); | ||
| 282 | + } | ||
| 283 | + Err(e) => return Err(topcoat::Error::from(std::io::Error::other(e.to_string()))), | ||
| 284 | + }; | ||
| 281 | // Mailed when a sender is configured; shown once on the page otherwise. | 285 | // Mailed when a sender is configured; shown once on the page otherwise. |
| 282 | let mailed = mailer::send_invite(store, &app(cx).config.issuer, &email, token.expose()) | 286 | let mailed = mailer::send_invite(store, &app(cx).config.issuer, &email, token.expose()) |
| 283 | .await | 287 | .await |
crates/im-web/src/main.rsmodified+54 -4
| @@ -77,15 +77,58 @@ async fn main() { | |||
| 77 | } | 77 | } |
| 78 | create_client(config, name, uris).await; | 78 | create_client(config, name, uris).await; |
| 79 | } | 79 | } |
| 80 | + Some("promote") => { | ||
| 81 | + let Some(email) = args.get(2) else { | ||
| 82 | + eprintln!("usage: im-web promote <email>"); | ||
| 83 | + std::process::exit(2); | ||
| 84 | + }; | ||
| 85 | + set_admin_cli(config, email, true).await; | ||
| 86 | + } | ||
| 87 | + Some("demote") => { | ||
| 88 | + let Some(email) = args.get(2) else { | ||
| 89 | + eprintln!("usage: im-web demote <email>"); | ||
| 90 | + std::process::exit(2); | ||
| 91 | + }; | ||
| 92 | + set_admin_cli(config, email, false).await; | ||
| 93 | + } | ||
| 80 | Some(other) => { | 94 | Some(other) => { |
| 81 | eprintln!( | 95 | eprintln!( |
| 82 | - "im: unknown command {other:?} (expected: invite, revoke, create-client, or none to serve)" | 96 | + "im: unknown command {other:?} (expected: invite, revoke, promote, demote, create-client, or none to serve)" |
| 83 | ); | 97 | ); |
| 84 | std::process::exit(2); | 98 | std::process::exit(2); |
| 85 | } | 99 | } |
| 86 | } | 100 | } |
| 87 | } | 101 | } |
| 88 | 102 | ||
| 103 | +/// `im-web promote|demote <email>`: the admin flag for an account that | ||
| 104 | +/// already exists — the migration path's way in, since invites refuse | ||
| 105 | +/// addresses with accounts. | ||
| 106 | +async fn set_admin_cli(config: Config, email: &str, admin: bool) { | ||
| 107 | + let store = open_store(&config).await; | ||
| 108 | + let Some(user) = im_core::accounts::user_by_email(&store, email) | ||
| 109 | + .await | ||
| 110 | + .expect("failed to read users") | ||
| 111 | + else { | ||
| 112 | + eprintln!("im: no account for {email}"); | ||
| 113 | + std::process::exit(1); | ||
| 114 | + }; | ||
| 115 | + im_core::accounts::set_admin(&store, &user.id, admin) | ||
| 116 | + .await | ||
| 117 | + .expect("failed to update"); | ||
| 118 | + im_core::events::log( | ||
| 119 | + &store, | ||
| 120 | + if admin { | ||
| 121 | + "user_promoted" | ||
| 122 | + } else { | ||
| 123 | + "user_demoted" | ||
| 124 | + }, | ||
| 125 | + Some("cli"), | ||
| 126 | + Some(email), | ||
| 127 | + ) | ||
| 128 | + .await; | ||
| 129 | + println!("im {email}: admin = {admin}"); | ||
| 130 | +} | ||
| 131 | + | ||
| 89 | async fn open_store(config: &Config) -> Arc<Store> { | 132 | async fn open_store(config: &Config) -> Arc<Store> { |
| 90 | Arc::new( | 133 | Arc::new( |
| 91 | Store::open(&config.database) | 134 | Store::open(&config.database) |
| @@ -101,9 +144,16 @@ async fn open_store(config: &Config) -> Arc<Store> { | |||
| 101 | /// process would hold the same file. | 144 | /// process would hold the same file. |
| 102 | async fn invite(config: Config, email: &str, admin: bool) { | 145 | async fn invite(config: Config, email: &str, admin: bool) { |
| 103 | let store = open_store(&config).await; | 146 | let store = open_store(&config).await; |
| 104 | - let token = im_core::accounts::create_invite(&store, email, None, admin) | 147 | + let token = match im_core::accounts::create_invite(&store, email, None, admin).await { |
| 105 | - .await | 148 | + Ok(token) => token, |
| 106 | - .expect("failed to create the invite"); | 149 | + Err(im_core::accounts::AccountError::EmailTaken) => { |
| 150 | + eprintln!( | ||
| 151 | + "im: {email} already has an account — `im-web promote {email}` if you mean admin" | ||
| 152 | + ); | ||
| 153 | + std::process::exit(1); | ||
| 154 | + } | ||
| 155 | + Err(e) => panic!("failed to create the invite: {e}"), | ||
| 156 | + }; | ||
| 107 | im_core::events::log( | 157 | im_core::events::log( |
| 108 | &store, | 158 | &store, |
| 109 | "invite_created", | 159 | "invite_created", |