diff --git a/src/main.rs b/src/main.rs index 1a71bec..83e7829 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,7 +75,7 @@ async fn account(db: Connection, cookies: &CookieJar<'_>) -> String { match token { Some(t) => { match User::get_by_token(db, t.to_string().split('=').collect::>()[1].to_string() /*GOD I LOVE RUST*/).await { - Some(user) => format!("Username: {}", user.username), + Some(user) => format!("Username: {}\nAdmin: {}\nMake Posts: {}\nComment: {}", user.username, user.admin, user.make_posts, user.comment), None => "User doesn't exist.".to_string() } }, @@ -183,6 +183,14 @@ async fn migrate(rocket: Rocket) -> Rocket { )) .await; + let _ = conn.fetch_one(sqlx::query( + "ALTER TABLE users + ADD COLUMN IF NOT EXISTS admin BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS make_posts BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS comment BOOLEAN NOT NULL DEFAULT TRUE + " + )).await; + match conn.fetch_one(sqlx::query("SELECT * from users WHERE username = $1").bind(std::env::var("ADMIN_USERNAME").expect("make ADMIN_USERNAME env var"))).await { Ok(_) => (), Err(_) => { @@ -194,15 +202,9 @@ async fn migrate(rocket: Rocket) -> Rocket { eprintln!("{username}\n{password}"); conn.fetch_one(sqlx::query( - - - - - - r#" - INSERT INTO users (username, password) - VALUES ($1, $2); + INSERT INTO users (username, password, admin) + VALUES ($1, $2, true); "#, ) .bind(username) diff --git a/src/tables.rs b/src/tables.rs index 98329bd..1256431 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -60,6 +60,14 @@ pub struct User { pub username: String, pub password: String, pub token: Option, + pub admin: bool, + pub make_posts: bool, + pub comment: bool, +} +pub enum UserRole { + Admin, + MakePosts, + Comment, } impl User { pub async fn create(mut db: Connection, username: &String, password: &String) { @@ -92,6 +100,9 @@ impl User { username: user.get::("username"), password: user.get::("password"), token: user.get::, _>("token"), + admin: user.get::("admin"), + make_posts: user.get::("make_posts"), + comment: user.get::("comment"), }), Err(_) => None, } @@ -106,6 +117,9 @@ impl User { username: user.get::("username"), password: user.get::("password"), token: user.get::, _>("token"), + admin: user.get::("admin"), + make_posts: user.get::("make_posts"), + comment: user.get::("comment"), }), Err(_) => None, } @@ -120,6 +134,9 @@ impl User { username: user.get::("username"), password: user.get::("password"), token: user.get::, _>("token"), + admin: user.get::("admin"), + make_posts: user.get::("make_posts"), + comment: user.get::("comment"), }), Err(_) => None, } @@ -143,8 +160,22 @@ impl User { } } - // god i fucking love rust - pub fn exists(&self) -> bool { - true + pub async fn set_role(&self, mut db: Connection, role: UserRole, value: bool) -> Result { + match db + .fetch_one( + sqlx::query("UPDATE users SET $1 = $2 WHERE id = $3") + .bind(match role { + UserRole::Admin => "admin", + UserRole::MakePosts => "make_posts", + UserRole::Comment => "comment", + }) + .bind(value) + .bind(self.id), + ) + .await + { + Ok(_) => Ok("Succesfully updated role.".to_string()), + Err(why) => Err(why.to_string()), + } } }