|
|
@@ -1,16 +1,30 @@
|
|
|
use actix_session::Session;
|
|
|
-use actix_web::{ get, http::{header, StatusCode}, post, web, HttpResponse, Responder};
|
|
|
+use actix_web::{ body, get, http::{header, StatusCode}, post, web, HttpResponse, Responder};
|
|
|
use ldap::LdapWrapper;
|
|
|
-use serde::Deserialize;
|
|
|
+use serde::{ser::Impossible, Deserialize};
|
|
|
use tera::Tera;
|
|
|
-use crate::users::controller;
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
-struct FormLoginData {
|
|
|
+pub struct FormLoginData {
|
|
|
id: String,
|
|
|
password: String,
|
|
|
}
|
|
|
|
|
|
+#[derive(Deserialize)]
|
|
|
+struct FormChangePasswd {
|
|
|
+ current_password: String,
|
|
|
+ new_password: String,
|
|
|
+ new_password_conf: String,
|
|
|
+}
|
|
|
+
|
|
|
+async fn get_template(template_name: String) -> String {
|
|
|
+ let tera = Tera::new("templates/*.html")
|
|
|
+ .expect("Failed to parse template files");
|
|
|
+ let ctx = tera::Context::new();
|
|
|
+ tera.render(&template_name, &ctx)
|
|
|
+ .expect(format!("Faile to render template {}", template_name).as_str())
|
|
|
+}
|
|
|
+
|
|
|
fn validate_session(session: &Session) -> bool {
|
|
|
let user_id: Option<String> = session
|
|
|
.get("user_id")
|
|
|
@@ -25,16 +39,7 @@ fn validate_session(session: &Session) -> bool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-async fn get_template(template_name: String) -> String {
|
|
|
- let tera = Tera::new("templates/*.html")
|
|
|
- .expect("Failed to parse template files");
|
|
|
- let ctx = tera::Context::new();
|
|
|
- tera.render(&template_name, &ctx)
|
|
|
- .expect(format!("Faile to render template {}", template_name).as_str())
|
|
|
-}
|
|
|
-
|
|
|
-#[get("/")]
|
|
|
-async fn index(session: Session) -> impl Responder {
|
|
|
+pub async fn index(session: Session) -> impl Responder {
|
|
|
if validate_session(&session) {
|
|
|
return HttpResponse::Ok()
|
|
|
.status(StatusCode::FOUND)
|
|
|
@@ -47,27 +52,22 @@ async fn index(session: Session) -> impl Responder {
|
|
|
.body(body)
|
|
|
}
|
|
|
|
|
|
-#[post("/auth")]
|
|
|
-async fn auth(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormLoginData>, session: Session) -> impl Responder {
|
|
|
- let user_authed = controller::login(&ldap_wrapper, &form.id, &form.password)
|
|
|
- .await;
|
|
|
+pub async fn auth(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormLoginData>, session: Session) -> impl Responder {
|
|
|
|
|
|
- if !user_authed {
|
|
|
- return HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/"))
|
|
|
- .finish();
|
|
|
+ match ldap_wrapper.auth(form.id.clone(), form.password.clone())
|
|
|
+ .await {
|
|
|
+ Ok(user) => {
|
|
|
+ session.insert("user_id", user.uid).unwrap();
|
|
|
+ return HttpResponse::Ok()
|
|
|
+ .status(StatusCode::FOUND)
|
|
|
+ .append_header((header::LOCATION, "/home"))
|
|
|
+ .finish();
|
|
|
+ },
|
|
|
+ Err(_e) => return HttpResponse::Ok().status(StatusCode::FOUND).append_header((header::LOCATION, "/")).finish(),
|
|
|
}
|
|
|
-
|
|
|
- session.insert("user_id", &form.id).unwrap();
|
|
|
- HttpResponse::Ok()
|
|
|
- .status(StatusCode::FOUND)
|
|
|
- .append_header((header::LOCATION, "/home"))
|
|
|
- .finish()
|
|
|
}
|
|
|
|
|
|
-#[get("/home")]
|
|
|
-async fn home(session: Session) -> impl Responder {
|
|
|
+pub async fn home(session: Session) -> impl Responder {
|
|
|
if !validate_session(&session) {
|
|
|
return HttpResponse::Ok()
|
|
|
.status(StatusCode::FOUND)
|
|
|
@@ -79,8 +79,7 @@ async fn home(session: Session) -> impl Responder {
|
|
|
.body(body)
|
|
|
}
|
|
|
|
|
|
-#[get("/signout")]
|
|
|
-async fn signout(session: Session) -> impl Responder {
|
|
|
+pub async fn signout(session: Session) -> impl Responder {
|
|
|
session.purge();
|
|
|
HttpResponse::Ok()
|
|
|
.status(StatusCode::FOUND)
|