From bf02c70f74ea13452d9ee129ad047075e64cdfd6 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 12 Apr 2018 01:28:37 +0200 Subject: feat(handlers/main): Add 'anonymous' mode for forum Adds a `REQUIRE_LOGIN` environment variable which, if set to anything other than true, will let users post anonymously on the forums. --- src/handlers.rs | 23 +++++++++++++++++------ src/main.rs | 13 ++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/handlers.rs b/src/handlers.rs index 0848740bc10b..c97e677311a0 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -64,6 +64,15 @@ pub fn new_thread(state: State) -> ConverseResponse { .responder() } +/// This function provides an anonymous "default" author if logins are +/// not required. +fn anonymous() -> Author { + Author { + name: "Anonymous".into(), + email: "anonymous@nothing.org".into(), + } +} + #[derive(Deserialize)] pub struct NewThreadForm { pub title: String, @@ -90,9 +99,10 @@ pub fn submit_thread(state: State, .responder(); } - // Author is "unwrapped" because the RequireLogin middleware - // guarantees it to be present. - let author: Author = req.session().get(AUTHOR).unwrap().unwrap(); + let author: Author = req.session().get(AUTHOR) + .unwrap_or_else(|_| Some(anonymous())) + .unwrap_or_else(anonymous); + let new_thread = NewThread { title: input.0.title, body: input.0.body, @@ -123,9 +133,10 @@ pub struct NewPostForm { pub fn reply_thread(state: State, input: Form, mut req: HttpRequest) -> ConverseResponse { - // Author is "unwrapped" because the RequireLogin middleware - // guarantees it to be present. - let author: Author = req.session().get(AUTHOR).unwrap().unwrap(); + let author: Author = req.session().get(AUTHOR) + .unwrap_or_else(|_| Some(anonymous())) + .unwrap_or_else(anonymous); + let new_post = NewPost { thread_id: input.thread_id, body: input.0.body, diff --git a/src/main.rs b/src/main.rs index eeab96e83ced..8188d498712d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,6 +112,8 @@ fn main() { key_bytes }; + let require_login = config_default("REQUIRE_LOGIN", "true".into()) == "true"; + server::new(move || { let state = AppState { db: db_addr.clone(), @@ -123,17 +125,22 @@ fn main() { CookieSessionBackend::signed(&key) .secure(base_url.starts_with("https"))); - App::with_state(state) + let app = App::with_state(state) .middleware(Logger::default()) .middleware(sessions) - .middleware(RequireLogin) .resource("/", |r| r.method(Method::GET).with(forum_index)) .resource("/thread/new", |r| r.method(Method::GET).with(new_thread)) .resource("/thread/submit", |r| r.method(Method::POST).with3(submit_thread)) .resource("/thread/reply", |r| r.method(Method::POST).with3(reply_thread)) .resource("/thread/{id}", |r| r.method(Method::GET).with2(forum_thread)) .resource("/oidc/login", |r| r.method(Method::GET).with(login)) - .resource("/oidc/callback", |r| r.method(Method::POST).with3(callback))}) + .resource("/oidc/callback", |r| r.method(Method::POST).with3(callback)); + + if require_login { + app.middleware(RequireLogin) + } else { + app + }}) .bind(&bind_host).expect(&format!("Could not bind on '{}'", bind_host)) .start(); -- cgit 1.4.1