Skip to content

Commit

Permalink
feat: add select owner when user create
Browse files Browse the repository at this point in the history
  • Loading branch information
erfjab committed Oct 11, 2024
1 parent 997502a commit 1611518
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
ConfirmCallbacks,
UserStatusCallbacks,
UserInboundsCallbacks,
AdminSelectCallbacks,
)
4 changes: 4 additions & 0 deletions models/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ class UserInboundsCallbacks(CallbackData, prefix="user_inbounds"):
is_selected: bool | None = None
action: AdminActions
is_done: bool = False


class AdminSelectCallbacks(CallbackData, prefix="admin_select"):
username: str
1 change: 1 addition & 0 deletions models/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class UserCreateForm(StatesGroup):
data_limit = State()
date_limit = State()
status = State()
admin = State()
inbounds = State()
16 changes: 16 additions & 0 deletions routers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from marzban import ProxyInbound

from utils.config import MARZBAN_USERNAME
from utils.lang import MessageTexts
from utils.keys import BotKeyboards
from utils.statedb import storage
Expand All @@ -16,6 +17,7 @@
UserCreateForm,
UserStatusCallbacks,
UserInboundsCallbacks,
AdminSelectCallbacks,
)


Expand Down Expand Up @@ -115,6 +117,18 @@ async def user_create_status(
callback: CallbackQuery, callback_data: UserStatusCallbacks, state: FSMContext
):
await state.update_data(status=callback_data.status)
admins = await panel.admins()
return await callback.message.edit_text(
text=MessageTexts.AskCreateAdminUsername,
reply_markup=BotKeyboards.admins(admins),
)


@router.callback_query(AdminSelectCallbacks.filter())
async def user_create_owner_select(
callback: CallbackQuery, callback_data: AdminSelectCallbacks, state: FSMContext
):
await state.update_data(admin=callback_data.username)
inbounds = await panel.inbounds()
await state.update_data(inbounds=inbounds)
return await callback.message.edit_text(
Expand Down Expand Up @@ -191,6 +205,8 @@ async def user_create_inbounds_save(callback: CallbackQuery, state: FSMContext):
)

if new_user:
if data["admin"] != MARZBAN_USERNAME:
await panel.set_owner(data['admin'], new_user.username)
qr_bytes = await helpers.create_qr(new_user.subscription_url)
await callback.message.answer_photo(
caption=text_info.user_info(new_user),
Expand Down
22 changes: 20 additions & 2 deletions utils/keys.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from aiogram.types import InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder, InlineKeyboardButton

from marzban import ProxyInbound
from marzban import ProxyInbound, Admin

from utils.lang import KeyboardTexts
from models import (
PagesActions,
PagesCallbacks,
AdminActions,
ConfirmCallbacks,
AdminSelectCallbacks,
UserStatusCallbacks,
UserInboundsCallbacks,
)
Expand Down Expand Up @@ -91,3 +91,21 @@ def inbounds(
),
)
return kb.adjust(2).as_markup()

@staticmethod
def admins(admins: list[Admin]) -> InlineKeyboardMarkup:
kb = InlineKeyboardBuilder()

for admin in admins:
kb.button(
text=admin.username,
callback_data=AdminSelectCallbacks(username=admin.username),
)

kb.row(
InlineKeyboardButton(
text=KeyboardTexts.Home,
callback_data=PagesCallbacks(page=PagesActions.Home).pack(),
),
)
return kb.adjust(2).as_markup()
1 change: 1 addition & 0 deletions utils/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MessageTexts(str, Enum):
AskCreateUserDataLimit = "📊 Please enter the <b>data limit</b> in GB"
AskCreateUserDateLimit = "📅 Please enter the <b>date limit</b> in days"
AskCreateUserStatus = "🔄 Select the <b>user status</b>"
AskCreateAdminUsername = "👤 Select the <b>owner admin</b>"
AskCreateUserInbouds = "🌐 Select the <b>user inbounds</b>"
JustNumber = "🔢 Please enter <b>numbers only</b>"
NoneUserInbounds = "⚠️ Please select an <b>inbound</b> first"
Expand Down
26 changes: 23 additions & 3 deletions utils/panel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import json

from marzban import MarzbanAPI, ProxyInbound, UserResponse, UserCreate
from marzban import MarzbanAPI, ProxyInbound, UserResponse, UserCreate, Admin
from datetime import datetime, timedelta
from utils.config import MARZBAN_ADDRESS
from db import TokenManager
Expand Down Expand Up @@ -55,3 +53,25 @@ async def create_user(
except Exception as e:
logger.error(f"Error create user: {e}")
return False


async def admins() -> list[Admin]:
try:
get_token = await TokenManager.get()
admins = await marzban_panel.get_admins(get_token.token)
return admins or False
except Exception as e:
logger.error(f"Error getting token: {e}")
return False


async def set_owner(admin: str, user: str) -> bool:
try:
get_token = await TokenManager.get()
user = await marzban_panel.set_owner(
username=user, admin_username=admin, token=get_token.token
)
return user or False
except Exception as e:
logger.error(f"Error getting token: {e}")
return False

0 comments on commit 1611518

Please sign in to comment.