From 8ee624a5429ab1f95e3d5efdff0b6a1cbfb506f8 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 15 Mar 2021 14:02:27 -0400 Subject: [PATCH] Some changes - Changing claim name to local_user_id to facilitate logout. - Changing AddAdmin back to using person_id --- crates/api/src/lib.rs | 4 ++-- crates/api/src/local_user.rs | 4 ++-- crates/api_structs/src/person.rs | 2 +- crates/db_queries/src/source/local_user.rs | 6 +++--- crates/routes/src/feeds.rs | 9 ++++++--- crates/utils/src/claims.rs | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 65d97c4a..21afcee2 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -103,7 +103,7 @@ pub(crate) async fn get_local_user_view_from_jwt( Ok(claims) => claims.claims, Err(_e) => return Err(ApiError::err("not_logged_in").into()), }; - let local_user_id = claims.id; + let local_user_id = claims.local_user_id; let local_user_view = blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??; // Check for a site ban @@ -131,7 +131,7 @@ pub(crate) async fn get_local_user_settings_view_from_jwt( Ok(claims) => claims.claims, Err(_e) => return Err(ApiError::err("not_logged_in").into()), }; - let local_user_id = claims.id; + let local_user_id = claims.local_user_id; let local_user_view = blocking(pool, move |conn| { LocalUserSettingsView::read(conn, local_user_id) }) diff --git a/crates/api/src/local_user.rs b/crates/api/src/local_user.rs index b3ec9282..50cc028b 100644 --- a/crates/api/src/local_user.rs +++ b/crates/api/src/local_user.rs @@ -650,9 +650,9 @@ impl Perform for AddAdmin { is_admin(&local_user_view)?; let added = data.added; - let added_local_user_id = data.local_user_id; + let added_person_id = data.person_id; let added_admin = match blocking(context.pool(), move |conn| { - LocalUser::add_admin(conn, added_local_user_id, added) + LocalUser::add_admin(conn, added_person_id, added) }) .await? { diff --git a/crates/api_structs/src/person.rs b/crates/api_structs/src/person.rs index 53e6d9f8..5555bb4b 100644 --- a/crates/api_structs/src/person.rs +++ b/crates/api_structs/src/person.rs @@ -107,7 +107,7 @@ pub struct MarkAllAsRead { #[derive(Deserialize)] pub struct AddAdmin { - pub local_user_id: i32, + pub person_id: i32, pub added: bool, pub auth: String, } diff --git a/crates/db_queries/src/source/local_user.rs b/crates/db_queries/src/source/local_user.rs index 4ca557c5..a3d2b85b 100644 --- a/crates/db_queries/src/source/local_user.rs +++ b/crates/db_queries/src/source/local_user.rs @@ -69,7 +69,7 @@ pub trait LocalUser_ { local_user_id: i32, new_password: &str, ) -> Result; - fn add_admin(conn: &PgConnection, local_user_id: i32, added: bool) -> Result; + fn add_admin(conn: &PgConnection, person_id: i32, added: bool) -> Result; } impl LocalUser_ for LocalUser { @@ -94,8 +94,8 @@ impl LocalUser_ for LocalUser { .get_result::(conn) } - fn add_admin(conn: &PgConnection, local_user_id: i32, added: bool) -> Result { - diesel::update(local_user.find(local_user_id)) + fn add_admin(conn: &PgConnection, for_person_id: i32, added: bool) -> Result { + diesel::update(local_user.filter(person_id.eq(for_person_id))) .set(admin.eq(added)) .get_result::(conn) } diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 56810798..30f5f963 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -5,10 +5,11 @@ use diesel::PgConnection; use lemmy_api_structs::blocking; use lemmy_db_queries::{ source::{community::Community_, person::Person_}, + Crud, ListingType, SortType, }; -use lemmy_db_schema::source::{community::Community, person::Person}; +use lemmy_db_schema::source::{community::Community, local_user::LocalUser, person::Person}; use lemmy_db_views::{ comment_view::{CommentQueryBuilder, CommentView}, post_view::{PostQueryBuilder, PostView}, @@ -223,7 +224,8 @@ fn get_feed_front( jwt: String, ) -> Result { let site_view = SiteView::read(&conn)?; - let person_id = Claims::decode(&jwt)?.claims.id; + let local_user_id = Claims::decode(&jwt)?.claims.local_user_id; + let person_id = LocalUser::read(&conn, local_user_id)?.person_id; let posts = PostQueryBuilder::create(&conn) .listing_type(&ListingType::Subscribed) @@ -249,7 +251,8 @@ fn get_feed_front( fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result { let site_view = SiteView::read(&conn)?; - let person_id = Claims::decode(&jwt)?.claims.id; + let local_user_id = Claims::decode(&jwt)?.claims.local_user_id; + let person_id = LocalUser::read(&conn, local_user_id)?.person_id; let sort = SortType::New; diff --git a/crates/utils/src/claims.rs b/crates/utils/src/claims.rs index 3529f5f1..e84f34f9 100644 --- a/crates/utils/src/claims.rs +++ b/crates/utils/src/claims.rs @@ -6,7 +6,7 @@ type Jwt = String; #[derive(Debug, Serialize, Deserialize)] pub struct Claims { - pub id: i32, + pub local_user_id: i32, pub iss: String, } @@ -25,7 +25,7 @@ impl Claims { pub fn jwt(local_user_id: i32, hostname: String) -> Result { let my_claims = Claims { - id: local_user_id, + local_user_id, iss: hostname, }; encode( -- 2.44.1