Merge pull request 'remove notes from users (switched to activity)' (#1002) from remove-notes into main
Reviewed-on: #1002
This commit was merged in pull request #1002.
	This commit is contained in:
		| @@ -1,7 +1,7 @@ | ||||
| use std::ops::DerefMut; | ||||
|  | ||||
| use serde::Serialize; | ||||
| use sqlx::{FromRow, Sqlite, SqlitePool, Transaction, sqlite::SqliteQueryResult}; | ||||
| use sqlx::{sqlite::SqliteQueryResult, FromRow, Sqlite, SqlitePool, Transaction}; | ||||
|  | ||||
| use super::user::User; | ||||
|  | ||||
| @@ -86,7 +86,7 @@ GROUP BY family.id;" | ||||
|     } | ||||
|  | ||||
|     pub async fn members(&self, db: &SqlitePool) -> Vec<User> { | ||||
|         sqlx::query_as!(User, "SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token FROM user WHERE family_id = ?", self.id) | ||||
|         sqlx::query_as!(User, "SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token FROM user WHERE family_id = ?", self.id) | ||||
|             .fetch_all(db) | ||||
|             .await | ||||
|             .unwrap() | ||||
|   | ||||
| @@ -23,7 +23,7 @@ impl Rower { | ||||
|         sqlx::query_as!( | ||||
|             User, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
| FROM user | ||||
| WHERE id in (SELECT rower_id FROM rower WHERE logbook_id=?) | ||||
|         ", | ||||
|   | ||||
| @@ -1,21 +1,20 @@ | ||||
| use std::{fmt::Display, ops::DerefMut}; | ||||
|  | ||||
| use argon2::{Argon2, PasswordHasher, password_hash::SaltString}; | ||||
| use argon2::{password_hash::SaltString, Argon2, PasswordHasher}; | ||||
| use chrono::{Datelike, Local, NaiveDate}; | ||||
| use log::info; | ||||
| use rocket::async_trait; | ||||
| use rocket::{ | ||||
|     Request, | ||||
|     http::{Cookie, Status}, | ||||
|     request::{FromRequest, Outcome}, | ||||
|     time::{Duration, OffsetDateTime}, | ||||
|     Request, | ||||
| }; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; | ||||
|  | ||||
| use super::activity::ActivityBuilder; | ||||
| use super::{ | ||||
|     Day, | ||||
|     log::Log, | ||||
|     logbook::Logbook, | ||||
|     mail::Mail, | ||||
| @@ -24,6 +23,7 @@ use super::{ | ||||
|     role::Role, | ||||
|     stat::Stat, | ||||
|     tripdetails::TripDetails, | ||||
|     Day, | ||||
| }; | ||||
| use crate::AMOUNT_DAYS_TO_SHOW_TRIPS_AHEAD; | ||||
| use scheckbuch::ScheckbuchUser; | ||||
| @@ -53,7 +53,6 @@ pub struct User { | ||||
|     pub birthdate: Option<String>, | ||||
|     pub mail: Option<String>, | ||||
|     pub nickname: Option<String>, | ||||
|     pub notes: Option<String>, | ||||
|     pub phone: Option<String>, | ||||
|     pub address: Option<String>, | ||||
|     pub family_id: Option<i64>, | ||||
| @@ -262,7 +261,7 @@ AND r.cluster = 'skill'; | ||||
|         sqlx::query_as!( | ||||
|             Self, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
| FROM user  | ||||
| WHERE id like ? | ||||
|         ", | ||||
| @@ -277,7 +276,7 @@ WHERE id like ? | ||||
|         sqlx::query_as!( | ||||
|             Self, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
| FROM user  | ||||
| WHERE id like ? | ||||
|         ", | ||||
| @@ -294,7 +293,7 @@ WHERE id like ? | ||||
|         sqlx::query_as!( | ||||
|             Self, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
| FROM user  | ||||
| WHERE lower(name)=? | ||||
|         ", | ||||
| @@ -339,7 +338,7 @@ WHERE lower(name)=? | ||||
|     pub async fn all_with_order(db: &SqlitePool, sort: &str, asc: bool) -> Vec<Self> { | ||||
|         let mut query = format!( | ||||
|         " | ||||
|         SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
|         SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
|         FROM user | ||||
|         WHERE deleted = 0 | ||||
|         ORDER BY {} | ||||
| @@ -367,7 +366,7 @@ WHERE lower(name)=? | ||||
|         sqlx::query_as!( | ||||
|             Self, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
| FROM user u | ||||
| JOIN user_role ur ON u.id = ur.user_id | ||||
| WHERE ur.role_id = ? AND deleted = 0 | ||||
| @@ -383,14 +382,14 @@ ORDER BY name; | ||||
|         sqlx::query_as!( | ||||
|             Self, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token FROM user  | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token FROM user  | ||||
| WHERE family_id IS NOT NULL | ||||
| GROUP BY family_id | ||||
|  | ||||
| UNION | ||||
|  | ||||
| -- Select users with a null family_id, without grouping | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token FROM user  | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token FROM user  | ||||
| WHERE family_id IS NULL; | ||||
|         " | ||||
|         ) | ||||
| @@ -408,7 +407,7 @@ WHERE family_id IS NULL; | ||||
|         sqlx::query_as!( | ||||
|             Self, | ||||
|             " | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token | ||||
| SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, phone, address, family_id, user_token | ||||
| FROM user | ||||
| WHERE deleted = 0 AND (SELECT COUNT(*) FROM user_role WHERE user_id=user.id AND role_id = (SELECT id FROM role WHERE name = 'cox')) > 0 | ||||
| ORDER BY last_access DESC | ||||
| @@ -512,7 +511,7 @@ ASKÖ Ruderverein Donau Linz", self.name), | ||||
|             .save(db) | ||||
|             .await; | ||||
|             return Err(LoginError::InvalidAuthenticationCombo); //User existed sometime ago; has | ||||
|             //been deleted | ||||
|                                                                 //been deleted | ||||
|         } | ||||
|  | ||||
|         if let Some(user_pw) = user.pw.as_ref() { | ||||
| @@ -618,9 +617,9 @@ ASKÖ Ruderverein Donau Linz", self.name), | ||||
|     pub(crate) async fn amount_days_to_show(&self, db: &SqlitePool) -> i64 { | ||||
|         if self.allowed_to_steer(db).await { | ||||
|             let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap(); //Ok, | ||||
|             //december | ||||
|             //has 31 | ||||
|             //days | ||||
|                                                                                              //december | ||||
|                                                                                              //has 31 | ||||
|                                                                                              //days | ||||
|             let days_left_in_year = end_of_year | ||||
|                 .signed_duration_since(Local::now().date_naive()) | ||||
|                 .num_days() | ||||
| @@ -629,9 +628,9 @@ ASKÖ Ruderverein Donau Linz", self.name), | ||||
|             if days_left_in_year <= 31 { | ||||
|                 let end_of_next_year = | ||||
|                     NaiveDate::from_ymd_opt(Local::now().year() + 1, 12, 31).unwrap(); //Ok, | ||||
|                 //december | ||||
|                 //has 31 | ||||
|                 //days | ||||
|                                                                                        //december | ||||
|                                                                                        //has 31 | ||||
|                                                                                        //days | ||||
|                 end_of_next_year | ||||
|                     .signed_duration_since(Local::now().date_naive()) | ||||
|                     .num_days() | ||||
| @@ -863,8 +862,8 @@ special_user!(SteeringUser, +"cox", +"Bootsführer"); | ||||
| special_user!(AdminUser, +"admin"); | ||||
| special_user!(AllowedForPlannedTripsUser, +"Donau Linz", +"scheckbuch", +"Förderndes Mitglied"); | ||||
| special_user!(DonauLinzUser, +"Donau Linz", -"Unterstützend", -"Förderndes Mitglied"); // TODO: | ||||
| // remove -> | ||||
| // RegularUser | ||||
|                                                                                        // remove -> | ||||
|                                                                                        // RegularUser | ||||
| special_user!(SchnupperBetreuerUser, +"schnupper-betreuer"); | ||||
| special_user!(VorstandUser, +"admin", +"Vorstand"); | ||||
| special_user!(EventUser, +"manage_events"); | ||||
| @@ -978,21 +977,17 @@ mod test { | ||||
|     #[sqlx::test] | ||||
|     fn wrong_pw() { | ||||
|         let pool = testdb!(); | ||||
|         assert!( | ||||
|             User::login(&pool, "admin".into(), "admi".into()) | ||||
|                 .await | ||||
|                 .is_err() | ||||
|         ); | ||||
|         assert!(User::login(&pool, "admin".into(), "admi".into()) | ||||
|             .await | ||||
|             .is_err()); | ||||
|     } | ||||
|  | ||||
|     #[sqlx::test] | ||||
|     fn wrong_username() { | ||||
|         let pool = testdb!(); | ||||
|         assert!( | ||||
|             User::login(&pool, "admi".into(), "admin".into()) | ||||
|                 .await | ||||
|                 .is_err() | ||||
|         ); | ||||
|         assert!(User::login(&pool, "admi".into(), "admin".into()) | ||||
|             .await | ||||
|             .is_err()); | ||||
|     } | ||||
|  | ||||
|     #[sqlx::test] | ||||
| @@ -1011,11 +1006,9 @@ mod test { | ||||
|         let pool = testdb!(); | ||||
|         let user = User::find_by_id(&pool, 1).await.unwrap(); | ||||
|  | ||||
|         assert!( | ||||
|             User::login(&pool, "admin".into(), "abc".into()) | ||||
|                 .await | ||||
|                 .is_err() | ||||
|         ); | ||||
|         assert!(User::login(&pool, "admin".into(), "abc".into()) | ||||
|             .await | ||||
|             .is_err()); | ||||
|  | ||||
|         user.update_pw(&pool, "abc".into()).await; | ||||
|  | ||||
|   | ||||
| @@ -3,3 +3,46 @@ INSERT INTO user(name) VALUES('Marie'); | ||||
| INSERT INTO "user_role" (user_id, role_id) VALUES((SELECT id from user where name = 'Marie'),(SELECT id FROM role where name = 'Donau Linz')); | ||||
| INSERT INTO user(name) VALUES('Philipp'); | ||||
| INSERT INTO "user_role" (user_id, role_id) VALUES((SELECT id from user where name = 'Philipp'),(SELECT id FROM role where name = 'Donau Linz')); | ||||
|  | ||||
|  | ||||
| -- Step 1: Create a new table without the 'notes' column | ||||
| CREATE TABLE "user_new" ( | ||||
|     "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, | ||||
|     "name" text NOT NULL UNIQUE, | ||||
|     "pw" text,  | ||||
|     "deleted" boolean NOT NULL DEFAULT FALSE, | ||||
|     "last_access" DATETIME, | ||||
|     "dob" text, | ||||
|     "weight" text, | ||||
|     "sex" text, | ||||
|     "dirty_thirty" text, | ||||
|     "dirty_dozen" text, | ||||
|     "member_since_date" text, | ||||
|     "birthdate" text, | ||||
|     "mail" text, | ||||
|     "nickname" text, | ||||
|     "phone" text, | ||||
|     "address" text, | ||||
|     "family_id" INTEGER REFERENCES family(id), | ||||
|     "membership_pdf" BLOB, | ||||
|     "user_token" TEXT NOT NULL DEFAULT (lower(hex(randomblob(16)))) | ||||
| ); | ||||
|  | ||||
| -- Step 2: Copy data from the old table to the new one (excluding 'notes') | ||||
| INSERT INTO user_new ( | ||||
|     id, name, pw, deleted, last_access, dob, weight, sex,  | ||||
|     dirty_thirty, dirty_dozen, member_since_date, birthdate,  | ||||
|     mail, nickname, phone, address, family_id, membership_pdf, user_token | ||||
| ) | ||||
| SELECT  | ||||
|     id, name, pw, deleted, last_access, dob, weight, sex,  | ||||
|     dirty_thirty, dirty_dozen, member_since_date, birthdate,  | ||||
|     mail, nickname, phone, address, family_id, membership_pdf, user_token | ||||
| FROM user; | ||||
|  | ||||
| -- Step 3: Drop the old table | ||||
| DROP TABLE user; | ||||
|  | ||||
| -- Step 4: Rename the new table to the original name | ||||
| ALTER TABLE user_new RENAME TO user; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user