Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide admin page for unauthorized users #252

Closed
2 tasks done
bralbral opened this issue Jul 31, 2022 · 2 comments
Closed
2 tasks done

Hide admin page for unauthorized users #252

bralbral opened this issue Jul 31, 2022 · 2 comments

Comments

@bralbral
Copy link

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

According documentation we can use middleware for hiding admin page from some users.

If user is not is_authenticated it shows:

Screenshot from 2022-07-31 16-07-21

Steps to reproduce the bug

Just add auth middleware like this:

from sqladmin import Admin, ModelAdmin
from starlette.applications import Starlette
from starlette.requests import Request

# AuthenticationMiddleware explained in Starlette docs

middlewares = [
    Middleware(AuthenticationMiddleware, backend=BasicAuthBackend())
]

app = Starlette()
admin = Admin(app=app, engine=engine, middlewares=middlewares)


class AuthModelAdmin(ModelAdmin):
    def is_accessible(self, request: Request) -> bool:
        # With Authentication backend you can now access request.user.
        if request.user.is_authenticated:
            return True
        return False

Expected behavior

Do not show page; raise error

Actual behavior

No response

Debugging material

No response

Environment

SMP Debian 5.10.106-1 (2022-03-17)
Python 3.9

Additional context

No response

@aminalaee
Copy link
Owner

Hey, thanks for the suggestion. Yes that makes sense.
The problem is there might be some classes not inheriting from AuthModelAdmin so they might be visible in the page.
There should be a generic way to control this via middlewares maybe. Let me take a look.

@aminalaee
Copy link
Owner

I've added basic support for Authentication in #277

I'll update the documentation how to use it, but this will achieve what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants