Was easier to get a db handle than expected

This commit is contained in:
Alex Wright 2020-03-02 00:57:14 +01:00
parent 9a6bd240ad
commit 7b7775e438
2 changed files with 16 additions and 8 deletions

View File

@ -226,14 +226,16 @@ fn oidc_config() -> Json<OidcConfig> {
impl<'a, 'r> FromRequest<'a, 'r> for User { impl<'a, 'r> FromRequest<'a, 'r> for User {
type Error = (); type Error = ();
fn from_request(_request: &'a Request<'r>) -> Outcome<User, Self::Error> { fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
Outcome::Success(User { let mut user_id = match request.cookies().get_private("user_id") {
id: 1, Some(cookie) => cookie.value().to_string(),
username: "Alex".to_string(), None => return Outcome::Forward(()),
is_active: true, };
created_at: None, let conn = request.guard::<AuthDb>().unwrap();
updated_at: None, match User::get_with_id(&conn, user_id) {
}) Ok(user) => Outcome::Success(user),
_ => Outcome::Forward(()),
}
} }
} }

View File

@ -25,6 +25,12 @@ pub struct NewUser {
} }
impl User { impl User {
pub fn get_with_id(conn: &PgConnection, user_id_str: String) -> QueryResult<User> {
use crate::schema::users::dsl::{ id, users };
let user_id = user_id_str.parse::<i32>().unwrap();
users.filter(id.eq(&user_id)).first::<User>(conn)
}
pub fn get_with_username(conn: &PgConnection, name: String) -> QueryResult<User> { pub fn get_with_username(conn: &PgConnection, name: String) -> QueryResult<User> {
use crate::schema::users::dsl::{ username, users }; use crate::schema::users::dsl::{ username, users };
users.filter(username.eq(&name)).first::<User>(conn) users.filter(username.eq(&name)).first::<User>(conn)