mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2026-03-13 10:32:25 +08:00
Make get_current_admin error to 401 and add 401 error page.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
- Fix `get_m2m_field`.
|
||||
- Refactor `ComputeField` and remove `get_compute_fields`.
|
||||
- Upgrade `aioredis` to `2.0`.
|
||||
- Make `get_current_admin` error to `401` and add `401` error page.
|
||||
|
||||
### 1.0.2
|
||||
|
||||
|
||||
@@ -6,7 +6,12 @@ from fastapi import FastAPI
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
from starlette.responses import RedirectResponse
|
||||
from starlette.staticfiles import StaticFiles
|
||||
from starlette.status import HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND, HTTP_500_INTERNAL_SERVER_ERROR
|
||||
from starlette.status import (
|
||||
HTTP_401_UNAUTHORIZED,
|
||||
HTTP_403_FORBIDDEN,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
from tortoise.contrib.fastapi import register_tortoise
|
||||
|
||||
from examples import settings
|
||||
@@ -18,6 +23,7 @@ from fastapi_admin.exceptions import (
|
||||
forbidden_error_exception,
|
||||
not_found_error_exception,
|
||||
server_error_exception,
|
||||
unauthorized_error_exception,
|
||||
)
|
||||
|
||||
|
||||
@@ -36,6 +42,7 @@ def create_app():
|
||||
admin_app.add_exception_handler(HTTP_500_INTERNAL_SERVER_ERROR, server_error_exception)
|
||||
admin_app.add_exception_handler(HTTP_404_NOT_FOUND, not_found_error_exception)
|
||||
admin_app.add_exception_handler(HTTP_403_FORBIDDEN, forbidden_error_exception)
|
||||
admin_app.add_exception_handler(HTTP_401_UNAUTHORIZED, unauthorized_error_exception)
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import List, Optional, Type
|
||||
from fastapi import Depends, HTTPException
|
||||
from fastapi.params import Path
|
||||
from starlette.requests import Request
|
||||
from starlette.status import HTTP_404_NOT_FOUND
|
||||
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND
|
||||
from tortoise import Tortoise
|
||||
|
||||
from fastapi_admin.exceptions import InvalidResource
|
||||
@@ -68,5 +68,5 @@ def get_redis(request: Request):
|
||||
def get_current_admin(request: Request):
|
||||
admin = request.state.admin
|
||||
if not admin:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND)
|
||||
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED)
|
||||
return admin
|
||||
|
||||
@@ -54,3 +54,9 @@ async def forbidden_error_exception(request: Request, exc: HTTPException):
|
||||
return templates.TemplateResponse(
|
||||
"errors/403.html", status_code=exc.status_code, context={"request": request}
|
||||
)
|
||||
|
||||
|
||||
async def unauthorized_error_exception(request: Request, exc: HTTPException):
|
||||
return templates.TemplateResponse(
|
||||
"errors/401.html", status_code=exc.status_code, context={"request": request}
|
||||
)
|
||||
|
||||
27
fastapi_admin/templates/errors/401.html
Normal file
27
fastapi_admin/templates/errors/401.html
Normal file
@@ -0,0 +1,27 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block body %}
|
||||
<div class="page page-center">
|
||||
<div class="container-tight py-4">
|
||||
<div class="empty">
|
||||
<div class="empty-header">401</div>
|
||||
<p class="empty-title">Oops… You are unauthorized</p>
|
||||
<p class="empty-subtitle text-muted">
|
||||
We are sorry but the page you are looking for need authorization.
|
||||
</p>
|
||||
<div class="empty-action">
|
||||
<a href="{{ request.app.admin_path }}" class="btn btn-primary">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
|
||||
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
|
||||
stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||||
<line x1="5" y1="12" x2="19" y2="12"/>
|
||||
<line x1="5" y1="12" x2="11" y2="18"/>
|
||||
<line x1="5" y1="12" x2="11" y2="6"/>
|
||||
</svg>
|
||||
{{ _('return_home') }}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user