Commit 2b335d94 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Merge branch 'address'

parents 33b9b943 8e18e9a1
......@@ -116,13 +116,19 @@ impl MedalConnection for Connection {
logincode = ?4,
firstname = ?5,
lastname = ?6,
grade = ?7 WHERE id = ?",
street = ?7,
zip = ?8,
city = ?9,
grade = ?10 WHERE id = ?",
&[&session.username,
&session.password,
&session.salt,
&session.logincode,
&session.firstname,
&session.lastname,
&session.street,
&session.zip,
&session.city,
&session.grade,
&session.id])
.unwrap();
......
......@@ -582,11 +582,15 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: String, user_id
None => {
data.insert("firstname".to_string(), to_json(&session.firstname));
data.insert("lastname".to_string(), to_json(&session.lastname));
data.insert("street".to_string(), to_json(&session.street));
data.insert("zip".to_string(), to_json(&session.zip));
data.insert("city".to_string(), to_json(&session.city));
data.insert(format!("sel{}", session.grade), to_json(&"selected"));
data.insert("logincode".to_string(), to_json(&session.logincode));
if session.password.is_some() {
data.insert("username".to_string(), to_json(&session.username));
data.insert("not_in_group".into(), to_json(&true));
}
data.insert("ownprofile".into(), to_json(&true));
......@@ -611,18 +615,29 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: String, user_id
data.insert("firstname".to_string(), to_json(&user.firstname));
data.insert("lastname".to_string(), to_json(&user.lastname));
data.insert("street".to_string(), to_json(&session.street));
data.insert("zip".to_string(), to_json(&session.zip));
data.insert("city".to_string(), to_json(&session.city));
data.insert(format!("sel{}", user.grade), to_json(&"selected"));
data.insert("logincode".to_string(), to_json(&user.logincode));
if user.password.is_some() {
data.insert("username".to_string(), to_json(&user.username));
data.insert("not_in_group".into(), to_json(&true));
}
data.insert("ownprofile".into(), to_json(&false));
data.insert("csrftoken".to_string(), to_json(&session.csrf_token));
// data.insert("query_string".to_string(), to_json(&query_string.unwrap()));
if let Some(query) = query_string {
if query.starts_with("status=") {
let status: &str = &query[7..];
if ["NothingChanged", "DataChanged", "PasswordChanged", "PasswordMissmatch"].contains(&status) {
data.insert((status).to_string(), to_json(&true));
}
}
}
}
}
......@@ -649,8 +664,9 @@ impl std::convert::Into<String> for ProfileStatus {
}
pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: String, user_id: Option<u32>, csrf_token: String,
firstname: String, lastname: String, password: String,
password_repeat: String, grade: u8)
firstname: String, lastname: String, street: Option<String>,
zip: Option<String>, city: Option<String>, password: Option<String>,
password_repeat: Option<String>, grade: u8)
-> MedalResult<ProfileStatus>
{
let mut session = conn.get_session(&session_token)
......@@ -664,26 +680,31 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: String, user_id
if session.firstname.as_ref() == Some(&firstname)
&& session.lastname.as_ref() == Some(&lastname)
&& session.street == street
&& session.zip == zip
&& session.city == city
&& session.grade == grade
&& password == ""
&& password_repeat == ""
{
return Ok(ProfileStatus::NothingChanged);
}
let mut result = ProfileStatus::DataChanged;
let mut password_salt = None;
let mut password_and_salt = None;
if password != "" || password_repeat != "" {
if password == password_repeat {
let salt: String = thread_rng().sample_iter(&Alphanumeric).take(10).collect();
let hash = hash_password(&password, &salt)?;
if let (Some(password), Some(password_repeat)) = (password, password_repeat) {
if password != "" || password_repeat != "" {
if password == password_repeat {
let salt: String = thread_rng().sample_iter(&Alphanumeric).take(10).collect();
let hash = hash_password(&password, &salt)?;
password_salt = Some((hash, salt));
result = ProfileStatus::PasswordChanged;
password_and_salt = Some((hash, salt));
result = ProfileStatus::PasswordChanged;
} else {
result = ProfileStatus::PasswordMissmatch;
}
} else {
result = ProfileStatus::PasswordMissmatch;
return Ok(ProfileStatus::NothingChanged);
}
}
......@@ -693,7 +714,17 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: String, user_id
session.lastname = Some(lastname);
session.grade = grade;
if let Some((password, salt)) = password_salt {
if street.is_some() {
session.street = street;
}
if zip.is_some() {
session.zip = zip;
}
if city.is_some() {
session.city = city;
}
if let Some((password, salt)) = password_and_salt {
session.password = Some(password);
session.salt = Some(salt);
}
......@@ -712,7 +743,17 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: String, user_id
user.lastname = Some(lastname);
user.grade = grade;
if let Some((password, salt)) = password_salt {
if street.is_some() {
user.street = street;
}
if zip.is_some() {
user.zip = zip;
}
if city.is_some() {
user.city = city;
}
if let Some((password, salt)) = password_and_salt {
user.password = Some(password);
user.salt = Some(salt);
}
......
......@@ -550,29 +550,37 @@ fn profile(req: &mut Request) -> IronResult<Response> {
fn profile_post(req: &mut Request) -> IronResult<Response> {
let session_token = req.expect_session_token()?;
let (csrf_token, firstname, lastname, pwd, pwd_repeat, grade) = {
let (csrf_token, firstname, lastname, street, zip, city, pwd, pwd_repeat, grade) = {
let formdata = itry!(req.get_ref::<UrlEncodedBody>());
(iexpect!(formdata.get("csrftoken"))[0].to_owned(),
iexpect!(formdata.get("firstname"))[0].to_owned(),
iexpect!(formdata.get("lastname"))[0].to_owned(),
iexpect!(formdata.get("password"))[0].to_owned(),
iexpect!(formdata.get("password_repeat"))[0].to_owned(),
formdata.get("street").map(|x| x[0].to_owned()),
formdata.get("zip").map(|x| x[0].to_owned()),
formdata.get("city").map(|x| x[0].to_owned()),
formdata.get("password").map(|x| x[0].to_owned()),
formdata.get("password_repeat").map(|x| x[0].to_owned()),
iexpect!(formdata.get("grade"))[0].parse::<u8>().unwrap_or(0))
};
//TODO: use profilechangeresult
let _profilechangeresult = with_conn![functions::edit_profile,
req,
session_token,
None,
csrf_token,
firstname,
lastname,
pwd,
pwd_repeat,
grade].aug(req)?;
Ok(Response::with((status::Found, Redirect(url_for!(req, "profile")))))
let profilechangeresult = with_conn![functions::edit_profile,
req,
session_token,
None,
csrf_token,
firstname,
lastname,
street,
zip,
city,
pwd,
pwd_repeat,
grade].aug(req)?;
Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
&url_for!(req, "profile"),
profilechangeresult)).unwrap()))))
}
fn user(req: &mut Request) -> IronResult<Response> {
......@@ -591,29 +599,38 @@ fn user(req: &mut Request) -> IronResult<Response> {
fn user_post(req: &mut Request) -> IronResult<Response> {
let user_id = req.expect_int::<u32>("userid")?;
let session_token = req.expect_session_token()?;
let (csrf_token, firstname, lastname, pwd, pwd_repeat, grade) = {
let (csrf_token, firstname, lastname, street, zip, city, pwd, pwd_repeat, grade) = {
let formdata = itry!(req.get_ref::<UrlEncodedBody>());
(iexpect!(formdata.get("csrftoken"))[0].to_owned(),
iexpect!(formdata.get("firstname"))[0].to_owned(),
iexpect!(formdata.get("lastname"))[0].to_owned(),
iexpect!(formdata.get("password"))[0].to_owned(),
iexpect!(formdata.get("password_repeat"))[0].to_owned(),
formdata.get("street").map(|x| x[0].to_owned()),
formdata.get("zip").map(|x| x[0].to_owned()),
formdata.get("city").map(|x| x[0].to_owned()),
formdata.get("password").map(|x| x[0].to_owned()),
formdata.get("password_repeat").map(|x| x[0].to_owned()),
iexpect!(formdata.get("grade"))[0].parse::<u8>().unwrap_or(0))
};
//TODO: use profilechangeresult
let _profilechangeresult = with_conn![functions::edit_profile,
req,
session_token,
Some(user_id),
csrf_token,
firstname,
lastname,
pwd,
pwd_repeat,
grade].aug(req)?;
Ok(Response::with((status::Found, Redirect(url_for!(req, "user", "userid" => format!("{}",user_id))))))
let profilechangeresult = with_conn![functions::edit_profile,
req,
session_token,
Some(user_id),
csrf_token,
firstname,
lastname,
street,
zip,
city,
pwd,
pwd_repeat,
grade].aug(req)?;
Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
&url_for!(req, "user", "userid" => format!("{}",user_id)),
profilechangeresult)).unwrap()))))
//old: Ok(Response::with((status::Found, Redirect(url_for!(req, "user", "userid" => format!("{}",user_id))))))
}
#[derive(Deserialize, Debug)]
......
......@@ -20,6 +20,17 @@
<tr>
<td>Nachname:</td><td><input name="lastname" value="{{ lastname }}"></td>
</tr>
{{#if not_in_group}}
<tr>
<td>Straße und Hausnummer:</td><td><input name="street" value="{{ street }}"></td>
</tr>
<tr>
<td>Postleitzahl:</td><td><input name="zip" value="{{ zip }}"></td>
</tr>
<tr>
<td>Stadt:</td><td><input name="city" value="{{ city }}"></td>
</tr>
{{/if}}
<tr>
<td>Jahrgangsstufe:</td><td>
<select name="grade">
......@@ -44,12 +55,14 @@
</select>
</td>
</tr>
<tr>
<td>Neues Passwort:</td><td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td>Neues Passwort (wdh):</td><td><input type="password" name="password_repeat" value=""></td>
</tr>
{{#if not_in_group}}
<tr>
<td>Neues Passwort:</td><td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td>Neues Passwort (wdh):</td><td><input type="password" name="password_repeat" value=""></td>
</tr>
{{/if}}
<tr>
<td></td><td><input type="hidden" name="csrftoken" value="{{ csrftoken }}"><input type="submit" value="Speichern"></td>
</tr>
......@@ -57,22 +70,22 @@
</form>
<div style="color:green; font-weight:bold">
{{#if nothing_changed}}
{{#if NothingChanged}}
<p>Keine Änderungen.</p>
{{/if}}
{{#if data_changed}}
{{#if DataChanged}}
<p>Daten wurden erfolgreich geändert.</p>
{{/if}}
{{#if password_changed}}
{{#if PasswordChanged}}
<p>Passwort wurde erfolgreich geändert.</p>
{{/if}}
{{#if password_missmatch}}
{{#if PasswordMissmatch}}
<p style="color:red;">Die eingegebenen Paswörter stimmen nicht überein. Passwort wurde nicht geändert.</p>
{{/if}}
<div>
</div>
<a href="/">Zur Startseite</a>
......
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