its half broken but roles are somewhat implemented

This commit is contained in:
SadlyNotSappho 2024-02-06 12:03:00 -08:00
parent 59d0ed12c1
commit d6c418a899
2 changed files with 45 additions and 12 deletions

View File

@ -75,7 +75,7 @@ async fn account(db: Connection<Db>, cookies: &CookieJar<'_>) -> String {
match token { match token {
Some(t) => { Some(t) => {
match User::get_by_token(db, t.to_string().split('=').collect::<Vec<&str>>()[1].to_string() /*GOD I LOVE RUST*/).await { match User::get_by_token(db, t.to_string().split('=').collect::<Vec<&str>>()[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() None => "User doesn't exist.".to_string()
} }
}, },
@ -183,6 +183,14 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
)) ))
.await; .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 { 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(_) => (), Ok(_) => (),
Err(_) => { Err(_) => {
@ -194,15 +202,9 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
eprintln!("{username}\n{password}"); eprintln!("{username}\n{password}");
conn.fetch_one(sqlx::query( conn.fetch_one(sqlx::query(
r#" r#"
INSERT INTO users (username, password) INSERT INTO users (username, password, admin)
VALUES ($1, $2); VALUES ($1, $2, true);
"#, "#,
) )
.bind(username) .bind(username)

View File

@ -60,6 +60,14 @@ pub struct User {
pub username: String, pub username: String,
pub password: String, pub password: String,
pub token: Option<String>, pub token: Option<String>,
pub admin: bool,
pub make_posts: bool,
pub comment: bool,
}
pub enum UserRole {
Admin,
MakePosts,
Comment,
} }
impl User { impl User {
pub async fn create(mut db: Connection<Db>, username: &String, password: &String) { pub async fn create(mut db: Connection<Db>, username: &String, password: &String) {
@ -92,6 +100,9 @@ impl User {
username: user.get::<String, _>("username"), username: user.get::<String, _>("username"),
password: user.get::<String, _>("password"), password: user.get::<String, _>("password"),
token: user.get::<Option<String>, _>("token"), token: user.get::<Option<String>, _>("token"),
admin: user.get::<bool, _>("admin"),
make_posts: user.get::<bool, _>("make_posts"),
comment: user.get::<bool, _>("comment"),
}), }),
Err(_) => None, Err(_) => None,
} }
@ -106,6 +117,9 @@ impl User {
username: user.get::<String, _>("username"), username: user.get::<String, _>("username"),
password: user.get::<String, _>("password"), password: user.get::<String, _>("password"),
token: user.get::<Option<String>, _>("token"), token: user.get::<Option<String>, _>("token"),
admin: user.get::<bool, _>("admin"),
make_posts: user.get::<bool, _>("make_posts"),
comment: user.get::<bool, _>("comment"),
}), }),
Err(_) => None, Err(_) => None,
} }
@ -120,6 +134,9 @@ impl User {
username: user.get::<String, _>("username"), username: user.get::<String, _>("username"),
password: user.get::<String, _>("password"), password: user.get::<String, _>("password"),
token: user.get::<Option<String>, _>("token"), token: user.get::<Option<String>, _>("token"),
admin: user.get::<bool, _>("admin"),
make_posts: user.get::<bool, _>("make_posts"),
comment: user.get::<bool, _>("comment"),
}), }),
Err(_) => None, Err(_) => None,
} }
@ -143,8 +160,22 @@ impl User {
} }
} }
// god i fucking love rust pub async fn set_role(&self, mut db: Connection<Db>, role: UserRole, value: bool) -> Result<String, String> {
pub fn exists(&self) -> bool { match db
true .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()),
}
} }
} }