replace username by email

This commit is contained in:
Hannaeko 2023-02-22 13:52:12 +01:00
parent 515cc06dec
commit 473daa532a
6 changed files with 11 additions and 15 deletions

View file

@ -1,7 +1,7 @@
-- Your SQL goes here -- Your SQL goes here
CREATE TABLE localuser ( CREATE TABLE localuser (
`user_id` VARCHAR NOT NULL PRIMARY KEY, `user_id` VARCHAR NOT NULL PRIMARY KEY,
`username` VARCHAR NOT NULL UNIQUE, `email` VARCHAR NOT NULL UNIQUE,
`password` VARCHAR NOT NULL, `password` VARCHAR NOT NULL,
`role` TEXT CHECK(role IN ('admin', 'zoneadmin')) NOT NULL, -- note: migrate to postgres so enum are actually a thing `role` TEXT CHECK(role IN ('admin', 'zoneadmin')) NOT NULL, -- note: migrate to postgres so enum are actually a thing
FOREIGN KEY(user_id) REFERENCES user(id) FOREIGN KEY(user_id) REFERENCES user(id)

View file

@ -17,8 +17,6 @@ pub enum UserCommand {
#[derive(Parser)] #[derive(Parser)]
pub struct AddUserCommand { pub struct AddUserCommand {
#[clap(long = "--name", short = 'n')]
pub name: String,
#[clap(long = "--email", short = 'e')] #[clap(long = "--email", short = 'e')]
pub email: String, pub email: String,
#[clap(long = "--is-admin", short = 'a')] #[clap(long = "--is-admin", short = 'a')]
@ -39,7 +37,6 @@ impl NomiloCommand for UserCommand {
impl NomiloCommand for AddUserCommand { impl NomiloCommand for AddUserCommand {
fn run(self, figment: Figment, _app_config: Config) { fn run(self, figment: Figment, _app_config: Config) {
let res = LocalUser::create_user(&get_db_conn(&figment), CreateUserRequest { let res = LocalUser::create_user(&get_db_conn(&figment), CreateUserRequest {
username: self.name,
email: self.email, email: self.email,
role: Some(if self.is_admin { Role::Admin } else { Role::ZoneAdmin }), role: Some(if self.is_admin { Role::Admin } else { Role::ZoneAdmin }),
password: self.password.unwrap(), password: self.password.unwrap(),

View file

@ -17,7 +17,7 @@ pub async fn do_login(
let session = conn.run(move |c| { let session = conn.run(move |c| {
let user_info = models::LocalUser::get_user_by_creds( let user_info = models::LocalUser::get_user_by_creds(
c, c,
&auth_request.username, &auth_request.email,
&auth_request.password &auth_request.password
)?; )?;

View file

@ -22,7 +22,7 @@ pub const COOKIE_NAME: &str = "session_id";
#[derive(Debug, Deserialize, FromForm)] #[derive(Debug, Deserialize, FromForm)]
pub struct AuthTokenRequest { pub struct AuthTokenRequest {
pub username: String, pub email: String,
pub password: String, pub password: String,
} }

View file

@ -40,7 +40,7 @@ pub struct User {
#[primary_key(user_id)] #[primary_key(user_id)]
pub struct LocalUser { pub struct LocalUser {
pub user_id: String, pub user_id: String,
pub username: String, pub email: String,
pub password: String, pub password: String,
pub role: Role, pub role: Role,
} }
@ -55,7 +55,6 @@ pub struct UserZone {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct CreateUserRequest { pub struct CreateUserRequest {
pub username: String,
pub password: String, pub password: String,
pub email: String, pub email: String,
pub role: Option<Role> pub role: Option<Role>
@ -159,7 +158,7 @@ impl LocalUser {
let new_localuser = LocalUser { let new_localuser = LocalUser {
user_id: new_user_id, user_id: new_user_id,
username: user_request.username.clone(), email: user_request.email.clone(),
password: LocalUser::hash_password(&user_request.password), password: LocalUser::hash_password(&user_request.password),
role: if let Some(user_role) = user_request.role { user_role } else { Role::ZoneAdmin }, role: if let Some(user_role) = user_request.role { user_role } else { Role::ZoneAdmin },
}; };
@ -167,7 +166,7 @@ impl LocalUser {
let res = UserInfo { let res = UserInfo {
id: new_user.id.clone(), id: new_user.id.clone(),
role: new_localuser.role.clone(), role: new_localuser.role.clone(),
username: new_localuser.username.clone(), username: new_localuser.email.clone(),
}; };
conn.immediate_transaction(|| -> diesel::QueryResult<()> { conn.immediate_transaction(|| -> diesel::QueryResult<()> {
@ -190,7 +189,7 @@ impl LocalUser {
pub fn get_user_by_creds( pub fn get_user_by_creds(
conn: &diesel::SqliteConnection, conn: &diesel::SqliteConnection,
request_username: &str, request_email: &str,
request_password: &str request_password: &str
) -> Result<UserInfo, UserError> { ) -> Result<UserInfo, UserError> {
@ -198,7 +197,7 @@ impl LocalUser {
use crate::schema::user::dsl::*; use crate::schema::user::dsl::*;
let (client_user, client_localuser): (User, LocalUser) = user.inner_join(localuser) let (client_user, client_localuser): (User, LocalUser) = user.inner_join(localuser)
.filter(username.eq(request_username)) .filter(email.eq(request_email))
.get_result(conn) .get_result(conn)
.map_err(|e| match e { .map_err(|e| match e {
DieselError::NotFound => UserError::BadCreds, DieselError::NotFound => UserError::BadCreds,
@ -212,7 +211,7 @@ impl LocalUser {
Ok(UserInfo { Ok(UserInfo {
id: client_user.id, id: client_user.id,
role: client_localuser.role, role: client_localuser.role,
username: client_localuser.username, username: client_localuser.email,
}) })
} }
@ -231,7 +230,7 @@ impl LocalUser {
Ok(UserInfo { Ok(UserInfo {
id: client_user.id, id: client_user.id,
role: client_localuser.role, role: client_localuser.role,
username: client_localuser.username, username: client_localuser.email,
}) })
} }
} }

View file

@ -4,7 +4,7 @@ table! {
localuser (user_id) { localuser (user_id) {
user_id -> Text, user_id -> Text,
username -> Text, email -> Text,
password -> Text, password -> Text,
role -> RoleMapping, role -> RoleMapping,
} }