Skip to content

Commit

Permalink
fix: support jump back to page require login
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 19, 2024
1 parent a14a947 commit dc2d42a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def index(request: Request) -> Template:
)


def __index_row_sorter(r: asyncpg.Record):
def __index_row_sorter(r: asyncpg.Record) -> tuple[int, datetime]:
if r["state"] == PatchState.Pending:
return 1, r["created_at"]

Expand Down
10 changes: 8 additions & 2 deletions server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def retrieve_user_from_session(
try:
return __user_from_session(session)
except KeyError:
req.clear_session()
return None


def __user_from_session(session: dict[str, Any]) -> User:
Expand Down Expand Up @@ -121,6 +121,12 @@ async def callback(code: str, request: Request) -> Redirect:

group_id = user["user_group"]

# litestar type this as dict[str, Any], but it maybe Empty
if request.session is not Empty: # type: ignore
back_to = request.session.get("backTo", "/")
else:
back_to = "/"

request.set_session(
{
"user_id": user_id,
Expand All @@ -132,7 +138,7 @@ async def callback(code: str, request: Request) -> Redirect:
}
)

return Redirect("/")
return Redirect(back_to)


def require_user_editor(connection: ASGIConnection[Any, Any, Any, Any], _: Any) -> None:
Expand Down
10 changes: 8 additions & 2 deletions server/contrib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from dataclasses import dataclass
from datetime import datetime
from typing import Annotated
from typing import Annotated, Any

import litestar
import orjson
from litestar import Response
from litestar.enums import RequestEncodingType
from litestar.exceptions import (
HTTPException,
Expand All @@ -21,9 +22,14 @@


@litestar.get("/suggest")
async def suggest_ui(subject_id: int = 0) -> Template:
async def suggest_ui(request: Request, subject_id: int = 0) -> Response[Any]:
if subject_id == 0:
return Template("select-subject.html.jinja2")

if not request.auth:
request.set_session({"backTo": request.url.path + f"?subject_id={subject_id}"})
return Redirect("/login")

async with http_client.get(
f"https://next.bgm.tv/p1/wiki/subjects/{subject_id}", allow_redirects=False
) as res:
Expand Down
4 changes: 0 additions & 4 deletions server/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ def is_access_token_fresh(self) -> bool:
def allow_edit(self) -> bool:
return self.group_id in {2, 11}

@property
def allow_admin(self) -> bool:
return self.group_id in {2}


class PatchState(enum.IntEnum):
Pending = 0
Expand Down

0 comments on commit dc2d42a

Please sign in to comment.