Reinami / sanic_crud

MIT License
54 stars 12 forks source link

Limit the number of methods available via API. #71

Open jakubjanuzik opened 6 years ago

jakubjanuzik commented 6 years ago

That's more a question, is it possible to limit number of HTTP methods for a resource?

e.g. I'd like only allow POST and GET ?

Or any other way to prevent users from modifying data?

Let's assume I have a BLOG page where admin user should be allowed to create, delete, modify and view posts, but user should be only allowed to fetch list of posts or a particular one. How do I achieve that? Unfortunately could not find anything in the docs.

skewty commented 5 years ago

I / we need this too.

Perhaps something like:

from sanic import Sanic
from sanic_crud import generate_crud
from .model import db, Person, Place

db.create_tables([Person, Place])
app = Sanic(__name__)

def my_authentication_method(app: Sanic, request: Request) ->  bool:
  # requests should be stuffed with request.crud.model: Model, request.crud.operation: str, etc
  # do your checks, decide what to return
    return False

generate_crud(app, [
    Person,  # if isinstance(value, tuple) is False: just a model
    (Place, my_authenticaion_method)  # else it's a tuple with a method
])  # still backwards compatible

app.run(host="0.0.0.0", port=8000, debug=True)
skewty commented 5 years ago

This may indirectly relate to #11 (OpenAPI / Swagger support).