Merge pull request #45 from fdb-hiroshima/master

Add fallback to url requiring to be logged in
This commit is contained in:
Baptiste Gelez 2018-06-05 17:35:28 +01:00 committed by GitHub
commit b88ee53188
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 77 additions and 15 deletions

View File

@ -71,9 +71,11 @@ fn main() {
routes::blogs::activity_details, routes::blogs::activity_details,
routes::blogs::outbox, routes::blogs::outbox,
routes::blogs::new, routes::blogs::new,
routes::blogs::new_auth,
routes::blogs::create, routes::blogs::create,
routes::comments::new, routes::comments::new,
routes::comments::new_auth,
routes::comments::create, routes::comments::create,
routes::instance::index, routes::instance::index,
@ -82,8 +84,10 @@ fn main() {
routes::instance::shared_inbox, routes::instance::shared_inbox,
routes::likes::create, routes::likes::create,
routes::likes::create_auth,
routes::notifications::notifications, routes::notifications::notifications,
routes::notifications::notifications_auth,
routes::posts::details, routes::posts::details,
routes::posts::activity_details, routes::posts::activity_details,
@ -92,8 +96,10 @@ fn main() {
routes::posts::create, routes::posts::create,
routes::reshares::create, routes::reshares::create,
routes::reshares::create_auth,
routes::session::new, routes::session::new,
routes::session::new_message,
routes::session::create, routes::session::create,
routes::session::delete, routes::session::delete,

View File

@ -1,7 +1,7 @@
use activitystreams_types::collection::OrderedCollection; use activitystreams_types::collection::OrderedCollection;
use rocket::{ use rocket::{
request::Form, request::Form,
response::Redirect response::{Redirect, Flash}
}; };
use rocket_contrib::Template; use rocket_contrib::Template;
use serde_json; use serde_json;
@ -53,6 +53,11 @@ fn new(user: User) -> Template {
})) }))
} }
#[get("/blogs/new", rank = 2)]
fn new_auth() -> Flash<Redirect>{
utils::requires_login("You need to be logged in order to create a new blog", "/blogs/new")
}
#[derive(FromForm)] #[derive(FromForm)]
struct NewBlogForm { struct NewBlogForm {
pub title: String pub title: String

View File

@ -1,4 +1,7 @@
use rocket::{ request::Form, response::Redirect}; use rocket::{
request::Form,
response::{Redirect, Flash}
};
use rocket_contrib::Template; use rocket_contrib::Template;
use activity_pub::broadcast; use activity_pub::broadcast;
@ -9,6 +12,8 @@ use models::{
users::User users::User
}; };
use utils;
#[get("/~/<_blog>/<slug>/comment")] #[get("/~/<_blog>/<slug>/comment")]
fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template { fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template {
let post = Post::find_by_slug(&*conn, slug).unwrap(); let post = Post::find_by_slug(&*conn, slug).unwrap();
@ -18,6 +23,11 @@ fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template {
})) }))
} }
#[get("/~/<blog>/<slug>/comment", rank=2)]
fn new_auth(blog: String, slug: String) -> Flash<Redirect>{
utils::requires_login("You need to be logged in order to post a comment", &format!("~/{}/{}/comment", blog, slug))
}
#[derive(FromForm)] #[derive(FromForm)]
struct CommentQuery { struct CommentQuery {
responding_to: Option<i32> responding_to: Option<i32>

View File

@ -1,4 +1,4 @@
use rocket::response::Redirect; use rocket::response::{Redirect, Flash};
use activity_pub::broadcast; use activity_pub::broadcast;
use db_conn::DbConn; use db_conn::DbConn;
@ -8,6 +8,8 @@ use models::{
users::User users::User
}; };
use utils;
#[get("/~/<blog>/<slug>/like")] #[get("/~/<blog>/<slug>/like")]
fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
let post = Post::find_by_slug(&*conn, slug.clone()).unwrap(); let post = Post::find_by_slug(&*conn, slug.clone()).unwrap();
@ -29,3 +31,8 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref()) Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref())
} }
#[get("/~/<blog>/<slug>/like", rank = 2)]
fn create_auth(blog: String, slug: String) -> Flash<Redirect>{
utils::requires_login("You need to be logged in order to like a post", &format!("/~/{}/{}/like", blog, slug))
}

View File

@ -1,8 +1,11 @@
use rocket::response::{Redirect, Flash};
use rocket_contrib::Template; use rocket_contrib::Template;
use db_conn::DbConn; use db_conn::DbConn;
use models::{notifications::Notification, users::User}; use models::{notifications::Notification, users::User};
use utils;
#[get("/notifications")] #[get("/notifications")]
fn notifications(conn: DbConn, user: User) -> Template { fn notifications(conn: DbConn, user: User) -> Template {
Template::render("notifications/index", json!({ Template::render("notifications/index", json!({
@ -10,3 +13,8 @@ fn notifications(conn: DbConn, user: User) -> Template {
"notifications": Notification::find_for_user(&*conn, &user) "notifications": Notification::find_for_user(&*conn, &user)
})) }))
} }
#[get("/notifications", rank = 2)]
fn notifications_auth() -> Flash<Redirect>{
utils::requires_login("You need to be logged in order to see your notifications", "/notifications")
}

View File

@ -1,7 +1,7 @@
use comrak::{markdown_to_html, ComrakOptions}; use comrak::{markdown_to_html, ComrakOptions};
use heck::KebabCase; use heck::KebabCase;
use rocket::request::Form; use rocket::request::Form;
use rocket::response::Redirect; use rocket::response::{Redirect, Flash};
use rocket_contrib::Template; use rocket_contrib::Template;
use serde_json; use serde_json;
@ -57,9 +57,9 @@ fn activity_details(_blog: String, slug: String, conn: DbConn) -> ActivityPub {
activity_pub(act) activity_pub(act)
} }
#[get("/~/<_blog>/new", rank = 2)] #[get("/~/<blog>/new", rank = 2)]
fn new_auth(_blog: String) -> Redirect { fn new_auth(blog: String) -> Flash<Redirect> {
utils::requires_login() utils::requires_login("You need to be logged in order to write a new post", &format!("/~/{}/new",blog))
} }
#[get("/~/<_blog>/new", rank = 1)] #[get("/~/<_blog>/new", rank = 1)]

