Commit fcabc239 authored by Robert Czechowski's avatar Robert Czechowski

Add sex in profile, cleanup profile, fix bug with profile changes of users not in groups

parent 00b914f7
Pipeline #449 passed with stages
in 25 minutes and 41 seconds
......@@ -817,6 +817,11 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
data.insert("profile_zip".to_string(), to_json(&session.zip));
data.insert("profile_city".to_string(), to_json(&session.city));
data.insert(format!("sel{}", session.grade), to_json(&"selected"));
if let Some(sex) = session.sex {
data.insert(format!("sex_{}", sex), to_json(&"selected"));
} else {
data.insert("sex_None".to_string(), to_json(&"selected"));
}
data.insert("profile_logincode".to_string(), to_json(&session.logincode));
if session.password.is_some() {
......@@ -850,9 +855,14 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
data.insert("profile_zip".to_string(), to_json(&session.zip));
data.insert("profile_city".to_string(), to_json(&session.city));
data.insert(format!("sel{}", user.grade), to_json(&"selected"));
if let Some(sex) = user.sex {
data.insert(format!("sex_{}", sex), to_json(&"selected"));
} else {
data.insert("sex_None".to_string(), to_json(&"selected"));
}
data.insert("profile_logincode".to_string(), to_json(&user.logincode));
if user.password.is_some() {
if user.username.is_some() {
data.insert("profile_username".to_string(), to_json(&user.username));
}
if user.managed_by.is_none() {
......@@ -875,7 +885,7 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
Ok(("profile".to_string(), data))
}
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum ProfileStatus {
NothingChanged,
DataChanged,
......@@ -887,16 +897,23 @@ impl std::convert::Into<String> for ProfileStatus {
}
pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id: Option<i32>, csrf_token: &str,
(firstname, lastname, street, zip, city, password, password_repeat, grade): (
String,
String,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
i32,
))
(firstname,
lastname,
street,
zip,
city,
password,
password_repeat,
grade,
sex): (String,
String,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
Option<String>,
i32,
Option<i32>))
-> MedalResult<ProfileStatus>
{
let mut session = conn.get_session(&session_token).ensure_logged_in().ok_or(MedalError::NotLoggedIn)?;
......@@ -905,17 +922,7 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
return Err(MedalError::AccessDenied); // CsrfError
}
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
{
return Ok(ProfileStatus::NothingChanged);
}
let mut result = ProfileStatus::DataChanged;
let mut result = ProfileStatus::NothingChanged;
let mut password_and_salt = None;
......@@ -930,8 +937,21 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
} else {
result = ProfileStatus::PasswordMissmatch;
}
} else {
}
}
if result == ProfileStatus::NothingChanged {
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
&& session.sex == sex
{
return Ok(ProfileStatus::NothingChanged);
} else {
result = ProfileStatus::DataChanged;
}
}
......@@ -940,6 +960,7 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
session.firstname = Some(firstname);
session.lastname = Some(lastname);
session.grade = grade;
session.sex = sex;
if street.is_some() {
session.street = street;
......@@ -969,6 +990,7 @@ pub fn edit_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
user.firstname = Some(firstname);
user.lastname = Some(lastname);
user.grade = grade;
user.sex = sex;
if street.is_some() {
user.street = street;
......
......@@ -478,7 +478,7 @@ impl MedalConnection for Connection {
let query = "INSERT INTO session (session_token, csrf_token, last_login, last_activity, permanent_login,
logincode, grade, sex, is_teacher, managed_by)
VALUES ($1, $2, $3, $3, $4, $5, $6, $7, $8, $9, $10)";
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)";
self.execute(query,
&[&session_token,
&csrf_token,
......
......@@ -574,7 +574,7 @@ impl MedalConnection for Connection {
let query = "INSERT INTO session (session_token, csrf_token, last_login, last_activity, permanent_login,
logincode, grade, sex, is_teacher, managed_by)
VALUES ($1, $2, $3, $3, $4, $5, $6, $7, $8, $9, $10)";
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)";
self.execute(query,
&[&session_token,
&csrf_token,
......
......@@ -574,7 +574,7 @@ impl MedalConnection for Connection {
let query = "INSERT INTO session (session_token, csrf_token, last_login, last_activity, permanent_login,
logincode, grade, sex, is_teacher, managed_by)
VALUES (?1, ?2, ?3, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)";
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)";
self.execute(query,
&[&session_token,
&csrf_token,
......
......@@ -763,7 +763,7 @@ fn profile<C>(req: &mut Request) -> IronResult<Response>
fn profile_post<C>(req: &mut Request) -> IronResult<Response>
where C: MedalConnection + std::marker::Send + 'static {
let session_token = req.expect_session_token()?;
let (csrf_token, firstname, lastname, street, zip, city, pwd, pwd_repeat, grade) = {
let (csrf_token, firstname, lastname, street, zip, city, pwd, pwd_repeat, grade, sex) = {
let formdata = itry!(req.get_ref::<UrlEncodedBody>());
(iexpect!(formdata.get("csrf_token"))[0].to_owned(),
iexpect!(formdata.get("firstname"))[0].to_owned(),
......@@ -773,16 +773,18 @@ fn profile_post<C>(req: &mut Request) -> IronResult<Response>
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::<i32>().unwrap_or(0))
iexpect!(formdata.get("grade"))[0].parse::<i32>().unwrap_or(0),
iexpect!(formdata.get("sex"))[0].parse::<i32>().ok())
};
let profilechangeresult = with_conn![core::edit_profile,
C,
req,
&session_token,
None,
&csrf_token,
(firstname, lastname, street, zip, city, pwd, pwd_repeat, grade)].aug(req)?;
let profilechangeresult =
with_conn![core::edit_profile,
C,
req,
&session_token,
None,
&csrf_token,
(firstname, lastname, street, zip, city, pwd, pwd_repeat, grade, sex)].aug(req)?;
Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
......@@ -808,7 +810,7 @@ fn user_post<C>(req: &mut Request) -> IronResult<Response>
where C: MedalConnection + std::marker::Send + 'static {
let user_id = req.expect_int::<i32>("userid")?;
let session_token = req.expect_session_token()?;
let (csrf_token, firstname, lastname, street, zip, city, pwd, pwd_repeat, grade) = {
let (csrf_token, firstname, lastname, street, zip, city, pwd, pwd_repeat, grade, sex) = {
let formdata = itry!(req.get_ref::<UrlEncodedBody>());
(iexpect!(formdata.get("csrf_token"))[0].to_owned(),
iexpect!(formdata.get("firstname"))[0].to_owned(),
......@@ -818,16 +820,18 @@ fn user_post<C>(req: &mut Request) -> IronResult<Response>
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::<i32>().unwrap_or(0))
iexpect!(formdata.get("grade"))[0].parse::<i32>().unwrap_or(0),
iexpect!(formdata.get("sex"))[0].parse::<i32>().ok())
};
let profilechangeresult = with_conn![core::edit_profile,
C,
req,
&session_token,
Some(user_id),
&csrf_token,
(firstname, lastname, street, zip, city, pwd, pwd_repeat, grade)].aug(req)?;
let profilechangeresult =
with_conn![core::edit_profile,
C,
req,
&session_token,
Some(user_id),
&csrf_token,
(firstname, lastname, street, zip, city, pwd, pwd_repeat, grade, sex)].aug(req)?;
Ok(Response::with((status::Found,
Redirect(iron::Url::parse(&format!("{}?status={:?}",
......
......@@ -55,6 +55,17 @@
</select>
</td>
</tr>
<tr>
<td>Geschlecht (optional):</td><td>
<select name="sex">
<option value="n" {{ sex_None }}></option>
<option value="0" {{ sex_0 }}>Keine Angabe</option>
<option value="2" {{ sel_2 }}>weiblich</option>
<option value="1" {{ sel_1 }}>männlich</option>
<option value="4" {{ sel_4 }}>anderes</option>
</select>
</td>
</tr>
{{#if profile_not_in_group}}
<tr>
<td>Neues Passwort:</td><td><input type="password" name="password" value=""></td>
......
......@@ -107,13 +107,26 @@
</select>
</td>
</tr>
<tr>
<td>Geschlecht (optional):</td><td>
<select name="sex">
<option value="n" {{ sex_None }}></option>
<option value="0" {{ sex_0 }}>Keine Angabe</option>
<option value="2" {{ sex_2 }}>weiblich</option>
<option value="1" {{ sex_1 }}>männlich</option>
<option value="4" {{ sex_4 }}>anderes</option>
</select>
</td>
</tr>
{{#if profile_not_in_group}}
{{#if profile_username}}
<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}}
{{/if}}
<tr>
<td></td><td><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="submit" value="Speichern"></td>
......@@ -160,7 +173,7 @@
{{/if}}
{{#if PasswordChanged}}
<a href="/" class="button is-primary">Zurück zur Startseite!</a>
<a href="/" class="button is-warning">Zurück zur Startseite!</a>
<a href="/contest/open/" class="button is-success">Zum Training!</a>
<a href="/contest/current/" class="button is-info">Zu den Wettbewerben!</a>
{{/if}}
......
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