Commit 538996da authored by Robert Czechowski's avatar Robert Czechowski

Format signup code

parent 330a45e7
......@@ -557,11 +557,12 @@ pub fn logout<T: MedalConnection>(conn: &T, session_token: Option<String>) {
}
#[cfg(feature = "signup")]
pub fn signup<T: MedalConnection>(conn: &T, session_token:Option<String>, signup_data: (String, String, String)) -> MedalResult<SignupResult> {
pub fn signup<T: MedalConnection>(conn: &T, session_token: Option<String>, signup_data: (String, String, String))
-> MedalResult<SignupResult> {
let (username, email, password) = signup_data;
if username == "" || email == "" || password == "" {
return Ok(SignupResult::EmptyFields)
return Ok(SignupResult::EmptyFields);
}
let salt = helpers::make_salt();
......@@ -572,7 +573,7 @@ pub fn signup<T: MedalConnection>(conn: &T, session_token:Option<String>, signup
}
#[cfg(feature = "signup")]
pub fn signupdata (query_string: Option<String>) -> json_val::Map<String, json_val::Value> {
pub fn signupdata(query_string: Option<String>) -> json_val::Map<String, json_val::Value> {
let mut data = json_val::Map::new();
if let Some(query) = query_string {
if query.starts_with("status=") {
......@@ -955,7 +956,13 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
if let Some(query) = query_string {
if query.starts_with("status=") {
let status: &str = &query[7..];
if ["NothingChanged", "DataChanged", "PasswordChanged", "PasswordMissmatch", "firstlogin", "SignedUp"].contains(&status) {
if ["NothingChanged",
"DataChanged",
"PasswordChanged",
"PasswordMissmatch",
"firstlogin",
"SignedUp"].contains(&status)
{
data.insert((status).to_string(), to_json(&true));
}
}
......
......@@ -569,24 +569,29 @@ impl MedalConnection for Connection {
self.execute(query, &[&session]).unwrap();
}
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str) -> SignupResult {
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str)
-> SignupResult {
let mut session_user = self.get_session_or_new(&session_token);
if session_user.is_logged_in() {
return SignupResult::UserLoggedIn
return SignupResult::UserLoggedIn;
}
if let Ok(None) = self.query_map_one("SELECT username FROM session WHERE username = $1",
&[&username],
|row| -> Option<String> { row.get(0) }) {} else {
&[&username],
|row| -> Option<String> { row.get(0) })
{
} else {
//This username already exists!
return SignupResult::UsernameTaken
return SignupResult::UsernameTaken;
}
if let Ok(None) = self.query_map_one("SELECT email, email_unconfirmed FROM session WHERE email = $1 OR email_unconfirmed = $1",
&[&email],
|row| -> (Option<String>, Option<String>) { (row.get(0), row.get(1)) }) {} else {
&[&email],
|row| -> (Option<String>, Option<String>) { (row.get(0), row.get(1)) })
{
} else {
//This email already exists!
return SignupResult::EmailTaken
return SignupResult::EmailTaken;
}
session_user.username = Some(username.to_string());
......
......@@ -73,7 +73,8 @@ pub trait MedalConnection {
/// to `NULL`.
fn logout(&self, session: &str);
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str) -> SignupResult;
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str)
-> SignupResult;
fn load_submission(&self, session: &SessionUser, task: i32, subtask: Option<&str>) -> Option<Submission>;
fn get_all_submissions(&self, session_id: i32, task: i32, subtask: Option<&str>) -> Vec<Submission>;
......
......@@ -681,24 +681,29 @@ impl MedalConnection for Connection {
self.execute(query, &[&session]).unwrap();
}
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str) -> SignupResult {
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str)
-> SignupResult {
let mut session_user = self.get_session_or_new(&session_token);
if session_user.is_logged_in() {
return SignupResult::UserLoggedIn
return SignupResult::UserLoggedIn;
}
if let Ok(None) = self.query_map_one("SELECT username FROM session WHERE username = $1",
&[&username],
|row| -> Option<String> { row.get(0) }) {} else {
&[&username],
|row| -> Option<String> { row.get(0) })
{
} else {
//This username already exists!
return SignupResult::UsernameTaken
return SignupResult::UsernameTaken;
}
if let Ok(None) = self.query_map_one("SELECT email, email_unconfirmed FROM session WHERE email = $1 OR email_unconfirmed = $1",
&[&email],
|row| -> (Option<String>, Option<String>) { (row.get(0), row.get(1)) }) {} else {
&[&email],
|row| -> (Option<String>, Option<String>) { (row.get(0), row.get(1)) })
{
} else {
//This email already exists!
return SignupResult::EmailTaken
return SignupResult::EmailTaken;
}
session_user.username = Some(username.to_string());
......
......@@ -681,24 +681,29 @@ impl MedalConnection for Connection {
self.execute(query, &[&session]).unwrap();
}
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str) -> SignupResult {
fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str)
-> SignupResult {
let mut session_user = self.get_session_or_new(&session_token);
if session_user.is_logged_in() {
return SignupResult::UserLoggedIn
return SignupResult::UserLoggedIn;
}
if let Ok(None) = self.query_map_one("SELECT username FROM session WHERE username = ?1",
&[&username],
|row| -> Option<String> { row.get(0) }) {} else {
&[&username],
|row| -> Option<String> { row.get(0) })
{
} else {
//This username already exists!
return SignupResult::UsernameTaken
return SignupResult::UsernameTaken;
}
if let Ok(None) = self.query_map_one("SELECT email, email_unconfirmed FROM session WHERE email = ?1 OR email_unconfirmed = ?1",
&[&email],
|row| -> (Option<String>, Option<String>) { (row.get(0), row.get(1)) }) {} else {
&[&email],
|row| -> (Option<String>, Option<String>) { (row.get(0), row.get(1)) })
{
} else {
//This email already exists!
return SignupResult::EmailTaken
return SignupResult::EmailTaken;
}
session_user.username = Some(username.to_string());
......
......@@ -253,7 +253,7 @@ impl<'c, 'a, 'b> From<AugMedalError<'c, 'a, 'b>> for IronError {
fn from(AugMedalError(me, req): AugMedalError<'c, 'a, 'b>) -> Self {
match me {
core::MedalError::NotLoggedIn => {
IronError { error: Box::new(SessionError { message:
IronError { error: Box::new(SessionError { message:
"Not Logged in, redirecting to login page".to_string() }),
response: Response::with((status::Found,
RedirectRaw(format!("/login?{}", req.url.path().join("/"))))) }
......@@ -281,10 +281,9 @@ impl<'c, 'a, 'b> From<AugMedalError<'c, 'a, 'b>> for IronError {
"The two passwords did not match.".to_string() }),
response: Response::with(status::Forbidden) }
}
core::MedalError::NotFound => {
IronError { error: Box::new(SessionError { message: "Not found".to_string() }),
response: Response::with(status::NotFound) }
}
core::MedalError::NotFound => IronError { error: Box::new(SessionError { message:
"Not found".to_string() }),
response: Response::with(status::NotFound) },
core::MedalError::OauthError(errstr) => {
IronError { error: Box::new(SessionError { message: format!("Access denied (Error {})", errstr) }),
response: Response::with(status::Unauthorized) }
......@@ -601,21 +600,21 @@ fn signup_post<C>(req: &mut Request) -> IronResult<Response>
let session_token = req.get_session_token();
let signupdata = {
let formdata = itry!(req.get_ref::<UrlEncodedBody>());
(iexpect!(formdata.get("username"))[0].to_owned(), iexpect!(formdata.get("email"))[0].to_owned(), iexpect!(formdata.get("password"))[0].to_owned())
(iexpect!(formdata.get("username"))[0].to_owned(),
iexpect!(formdata.get("email"))[0].to_owned(),
iexpect!(formdata.get("password"))[0].to_owned())
};
let signupresult = with_conn![core::signup, C, req, session_token, signupdata].aug(req)?;
match signupresult {
SignupResult::SignedUp =>
Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
&url_for!(req, "profile"),
signupresult)).unwrap())))),
_ =>
Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
&url_for!(req, "signup"),
signupresult)).unwrap()))))
SignupResult::SignedUp => Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
&url_for!(req, "profile"),
signupresult)).unwrap())))),
_ => Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
&url_for!(req, "signup"),
signupresult)).unwrap())))),
}
}
......@@ -631,11 +630,10 @@ fn signup_post<C>(req: &mut Request) -> IronResult<Response>
Err(core::MedalError::NotFound).aug(req).map_err(|x| x.into())
}
fn submission<C>(req: &mut Request) -> IronResult<Response>
where C: MedalConnection + std::marker::Send + 'static {
let task_id = req.expect_int::<i32>("taskid")?;
let session_token = req.expect_session_token()?;
let subtask: Option<String> = (|| -> Option<String> {
req.get_ref::<UrlEncodedQuery>().ok()?.get("subtask")?.get(0).map(|x| x.to_owned())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment