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

provide better example of how to use forbidden exception in tutorials #433

Closed
mmerickel opened this issue Feb 11, 2012 · 6 comments
Closed
Labels

Comments

@mmerickel
Copy link
Member

Currently the tutorial describes a poor way to use the forbidden exception. The exception should be caught by a dedicated view that can handle authentication because usually it's a bad idea to display the login view for a user who is already logged in... which is what the current code does. Also, what's the point of having a "login url" if we are just displaying the login template at the url of the content, instead of redirecting the user to the login url.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/tutorials/wiki2/authorization.html#adding-login-and-logout-views

@mcdonc
Copy link
Member

mcdonc commented Feb 11, 2012

I agree with the above, I suppose, although in reality what the login-form-invoked-as-the-result-of-an-HTTPForbidden-when-the-user-already-is-not-just-Everyone could say is "your current credentials do not allow you to perform the specified operation, but you can try to log in with another set of credentials if you like, and the operation will then be performed if those credentials are permitted to perform the operation". This is also the answer to the second question above.

@simonyarde
Copy link

Please see related issue #436

@mcdonc
Copy link
Member

mcdonc commented Feb 14, 2012

Disregarding #436 temporarily (that issue could go on for weeks), can someone suggest some concrete changes to the tutorial that makes it "more correct" or whatever without requiring changes to Pyramid.

@merwok
Copy link
Contributor

merwok commented May 21, 2013

I’m writing something along these lines: I have a special template to prompt people to log in when AuthTktAuthenticationPolicy raises HTTPForbidden, and I want another template to be used when ACLAuthorizationPolicy denies a user. Permissions are not predicate parameters but effective_principal is, so if I am successful with that (and thus get things working with two view_configs intead of an ugly if in one view), I’ll report here. Reading the linked bug about 401 vs. 403 sure was enlightening :)

@merwok
Copy link
Contributor

merwok commented May 21, 2013

That’s a raw copy-paste of what I did:

@view_config(context=HTTPForbidden,
             permission=NO_PERMISSION_REQUIRED,
             renderer='login.mako')
def login_button(context, request):
    """Special template is used when not logged in."""
    # preserve status code
    request.response.status_code = context.code
    return {}


@view_config(context=HTTPForbidden,
             effective_principals=Authenticated,
             # no permission is needed because of default permission 'view'
             permission=NO_PERMISSION_REQUIRED,
             renderer='denied.mako')
def forbidden_message(context, request):
    """Template body used for denied actions.

    For historical reasons, the authn system in Pyramid raises
    HTTPForbidden instead of the arguably more correct HTTPUnauthorized
    exception, so we need custom predicates to make a difference between
    "not logged in" (login_button) and "not authorized" (this view).
    """
    request.response.status_code = context.code
    return {}

@mmerickel
Copy link
Member Author

The wiki2 tutorial (#2024) was rewritten to use a proper forbidden view that redirects to the login view. Marking this as completed.

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

No branches or pull requests

4 participants