View File

@ -1,4 +1,4 @@
use rocket::response::Redirect; use rocket::response::{Redirect, Flash};
use activity_pub::broadcast; use activity_pub::broadcast;
use db_conn::DbConn; use db_conn::DbConn;
@ -8,6 +8,8 @@ use models::{
users::User users::User
}; };
use utils;
#[get("/~/<blog>/<slug>/reshare")] #[get("/~/<blog>/<slug>/reshare")]
fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
let post = Post::find_by_slug(&*conn, slug.clone()).unwrap(); let post = Post::find_by_slug(&*conn, slug.clone()).unwrap();
@ -29,3 +31,8 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref()) Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref())
} }
#[get("/~/<blog>/<slug>/reshare", rank=1)]
fn create_auth(blog: String, slug: String) -> Flash<Redirect> {
utils::requires_login("You need to be logged in order to reshare a post", &format!("/~/{}/{}/reshare",blog, slug))
}

View File

@ -1,7 +1,7 @@
use rocket::{ use rocket::{
http::{Cookie, Cookies}, http::{Cookie, Cookies},
response::{Redirect, status::NotFound}, response::{Redirect, status::NotFound},
request::Form request::{Form,FlashMessage}
}; };
use rocket_contrib::Template; use rocket_contrib::Template;
@ -15,6 +15,20 @@ fn new(user: Option<User>) -> Template {
})) }))
} }
#[derive(FromForm)]
struct Message {
m: String
}
#[get("/login?<message>")]
fn new_message(user: Option<User>, message: Message) -> Template {
Template::render("session/login", json!({
"account": user,
"message": message.m
}))
}
#[derive(FromForm)] #[derive(FromForm)]
struct LoginForm { struct LoginForm {
email_or_name: String, email_or_name: String,
@ -22,7 +36,7 @@ struct LoginForm {
} }
#[post("/login", data = "<data>")] #[post("/login", data = "<data>")]
fn create(conn: DbConn, data: Form<LoginForm>, mut cookies: Cookies) -> Result<Redirect, NotFound<String>> { fn create(conn: DbConn, data: Form<LoginForm>, flash: Option<FlashMessage>, mut cookies: Cookies) -> Result<Redirect, NotFound<String>> {
let form = data.get(); let form = data.get();
let user = match User::find_by_email(&*conn, form.email_or_name.to_string()) { let user = match User::find_by_email(&*conn, form.email_or_name.to_string()) {
Some(usr) => Ok(usr), Some(usr) => Ok(usr),
@ -31,12 +45,14 @@ fn create(conn: DbConn, data: Form<LoginForm>, mut cookies: Cookies) -> Result<R
None => Err("Invalid username or password") None => Err("Invalid username or password")
} }
}; };
match user { match user {
Ok(usr) => { Ok(usr) => {
if usr.auth(form.password.to_string()) { if usr.auth(form.password.to_string()) {
cookies.add_private(Cookie::new(AUTH_COOKIE, usr.id.to_string())); cookies.add_private(Cookie::new(AUTH_COOKIE, usr.id.to_string()));
Ok(Redirect::to("/")) Ok(Redirect::to(&flash
.and_then(|f| if f.name()=="callback" { Some(f.msg().to_owned()) } else { None })
.unwrap_or("/".to_owned()))
)
} else { } else {
Err(NotFound(String::from("Invalid username or password"))) Err(NotFound(String::from("Invalid username or password")))
} }

View File

@ -1,5 +1,5 @@
use heck::CamelCase; use heck::CamelCase;
use rocket::response::Redirect; use rocket::response::{Redirect, Flash};
/// Remove non alphanumeric characters and CamelCase a string /// Remove non alphanumeric characters and CamelCase a string
pub fn make_actor_id(name: String) -> String { pub fn make_actor_id(name: String) -> String {
@ -11,6 +11,6 @@ pub fn make_actor_id(name: String) -> String {
.collect() .collect()
} }
pub fn requires_login() -> Redirect { pub fn requires_login(message: &str, url: &str) -> Flash<Redirect> {
Redirect::to("/login") Flash::new(Redirect::to(&format!("/login?m={}", message)), "callback", url)
} }

View File

@ -6,6 +6,9 @@ Login
{% block content %} {% block content %}
<h1>Login</h1> <h1>Login</h1>
{% if message %}
<p>{{ message | escape }}</p>
{% endif %}
<form method="post"> <form method="post">
<label for="email_or_name">Username or email</label> <label for="email_or_name">Username or email</label>
<input name="email_or_name"> <input name="email_or_name">