Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Add IGNORE_USER_PERMISSIONS setting #705

Closed

Conversation

craiga
Copy link

@craiga craiga commented Oct 18, 2017

Makes it possible to ignore user permissions when generating a schema.

I have an endpoint which lists search results (/v1/search_results/), but I'm only given access if I specify a search ID on the query string (/v1/search_results/?search=456). My code checks that the logged in user owns search 456 before allowing the user to access this endpoint.

Without this setting, /v1/search_results/ won't appear in the generated documentation.

@craiga craiga changed the title Add IGNORE_USER_PERMISSIONS setting. Add IGNORE_USER_PERMISSIONS setting Oct 18, 2017
@nicholasserra
Copy link

I've also run into this and think it's a great idea. I'm also running into issues where my permissions are causing my swagger docs to 500, as the state is much different when displaying the schema vs actually hitting the API.

I don't think permissions should be necessary when simply mapping out the urls.

+1 !

@nicholasserra
Copy link

@craiga do you know of a workaround on a per-view basis I could implement in the meantime? Looking like something around custom schemas attached to my views might work?

@craiga
Copy link
Author

craiga commented Dec 19, 2017

@nicholasserra Sorry, but I don’t. I’ve been using my own branch with this PR included for a while now and it seems to be working fine.

@ghost
Copy link

ghost commented Oct 19, 2018

Is this going to be merged into master anytime soon? Since I see that the PR is hanging for a couple of months already and we are in need of this to simplify things.

@craiga
Copy link
Author

craiga commented Oct 19, 2018

@lukzmu I just noticed that Marc posted a call for help to deal with stuff like this, so it might be a while unless someone's able to volunteer to help out.

@ghost
Copy link

ghost commented Oct 19, 2018

Oh just saw that as well, think we will have to import the PR for now then :)
Hopefully this will be sorted out in the future and hope @marcgibbons will get help :)

@ghost ghost mentioned this pull request Oct 19, 2018
Copy link

@oneandonlyonebutyou oneandonlyonebutyou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@oneandonlyonebutyou
Copy link

Could someone please approve this to push this forward.

I would really appreciate it.

@BenjaminHabert
Copy link

I just tested the proposed pull request. It works great for me with one exception. I have one View that uses the get_serializer_class(self) method. In this method I use self.request.method which fails as request is None when running with the IGNORE_USER_PERMISSIONS setting.

It is easy enough for me to adapt the get_serializer_class() code to take this case into account. I am in favor of this pull request; thanks.

@craiga
Copy link
Author

craiga commented Feb 7, 2019

@BenjaminHabert Can you provide a stack trace of where that problem is happening?

The docs state that the request argument to get_schema is optional, so I wonder if self.request being None is valid when generating the schema.

@BenjaminHabert
Copy link

here is the stacktrace:

Internal Server Error: /swagger/
Traceback (most recent call last):
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework_swagger/views.py", line 32, in get
    schema = generator.get_schema()
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 281, in get_schema
    links = self.get_links(None if public else request)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 319, in get_links
    link = view.schema.get_link(path, method, base_url=self.url)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 215, in get_link
    fields += self.get_serializer_fields(path, method)
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 336, in get_serializer_fields
    serializer = view.get_serializer()
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/.venv/lib/python3.6/site-packages/rest_framework/generics.py", line 110, in get_serializer
    serializer_class = self.get_serializer_class()
  File "/Users/benjamin/Documents/Quantmetry_Missions/Plasidia/moon-api/moon/glycemia/views.py", line 88, in get_serializer_class
    if self.request.method == 'GET':
AttributeError: 'NoneType' object has no attribute 'method'
[07/Feb/2019 15:23:11] "GET /swagger/ HTTP/1.1" 500 18760

And here is the culprit (which was easy enough to modify and protect against this error):

# file: moon-api/moon/glycemia/views.py
class GlycemiaAPIView(generics.ListCreateAPIView):
    def get_serializer_class(self):
        if self.request.method == 'GET':
            return GlycemiaSerializer
        return GlycemiaListCreateSerializer

@craiga
Copy link
Author

craiga commented Feb 8, 2019

@BenjaminHabert Thanks for that. self.request being None is the correct behaviour here … are you comfortable with that?

@BenjaminHabert
Copy link

@craiga yes

Copy link

@BenjaminHabert BenjaminHabert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been using this proposed version for a few months. I think this change is useful and can be accepted

@aliaksandrmelnik1
Copy link

so, when you a planning to merge this PR to master?

@ulgens
Copy link

ulgens commented Oct 10, 2019

Any progress on this?

@craiga
Copy link
Author

craiga commented Oct 14, 2019

As this project is deprecated, I'm closing this PR to make this clear to subsequent visitors. Check the project's readme for more info and an alternative library to use.

@craiga craiga closed this Oct 14, 2019
@ulgens
Copy link

ulgens commented Oct 14, 2019

@craiga Thanks for the response. What will be the other open issues and PRs? I think it would be better if you "archive" the project and make it read only.

@craiga
Copy link
Author

craiga commented Oct 14, 2019

@ulgens I'm not sure. I have no special status in this project, I just happened to open a moderately popular pull request. ;)

@marcgibbons What do you think of archiving this project?

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

Successfully merging this pull request may close these issues.

None yet

6 participants