diff --git a/src/api.rs b/src/api.rs index eacc1f5..206b26c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -19,10 +19,11 @@ use handlebars::Handlebars; use ldap3::{ldap_escape, LdapConn}; use ldap3::exop::PasswordModify; use ldap3::result::LdapError; +use rocket::http::Status; use rocket_contrib::json::Json; use rocket::post; -use serde_derive::{Deserialize, Serialize}; +use serde_derive::Deserialize; use serde_json::json; use crate::context::Context; @@ -34,18 +35,6 @@ pub struct PasswordData { new_password: String, } -#[derive(Serialize)] -enum Message { - InvalidCredentials, - ServerError, -} - -#[derive(Serialize)] -pub struct Response { - success: bool, - message: Option, -} - fn change_password(data: &Json, context: &rocket::State) -> Result<()> { @@ -80,33 +69,23 @@ fn change_password(data: &Json, #[post("/update", data = "")] pub fn update(data: Json, - context: rocket::State) -> Json { - Json( - match change_password(&data, &context) { - Ok(_) => Response{ - success: true, - message: None, - }, - Err(error) => { - eprintln!("LDAP error: {}", error); - Response { - success: false, - message: Some( - match error.downcast::().expect( - "No other error should occur here" - ) { - LdapError::LdapResult{ result } => { - if result.rc == 49 { - Message::InvalidCredentials - } else { - Message::ServerError - } - }, - _ => Message::ServerError, - } - ), - } - }, - } - ) + context: rocket::State) -> Status { + match change_password(&data, &context) { + Ok(_) => Status::Accepted, + Err(error) => { + eprintln!("LDAP error: {}", error); + match error.downcast::().expect( + "No other error should occur here" + ) { + LdapError::LdapResult{ result } => { + if result.rc == 49 { + Status::Unauthorized + } else { + Status::InternalServerError + } + }, + _ => Status::InternalServerError, + } + }, + } } diff --git a/src/static/webldappasswd.js b/src/static/webldappasswd.js index 8e557d4..ab113a5 100644 --- a/src/static/webldappasswd.js +++ b/src/static/webldappasswd.js @@ -27,9 +27,8 @@ MODAL_STATES = { }, "FetchError" : { button_disabled : false, - message : function(parameters) { - `An error occurred while contacting the server (${parameters.statusCode})` - }, + message : "An error occurred while contacting the server", + icon_url : "cross.svg", }, "ServerError" : { button_disabled : false, @@ -43,7 +42,7 @@ MODAL_STATES = { }, } -function showModal(state_name, parameters) { +function showModal(state_name) { const state = MODAL_STATES[state_name]; const message = document.getElementById("message"); @@ -52,9 +51,7 @@ function showModal(state_name, parameters) { img.src = state.icon_url; const text = message.childNodes[1]; - text.textContent = " " + (state.message instanceof Function - ? state.message(parameters) - : state.message); + text.textContent = " " + state.message; const button = document.getElementById("close_button"); button.disabled = state.button_disabled; @@ -97,17 +94,23 @@ function changePasswords() { method: "POST", body: JSON.stringify(query), }).then(response => { - if(response.status == 200) { - return response.json(); - } else { - showModal("FetchError", { statusCode : response.status }); + switch(response.status) { + case 202: + showModal("Success"); + break; + case 401: + showModal("InvalidCredentials"); + break; + case 500: + showModal("ServerError"); + break; + default: + console.log("Unexpected status code: " + response.status); + showModal("FetchError"); + break; } - }).then(response => { - if(response.success) { - showModal("Success"); - clearFields(); - } else { - showModal(response.message); - } - }); + }, error => { + console.log("Fetch error: " + error); + showModal("FetchError"); + }) }