Return privilege level of user after authentication

This commit is contained in:
Antoine Gersant 2017-07-03 18:43:13 -07:00
parent 27cfa19b77
commit a08401fa76

View file

@ -303,14 +303,28 @@ fn auth(request: &mut Request, db: &DB) -> IronResult<Response> {
_ => return Err(Error::from(ErrorKind::MissingPassword).into()),
};
}
if user::auth(db, username.as_str(), password.as_str())? {
if !user::auth(db, username.as_str(), password.as_str())? {
return Err(Error::from(ErrorKind::IncorrectCredentials).into());
}
request
.extensions
.insert::<SessionKey>(Session { username: username.clone() });
Ok(Response::with((status::Ok, "")))
} else {
Err(Error::from(ErrorKind::IncorrectCredentials).into())
#[derive(Serialize)]
struct AuthOutput {
admin: bool,
}
let auth_output = AuthOutput { admin: user::is_admin(db.deref(), &username)? };
let result_json = serde_json::to_string(&auth_output);
let result_json = match result_json {
Ok(j) => j,
Err(e) => return Err(IronError::new(e, status::InternalServerError)),
};
Ok(Response::with((status::Ok, result_json)))
}
fn browse(request: &mut Request, db: &DB) -> IronResult<Response> {