| 1 |
f7af2b3 |
Dawsh |
2y ago |
import asyncio |
| 2 |
f7af2b3 |
Dawsh |
2y ago |
import logging |
| 3 |
d377c4c |
Dawsh |
2y ago |
from datetime import datetime, timedelta |
| 4 |
d4ec019 |
Dawsh |
2y ago |
from enum import Enum |
| 5 |
f7af2b3 |
Dawsh |
2y ago |
|
| 6 |
f7af2b3 |
Dawsh |
2y ago |
import sqlalchemy |
| 7 |
f7af2b3 |
Dawsh |
2y ago |
from fastapi import APIRouter |
| 8 |
f7af2b3 |
Dawsh |
2y ago |
from fastapi import HTTPException, Query |
| 9 |
d4ec019 |
Dawsh |
2y ago |
from fastapi_pagination.ext.sqlalchemy import paginate |
| 10 |
d4ec019 |
Dawsh |
2y ago |
from fastapi_pagination.links import Page |
| 11 |
f7af2b3 |
Dawsh |
2y ago |
|
| 12 |
f7af2b3 |
Dawsh |
2y ago |
from app import marznode |
| 13 |
d4ec019 |
Dawsh |
2y ago |
from app.db import crud, User |
| 14 |
db68a58 |
Dawsh |
2y ago |
from app.db.models import Service |
| 15 |
01cdf2e |
Dawsh |
2y ago |
from app.dependencies import ( |
| 16 |
01cdf2e |
Dawsh |
2y ago |
DBDep, |
| 17 |
01cdf2e |
Dawsh |
2y ago |
AdminDep, |
| 18 |
01cdf2e |
Dawsh |
2y ago |
SudoAdminDep, |
| 19 |
01cdf2e |
Dawsh |
2y ago |
UserDep, |
| 20 |
01cdf2e |
Dawsh |
2y ago |
StartDateDep, |
| 21 |
01cdf2e |
Dawsh |
2y ago |
EndDateDep, |
| 22 |
01cdf2e |
Dawsh |
2y ago |
) |
| 23 |
db68a58 |
Dawsh |
2y ago |
from app.models.service import ServiceResponse |
| 24 |
01cdf2e |
Dawsh |
2y ago |
from app.models.user import ( |
| 25 |
01cdf2e |
Dawsh |
2y ago |
UserCreate, |
| 26 |
01cdf2e |
Dawsh |
2y ago |
UserModify, |
| 27 |
01cdf2e |
Dawsh |
2y ago |
UserResponse, |
| 28 |
01cdf2e |
Dawsh |
2y ago |
UserUsagesResponse, |
| 29 |
01cdf2e |
Dawsh |
2y ago |
) |
| 30 |
f7af2b3 |
Dawsh |
2y ago |
from app.utils import report |
| 31 |
f7af2b3 |
Dawsh |
2y ago |
|
| 32 |
f7af2b3 |
Dawsh |
2y ago |
logger = logging.getLogger(__name__) |
| 33 |
01cdf2e |
Dawsh |
2y ago |
router = APIRouter(prefix="/users", tags=["User"]) |
| 34 |
f7af2b3 |
Dawsh |
2y ago |
|
| 35 |
f7af2b3 |
Dawsh |
2y ago |
|
| 36 |
d4ec019 |
Dawsh |
2y ago |
class UsersSortingOptions(str, Enum): |
| 37 |
d377c4c |
Dawsh |
2y ago |
USERNAME = "username" |
| 38 |
d377c4c |
Dawsh |
2y ago |
USED_TRAFFIC = "used_traffic" |
| 39 |
d377c4c |
Dawsh |
2y ago |
DATA_LIMIT = "data_limit" |
| 40 |
d377c4c |
Dawsh |
2y ago |
EXPIRE_DATE = "expire_date" |
| 41 |
d377c4c |
Dawsh |
2y ago |
CREATED_AT = "created_at" |
| 42 |
d377c4c |
Dawsh |
2y ago |
|
| 43 |
d377c4c |
Dawsh |
2y ago |
|
| 44 |
d377c4c |
Dawsh |
2y ago |
user_filters = [ |
| 45 |
d377c4c |
Dawsh |
2y ago |
"username", |
| 46 |
d377c4c |
Dawsh |
2y ago |
"is_active", |
| 47 |
d377c4c |
Dawsh |
2y ago |
"activated", |
| 48 |
d377c4c |
Dawsh |
2y ago |
"expired", |
| 49 |
d377c4c |
Dawsh |
2y ago |
"data_limit_reached", |
| 50 |
d377c4c |
Dawsh |
2y ago |
"enabled", |
| 51 |
d377c4c |
Dawsh |
2y ago |
] |
| 52 |
d4ec019 |
Dawsh |
2y ago |
|
| 53 |
d4ec019 |
Dawsh |
2y ago |
|
| 54 |
d4ec019 |
Dawsh |
2y ago |
@router.get("", response_model=Page[UserResponse]) |
| 55 |
01cdf2e |
Dawsh |
2y ago |
def get_users( |
| 56 |
01cdf2e |
Dawsh |
2y ago |
db: DBDep, |
| 57 |
01cdf2e |
Dawsh |
2y ago |
admin: AdminDep, |
| 58 |
01cdf2e |
Dawsh |
2y ago |
username: list[str] = Query(None), |
| 59 |
01cdf2e |
Dawsh |
2y ago |
order_by: UsersSortingOptions = Query(None), |
| 60 |
01cdf2e |
Dawsh |
2y ago |
descending: bool = Query(False), |
| 61 |
d377c4c |
Dawsh |
2y ago |
is_active: bool | None = Query(None), |
| 62 |
d377c4c |
Dawsh |
2y ago |
activated: bool | None = Query(None), |
| 63 |
d377c4c |
Dawsh |
2y ago |
expired: bool | None = Query(None), |
| 64 |
d377c4c |
Dawsh |
2y ago |
data_limit_reached: bool | None = Query(None), |
| 65 |
d377c4c |
Dawsh |
2y ago |
enabled: bool | None = Query(None), |
| 66 |
01cdf2e |
Dawsh |
2y ago |
): |
| 67 |
d4ec019 |
Dawsh |
2y ago |
""" |
| 68 |
d4ec019 |
Dawsh |
2y ago |
Filters users based on the options |
| 69 |
d4ec019 |
Dawsh |
2y ago |
""" |
| 70 |
d4ec019 |
Dawsh |
2y ago |
dbadmin = crud.get_admin(db, admin.username) |
| 71 |
d4ec019 |
Dawsh |
2y ago |
|
| 72 |
d4ec019 |
Dawsh |
2y ago |
query = db.query(User) |
| 73 |
d4ec019 |
Dawsh |
2y ago |
|
| 74 |
d4ec019 |
Dawsh |
2y ago |
admin = dbadmin if not admin.is_sudo else None |
| 75 |
d4ec019 |
Dawsh |
2y ago |
|
| 76 |
d377c4c |
Dawsh |
2y ago |
for name in user_filters: |
| 77 |
d377c4c |
Dawsh |
2y ago |
value = locals().get(name) |
| 78 |
d377c4c |
Dawsh |
2y ago |
if value is not None: |
| 79 |
d377c4c |
Dawsh |
2y ago |
if name == "username": |
| 80 |
d377c4c |
Dawsh |
2y ago |
if len(username) > 1: |
| 81 |
d377c4c |
Dawsh |
2y ago |
query = query.filter(User.username.in_(username)) |
| 82 |
d377c4c |
Dawsh |
2y ago |
else: |
| 83 |
d377c4c |
Dawsh |
2y ago |
query = query.filter( |
| 84 |
d377c4c |
Dawsh |
2y ago |
User.username.ilike(f"%{username[0]}%") |
| 85 |
d377c4c |
Dawsh |
2y ago |
) |
| 86 |
d377c4c |
Dawsh |
2y ago |
else: |
| 87 |
d377c4c |
Dawsh |
2y ago |
query = query.filter(getattr(User, name) == value) |
| 88 |
d4ec019 |
Dawsh |
2y ago |
|
| 89 |
d4ec019 |
Dawsh |
2y ago |
if admin: |
| 90 |
d4ec019 |
Dawsh |
2y ago |
query = query.filter(User.admin == admin) |
| 91 |
d4ec019 |
Dawsh |
2y ago |
|
| 92 |
d4ec019 |
Dawsh |
2y ago |
if order_by: |
| 93 |
d4ec019 |
Dawsh |
2y ago |
order_column = getattr(User, order_by) |
| 94 |
d4ec019 |
Dawsh |
2y ago |
if descending: |
| 95 |
d4ec019 |
Dawsh |
2y ago |
order_column = order_column.desc() |
| 96 |
d4ec019 |
Dawsh |
2y ago |
query = query.order_by(order_column) |
| 97 |
d4ec019 |
Dawsh |
2y ago |
|
| 98 |
d4ec019 |
Dawsh |
2y ago |
return paginate(db, query) |
| 99 |
d4ec019 |
Dawsh |
2y ago |
|
| 100 |
d4ec019 |
Dawsh |
2y ago |
|
| 101 |
5bb2712 |
Mardin |
2y ago |
@router.post("", response_model=UserResponse) |
| 102 |
01cdf2e |
Dawsh |
2y ago |
async def add_user(new_user: UserCreate, db: DBDep, admin: AdminDep): |
| 103 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 104 |
f7af2b3 |
Dawsh |
2y ago |
Add a new user |
| 105 |
f7af2b3 |
Dawsh |
2y ago |
|
| 106 |
f7af2b3 |
Dawsh |
2y ago |
- **username** must have 3 to 32 characters and is allowed to contain a-z, 0-9, and underscores in between |
| 107 |
d377c4c |
Dawsh |
2y ago |
- **expire_date** must be a datetime |
| 108 |
f7af2b3 |
Dawsh |
2y ago |
- **data_limit** must be in Bytes, e.g. 1073741824B = 1GB |
| 109 |
f7af2b3 |
Dawsh |
2y ago |
- **services** list of service ids |
| 110 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 111 |
f7af2b3 |
Dawsh |
2y ago |
|
| 112 |
f7af2b3 |
Dawsh |
2y ago |
try: |
| 113 |
01cdf2e |
Dawsh |
2y ago |
db_user = crud.create_user( |
| 114 |
01cdf2e |
Dawsh |
2y ago |
db, new_user, admin=crud.get_admin(db, admin.username) |
| 115 |
01cdf2e |
Dawsh |
2y ago |
) |
| 116 |
f7af2b3 |
Dawsh |
2y ago |
except sqlalchemy.exc.IntegrityError: |
| 117 |
f7af2b3 |
Dawsh |
2y ago |
db.rollback() |
| 118 |
f7af2b3 |
Dawsh |
2y ago |
raise HTTPException(status_code=409, detail="User already exists") |
| 119 |
f7af2b3 |
Dawsh |
2y ago |
|
| 120 |
f7af2b3 |
Dawsh |
2y ago |
user = UserResponse.model_validate(db_user) |
| 121 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(user=db_user) |
| 122 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future( |
| 123 |
f1ab16c |
Dawsh |
2y ago |
report.user_created(user=user, user_id=db_user.id, by=admin) |
| 124 |
f1ab16c |
Dawsh |
2y ago |
) |
| 125 |
f7af2b3 |
Dawsh |
2y ago |
logger.info("New user `%s` added", db_user.username) |
| 126 |
f7af2b3 |
Dawsh |
2y ago |
return user |
| 127 |
f7af2b3 |
Dawsh |
2y ago |
|
| 128 |
f7af2b3 |
Dawsh |
2y ago |
|
| 129 |
d4ec019 |
Dawsh |
2y ago |
@router.post("/reset") |
| 130 |
01cdf2e |
Dawsh |
2y ago |
async def reset_users_data_usage(db: DBDep, admin: SudoAdminDep): |
| 131 |
d4ec019 |
Dawsh |
2y ago |
""" |
| 132 |
d4ec019 |
Dawsh |
2y ago |
Reset all users data usage, |
| 133 |
d4ec019 |
Dawsh |
2y ago |
You will need to restart for this to take effect for now |
| 134 |
d4ec019 |
Dawsh |
2y ago |
""" |
| 135 |
d4ec019 |
Dawsh |
2y ago |
dbadmin = crud.get_admin(db, admin.username) |
| 136 |
d4ec019 |
Dawsh |
2y ago |
crud.reset_all_users_data_usage(db=db, admin=dbadmin) |
| 137 |
d4ec019 |
Dawsh |
2y ago |
return {} |
| 138 |
d4ec019 |
Dawsh |
2y ago |
|
| 139 |
d4ec019 |
Dawsh |
2y ago |
|
| 140 |
d4ec019 |
Dawsh |
2y ago |
@router.delete("/expired") |
| 141 |
d377c4c |
Dawsh |
2y ago |
async def delete_expired(passed_time: int, db: DBDep, admin: AdminDep): |
| 142 |
d4ec019 |
Dawsh |
2y ago |
""" |
| 143 |
d4ec019 |
Dawsh |
2y ago |
Delete expired users |
| 144 |
d4ec019 |
Dawsh |
2y ago |
- **passed_time** must be a timestamp |
| 145 |
d4ec019 |
Dawsh |
2y ago |
- This function will delete all expired users that meet the specified number of days passed and can't be undone. |
| 146 |
d4ec019 |
Dawsh |
2y ago |
""" |
| 147 |
d4ec019 |
Dawsh |
2y ago |
|
| 148 |
d4ec019 |
Dawsh |
2y ago |
dbadmin = crud.get_admin(db, admin.username) |
| 149 |
d4ec019 |
Dawsh |
2y ago |
|
| 150 |
01cdf2e |
Dawsh |
2y ago |
db_users = crud.get_users( |
| 151 |
01cdf2e |
Dawsh |
2y ago |
db=db, |
| 152 |
d377c4c |
Dawsh |
2y ago |
expired=True, |
| 153 |
01cdf2e |
Dawsh |
2y ago |
admin=dbadmin if not admin.is_sudo else None, |
| 154 |
01cdf2e |
Dawsh |
2y ago |
) |
| 155 |
d4ec019 |
Dawsh |
2y ago |
|
| 156 |
d377c4c |
Dawsh |
2y ago |
current_time = datetime.utcnow() |
| 157 |
d377c4c |
Dawsh |
2y ago |
expiration_threshold = current_time - timedelta(seconds=passed_time) |
| 158 |
d4ec019 |
Dawsh |
2y ago |
expired_users = [ |
| 159 |
01cdf2e |
Dawsh |
2y ago |
user |
| 160 |
01cdf2e |
Dawsh |
2y ago |
for user in db_users |
| 161 |
01cdf2e |
Dawsh |
2y ago |
if user.expire is not None and user.expire <= expiration_threshold |
| 162 |
01cdf2e |
Dawsh |
2y ago |
] |
| 163 |
d4ec019 |
Dawsh |
2y ago |
if not expired_users: |
| 164 |
d4ec019 |
Dawsh |
2y ago |
raise HTTPException(status_code=404, detail="No expired user found.") |
| 165 |
d4ec019 |
Dawsh |
2y ago |
|
| 166 |
d4ec019 |
Dawsh |
2y ago |
for db_user in expired_users: |
| 167 |
d377c4c |
Dawsh |
2y ago |
if db_user.activated and not db_user.is_active: |
| 168 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user) |
| 169 |
d4ec019 |
Dawsh |
2y ago |
crud.remove_user(db, db_user) |
| 170 |
d4ec019 |
Dawsh |
2y ago |
|
| 171 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future( |
| 172 |
f1ab16c |
Dawsh |
2y ago |
report.user_deleted(username=db_user.username, by=admin) |
| 173 |
f1ab16c |
Dawsh |
2y ago |
) |
| 174 |
d4ec019 |
Dawsh |
2y ago |
|
| 175 |
d4ec019 |
Dawsh |
2y ago |
logger.info("User `%s` removed", db_user.username) |
| 176 |
d4ec019 |
Dawsh |
2y ago |
|
| 177 |
d4ec019 |
Dawsh |
2y ago |
return {} |
| 178 |
d4ec019 |
Dawsh |
2y ago |
|
| 179 |
d4ec019 |
Dawsh |
2y ago |
|
| 180 |
5bb2712 |
Mardin |
2y ago |
@router.get("/{username}", response_model=UserResponse) |
| 181 |
f7af2b3 |
Dawsh |
2y ago |
def get_user(db_user: UserDep): |
| 182 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 183 |
f7af2b3 |
Dawsh |
2y ago |
Get users information |
| 184 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 185 |
f7af2b3 |
Dawsh |
2y ago |
return db_user |
| 186 |
f7af2b3 |
Dawsh |
2y ago |
|
| 187 |
f7af2b3 |
Dawsh |
2y ago |
|
| 188 |
5bb2712 |
Mardin |
2y ago |
@router.put("/{username}", response_model=UserResponse) |
| 189 |
01cdf2e |
Dawsh |
2y ago |
async def modify_user( |
| 190 |
01cdf2e |
Dawsh |
2y ago |
db_user: UserDep, modifications: UserModify, db: DBDep, admin: AdminDep |
| 191 |
01cdf2e |
Dawsh |
2y ago |
): |
| 192 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 193 |
f7af2b3 |
Dawsh |
2y ago |
Modify a user |
| 194 |
f7af2b3 |
Dawsh |
2y ago |
|
| 195 |
f7af2b3 |
Dawsh |
2y ago |
- set **data_limit** to 0 to make the user unlimited in data, null for no change |
| 196 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 197 |
d377c4c |
Dawsh |
2y ago |
active_before = db_user.is_active |
| 198 |
f7af2b3 |
Dawsh |
2y ago |
|
| 199 |
f7af2b3 |
Dawsh |
2y ago |
old_inbounds = {(i.node_id, i.protocol, i.tag) for i in db_user.inbounds} |
| 200 |
f7af2b3 |
Dawsh |
2y ago |
new_user = crud.update_user(db, db_user, modifications) |
| 201 |
d377c4c |
Dawsh |
2y ago |
active_after = new_user.is_active |
| 202 |
f7af2b3 |
Dawsh |
2y ago |
new_inbounds = {(i.node_id, i.protocol, i.tag) for i in new_user.inbounds} |
| 203 |
f7af2b3 |
Dawsh |
2y ago |
|
| 204 |
f7af2b3 |
Dawsh |
2y ago |
inbound_change = old_inbounds != new_inbounds |
| 205 |
d377c4c |
Dawsh |
2y ago |
|
| 206 |
cba9caa |
Dawsh |
2y ago |
if ( |
| 207 |
d377c4c |
Dawsh |
2y ago |
inbound_change and new_user.is_active |
| 208 |
d377c4c |
Dawsh |
2y ago |
) or active_before != active_after: |
| 209 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user( |
| 210 |
a26139d |
Dawsh |
2y ago |
new_user, old_inbounds, remove=not db_user.is_active |
| 211 |
d377c4c |
Dawsh |
2y ago |
) |
| 212 |
d377c4c |
Dawsh |
2y ago |
db_user.activated = db_user.is_active |
| 213 |
d377c4c |
Dawsh |
2y ago |
db.commit() |
| 214 |
f7af2b3 |
Dawsh |
2y ago |
|
| 215 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future( |
| 216 |
d377c4c |
Dawsh |
2y ago |
report.user_updated( |
| 217 |
d377c4c |
Dawsh |
2y ago |
user=UserResponse.model_validate(db_user), by=admin |
| 218 |
d377c4c |
Dawsh |
2y ago |
) |
| 219 |
d377c4c |
Dawsh |
2y ago |
) |
| 220 |
01cdf2e |
Dawsh |
2y ago |
|
| 221 |
d377c4c |
Dawsh |
2y ago |
logger.info("User `%s` modified", db_user.username) |
| 222 |
f7af2b3 |
Dawsh |
2y ago |
|
| 223 |
d377c4c |
Dawsh |
2y ago |
if active_before != active_after: |
| 224 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future( |
| 225 |
01cdf2e |
Dawsh |
2y ago |
report.status_change( |
| 226 |
d377c4c |
Dawsh |
2y ago |
username=db_user.username, |
| 227 |
d377c4c |
Dawsh |
2y ago |
activation=db_user.activated, |
| 228 |
d377c4c |
Dawsh |
2y ago |
user=UserResponse.model_validate(db_user), |
| 229 |
d377c4c |
Dawsh |
2y ago |
by=admin, |
| 230 |
01cdf2e |
Dawsh |
2y ago |
) |
| 231 |
01cdf2e |
Dawsh |
2y ago |
) |
| 232 |
01cdf2e |
Dawsh |
2y ago |
|
| 233 |
01cdf2e |
Dawsh |
2y ago |
logger.info( |
| 234 |
d377c4c |
Dawsh |
2y ago |
"User `%s` activation changed from `%s` to `%s`", |
| 235 |
d377c4c |
Dawsh |
2y ago |
db_user.username, |
| 236 |
d377c4c |
Dawsh |
2y ago |
active_before, |
| 237 |
d377c4c |
Dawsh |
2y ago |
active_after, |
| 238 |
01cdf2e |
Dawsh |
2y ago |
) |
| 239 |
f7af2b3 |
Dawsh |
2y ago |
|
| 240 |
d377c4c |
Dawsh |
2y ago |
return db_user |
| 241 |
f7af2b3 |
Dawsh |
2y ago |
|
| 242 |
f7af2b3 |
Dawsh |
2y ago |
|
| 243 |
5bb2712 |
Mardin |
2y ago |
@router.delete("/{username}") |
| 244 |
01cdf2e |
Dawsh |
2y ago |
async def remove_user(db_user: UserDep, db: DBDep, admin: AdminDep): |
| 245 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 246 |
f7af2b3 |
Dawsh |
2y ago |
Remove a user |
| 247 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 248 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user, remove=True) |
| 249 |
f7af2b3 |
Dawsh |
2y ago |
|
| 250 |
f7af2b3 |
Dawsh |
2y ago |
crud.remove_user(db, db_user) |
| 251 |
f7af2b3 |
Dawsh |
2y ago |
db.flush() |
| 252 |
f7af2b3 |
Dawsh |
2y ago |
|
| 253 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future( |
| 254 |
f1ab16c |
Dawsh |
2y ago |
report.user_deleted(username=db_user.username, by=admin) |
| 255 |
f1ab16c |
Dawsh |
2y ago |
) |
| 256 |
f7af2b3 |
Dawsh |
2y ago |
logger.info("User %s deleted", db_user.username) |
| 257 |
f7af2b3 |
Dawsh |
2y ago |
return {} |
| 258 |
f7af2b3 |
Dawsh |
2y ago |
|
| 259 |
f7af2b3 |
Dawsh |
2y ago |
|
| 260 |
db68a58 |
Dawsh |
2y ago |
@router.get("/{username}/services", response_model=Page[ServiceResponse]) |
| 261 |
db68a58 |
Dawsh |
2y ago |
def get_user_services(username: str, db: DBDep): |
| 262 |
db68a58 |
Dawsh |
2y ago |
""" |
| 263 |
db68a58 |
Dawsh |
2y ago |
Get user services |
| 264 |
db68a58 |
Dawsh |
2y ago |
""" |
| 265 |
db68a58 |
Dawsh |
2y ago |
user = crud.get_user(db, username) |
| 266 |
db68a58 |
Dawsh |
2y ago |
|
| 267 |
db68a58 |
Dawsh |
2y ago |
if not user: |
| 268 |
db68a58 |
Dawsh |
2y ago |
raise HTTPException(status_code=404, detail="User not found") |
| 269 |
db68a58 |
Dawsh |
2y ago |
|
| 270 |
f1ab16c |
Dawsh |
2y ago |
query = ( |
| 271 |
f1ab16c |
Dawsh |
2y ago |
db.query(Service).join(Service.users).where(User.username == username) |
| 272 |
f1ab16c |
Dawsh |
2y ago |
) |
| 273 |
db68a58 |
Dawsh |
2y ago |
|
| 274 |
db68a58 |
Dawsh |
2y ago |
return paginate(query) |
| 275 |
db68a58 |
Dawsh |
2y ago |
|
| 276 |
db68a58 |
Dawsh |
2y ago |
|
| 277 |
5bb2712 |
Mardin |
2y ago |
@router.post("/{username}/reset", response_model=UserResponse) |
| 278 |
01cdf2e |
Dawsh |
2y ago |
async def reset_user_data_usage(db_user: UserDep, db: DBDep, admin: AdminDep): |
| 279 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 280 |
f7af2b3 |
Dawsh |
2y ago |
Reset user data usage |
| 281 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 282 |
d377c4c |
Dawsh |
2y ago |
was_active = db_user.status |
| 283 |
f7af2b3 |
Dawsh |
2y ago |
db_user = crud.reset_user_data_usage(db, db_user) |
| 284 |
f7af2b3 |
Dawsh |
2y ago |
|
| 285 |
d377c4c |
Dawsh |
2y ago |
if db_user.is_active and not was_active: |
| 286 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user) |
| 287 |
d377c4c |
Dawsh |
2y ago |
db_user.activated = True |
| 288 |
d377c4c |
Dawsh |
2y ago |
db.commit() |
| 289 |
f7af2b3 |
Dawsh |
2y ago |
|
| 290 |
f7af2b3 |
Dawsh |
2y ago |
user = UserResponse.model_validate(db_user) |
| 291 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future(report.user_data_usage_reset(user=user, by=admin)) |
| 292 |
f7af2b3 |
Dawsh |
2y ago |
|
| 293 |
f7af2b3 |
Dawsh |
2y ago |
logger.info("User `%s`'s usage was reset", db_user.username) |
| 294 |
f7af2b3 |
Dawsh |
2y ago |
|
| 295 |
f7af2b3 |
Dawsh |
2y ago |
return user |
| 296 |
f7af2b3 |
Dawsh |
2y ago |
|
| 297 |
f7af2b3 |
Dawsh |
2y ago |
|
| 298 |
cba9caa |
Dawsh |
2y ago |
@router.post("/{username}/enable", response_model=UserResponse) |
| 299 |
cba9caa |
Dawsh |
2y ago |
async def enable_user(db_user: UserDep, db: DBDep, admin: AdminDep): |
| 300 |
cba9caa |
Dawsh |
2y ago |
""" |
| 301 |
cba9caa |
Dawsh |
2y ago |
Enables a user |
| 302 |
cba9caa |
Dawsh |
2y ago |
""" |
| 303 |
cba9caa |
Dawsh |
2y ago |
if db_user.enabled: |
| 304 |
cba9caa |
Dawsh |
2y ago |
raise HTTPException(409, "User is already enabled") |
| 305 |
cba9caa |
Dawsh |
2y ago |
db_user.enabled = True |
| 306 |
d377c4c |
Dawsh |
2y ago |
db_user.activated = True |
| 307 |
cba9caa |
Dawsh |
2y ago |
db.commit() |
| 308 |
cba9caa |
Dawsh |
2y ago |
|
| 309 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user) |
| 310 |
cba9caa |
Dawsh |
2y ago |
|
| 311 |
cba9caa |
Dawsh |
2y ago |
user = UserResponse.model_validate(db_user) |
| 312 |
cba9caa |
Dawsh |
2y ago |
|
| 313 |
cba9caa |
Dawsh |
2y ago |
logger.info("User `%s` has been enabled", db_user.username) |
| 314 |
cba9caa |
Dawsh |
2y ago |
|
| 315 |
cba9caa |
Dawsh |
2y ago |
return user |
| 316 |
cba9caa |
Dawsh |
2y ago |
|
| 317 |
cba9caa |
Dawsh |
2y ago |
|
| 318 |
cba9caa |
Dawsh |
2y ago |
@router.post("/{username}/disable", response_model=UserResponse) |
| 319 |
cba9caa |
Dawsh |
2y ago |
async def disable_user(db_user: UserDep, db: DBDep, admin: AdminDep): |
| 320 |
cba9caa |
Dawsh |
2y ago |
""" |
| 321 |
cba9caa |
Dawsh |
2y ago |
Disables a user |
| 322 |
cba9caa |
Dawsh |
2y ago |
""" |
| 323 |
cba9caa |
Dawsh |
2y ago |
if not db_user.enabled: |
| 324 |
cba9caa |
Dawsh |
2y ago |
raise HTTPException(409, "User is not enabled") |
| 325 |
cba9caa |
Dawsh |
2y ago |
db_user.enabled = False |
| 326 |
d377c4c |
Dawsh |
2y ago |
db_user.activated = False |
| 327 |
cba9caa |
Dawsh |
2y ago |
db.commit() |
| 328 |
cba9caa |
Dawsh |
2y ago |
|
| 329 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user, remove=True) |
| 330 |
cba9caa |
Dawsh |
2y ago |
|
| 331 |
cba9caa |
Dawsh |
2y ago |
user = UserResponse.model_validate(db_user) |
| 332 |
cba9caa |
Dawsh |
2y ago |
|
| 333 |
cba9caa |
Dawsh |
2y ago |
logger.info("User `%s` has been disabled", db_user.username) |
| 334 |
cba9caa |
Dawsh |
2y ago |
|
| 335 |
cba9caa |
Dawsh |
2y ago |
return user |
| 336 |
cba9caa |
Dawsh |
2y ago |
|
| 337 |
cba9caa |
Dawsh |
2y ago |
|
| 338 |
5bb2712 |
Mardin |
2y ago |
@router.post("/{username}/revoke_sub", response_model=UserResponse) |
| 339 |
f1ab16c |
Dawsh |
2y ago |
async def revoke_user_subscription( |
| 340 |
f1ab16c |
Dawsh |
2y ago |
db_user: UserDep, db: DBDep, admin: AdminDep |
| 341 |
f1ab16c |
Dawsh |
2y ago |
): |
| 342 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 343 |
f7af2b3 |
Dawsh |
2y ago |
Revoke users subscription (Subscription link and proxies) |
| 344 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 345 |
f7af2b3 |
Dawsh |
2y ago |
db_user = crud.revoke_user_sub(db, db_user) |
| 346 |
f7af2b3 |
Dawsh |
2y ago |
|
| 347 |
d377c4c |
Dawsh |
2y ago |
if db_user.is_active: |
| 348 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user, remove=True) |
| 349 |
d377c4c |
Dawsh |
2y ago |
marznode.operations.update_user(db_user) |
| 350 |
f7af2b3 |
Dawsh |
2y ago |
user = UserResponse.model_validate(db_user) |
| 351 |
d377c4c |
Dawsh |
2y ago |
asyncio.ensure_future( |
| 352 |
d377c4c |
Dawsh |
2y ago |
report.user_subscription_revoked(user=user, by=admin) |
| 353 |
d377c4c |
Dawsh |
2y ago |
) |
| 354 |
f7af2b3 |
Dawsh |
2y ago |
|
| 355 |
f7af2b3 |
Dawsh |
2y ago |
logger.info("User %s subscription revoked", db_user.username) |
| 356 |
f7af2b3 |
Dawsh |
2y ago |
|
| 357 |
f7af2b3 |
Dawsh |
2y ago |
return user |
| 358 |
f7af2b3 |
Dawsh |
2y ago |
|
| 359 |
f7af2b3 |
Dawsh |
2y ago |
|
| 360 |
5bb2712 |
Mardin |
2y ago |
@router.get("/{username}/usage", response_model=UserUsagesResponse) |
| 361 |
01cdf2e |
Dawsh |
2y ago |
def get_user_usage( |
| 362 |
01cdf2e |
Dawsh |
2y ago |
db: DBDep, db_user: UserDep, start_date: StartDateDep, end_date: EndDateDep |
| 363 |
01cdf2e |
Dawsh |
2y ago |
): |
| 364 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 365 |
f7af2b3 |
Dawsh |
2y ago |
Get users usage |
| 366 |
f7af2b3 |
Dawsh |
2y ago |
""" |
| 367 |
f7af2b3 |
Dawsh |
2y ago |
usages = crud.get_user_usages(db, db_user, start_date, end_date) |
| 368 |
f7af2b3 |
Dawsh |
2y ago |
|
| 369 |
f7af2b3 |
Dawsh |
2y ago |
return {"usages": usages, "username": db_user.username} |
| 370 |
f7af2b3 |
Dawsh |
2y ago |
|
| 371 |
f7af2b3 |
Dawsh |
2y ago |
|
| 372 |
5bb2712 |
Mardin |
2y ago |
@router.put("/{username}/set-owner", response_model=UserResponse) |
| 373 |
f1ab16c |
Dawsh |
2y ago |
def set_owner( |
| 374 |
f1ab16c |
Dawsh |
2y ago |
username: str, admin_username: str, db: DBDep, admin: SudoAdminDep |
| 375 |
f1ab16c |
Dawsh |
2y ago |
): |
| 376 |
f7af2b3 |
Dawsh |
2y ago |
db_user = crud.get_user(db, username) |
| 377 |
f7af2b3 |
Dawsh |
2y ago |
if not db_user: |
| 378 |
f7af2b3 |
Dawsh |
2y ago |
raise HTTPException(status_code=404, detail="User not found") |
| 379 |
f7af2b3 |
Dawsh |
2y ago |
|
| 380 |
f7af2b3 |
Dawsh |
2y ago |
new_admin = crud.get_admin(db, username=admin_username) |
| 381 |
f7af2b3 |
Dawsh |
2y ago |
if not new_admin: |
| 382 |
f7af2b3 |
Dawsh |
2y ago |
raise HTTPException(status_code=404, detail="Admin not found") |
| 383 |
f7af2b3 |
Dawsh |
2y ago |
|
| 384 |
f7af2b3 |
Dawsh |
2y ago |
db_user = crud.set_owner(db, db_user, new_admin) |
| 385 |
f7af2b3 |
Dawsh |
2y ago |
user = UserResponse.model_validate(db_user) |
| 386 |
f7af2b3 |
Dawsh |
2y ago |
|
| 387 |
f1ab16c |
Dawsh |
2y ago |
logger.info( |
| 388 |
f1ab16c |
Dawsh |
2y ago |
"`%s`'s owner successfully set to `%s`", user.username, admin.username |
| 389 |
f1ab16c |
Dawsh |
2y ago |
) |
| 390 |
f7af2b3 |
Dawsh |
2y ago |
|
| 391 |
f7af2b3 |
Dawsh |
2y ago |
return user |