diff options
Diffstat (limited to '')
-rw-r--r-- | .sqlx/query-02f9efc3c66ab999a3ed07f6637086f5cac7b06f1ad459eb688b85200495045e.json | 15 | ||||
-rw-r--r-- | .sqlx/query-225521d5c1c07dd1be7873b7589f3d9a9789b2a3b099d98753a2997bc046e99e.json | 28 | ||||
-rw-r--r-- | .sqlx/query-9febe1398aa650b2d39688ebfc2c7af9ebadaf12e2a7069ee6f2cd5c07a8e0bf.json | 75 | ||||
-rw-r--r-- | .sqlx/query-c84004689d98d72b2f6d0d3bf67ec59e81334fd4e49e62ddd56bde4f30c1200d.json (renamed from .sqlx/query-7609165d94c8f1bea9d535b9b7ad727fd06592973d7f83017292d41acb203be6.json) | 10 | ||||
-rw-r--r-- | .sqlx/query-c9482dda19cb67fab64db269b2a993a7b12da41c392fbf8f66c29a1c08531327.json | 22 | ||||
-rw-r--r-- | .sqlx/query-f5dd8c96f94c54363097d61fde7344425f491da5e9ca9f7d6df9726fd457582a.json | 32 | ||||
-rw-r--r-- | Cargo.lock | 15 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | migrations/20231003193749_pinussy.sql | 43 | ||||
-rw-r--r-- | src/db/users.rs | 21 | ||||
-rw-r--r-- | src/routes/home.rs | 3 | ||||
-rw-r--r-- | src/routes/login.rs | 3 | ||||
-rw-r--r-- | src/users.rs | 4 |
13 files changed, 165 insertions, 109 deletions
diff --git a/.sqlx/query-02f9efc3c66ab999a3ed07f6637086f5cac7b06f1ad459eb688b85200495045e.json b/.sqlx/query-02f9efc3c66ab999a3ed07f6637086f5cac7b06f1ad459eb688b85200495045e.json deleted file mode 100644 index 3875b63..0000000 --- a/.sqlx/query-02f9efc3c66ab999a3ed07f6637086f5cac7b06f1ad459eb688b85200495045e.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "insert into users(username, password) values ($1, $2)", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Varchar", - "Varchar" - ] - }, - "nullable": [] - }, - "hash": "02f9efc3c66ab999a3ed07f6637086f5cac7b06f1ad459eb688b85200495045e" -} diff --git a/.sqlx/query-225521d5c1c07dd1be7873b7589f3d9a9789b2a3b099d98753a2997bc046e99e.json b/.sqlx/query-225521d5c1c07dd1be7873b7589f3d9a9789b2a3b099d98753a2997bc046e99e.json deleted file mode 100644 index 5cb27e4..0000000 --- a/.sqlx/query-225521d5c1c07dd1be7873b7589f3d9a9789b2a3b099d98753a2997bc046e99e.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "select id, password from users where username = $1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int4" - }, - { - "ordinal": 1, - "name": "password", - "type_info": "Varchar" - } - ], - "parameters": { - "Left": [ - "Text" - ] - }, - "nullable": [ - false, - false - ] - }, - "hash": "225521d5c1c07dd1be7873b7589f3d9a9789b2a3b099d98753a2997bc046e99e" -} diff --git a/.sqlx/query-9febe1398aa650b2d39688ebfc2c7af9ebadaf12e2a7069ee6f2cd5c07a8e0bf.json b/.sqlx/query-9febe1398aa650b2d39688ebfc2c7af9ebadaf12e2a7069ee6f2cd5c07a8e0bf.json new file mode 100644 index 0000000..535c9c3 --- /dev/null +++ b/.sqlx/query-9febe1398aa650b2d39688ebfc2c7af9ebadaf12e2a7069ee6f2cd5c07a8e0bf.json @@ -0,0 +1,75 @@ +{ + "db_name": "PostgreSQL", + "query": "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where username = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Uuid" + }, + { + "ordinal": 1, + "name": "username", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "password", + "type_info": "Varchar" + }, + { + "ordinal": 3, + "name": "email", + "type_info": "Varchar" + }, + { + "ordinal": 4, + "name": "bio", + "type_info": "Text" + }, + { + "ordinal": 5, + "name": "site", + "type_info": "Varchar" + }, + { + "ordinal": 6, + "name": "privacy: _", + "type_info": { + "Custom": { + "name": "privacy", + "kind": { + "Enum": [ + "private", + "unlisted", + "public" + ] + } + } + } + }, + { + "ordinal": 7, + "name": "admin", + "type_info": "Bool" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + false, + false, + false, + true, + true, + true, + false, + false + ] + }, + "hash": "9febe1398aa650b2d39688ebfc2c7af9ebadaf12e2a7069ee6f2cd5c07a8e0bf" +} diff --git a/.sqlx/query-7609165d94c8f1bea9d535b9b7ad727fd06592973d7f83017292d41acb203be6.json b/.sqlx/query-c84004689d98d72b2f6d0d3bf67ec59e81334fd4e49e62ddd56bde4f30c1200d.json index 85dc959..bf7c9ad 100644 --- a/.sqlx/query-7609165d94c8f1bea9d535b9b7ad727fd06592973d7f83017292d41acb203be6.json +++ b/.sqlx/query-c84004689d98d72b2f6d0d3bf67ec59e81334fd4e49e62ddd56bde4f30c1200d.json @@ -1,12 +1,12 @@ { "db_name": "PostgreSQL", - "query": "select * from users where id = $1", + "query": "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where id = $1", "describe": { "columns": [ { "ordinal": 0, "name": "id", - "type_info": "Int4" + "type_info": "Uuid" }, { "ordinal": 1, @@ -35,7 +35,7 @@ }, { "ordinal": 6, - "name": "privacy", + "name": "privacy: _", "type_info": { "Custom": { "name": "privacy", @@ -57,7 +57,7 @@ ], "parameters": { "Left": [ - "Int4" + "Uuid" ] }, "nullable": [ @@ -71,5 +71,5 @@ false ] }, - "hash": "7609165d94c8f1bea9d535b9b7ad727fd06592973d7f83017292d41acb203be6" + "hash": "c84004689d98d72b2f6d0d3bf67ec59e81334fd4e49e62ddd56bde4f30c1200d" } diff --git a/.sqlx/query-c9482dda19cb67fab64db269b2a993a7b12da41c392fbf8f66c29a1c08531327.json b/.sqlx/query-c9482dda19cb67fab64db269b2a993a7b12da41c392fbf8f66c29a1c08531327.json deleted file mode 100644 index 32952b5..0000000 --- a/.sqlx/query-c9482dda19cb67fab64db269b2a993a7b12da41c392fbf8f66c29a1c08531327.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "select username from users where id = $1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "username", - "type_info": "Varchar" - } - ], - "parameters": { - "Left": [ - "Int4" - ] - }, - "nullable": [ - false - ] - }, - "hash": "c9482dda19cb67fab64db269b2a993a7b12da41c392fbf8f66c29a1c08531327" -} diff --git a/.sqlx/query-f5dd8c96f94c54363097d61fde7344425f491da5e9ca9f7d6df9726fd457582a.json b/.sqlx/query-f5dd8c96f94c54363097d61fde7344425f491da5e9ca9f7d6df9726fd457582a.json new file mode 100644 index 0000000..849a6cc --- /dev/null +++ b/.sqlx/query-f5dd8c96f94c54363097d61fde7344425f491da5e9ca9f7d6df9726fd457582a.json @@ -0,0 +1,32 @@ +{ + "db_name": "PostgreSQL", + "query": "insert into users (id, username, password, email, bio, site, privacy, admin) values ($1, $2, $3, $4, $5, $6, $7, $8)", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Uuid", + "Varchar", + "Varchar", + "Varchar", + "Text", + "Varchar", + { + "Custom": { + "name": "privacy", + "kind": { + "Enum": [ + "private", + "unlisted", + "public" + ] + } + } + }, + "Bool" + ] + }, + "nullable": [] + }, + "hash": "f5dd8c96f94c54363097d61fde7344425f491da5e9ca9f7d6df9726fd457582a" +} @@ -1724,6 +1724,7 @@ dependencies = [ "serde", "sqlx", "tracing", + "uuid", "web-archive", ] @@ -2333,6 +2334,7 @@ dependencies = [ "tokio-stream", "tracing", "url", + "uuid", ] [[package]] @@ -2413,6 +2415,7 @@ dependencies = [ "stringprep", "thiserror", "tracing", + "uuid", "whoami", ] @@ -2452,6 +2455,7 @@ dependencies = [ "stringprep", "thiserror", "tracing", + "uuid", "whoami", ] @@ -2475,6 +2479,7 @@ dependencies = [ "sqlx-core", "tracing", "url", + "uuid", ] [[package]] @@ -2827,6 +2832,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] +name = "uuid" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +dependencies = [ + "getrandom 0.2.10", + "serde", +] + +[[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -15,6 +15,7 @@ actix-web = "4.4.0" bcrypt = "0.15.0" mime = "0.3.17" serde = "1.0.188" -sqlx = { version = "0.7.2", features = ["postgres", "runtime-tokio"] } +sqlx = { version = "0.7.2", features = ["uuid", "postgres", "runtime-tokio"] } tracing = "0.1.37" +uuid = { version = "1.6.1", features = ["v4", "serde"] } web-archive = "0.2.0" diff --git a/migrations/20231003193749_pinussy.sql b/migrations/20231003193749_pinussy.sql index cba545e..4ac7474 100644 --- a/migrations/20231003193749_pinussy.sql +++ b/migrations/20231003193749_pinussy.sql @@ -1,8 +1,9 @@ create type privacy as enum ('private', 'unlisted', 'public'); create type file_type as enum ('image', 'video', 'audio', 'text', 'document', 'site', 'other'); +create extension if not exists "uuid-ossp"; create table users ( - id integer primary key generated always as identity, + id uuid primary key default gen_random_uuid(), username varchar(32) not null, unique(username), password varchar(128) not null, @@ -13,15 +14,15 @@ create table users ( admin boolean not null default true ); -create table sessions ( - id varchar(128) primary key, - expires timestamp with time zone, - user_id integer not null, - foreign key (user_id) references users(id) -); +-- create table sessions ( +-- id varchar(128) primary key, +-- expires timestamp with time zone, +-- user_id integer not null, +-- foreign key (user_id) references users(id) +-- ); create table boards ( - id integer primary key generated always as identity, + id uuid primary key default gen_random_uuid(), idname varchar(256), name varchar(256), description text, @@ -29,15 +30,15 @@ create table boards ( ); create table board_ownership ( - board_id integer not null, - user_id integer not null, + board_id uuid not null, + user_id uuid not null, foreign key (board_id) references boards(id), foreign key (user_id) references users(id), primary key (board_id, user_id) ); create table pins ( - id integer primary key generated always as identity, + id uuid primary key default gen_random_uuid(), idname varchar(256), subject varchar(256), notes text, @@ -45,23 +46,23 @@ create table pins ( ); create table pin_ownership ( - pin_id integer not null, - user_id integer not null, + pin_id uuid not null, + user_id uuid not null, foreign key (pin_id) references pins(id), foreign key (user_id) references users(id), primary key (pin_id, user_id) ); create table pins_boards ( - pin_id integer not null, - board_id integer not null, + pin_id uuid not null, + board_id uuid not null, foreign key (pin_id) references pins(id), foreign key (board_id) references boards(id), primary key (pin_id, board_id) ); create table sources ( - id integer primary key generated always as identity, + id uuid primary key default gen_random_uuid(), idname varchar(256), title varchar(256), author varchar(256), @@ -72,22 +73,22 @@ create table sources ( ); create table pin_sources ( - source_id integer not null, - pin_id integer not null, + source_id uuid not null, + pin_id uuid not null, foreign key (source_id) references sources(id), foreign key (pin_id) references pins(id), primary key (source_id, pin_id) ); create table files ( - id integer primary key generated always as identity, + id uuid primary key default gen_random_uuid(), idname varchar(256), thumbnail varchar(256), path varchar(256) not null, title varchar(256), type file_type, alt_text text, - pin_id integer not null, + pin_id uuid not null, foreign key (pin_id) references pins(id) ); @@ -97,7 +98,7 @@ create table tags ( create table tags_pins ( tag varchar(128) not null, - pin_id integer not null, + pin_id uuid not null, foreign key (tag) references tags(tag), foreign key (pin_id) references pins(id), primary key (tag, pin_id) diff --git a/src/db/users.rs b/src/db/users.rs index 0fc7c64..a5759fd 100644 --- a/src/db/users.rs +++ b/src/db/users.rs @@ -1,11 +1,12 @@ use sqlx::{Pool, Postgres}; +use uuid::Uuid; use crate::users::User; use crate::Result; #[derive(Clone)] pub struct Users(Pool<Postgres>); -// code code code code code code code code code code code code code code + impl Users { pub fn new(pool: Pool<Postgres>) -> Self { Self(pool) @@ -13,7 +14,8 @@ impl Users { pub async fn create(&self, user: User) -> Result<()> { sqlx::query!( - r#"insert into users (username, password, email, bio, site, privacy, admin) values ($1, $2, $3, $4, $5, $6, $7)"#, + r#"insert into users (id, username, password, email, bio, site, privacy, admin) values ($1, $2, $3, $4, $5, $6, $7, $8)"#, + user.id, user.username, user.password, user.email, @@ -27,11 +29,11 @@ impl Users { Ok(()) } - pub async fn read(&self, user_id: i32) -> Result<User> { + pub async fn read(&self, user_id: Uuid) -> Result<User> { Ok( sqlx::query_as!( User, - "select username, password, email, bio, site, privacy as \"privacy: _\", admin from users where id = $1", + "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where id = $1", user_id ) .fetch_one(&self.0) @@ -43,7 +45,7 @@ impl Users { Ok( sqlx::query_as!( User, - "select username, password, email, bio, site, privacy as \"privacy: _\", admin from users where username = $1", + "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where username = $1", username ) .fetch_one(&self.0) @@ -51,15 +53,6 @@ impl Users { ) } - pub async fn get_id(&self, username: &str) -> Result<i32> { - Ok( - sqlx::query!("select id from users where username = $1", username) - .fetch_one(&self.0) - .await? - .id, - ) - } - pub async fn read_all(&self) -> Result<Vec<User>> { Ok(sqlx::query_as("select * from users") .fetch_all(&self.0) diff --git a/src/routes/home.rs b/src/routes/home.rs index a43eabc..f2642a5 100644 --- a/src/routes/home.rs +++ b/src/routes/home.rs @@ -1,5 +1,6 @@ use actix_session::Session; use actix_web::{get, web, HttpResponse}; +use uuid::Uuid; use crate::templates; use crate::{Pinussy, Result}; @@ -7,7 +8,7 @@ use crate::{Pinussy, Result}; #[get("/")] async fn get(session: Session, state: web::Data<Pinussy>) -> Result<HttpResponse> { let username: Option<String>; - if let Some(user_id) = session.get::<i32>("user_id")? { + if let Some(user_id) = session.get::<Uuid>("user_id")? { username = Some(state.db.users().read(user_id).await?.username) } else { username = None diff --git a/src/routes/login.rs b/src/routes/login.rs index c6cf077..bd7eaf1 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -30,8 +30,7 @@ async fn post( match state.db.users().read_username(&form.username).await { Ok(user) => { if user.verify_password(&form.password)? { - let user_id = state.db.users().get_id(&form.username).await?; - session.insert("user_id", user_id)?; + session.insert("user_id", user.id)?; return Ok(HttpResponse::SeeOther() .insert_header((LOCATION, "/")) .finish()); diff --git a/src/users.rs b/src/users.rs index 4cf9310..e8b0b67 100644 --- a/src/users.rs +++ b/src/users.rs @@ -1,12 +1,14 @@ use bcrypt::hash; use bcrypt::verify; use bcrypt::DEFAULT_COST; +use uuid::Uuid; use crate::Privacy; use crate::Result; #[derive(sqlx::FromRow)] pub struct User { + pub id: Uuid, pub username: String, pub password: String, pub email: Option<String>, @@ -19,7 +21,9 @@ pub struct User { impl User { pub fn new(username: String, password: String) -> Result<Self> { let password_hash = hash(password, DEFAULT_COST)?; + let id = Uuid::new_v4(); Ok(Self { + id, username, password: password_hash, email: None, |