admin forum list that can only be viewed when logged in

This commit is contained in:
Vivian Lim 2018-10-21 01:07:51 -07:00
parent 5041168f24
commit 9a784af4f2
2 changed files with 35 additions and 1 deletions

25
src/admin_forums.rs Normal file
View File

@ -0,0 +1,25 @@
//use models::User;
use session::{Session};
use warp::http::{Response};
use warp::{reject, Reply, Rejection};
use render_ructe::RenderRucte;
use templates;
fn check_permissions(session: &Session) -> bool {
match session.user() {
None => false,
Some(_user) => true // todo: check user permission level
}
}
pub fn admin_forums_form(session: Session) -> Result<impl Reply, Rejection> {
if !check_permissions(&session){
return Err(reject::server_error());
}
use forum::get_forums;
match get_forums(&session){
Ok(forums) => Response::builder().html(|o| templates::forum_list(o, &session, &forums, None, None)),
Err(err) => Err(reject::server_error().with(err))
}
}

View File

@ -22,12 +22,14 @@ mod render_ructe;
mod schema;
mod session;
mod forum;
mod admin_forums;
use diesel::insert_into;
use diesel::prelude::*;
use dotenv::dotenv;
use render_ructe::RenderRucte;
use session::{create_session_filter, Session};
use admin_forums::{admin_forums_form};
use std::env;
use std::io::{self, Write};
use std::time::{Duration, SystemTime};
@ -51,8 +53,15 @@ fn main() {
.and(path("static"))
.and(path::param())
.and_then(static_file);
let admin_routes = get()
.and(s()
.and(path("admin"))
.and(path("forums"))
.and_then(admin_forums_form))
.boxed();
let routes = warp::any()
.and(static_routes)
.or(admin_routes)
.or(get().and(
(s().and(index()).and_then(home_page))
.or(s().and(path("forums"))
@ -62,7 +71,7 @@ fn main() {
.or(s()
.and(path("signup"))
.and(index())
.and_then(signup_form)),
.and_then(signup_form))
)).or(post().and(
(s().and(path("login")).and(body::form()).and_then(do_login))
.or(s().and(path("logout")).and_then(do_logout))