From 74f5a27f0b4270b7f2d6189558f68d66667e9b7e Mon Sep 17 00:00:00 2001 From: yenicelik Date: Tue, 17 Jan 2023 21:44:16 +0100 Subject: [PATCH] added binary to change admin status of a user --- web3_proxy/src/admin_queries.rs | 4 ++ .../change_user_admin_status.rs | 64 +++++++++++++++++++ web3_proxy/src/bin/web3_proxy_cli/main.rs | 7 ++ 3 files changed, 75 insertions(+) create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_admin_status.rs 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 c60b9446..fa519162 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; @@ -44,6 +45,7 @@ pub struct CliConfig { 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), @@ -100,6 +102,11 @@ async fn main() -> anyhow::Result<()> { x.main(&db_conn).await } + SubCommand::ChangeUserAdminStatus(x) => { + let db_conn = get_db(cli_config.db_url, 1, 1).await?; + + x.main(&db_conn).await + } SubCommand::ChangeUserTierByAddress(x) => { let db_conn = get_db(cli_config.db_url, 1, 1).await?;