@@ -32,18 +32,24 @@ class KosyncDocument(BaseModel):
3232
3333@app .post ("/users/create" )
3434def register (kosync_user : KosyncUser ):
35- # check if username or password is missing
36- if kosync_user .username is None or kosync_user .password is None :
37- return JSONResponse (status_code = 400 , content = {"message" : f"Invalid request" })
38- # check if user already exists
39- QUser = Query ()
40- if users .contains (QUser .username == kosync_user .username ):
41- return JSONResponse (status_code = 409 , content = "Username is already registered." )
42- # register new user
43- if users .insert ({'username' : kosync_user .username , 'password' : kosync_user .password }):
44- return JSONResponse (status_code = 201 , content = {"username" : kosync_user .username })
45- # if something went wrong
46- return JSONResponse (status_code = 500 , content = "Unknown server error" )
35+ # Check whether new registrations are allowed on this server based on the OPEN_REGISTRATIONS environment variable.
36+ # By default registrations are enabled.
37+ registrations_allowed = bool (strtobool (getenv ("OPEN_REGISTRATIONS" , "True" )))
38+ if registrations_allowed :
39+ # check if username or password is missing
40+ if kosync_user .username is None or kosync_user .password is None :
41+ return JSONResponse (status_code = 400 , content = {"message" : f"Invalid request" })
42+ # check if user already exists
43+ QUser = Query ()
44+ if users .contains (QUser .username == kosync_user .username ):
45+ return JSONResponse (status_code = 409 , content = "Username is already registered." )
46+ # register new user
47+ if users .insert ({'username' : kosync_user .username , 'password' : kosync_user .password }):
48+ return JSONResponse (status_code = 201 , content = {"username" : kosync_user .username })
49+ # if something went wrong
50+ return JSONResponse (status_code = 500 , content = "Unknown server error" )
51+ else :
52+ return JSONResponse (status_code = 403 , content = "This server is currently not accepting new registrations." )
4753
4854
4955@app .get ("/users/auth" )
0 commit comments