my_rate-stat
All checks were successful
Deploy on push / deploy (push) Successful in 6s

This commit is contained in:
2024-11-12 17:56:20 +03:00
parent 34511a8edf
commit 8116160b4d
7 changed files with 83 additions and 7 deletions

View File

@@ -94,3 +94,39 @@ def login_required(f):
return await f(*args, **kwargs)
return decorated_function
def login_accepted(f):
"""
Декоратор, который добавляет данные авторизации в контекст, если они доступны,
но не блокирует доступ для неавторизованных пользователей.
"""
@wraps(f)
async def decorated_function(*args, **kwargs):
info = args[1]
req = info.context.get("request")
# Пробуем получить данные авторизации
user_id, user_roles = await check_auth(req)
if user_id and user_roles:
# Если пользователь авторизован, добавляем его данные в контекст
logger.info(f" got {user_id} roles: {user_roles}")
info.context["user_id"] = user_id.strip()
info.context["roles"] = user_roles
# Пробуем получить профиль автора
author = await get_cached_author_by_user_id(user_id, get_with_stat)
if not author:
logger.warning(f"author profile not found for user {user_id}")
info.context["author"] = author
else:
# Для неавторизованных пользователей очищаем контекст
info.context["user_id"] = None
info.context["roles"] = None
info.context["author"] = None
return await f(*args, **kwargs)
return decorated_function