Commit 96e36f03 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Set login data in all based templates

parent 23b19cec
......@@ -48,6 +48,18 @@ type MedalValue = (String, json_val::Map<String, json_val::Value>);
type MedalResult<T> = Result<T, MedalError>;
type MedalValueResult = MedalResult<MedalValue>;
fn fill_user_data(session: &SessionUser, data: &mut json_val::Map<String, serde_json::Value>) {
if session.is_logged_in() {
data.insert("logged_in".to_string(), to_json(&true));
data.insert("username".to_string(), to_json(&session.username));
data.insert("firstname".to_string(), to_json(&session.firstname));
data.insert("lastname".to_string(), to_json(&session.lastname));
data.insert("teacher".to_string(), to_json(&session.is_teacher));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
}
data.insert("parent".to_string(), to_json(&"base"));
}
pub fn index<T: MedalConnection>(conn: &T, session_token: Option<String>,
(self_url, oauth_providers): (Option<String>, Option<Vec<OauthProvider>>))
-> (String, json_val::Map<String, json_val::Value>)
......@@ -58,13 +70,7 @@ pub fn index<T: MedalConnection>(conn: &T, session_token: Option<String>,
if let Some(token) = session_token {
if let Some(session) = conn.get_session(&token) {
if session.is_logged_in() {
data.insert("logged_in".to_string(), to_json(&true));
data.insert("username".to_string(), to_json(&session.username));
data.insert("firstname".to_string(), to_json(&session.firstname));
data.insert("lastname".to_string(), to_json(&session.lastname));
data.insert("teacher".to_string(), to_json(&session.is_teacher));
}
fill_user_data(&session, &mut data);
}
}
......@@ -134,16 +140,15 @@ pub fn show_contests<T: MedalConnection>(conn: &T, session_token: &str, visibili
let mut data = json_val::Map::new();
let session = conn.get_session_or_new(&session_token);
fill_user_data(&session, &mut data);
if session.is_logged_in() {
data.insert("logged_in".to_string(), to_json(&true));
data.insert("can_start".to_string(), to_json(&true));
data.insert("username".to_string(), to_json(&session.username));
data.insert("firstname".to_string(), to_json(&session.firstname));
data.insert("lastname".to_string(), to_json(&session.lastname));
data.insert("teacher".to_string(), to_json(&session.is_teacher));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
}
let v: Vec<ContestInfo> = conn.get_contest_list()
.iter()
.map(|c| ContestInfo { id: c.id.unwrap(),
......@@ -165,7 +170,6 @@ pub fn show_contests<T: MedalConnection>(conn: &T, session_token: &str, visibili
ContestVisibility::All => "Alle Wettbewerbe",
}));
data.insert("parent".to_string(), to_json(&"base"));
("contests".to_owned(), data)
}
......@@ -287,6 +291,9 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token
pub fn show_contest_results<T: MedalConnection>(conn: &T, contest_id: i32, session_token: &str) -> MedalValueResult {
let session = conn.get_session(&session_token).ensure_logged_in().ok_or(MedalError::NotLoggedIn)?;
let mut data = json_val::Map::new();
fill_user_data(&session, &mut data);
let (tasknames, resultdata) = conn.get_contest_groups_grades(session.id, contest_id);
let mut results: Vec<(String, i32, Vec<(String, i32, Vec<String>)>)> = Vec::new();
......@@ -322,7 +329,7 @@ pub fn show_contest_results<T: MedalConnection>(conn: &T, contest_id: i32, sessi
results.push((format!("{}", group.name), group.id.unwrap_or(0), groupresults));
}
let mut data = json_val::Map::new();
data.insert("taskname".to_string(), to_json(&tasknames));
data.insert("result".to_string(), to_json(&results));
......@@ -336,7 +343,6 @@ pub fn show_contest_results<T: MedalConnection>(conn: &T, contest_id: i32, sessi
tasks: Vec::new() };
data.insert("contest".to_string(), to_json(&ci));
data.insert("parent".to_string(), to_json(&"base"));
Ok(("contestresults".to_owned(), data))
}
......@@ -520,6 +526,7 @@ pub fn show_groups<T: MedalConnection>(conn: &T, session_token: &str) -> MedalVa
// let groupvec = conn.get_group(session_token);
let mut data = json_val::Map::new();
fill_user_data(&session, &mut data);
let v: Vec<GroupInfo> =
conn.get_groups(session.id)
......@@ -532,7 +539,6 @@ pub fn show_groups<T: MedalConnection>(conn: &T, session_token: &str) -> MedalVa
data.insert("group".to_string(), to_json(&v));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
data.insert("parent".to_string(), to_json(&"base"));
Ok(("groups".to_string(), data))
}
......@@ -548,8 +554,9 @@ pub struct MemberInfo {
pub fn show_group<T: MedalConnection>(conn: &T, group_id: i32, session_token: &str) -> MedalValueResult {
let session = conn.get_session(&session_token).ensure_logged_in().ok_or(MedalError::NotLoggedIn)?;
let group = conn.get_group_complete(group_id).unwrap(); // TODO handle error
let mut data = json_val::Map::new();
fill_user_data(&session, &mut data);
if group.admin != session.id {
return Err(MedalError::AccessDenied);
......@@ -573,7 +580,6 @@ pub fn show_group<T: MedalConnection>(conn: &T, group_id: i32, session_token: &s
data.insert("group".to_string(), to_json(&gi));
data.insert("member".to_string(), to_json(&v));
data.insert("parent".to_string(), to_json(&"base"));
Ok(("group".to_string(), data))
}
......@@ -679,25 +685,24 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
let session = conn.get_session(&session_token).ensure_logged_in().ok_or(MedalError::NotLoggedIn)?;
let mut data = json_val::Map::new();
fill_user_data(&session, &mut data);
match 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("profile_firstname".to_string(), to_json(&session.firstname));
data.insert("profile_lastname".to_string(), to_json(&session.lastname));
data.insert("profile_street".to_string(), to_json(&session.street));
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"));
data.insert("logincode".to_string(), to_json(&session.logincode));
data.insert("profile_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("profile_username".to_string(), to_json(&session.username));
data.insert("profile_not_in_group".into(), to_json(&true));
}
data.insert("ownprofile".into(), to_json(&true));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
if let Some(query) = query_string {
if query.starts_with("status=") {
let status: &str = &query[7..];
......@@ -715,23 +720,21 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
return Err(MedalError::AccessDenied);
}
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("profile_firstname".to_string(), to_json(&user.firstname));
data.insert("profile_lastname".to_string(), to_json(&user.lastname));
data.insert("profile_street".to_string(), to_json(&session.street));
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"));
data.insert("logincode".to_string(), to_json(&user.logincode));
data.insert("profile_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("profile_username".to_string(), to_json(&user.username));
data.insert("profile_not_in_group".into(), to_json(&true));
}
data.insert("ownprofile".into(), to_json(&false));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
if let Some(query) = query_string {
if query.starts_with("status=") {
let status: &str = &query[7..];
......@@ -743,7 +746,6 @@ pub fn show_profile<T: MedalConnection>(conn: &T, session_token: &str, user_id:
}
}
data.insert("parent".to_string(), to_json(&"base"));
Ok(("profile".to_string(), data))
}
......
{{#if ownprofile}}
<h1>Mein Profil: {{firstname}} {{lastname}}</h1>
<h1>Mein Profil: {{profile_firstname}} {{profile_lastname}}</h1>
{{else}}
<h1>Benutzerdaten bearbeiten: {{firstname}} {{lastname}}</h1>
<h1>Benutzerdaten bearbeiten: {{profile_firstname}} {{profile_lastname}}</h1>
{{/if}}
{{#if username}}
<p>Benutzername: {{username}}</p>
{{#if profile_username}}
<p>Benutzername: {{profile_username}}</p>
{{/if}}
{{#if logincode}}
<p>Login-Code: {{logincode}}</p>
{{#if profile_logincode}}
<p>Login-Code: {{profile_logincode}}</p>
{{/if}}
<form action="" method="post">
<table>
<table>
<tr>
<td>Vorname:</td><td><input name="firstname" value="{{ profile_firstname }}"></td>
</tr>
<tr>
<td>Nachname:</td><td><input name="lastname" value="{{ profile_lastname }}"></td>
</tr>
{{#if not_in_group}}
<tr>
<td>Vorname:</td><td><input name="firstname" value="{{ firstname }}"></td>
<td>Straße und Hausnummer:</td><td><input name="street" value="{{ profile_street }}"></td>
</tr>
<tr>
<td>Nachname:</td><td><input name="lastname" value="{{ lastname }}"></td>
<td>Postleitzahl:</td><td><input name="zip" value="{{ profile_zip }}"></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">
<option value="0" {{ sel0 }}>Noch kein Schüler</option>
<option value="1" {{ sel1 }}>1</option>
<option value="2" {{ sel2 }}>2</option>
<option value="3" {{ sel3 }}>3</option>
<option value="4" {{ sel4 }}>4</option>
<option value="5" {{ sel5 }}>5</option>
<option value="6" {{ sel6 }}>6</option>
<option value="7" {{ sel7 }}>7</option>
<option value="8" {{ sel8 }}>8</option>
<option value="9" {{ sel9 }}>9</option>
<option value="10" {{ sel10 }}>10</option>
<option value="11" {{ sel11 }}>11 (G8)</option>
<option value="12" {{ sel12 }}>12 (G8)</option>
<option value="111" {{ sel111 }}>11 (G9)</option>
<option value="112" {{ sel112 }}>12 (G9)</option>
<option value="113" {{ sel113 }}>13 (G9)</option>
<option value="114" {{ sel114 }}>Berufsschule</option>
<option value="255" {{ sel255 }}>Kein Schüler mehr</option>
</select>
</td>
<td>Stadt:</td><td><input name="city" value="{{ profile_city }}"></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}}
{{/if}}
<tr>
<td>Jahrgangsstufe:</td><td>
<select name="grade">
<option value="0" {{ sel0 }}>Noch kein Schüler</option>
<option value="1" {{ sel1 }}>1</option>
<option value="2" {{ sel2 }}>2</option>
<option value="3" {{ sel3 }}>3</option>
<option value="4" {{ sel4 }}>4</option>
<option value="5" {{ sel5 }}>5</option>
<option value="6" {{ sel6 }}>6</option>
<option value="7" {{ sel7 }}>7</option>
<option value="8" {{ sel8 }}>8</option>
<option value="9" {{ sel9 }}>9</option>
<option value="10" {{ sel10 }}>10</option>
<option value="11" {{ sel11 }}>11 (G8)</option>
<option value="12" {{ sel12 }}>12 (G8)</option>
<option value="111" {{ sel111 }}>11 (G9)</option>
<option value="112" {{ sel112 }}>12 (G9)</option>
<option value="113" {{ sel113 }}>13 (G9)</option>
<option value="114" {{ sel114 }}>Berufsschule</option>
<option value="255" {{ sel255 }}>Kein Schüler mehr</option>
</select>
</td>
</tr>
{{#if profile_not_in_group}}
<tr>
<td></td><td><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="submit" value="Speichern"></td>
<td>Neues Passwort:</td><td><input type="password" name="password" value=""></td>
</tr>
</table>
<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="csrf_token" value="{{ csrf_token }}"><input type="submit" value="Speichern"></td>
</tr>
</table>
</form>
<div style="color:green; font-weight:bold">
......
......@@ -2,16 +2,16 @@
<div class="columns">
<div class="column is-12 is-offset-1">
/ <a href="/">Startseite</a> / Profil <em>{{firstname}} {{lastname}}</em>
/ <a href="/">Startseite</a> / Profil <em>{{profile_firstname}} {{profile_lastname}}</em>
</div>
</div>
<div class="columns">
<div class="column is-8 is-offset-2">
<h3 class="title is-4">
{{#if ownprofile}}
Mein Profil: <em>{{firstname}} {{lastname}}</em>
Mein Profil: <em>{{profile_firstname}} {{profile_lastname}}</em>
{{else}}
Benutzerdaten bearbeiten von <em>{{firstname}} {{lastname}}</em>
Benutzerdaten bearbeiten von <em>{{profile_firstname}} {{profile_lastname}}</em>
{{/if}}
</h3>
</div></div>
......@@ -19,31 +19,45 @@
<div class="column is-8 is-offset-2">
{{#if username}}
<p>Benutzername: {{username}}</p>
{{#if profile_username}}
<p>Benutzername: {{profile_username}}</p>
{{/if}}
{{#if logincode}}
<p>Login-Code: {{logincode}}</p>
{{#if profile_logincode}}
{{#if ownprofile}}
<h4 class="subtitle is-5">Logincode: <strong>{{profile_logincode}}</strong></h4>
<p>
Der Logincode erlaubt dir, dich erneut einzuloggen (anstelle eines Benutzernamen und eines Passworts).
Bitte schreibe ihn dir gut auf!
Wenn du deinen Logincode vergessen hast, frage deine Lehrerin oder deinen Lehrer, ihn dir zu sagen.
</p>
{{else}}
<p>Login-Code: <strong>{{profile_logincode}}</strong></p>
{{/if}}
{{/if}}
</div>
</div>
<div class="columns">
<div class="column is-8 is-offset-2">
<form action="" method="post">
<table>
<tr>
<td>Vorname:</td><td><input name="firstname" value="{{ firstname }}"></td>
<td>Vorname:</td><td><input name="firstname" value="{{ profile_firstname }}"></td>
</tr>
<tr>
<td>Nachname:</td><td><input name="lastname" value="{{ lastname }}"></td>
<td>Nachname:</td><td><input name="lastname" value="{{ profile_lastname }}"></td>
</tr>
{{#if not_in_group}}
<tr>
<td>Straße und Hausnummer:</td><td><input name="street" value="{{ street }}"></td>
<td>Straße und Hausnummer:</td><td><input name="street" value="{{ profile_street }}"></td>
</tr>
<tr>
<td>Postleitzahl:</td><td><input name="zip" value="{{ zip }}"></td>
<td>Postleitzahl:</td><td><input name="zip" value="{{ profile_zip }}"></td>
</tr>
<tr>
<td>Stadt:</td><td><input name="city" value="{{ city }}"></td>
<td>Stadt:</td><td><input name="city" value="{{ profile_city }}"></td>
</tr>
{{/if}}
<tr>
......@@ -70,7 +84,7 @@
</select>
</td>
</tr>
{{#if not_in_group}}
{{#if profile_not_in_group}}
<tr>
<td>Neues Passwort:</td><td><input type="password" name="password" value=""></td>
</tr>
......
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