refactor
All checks were successful
ci/woodpecker/push/test Pipeline was successful

This commit is contained in:
JMARyA 2024-12-29 20:39:10 +01:00
parent 3837302161
commit 439467f730
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
10 changed files with 45 additions and 32 deletions

View file

@ -24,6 +24,7 @@ pub struct Session {
pub kind: SessionKind,
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "session_kind", rename_all = "lowercase")]
pub enum SessionKind {
@ -59,7 +60,7 @@ impl Sessions for User {
}
/// End a user session
async fn end_session(&self) -> () {
async fn end_session(&self) {
sqlx::query("DELETE FROM user_session WHERE token = $1")
.bind(&self.session)
.execute(get_pg!())

View file

@ -81,6 +81,7 @@ impl User {
/// Change the password of a User
///
/// Returns a Result indicating whether the password change was successful or not
#[must_use]
pub async fn passwd(self, old: &str, new: &str) -> Result<(), ()> {
if self.verify_pw(old) {
sqlx::query("UPDATE users SET \"password\" = $1 WHERE username = $2;")
@ -97,6 +98,7 @@ impl User {
}
/// Find all users in the system
#[must_use]
pub async fn find_all() -> Vec<Self> {
sqlx::query_as("SELECT * FROM users")
.fetch_all(get_pg!())
@ -105,6 +107,7 @@ impl User {
}
/// Check if the user is an admin
#[must_use]
pub const fn is_admin(&self) -> bool {
matches!(self.user_role, UserRole::Admin)
}
@ -112,6 +115,7 @@ impl User {
/// Verify that a provided password matches the hashed password for the user
///
/// Returns a boolean indicating whether the passwords match or not
#[must_use]
pub fn verify_pw(&self, password: &str) -> bool {
bcrypt::verify(password, &self.password).unwrap()
}
@ -127,13 +131,12 @@ impl ToAPI for User {
}
/// extracts a user from a request with `session` cookie
async fn extract_user<'r>(request: &'r Request<'_>) -> Option<User> {
async fn extract_user(request: &Request<'_>) -> Option<User> {
if let Some(session_id) = request.cookies().get("session") {
if let Some(user) = User::from_session(session_id.value()).await {
return Some(user);
} else {
return None;
}
return None;
}
None
@ -146,9 +149,8 @@ impl<'r> FromRequest<'r> for User {
async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
if let Some(user) = extract_user(request).await {
return Outcome::Success(user);
} else {
return Outcome::Error((Status::Unauthorized, ()));
}
Outcome::Error((Status::Unauthorized, ()))
}
}
@ -164,9 +166,8 @@ impl<'r> FromRequest<'r> for APIUser {
Some(key) => {
if let Some(user) = User::from_session(key).await {
return Outcome::Success(APIUser(user));
} else {
return Outcome::Error((Status::Unauthorized, ()));
}
return Outcome::Error((Status::Unauthorized, ()));
}
None => Outcome::Error((Status::Unauthorized, ())),
}
@ -202,9 +203,9 @@ impl<'r> FromRequest<'r> for MaybeUser {
async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
if let Some(user) = extract_user(request).await {
return Outcome::Success(MaybeUser::User(user));
} else {
return Outcome::Success(MaybeUser::Anonymous);
}
Outcome::Success(MaybeUser::Anonymous)
}
}
@ -215,13 +216,15 @@ impl From<MaybeUser> for Option<User> {
}
impl MaybeUser {
pub fn user(&self) -> Option<&User> {
#[must_use]
pub const fn user(&self) -> Option<&User> {
match self {
MaybeUser::User(user) => Some(user),
MaybeUser::Anonymous => None,
}
}
#[must_use]
pub fn take_user(self) -> Option<User> {
match self {
MaybeUser::User(user) => Some(user),
@ -255,8 +258,8 @@ impl<'r> FromRequest<'r> for AdminUser {
if user.is_admin() {
return Outcome::Success(AdminUser(user));
}
} else {
}
Outcome::Error((Status::Unauthorized, ()))
}
}