Moving admin to person table. Fixes #1515

This commit is contained in:
Dessalines 2021-03-22 10:28:00 -04:00
parent c884510173
commit eb45bf2d0a
13 changed files with 44 additions and 24 deletions

View file

@ -79,7 +79,7 @@ pub(crate) async fn is_mod_or_admin(
} }
pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> { pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
if !local_user_view.local_user.admin { if !local_user_view.person.admin {
return Err(ApiError::err("not_an_admin").into()); return Err(ApiError::err("not_an_admin").into());
} }
Ok(()) Ok(())

View file

@ -204,6 +204,7 @@ impl Perform for Register {
public_key: Some(Some(actor_keypair.public_key)), public_key: Some(Some(actor_keypair.public_key)),
inbox_url: Some(generate_inbox_url(&actor_id)?), inbox_url: Some(generate_inbox_url(&actor_id)?),
shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)), shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
admin: Some(no_admins),
..PersonForm::default() ..PersonForm::default()
}; };
@ -224,7 +225,6 @@ impl Perform for Register {
person_id: inserted_person.id, person_id: inserted_person.id,
email: Some(data.email.to_owned()), email: Some(data.email.to_owned()),
password_encrypted: data.password.to_owned(), password_encrypted: data.password.to_owned(),
admin: Some(no_admins),
show_nsfw: Some(data.show_nsfw), show_nsfw: Some(data.show_nsfw),
theme: Some("browser".into()), theme: Some("browser".into()),
default_sort_type: Some(SortType::Active as i16), default_sort_type: Some(SortType::Active as i16),
@ -455,6 +455,7 @@ impl Perform for SaveUserSettings {
actor_id: None, actor_id: None,
bio, bio,
local: None, local: None,
admin: None,
private_key: None, private_key: None,
public_key: None, public_key: None,
last_refreshed_at: None, last_refreshed_at: None,
@ -477,7 +478,6 @@ impl Perform for SaveUserSettings {
person_id, person_id,
email, email,
password_encrypted, password_encrypted,
admin: None,
show_nsfw: data.show_nsfw, show_nsfw: data.show_nsfw,
theme: data.theme.to_owned(), theme: data.theme.to_owned(),
default_sort_type, default_sort_type,
@ -638,7 +638,7 @@ impl Perform for AddAdmin {
let added = data.added; let added = data.added;
let added_person_id = data.person_id; let added_person_id = data.person_id;
let added_admin = match blocking(context.pool(), move |conn| { let added_admin = match blocking(context.pool(), move |conn| {
LocalUser::add_admin(conn, added_person_id, added) Person::add_admin(conn, added_person_id, added)
}) })
.await? .await?
{ {
@ -651,7 +651,7 @@ impl Perform for AddAdmin {
// Mod tables // Mod tables
let form = ModAddForm { let form = ModAddForm {
mod_person_id: local_user_view.person.id, mod_person_id: local_user_view.person.id,
other_person_id: added_admin.person_id, other_person_id: added_admin.id,
removed: Some(!data.added), removed: Some(!data.added),
}; };

View file

@ -182,6 +182,7 @@ impl FromApubToForm<PersonExt> for PersonForm {
actor_id: Some(check_object_domain(person, expected_domain)?), actor_id: Some(check_object_domain(person, expected_domain)?),
bio: Some(bio), bio: Some(bio),
local: Some(false), local: Some(false),
admin: Some(false),
private_key: None, private_key: None,
public_key: Some(Some(person.ext_one.public_key.to_owned().public_key_pem)), public_key: Some(Some(person.ext_one.public_key.to_owned().public_key_pem)),
last_refreshed_at: Some(naive_now()), last_refreshed_at: Some(naive_now()),

View file

@ -6,7 +6,6 @@ use lemmy_db_schema::{
schema::local_user::dsl::*, schema::local_user::dsl::*,
source::local_user::{LocalUser, LocalUserForm}, source::local_user::{LocalUser, LocalUserForm},
LocalUserId, LocalUserId,
PersonId,
}; };
mod safe_settings_type { mod safe_settings_type {
@ -17,7 +16,6 @@ mod safe_settings_type {
id, id,
person_id, person_id,
email, email,
admin,
show_nsfw, show_nsfw,
theme, theme,
default_sort_type, default_sort_type,
@ -37,7 +35,6 @@ mod safe_settings_type {
id, id,
person_id, person_id,
email, email,
admin,
show_nsfw, show_nsfw,
theme, theme,
default_sort_type, default_sort_type,
@ -58,7 +55,6 @@ pub trait LocalUser_ {
local_user_id: LocalUserId, local_user_id: LocalUserId,
new_password: &str, new_password: &str,
) -> Result<LocalUser, Error>; ) -> Result<LocalUser, Error>;
fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<LocalUser, Error>;
} }
impl LocalUser_ for LocalUser { impl LocalUser_ for LocalUser {
@ -85,12 +81,6 @@ impl LocalUser_ for LocalUser {
)) ))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn add_admin(conn: &PgConnection, for_person_id: PersonId, added: bool) -> Result<Self, Error> {
diesel::update(local_user.filter(person_id.eq(for_person_id)))
.set(admin.eq(added))
.get_result::<Self>(conn)
}
} }
impl Crud<LocalUserForm, LocalUserId> for LocalUser { impl Crud<LocalUserForm, LocalUserId> for LocalUser {

View file

@ -28,6 +28,7 @@ mod safe_type {
inbox_url, inbox_url,
shared_inbox_url, shared_inbox_url,
matrix_user_id, matrix_user_id,
admin,
); );
impl ToSafe for Person { impl ToSafe for Person {
@ -49,6 +50,7 @@ mod safe_type {
inbox_url, inbox_url,
shared_inbox_url, shared_inbox_url,
matrix_user_id, matrix_user_id,
admin,
) )
} }
} }
@ -74,6 +76,7 @@ mod safe_type_alias_1 {
inbox_url, inbox_url,
shared_inbox_url, shared_inbox_url,
matrix_user_id, matrix_user_id,
admin,
); );
impl ToSafe for PersonAlias1 { impl ToSafe for PersonAlias1 {
@ -95,6 +98,7 @@ mod safe_type_alias_1 {
inbox_url, inbox_url,
shared_inbox_url, shared_inbox_url,
matrix_user_id, matrix_user_id,
admin,
) )
} }
} }
@ -120,6 +124,7 @@ mod safe_type_alias_2 {
inbox_url, inbox_url,
shared_inbox_url, shared_inbox_url,
matrix_user_id, matrix_user_id,
admin,
); );
impl ToSafe for PersonAlias2 { impl ToSafe for PersonAlias2 {
@ -141,6 +146,7 @@ mod safe_type_alias_2 {
inbox_url, inbox_url,
shared_inbox_url, shared_inbox_url,
matrix_user_id, matrix_user_id,
admin,
) )
} }
} }
@ -187,6 +193,7 @@ impl ApubObject<PersonForm> for Person {
pub trait Person_ { pub trait Person_ {
fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>; fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Person, Error>;
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>; fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>; fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>; fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
@ -199,6 +206,12 @@ impl Person_ for Person {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Self, Error> {
diesel::update(person.find(person_id))
.set(admin.eq(added))
.get_result::<Self>(conn)
}
fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> { fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
person person
.filter(deleted.eq(false)) .filter(deleted.eq(false))
@ -261,6 +274,7 @@ mod tests {
actor_id: inserted_person.actor_id.to_owned(), actor_id: inserted_person.actor_id.to_owned(),
bio: None, bio: None,
local: true, local: true,
admin: false,
private_key: None, private_key: None,
public_key: None, public_key: None,
last_refreshed_at: inserted_person.published, last_refreshed_at: inserted_person.published,

View file

@ -146,7 +146,6 @@ table! {
person_id -> Int4, person_id -> Int4,
password_encrypted -> Text, password_encrypted -> Text,
email -> Nullable<Text>, email -> Nullable<Text>,
admin -> Bool,
show_nsfw -> Bool, show_nsfw -> Bool,
theme -> Varchar, theme -> Varchar,
default_sort_type -> Int2, default_sort_type -> Int2,
@ -287,6 +286,7 @@ table! {
inbox_url -> Varchar, inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>, shared_inbox_url -> Nullable<Varchar>,
matrix_user_id -> Nullable<Text>, matrix_user_id -> Nullable<Text>,
admin -> Bool,
} }
} }
@ -486,6 +486,7 @@ table! {
inbox_url -> Varchar, inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>, shared_inbox_url -> Nullable<Varchar>,
matrix_user_id -> Nullable<Text>, matrix_user_id -> Nullable<Text>,
admin -> Bool,
} }
} }
@ -509,6 +510,7 @@ table! {
inbox_url -> Varchar, inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>, shared_inbox_url -> Nullable<Varchar>,
matrix_user_id -> Nullable<Text>, matrix_user_id -> Nullable<Text>,
admin -> Bool,
} }
} }

