implement redirect to source on admin login HTML endpoint

This commit is contained in:
Aravinth Manivannan 2021-10-13 14:28:18 +05:30
parent 4491fbb6f2
commit a8c6912c25
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
11 changed files with 58 additions and 25 deletions

View file

@ -17,10 +17,10 @@
use actix_identity::Identity;
use actix_web::http::header;
use actix_web::{web, HttpResponse, Responder};
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use super::{RedirectQuery, get_random};
use super::{get_random, RedirectQuery};
use crate::errors::*;
use crate::AppData;

View file

@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use sqlx::types::time::OffsetDateTime;
use uuid::Uuid;
use super::{RedirectQuery, get_uuid};
use super::{get_uuid, RedirectQuery};
use crate::errors::*;
use crate::AppData;
@ -121,8 +121,6 @@ pub mod runners {
}
}
#[my_codegen::get(path = "crate::V1_API_ROUTES.benches.register")]
async fn register(
data: AppData,

View file

@ -51,6 +51,7 @@ lazy_static! {
pub static ref CSS: &'static str =
FILES.get("./static/cache/bundle/css/main.css").unwrap();
pub static ref JS: &'static str =
FILES.get("./static/cache/bundle/bundle.js").unwrap();

View file

@ -22,6 +22,7 @@ use my_codegen::{get, post};
use sailfish::TemplateOnce;
use crate::api::v1::admin::auth::runners;
use crate::api::v1::RedirectQuery;
use crate::errors::*;
use crate::pages::errors::ErrorPage;
use crate::AppData;
@ -65,14 +66,22 @@ pub async fn login_submit(
id: Identity,
payload: web::Form<runners::Login>,
data: AppData,
path: web::Path<RedirectQuery>,
) -> PageResult<impl Responder> {
let payload = payload.into_inner();
match runners::login_runner(&payload, &data).await {
Ok(username) => {
id.remember(username);
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, PAGES.home))
.finish())
let path = path.into_inner();
if let Some(redirect_to) = path.redirect_to {
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, redirect_to))
.finish())
} else {
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, PAGES.home))
.finish())
}
}
Err(e) => {
let status = e.status_code();

View file

@ -18,8 +18,6 @@ pub mod join;
pub mod login;
pub mod sudo;
pub use crate::api::v1::admin::get_admin_check_login;
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(login::login);
cfg.service(login::login_submit);
@ -28,10 +26,29 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
}
pub mod routes {
use crate::middleware::auth::GetLoginRoute;
use url::Url;
pub struct Auth {
pub login: &'static str,
pub join: &'static str,
}
impl GetLoginRoute for Auth {
fn get_login_route(&self, src: Option<&str>) -> String {
if let Some(redirect_to) = src {
let mut url = Url::parse("http://x/").unwrap();
url.set_path(self.login);
url.query_pairs_mut()
.append_pair("redirect_to", redirect_to);
let path = format!("{}/?{}", url.path(), url.query().unwrap());
path
} else {
self.login.to_string()
}
}
}
impl Auth {
pub const fn new() -> Auth {
Auth {

View file

@ -30,6 +30,10 @@ pub fn services(cfg: &mut ServiceConfig) {
errors::services(cfg);
}
pub fn get_page_check_login() -> crate::CheckLogin<auth::routes::Auth> {
crate::CheckLogin::new(crate::PAGES.auth)
}
#[cfg(not(tarpaulin_include))]
#[cfg(test)]
mod tests {

View file

@ -25,7 +25,6 @@ use my_codegen::{get, post};
use sailfish::TemplateOnce;
use uuid::Uuid;
use super::get_admin_check_login;
use crate::api::v1::admin::auth::runners::{login_runner, Login, Password};
use crate::api::v1::admin::campaigns::runners;
use crate::errors::*;
@ -60,7 +59,7 @@ async fn get_title(
#[get(
path = "PAGES.panel.campaigns.delete",
wrap = "get_admin_check_login()"
wrap = "crate::pages::get_page_check_login()"
)]
pub async fn delete_campaign(
id: Identity,
@ -86,7 +85,7 @@ pub async fn delete_campaign(
#[post(
path = "PAGES.panel.campaigns.delete",
wrap = "get_admin_check_login()"
wrap = "crate::pages::get_page_check_login()"
)]
pub async fn delete_campaign_submit(
id: Identity,

View file

@ -22,7 +22,6 @@
//use crate::api::v1::admin::campaigns::{runners, GetFeedbackResp};
//use crate::AppData;
//use crate::PAGES;
//use super::get_admin_check_login;
//
//#[derive(TemplateOnce)]
//#[template(path = "panel/campaigns/get/index.html")]
@ -41,7 +40,7 @@
//
//#[get(
// path = "PAGES.panel.campaigns.get_feedback",
// wrap = "get_admin_check_login()"
// wrap = "crate::pages::get_page_check_login()"
//)]
//pub async fn get_feedback(
// id: Identity,

View file

@ -24,8 +24,6 @@ use crate::api::v1::admin::campaigns::{
use crate::AppData;
use crate::PAGES;
use super::get_admin_check_login;
pub mod delete;
pub mod get;
pub mod new;
@ -85,7 +83,10 @@ impl HomePage {
const PAGE: &str = "Campaigns";
#[get(path = "PAGES.panel.campaigns.home", wrap = "get_admin_check_login()")]
#[get(
path = "PAGES.panel.campaigns.home",
wrap = "crate::pages::get_page_check_login()"
)]
pub async fn home(data: AppData, id: Identity) -> impl Responder {
let username = id.identity().unwrap();
let campaigns = list_campaign_runner(&username, &data).await.unwrap();

View file

@ -27,8 +27,6 @@ use crate::pages::errors::ErrorPage;
use crate::AppData;
use crate::PAGES;
use super::get_admin_check_login;
#[derive(Clone, TemplateOnce)]
#[template(path = "panel/campaigns/new/index.html")]
struct NewCampaign<'a> {
@ -55,14 +53,20 @@ lazy_static! {
static ref INDEX: String = NewCampaign::default().render_once().unwrap();
}
#[get(path = "PAGES.panel.campaigns.new", wrap = "get_admin_check_login()")]
#[get(
path = "PAGES.panel.campaigns.new",
wrap = "crate::pages::get_page_check_login()"
)]
pub async fn new_campaign() -> impl Responder {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(&*INDEX)
}
#[post(path = "PAGES.panel.campaigns.new", wrap = "get_admin_check_login()")]
#[post(
path = "PAGES.panel.campaigns.new",
wrap = "crate::pages::get_page_check_login()"
)]
pub async fn new_campaign_submit(
id: Identity,
payload: web::Json<AddCapmaign>,

View file

@ -16,8 +16,6 @@
use actix_web::{http, HttpResponse, Responder};
use my_codegen::get;
pub use crate::api::v1::admin::get_admin_check_login;
use crate::PAGES;
mod campaigns;
@ -49,7 +47,10 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
campaigns::services(cfg);
}
#[get(path = "PAGES.panel.home", wrap = "get_admin_check_login()")]
#[get(
path = "PAGES.panel.home",
wrap = "crate::pages::get_page_check_login()"
)]
pub async fn home() -> impl Responder {
HttpResponse::Found()
.insert_header((http::header::LOCATION, PAGES.panel.campaigns.home))