Commit 435bbd50 authored by Robert Czechowski's avatar Robert Czechowski

OAuth: Set and update sex on OAuth login. Fixes #92

parent a38da699
......@@ -1231,7 +1231,7 @@ pub enum UserType {
Admin,
}
pub enum UserGender {
pub enum UserSex {
Female,
Male,
Unknown,
......@@ -1240,7 +1240,7 @@ pub enum UserGender {
pub struct ForeignUserData {
pub foreign_id: String,
pub foreign_type: UserType,
pub gender: UserGender,
pub sex: UserSex,
pub firstname: String,
pub lastname: String,
}
......@@ -1252,7 +1252,12 @@ pub fn login_oauth<T: MedalConnection>(conn: &T, user_data: ForeignUserData, oau
&user_data.foreign_id,
user_data.foreign_type != UserType::User,
&user_data.firstname,
&user_data.lastname)
&user_data.lastname,
match user_data.sex {
UserSex::Male => Some(1),
UserSex::Female => Some(2),
UserSex::Unknown => Some(0),
})
{
Ok(session_token) => Ok(session_token),
Err(()) => {
......
......@@ -442,7 +442,7 @@ impl MedalConnection for Connection {
//TODO: use session
fn login_foreign(&self, _session: Option<&str>, provider_id: &str, foreign_id: &str, is_teacher: bool,
firstname: &str, lastname: &str)
firstname: &str, lastname: &str, sex: Option<i32>)
-> Result<String, ()>
{
let session_token = helpers::make_session_token();
......@@ -457,9 +457,9 @@ impl MedalConnection for Connection {
Ok(Some(id)) => {
let query = "UPDATE session
SET session_token = $1, csrf_token = $2, last_login = $3, last_activity = $3,
is_teacher = $4, firstname = $5, lastname = $6
WHERE id = $7";
self.execute(query, &[&session_token, &csrf_token, &now, &is_teacher, &firstname, &lastname, &id]).unwrap();
is_teacher = $4, firstname = $5, lastname = $6, sex = $7
WHERE id = $8";
self.execute(query, &[&session_token, &csrf_token, &now, &is_teacher, &firstname, &lastname, &sex, &id]).unwrap();
Ok(session_token)
}
......@@ -475,7 +475,7 @@ impl MedalConnection for Connection {
&now,
&false,
&(if is_teacher { 255 } else { 0 }),
&None::<i32>,
&sex,
&is_teacher,
&foreign_id,
&provider_id,
......
......@@ -33,7 +33,7 @@ pub trait MedalConnection {
fn login(&self, session: Option<&str>, username: &str, password: &str) -> Result<String, ()>;
fn login_with_code(&self, session: Option<&str>, logincode: &str) -> Result<String, ()>;
fn login_foreign(&self, session: Option<&str>, provider_id: &str, foreign_id: &str, is_teacher: bool,
firstname: &str, lastname: &str)
firstname: &str, lastname: &str, sex: Option<i32>)
-> Result<String, ()>;
fn create_user_with_groupcode(&self, session: Option<&str>, groupcode: &str) -> Result<String, ()>;
fn create_group_with_users(&self, group: Group);
......
......@@ -552,7 +552,7 @@ impl MedalConnection for Connection {
//TODO: use session
fn login_foreign(&self, _session: Option<&str>, provider_id: &str, foreign_id: &str, is_teacher: bool,
firstname: &str, lastname: &str)
firstname: &str, lastname: &str, sex: Option<i32>)
-> Result<String, ()>
{
let session_token = helpers::make_session_token();
......@@ -567,9 +567,9 @@ impl MedalConnection for Connection {
Ok(Some(id)) => {
let query = "UPDATE session
SET session_token = $1, csrf_token = $2, last_login = $3, last_activity = $3,
is_teacher = $4, firstname = $5, lastname = $6
WHERE id = $7";
self.execute(query, &[&session_token, &csrf_token, &now, &is_teacher, &firstname, &lastname, &id]).unwrap();
is_teacher = $4, firstname = $5, lastname = $6, sex = $7
WHERE id = $8";
self.execute(query, &[&session_token, &csrf_token, &now, &is_teacher, &firstname, &lastname, &sex, &id]).unwrap();
Ok(session_token)
}
......@@ -585,7 +585,7 @@ impl MedalConnection for Connection {
&now,
&false,
&(if is_teacher { 255 } else { 0 }),
&None::<i32>,
&sex,
&is_teacher,
&foreign_id,
&provider_id,
......
......@@ -552,7 +552,7 @@ impl MedalConnection for Connection {
//TODO: use session
fn login_foreign(&self, _session: Option<&str>, provider_id: &str, foreign_id: &str, is_teacher: bool,
firstname: &str, lastname: &str)
firstname: &str, lastname: &str, sex: Option<i32>)
-> Result<String, ()>
{
let session_token = helpers::make_session_token();
......@@ -567,9 +567,9 @@ impl MedalConnection for Connection {
Ok(Some(id)) => {
let query = "UPDATE session
SET session_token = ?1, csrf_token = ?2, last_login = ?3, last_activity = ?3,
is_teacher = ?4, firstname = ?5, lastname = ?6
WHERE id = ?7";
self.execute(query, &[&session_token, &csrf_token, &now, &is_teacher, &firstname, &lastname, &id]).unwrap();
is_teacher = ?4, firstname = ?5, lastname = ?6, sex = ?7
WHERE id = ?8";
self.execute(query, &[&session_token, &csrf_token, &now, &is_teacher, &firstname, &lastname, &sex, &id]).unwrap();
Ok(session_token)
}
......@@ -585,7 +585,7 @@ impl MedalConnection for Connection {
&now,
&false,
&(if is_teacher { 255 } else { 0 }),
&None::<i32>,
&sex,
&is_teacher,
&foreign_id,
&provider_id,
......
......@@ -1085,7 +1085,7 @@ fn oauth<C>(req: &mut Request) -> IronResult<Response>
user_data.userId_int = Some(id);
}
use core::{UserGender, UserType};
use core::{UserSex, UserType};
let user_data = core::ForeignUserData { foreign_id: user_data.userId_int.unwrap(), // todo: don't unwrap here
foreign_type: match user_data.userType.as_ref() {
......@@ -1094,11 +1094,11 @@ fn oauth<C>(req: &mut Request) -> IronResult<Response>
"s" | "S" => UserType::User,
_ => UserType::User,
},
gender: match user_data.gender.as_ref() {
"m" | "M" => UserGender::Male,
"f" | "F" | "w" | "W" => UserGender::Female,
"?" => UserGender::Unknown,
_ => UserGender::Unknown,
sex: match user_data.gender.as_ref() {
"m" | "M" => UserSex::Male,
"f" | "F" | "w" | "W" => UserSex::Female,
"?" => UserSex::Unknown,
_ => UserSex::Unknown,
},
firstname: user_data.firstName,
lastname: user_data.lastName };
......
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