disable registrations by default

This commit is contained in:
Sebastian Hugentobler 2024-07-10 15:26:35 +02:00
parent 23ed80aa4b
commit d9388ae56d
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
4 changed files with 16 additions and 1 deletions

View File

@ -30,6 +30,7 @@ Usage: hesinde-sync [OPTIONS]
Options:
-a, --address <ADDRESS> Address to listen on [env: ADDRESS=] [default: localhost:3030]
-d, --db-connection <DB_CONNECTION> From which file to read the database connection string ("-" for stdin) [env: DB_CONNECTION=] [default: -]
-a, --allow-registering Whether new users can register [env: ALLOW_REGISTERING=]
-h, --help Print help
-V, --version Print version
```

View File

@ -78,6 +78,8 @@ pub struct Api;
impl Api {
/// Register a new user.
///
/// If registration is disabled return early.
///
/// If a user of that id already exist, return a conflict.
#[oai(path = "/users/create", method = "post")]
async fn register(
@ -85,7 +87,14 @@ impl Api {
req: Json<RegisterRequest>,
state: Data<&Arc<AppState>>,
) -> Result<payload::Response<payload::Json<UserCreated>>> {
let db = &state.0.db;
if !state.config.allow_registration {
return Err(Error::from_string(
"Registration is disabled.",
StatusCode::UNAUTHORIZED,
));
}
let db = &state.db;
if db.get_user(&req.username).await?.is_some() {
Err(Error::from_status(StatusCode::CONFLICT))
} else {

View File

@ -13,4 +13,8 @@ pub struct Config {
/// From which file to read the database connection string ("-" for stdin)
#[arg(short, long, env, default_value = "-")]
pub db_connection: String,
/// Whether new users can register.
#[arg(short = 'r', long, env, default_value = "false")]
pub allow_registration: bool,
}

View File

@ -15,6 +15,7 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let args = Config::parse();
println!("{}", args.allow_registration);
let db_url = read_db_url(&args.db_connection)?;
hesinde_sync::run(&args, &db_url).await