From bbe9061402d8f1345c2582d976d8f2d8124b88ac Mon Sep 17 00:00:00 2001 From: yenicelik Date: Sun, 19 Feb 2023 21:33:33 +0100 Subject: [PATCH] continue rebase --- entities/src/mod.rs | 1 - entities/src/prelude.rs | 1 - web3_proxy/src/admin_queries.rs | 4 ++ .../change_user_admin_status.rs | 64 +++++++++++++++++++ web3_proxy/src/bin/web3_proxy_cli/main.rs | 11 ++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_admin_status.rs diff --git a/entities/src/mod.rs b/entities/src/mod.rs index 209dead2..545dc4db 100644 --- a/entities/src/mod.rs +++ b/entities/src/mod.rs @@ -2,7 +2,6 @@ pub mod prelude; -pub mod credits; pub mod admin; pub mod login; pub mod pending_login; diff --git a/entities/src/prelude.rs b/entities/src/prelude.rs index 38337598..eba83250 100644 --- a/entities/src/prelude.rs +++ b/entities/src/prelude.rs @@ -9,5 +9,4 @@ pub use super::secondary_user::Entity as SecondaryUser; pub use super::user::Entity as User; pub use super::user_tier::Entity as UserTier; pub use super::referral::Entity as Referral; -pub use super::credits::Entity as Credits; pub use super::rpc_request::Entity as RpcRequest; diff --git a/web3_proxy/src/admin_queries.rs b/web3_proxy/src/admin_queries.rs index 92f75d51..32e0ab84 100644 --- a/web3_proxy/src/admin_queries.rs +++ b/web3_proxy/src/admin_queries.rs @@ -14,6 +14,7 @@ use hashbrown::HashMap; use http::StatusCode; use migration::sea_orm::{self, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter}; use log::info; +use redis_rate_limiter::redis::AsyncCommands; pub async fn query_admin_modify_usertier<'a>( @@ -110,6 +111,9 @@ pub async fn query_admin_modify_usertier<'a>( // Finally, remove the user from redis // TODO: Also remove the user from the redis + // redis_conn.zrem(); + // redis_conn.get::<_, u64>(&user.) // TODO: Where do i find the bearer token ... + Ok(Json(&response_body).into_response()) diff --git a/web3_proxy/src/bin/web3_proxy_cli/change_user_admin_status.rs b/web3_proxy/src/bin/web3_proxy_cli/change_user_admin_status.rs new file mode 100644 index 00000000..3058cb7e --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/change_user_admin_status.rs @@ -0,0 +1,64 @@ +use anyhow::Context; +use argh::FromArgs; +use entities::{admin, user}; +use ethers::types::Address; +use log::{debug, info}; +use migration::sea_orm::{ + self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, ModelTrait, IntoActiveModel, + QueryFilter, +}; + +/// change a user's admin status. eiter they are an admin, or they aren't +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "change_admin_status")] +pub struct ChangeUserAdminStatusSubCommand { + /// the address of the user whose admin status you want to modify + #[argh(positional)] + address: String, + + /// true if the user should be an admin, false otherwise + #[argh(positional)] + should_be_admin: bool, +} + +impl ChangeUserAdminStatusSubCommand { + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + let address: Address = self.address.parse()?; + let should_be_admin: bool = self.should_be_admin; + + let address: Vec = address.to_fixed_bytes().into(); + + // Find user in database + let user = user::Entity::find() + .filter(user::Column::Address.eq(address.clone())) + .one(db_conn) + .await? + .context("No user found with that address")?; + + // Check if there is a record in the database + let mut admin = admin::Entity::find() + .filter(admin::Column::UserId.eq(address)) + .all(db_conn) + .await?; + + debug!("user: {:#?}", user); + + match admin.pop() { + None if should_be_admin => { + // User is not an admin yet, but should be + let new_admin = admin::ActiveModel { + user_id: sea_orm::Set(user.id), + ..Default::default() + }; + new_admin.insert(db_conn).await?; + }, + Some(old_admin) if !should_be_admin => { + // User is already an admin, but shouldn't be + old_admin.delete(db_conn).await?; + }, + _ => {} + } + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index fba010b2..358967f3 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -1,4 +1,5 @@ mod change_user_address; +mod change_user_admin_status; mod change_user_tier; mod change_user_tier_by_address; mod change_user_tier_by_key; @@ -71,6 +72,7 @@ pub struct Web3ProxyCli { enum SubCommand { ChangeUserAddress(change_user_address::ChangeUserAddressSubCommand), ChangeUserTier(change_user_tier::ChangeUserTierSubCommand), + ChangeUserAdminStatus(change_user_admin_status::ChangeUserAdminStatusSubCommand), ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressSubCommand), ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeySubCommand), CheckConfig(check_config::CheckConfigSubCommand), @@ -286,6 +288,15 @@ fn main() -> anyhow::Result<()> { x.main(&db_conn).await } + SubCommand::ChangeUserAdminStatus(x) => { + let db_url = cli_config + .db_url + .expect("'--config' (with a db) or '--db-url' is required to run change user admin status"); + + let db_conn = get_db(db_url, 1, 1).await?; + + x.main(&db_conn).await + } SubCommand::ChangeUserTier(x) => { let db_url = cli_config .db_url