Commit a8878f9b authored by Daniel Brüning's avatar Daniel Brüning
Browse files

Merge remote-tracking branch 'remotes/origin/auto-add-admin' into change_password

parents 708cd26a 2b5b5a67
......@@ -14,6 +14,7 @@ pub trait MedalConnection {
fn save_session(&self, session: SessionUser);
fn get_session_or_new(&self, key: &str) -> SessionUser;
fn get_user_by_id(&self, user_id: u32) -> Option<SessionUser>;
fn get_user_and_group_by_id(&self, user_id: u32) -> Option<(SessionUser, Option<Group>)>;
//fn login(&self, session: &SessionUser, username: String, password: String) -> Result<String,()>;
......
......@@ -110,17 +110,18 @@ impl MedalConnection for Connection {
self.execute("UPDATE session_user SET
username = ?1,
password = ?2,
logincode = ?3,
firstname = ?4,
lastname = ?5,
grade = ?6 WHERE id = ?", &[&session.username, &session.password, &session.logincode, &session.firstname, &session.lastname, &session.grade, &session.id]).unwrap();
salt = ?3,
logincode = ?4,
firstname = ?5,
lastname = ?6,
grade = ?7 WHERE id = ?", &[&session.username, &session.password, &session.salt, &session.logincode, &session.firstname, &session.lastname, &session.grade, &session.id]).unwrap();
}
fn new_session(&self) -> SessionUser {
let session_token = "123".to_string();
let csrf_token = "123".to_string();
self.execute("INSERT INTO session_user (session_token, csrf_token)
VALUES (?1, ?2)",
self.execute("INSERT INTO session_user (session_token, csrf_token, permanent_login, is_teacher)
VALUES (?1, ?2, 0, 0)",
&[&session_token, &csrf_token]).unwrap();
let id = self.query_row("SELECT last_insert_rowid()", &[], |row| {row.get(0)}).unwrap();
......@@ -130,7 +131,7 @@ impl MedalConnection for Connection {
self.get_session(&key).unwrap_or_else(|| self.new_session())
}
fn get_user_and_group_by_id(&self, user_id: u32) -> Option<(SessionUser, Option<Group>)> {
fn get_user_by_id(&self, user_id: u32) -> Option<SessionUser> {
let res = self.query_row("SELECT session_token, csrf_token, last_login, last_activity, permanent_login, username, password, logincode, email, email_unconfirmed, email_confirmationcode, firstname, lastname, street, zip, city, nation, grade, is_teacher, managed_by, pms_id, pms_school_id FROM session_user WHERE id = ?1", &[&user_id], |row| {
SessionUser {
id: user_id,
......@@ -162,10 +163,12 @@ impl MedalConnection for Connection {
pms_school_id: row.get(21),
}
});
let session = match res {
Ok(session) => session,
_ => return None
};
res.ok()
}
fn get_user_and_group_by_id(&self, user_id: u32) -> Option<(SessionUser, Option<Group>)> {
let session = self.get_user_by_id(user_id)?;
println!("A");
let group_id = match session.managed_by {
Some(id) => id,
......@@ -850,3 +853,18 @@ impl MedalObject<Connection> for Group {
}
}
}
pub trait SetPassword {
fn set_password(&mut self, &str) -> Option<()>;
}
impl SetPassword for SessionUser {
fn set_password(&mut self, password: &str) -> Option<()> {
let salt = "blub";
let hash = hash_password(password, salt);
self.password = Some(hash);
self.salt = Some(salt.into());
Some(())
}
}
......@@ -29,6 +29,7 @@ mod db_conn_sqlite;
mod db_conn;
mod db_objects;
use db_conn_sqlite::SetPassword; // TODO: Refactor, so we don't need to take this from there!
use db_conn::{MedalConnection, MedalObject};
use db_objects::*;
......@@ -150,6 +151,22 @@ fn refresh_all_contests(conn : &mut Connection) {
}
}
fn add_admin_user(conn: &mut Connection) {
if conn.get_user_by_id(1).is_none() {
print!("New Database. Creating new admin user with credentials 'admin':'test' … ");
let mut admin = conn.new_session();
admin.username = Some("admin".into());
match admin.set_password("test") {
None => println!("FAILED! (Password hashing error)"),
_ => {
conn.save_session(admin);
println!("Done");
}
}
}
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
......@@ -171,6 +188,7 @@ fn main() {
println!("Hello, world!");
let contest = conn.get_contest_by_id_complete(1);
add_admin_user(&mut conn);
println!("Contest {}", contest.name);
......
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