View file

@ -8,7 +8,6 @@ pub struct LocalUser {
pub person_id: PersonId, pub person_id: PersonId,
pub password_encrypted: String, pub password_encrypted: String,
pub email: Option<String>, pub email: Option<String>,
pub admin: bool,
pub show_nsfw: bool, pub show_nsfw: bool,
pub theme: String, pub theme: String,
pub default_sort_type: i16, pub default_sort_type: i16,
@ -26,7 +25,6 @@ pub struct LocalUserForm {
pub person_id: PersonId, pub person_id: PersonId,
pub password_encrypted: String, pub password_encrypted: String,
pub email: Option<Option<String>>, pub email: Option<Option<String>>,
pub admin: Option<bool>,
pub show_nsfw: Option<bool>, pub show_nsfw: Option<bool>,
pub theme: Option<String>, pub theme: Option<String>,
pub default_sort_type: Option<i16>, pub default_sort_type: Option<i16>,
@ -43,7 +41,6 @@ pub struct LocalUserSettings {
pub id: LocalUserId, pub id: LocalUserId,
pub person_id: PersonId, pub person_id: PersonId,
pub email: Option<String>, pub email: Option<String>,
pub admin: bool,
pub show_nsfw: bool, pub show_nsfw: bool,
pub theme: String, pub theme: String,
pub default_sort_type: i16, pub default_sort_type: i16,

View file

@ -26,6 +26,7 @@ pub struct Person {
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool,
} }
/// A safe representation of person, without the sensitive info /// A safe representation of person, without the sensitive info
@ -47,6 +48,7 @@ pub struct PersonSafe {
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool,
} }
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
@ -70,6 +72,7 @@ pub struct PersonAlias1 {
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool,
} }
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
@ -90,6 +93,7 @@ pub struct PersonSafeAlias1 {
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool,
} }
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
@ -113,6 +117,7 @@ pub struct PersonAlias2 {
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool,
} }
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
@ -133,6 +138,7 @@ pub struct PersonSafeAlias2 {
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool,
} }
#[derive(Insertable, AsChangeset, Clone, Default)] #[derive(Insertable, AsChangeset, Clone, Default)]
@ -155,4 +161,5 @@ pub struct PersonForm {
pub inbox_url: Option<DbUrl>, pub inbox_url: Option<DbUrl>,
pub shared_inbox_url: Option<Option<DbUrl>>, pub shared_inbox_url: Option<Option<DbUrl>>,
pub matrix_user_id: Option<Option<String>>, pub matrix_user_id: Option<Option<String>>,
pub admin: Option<bool>,
} }

