added binary to change admin status of a user

This commit is contained in:
yenicelik 2023-01-17 21:44:16 +01:00
parent 95c4ba3273
commit 74f5a27f0b
3 changed files with 75 additions and 0 deletions

@ -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())

@ -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<u8> = 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(())
}
}

@ -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?;