View file

@ -526,6 +526,7 @@ mod tests {
local: true, local: true,
banned: false, banned: false,
deleted: false, deleted: false,
admin: false,
bio: None, bio: None,
banner: None, banner: None,
updated: None, updated: None,

View file

@ -546,6 +546,7 @@ mod tests {
avatar: None, avatar: None,
actor_id: inserted_person.actor_id.to_owned(), actor_id: inserted_person.actor_id.to_owned(),
local: true, local: true,
admin: false,
banned: false, banned: false,
deleted: false, deleted: false,
bio: None, bio: None,

View file

@ -9,7 +9,7 @@ use lemmy_db_queries::{
ViewToVec, ViewToVec,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
schema::{local_user, person, person_aggregates}, schema::{person, person_aggregates},
source::person::{Person, PersonSafe}, source::person::{Person, PersonSafe},
PersonId, PersonId,
}; };
@ -36,9 +36,8 @@ impl PersonViewSafe {
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> { pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
let admins = person::table let admins = person::table
.inner_join(person_aggregates::table) .inner_join(person_aggregates::table)
.inner_join(local_user::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns)) .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
.filter(local_user::admin.eq(true)) .filter(person::admin.eq(true))
.order_by(person::published) .order_by(person::published)
.load::<PersonViewSafeTuple>(conn)?; .load::<PersonViewSafeTuple>(conn)?;

View file

@ -1,12 +1,16 @@
alter table local_user add column matrix_user_id text; alter table local_user add column matrix_user_id text;
alter table local_user add column admin boolean default false not null;
update local_user lu update local_user lu
set matrix_user_id = p.matrix_user_id set
matrix_user_id = p.matrix_user_id,
admin = p.admin
from person p from person p
where p.id = lu.person_id; where p.id = lu.person_id;
drop view person_alias_1, person_alias_2; drop view person_alias_1, person_alias_2;
alter table person drop column matrix_user_id; alter table person drop column matrix_user_id;
alter table person drop column admin;
-- Regenerate the person_alias views -- Regenerate the person_alias views
create view person_alias_1 as select * from person; create view person_alias_1 as select * from person;

View file

@ -1,11 +1,15 @@
alter table person add column matrix_user_id text; alter table person add column matrix_user_id text;
alter table person add column admin boolean default false not null;
update person p update person p
set matrix_user_id = lu.matrix_user_id set
matrix_user_id = lu.matrix_user_id,
admin = lu.admin
from local_user lu from local_user lu
where p.id = lu.person_id; where p.id = lu.person_id;
alter table local_user drop column matrix_user_id; alter table local_user drop column matrix_user_id;
alter table local_user drop column admin;
-- Regenerate the person_alias views -- Regenerate the person_alias views
drop view person_alias_1, person_alias_2; drop view person_alias_1, person_alias_2;