{% macro nav_item(request, route_name, text) -%}
{% if request.view_execution_permitted(route_name) %}
<li>
<a href="{{ request.route_url(route_name) }}">{{ text }}</a>
</li>
{% endif %}
{%- endmacro %}
I'm looking for a function to fit in this usage:{% macro nav_item(request, route_name, text) -%}
{% if request.view_execution_permitted(route_name) %}
<li>
<a href="{{ request.route_url(route_name) }}">{{ text }}</a>
</li>
{% endif %}
{%- endmacro %}
My problems are the following:1. view_execution_permitted doesn't work like this, unlike other security functions, for example request.has_permission(). Why?
2. Going the hard way and making a custom wrapper around view_execution_permitted, and adding it to request via add_request_method, I'm still stuck in how to use view_execution_permitted, for the following reason:
2.1. It needs a context. What is a context? I never had to use any context in URL Dispatch with SessionAuthenticationPolicy, and it really isn't explained on the website. I tried Googling view_execution_permitted and grepping for code in Github, but I couldn't find anything, except a Github issue ticket saying: "view_execution_permitted does not work with URL dispatch", which didn't help me.
2.2. It needs a name. Is this route_name? I hope so?
Maybe I don't even need view_execution_permitted. I just want a simple request.is_allowed_route(route_name). How can I do it?
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/a5285237-b1a5-44e5-bd5e-3fd0e4b11c44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class RootFactory(object):
__acl__ = [
(Allow, Authenticated, 'user'),
(Allow, 'g:admin', 'admin'),
(Allow, 'g:superadmin', 'ALL_PERMISSIONS'),
]
def __init__(self, request):
pass
config = Configurator(
settings=settings,
root_factory=RootFactory,
authentication_policy=authn_policy,
authorization_policy=authz_policy,
session_factory=session_factory)@view_config(route_name='admin_db_list', renderer='admin/db_list.jinja2', permission='superadmin')
def db_list(request): ...
And my views are defined like this:@view_config(route_name='admin_db_list', renderer='admin/db_list.jinja2', permission='superadmin')
def db_list(request): ...So in this situation, my context is request.root (or request.context), is this right?If I try view_execution_permitted(request.root, request, name='admin_db_list'), I get an "TypeError: No registered view satisfies the constraints."
Do I understand correctly that the name should be a @view_config name _and_ this means using traversal, so I should just forget about using it?
=> So in conclusion, I can only use request.has_permission and duplicate the permission values in template as well?
On Thursday, 10 November 2016 22:50:37 UTC+1, Mikko Ohtamaa wrote:And to elaborate the following:I simply check for the permission I know the target has using request.has_permission():
https://websauna.org/docs/narrative/user/permissions.html?highlight=permissions#checking-permissions-in-templates- Define a Root object- In this root you have a dynamic __acl__() property that gives logged in users permissions based on their user id or group id- In your view you have @view_config(permission="my_permission")Example of setting a custom root:
https://websauna.org/docs/_modules/websauna/system.html#Initializer.configure_rootSome examples of dynamic __acl__
https://github.com/websauna/websauna.blog/blob/master/websauna/blog/views.py#L45
https://websauna.org/docs/narrative/crud/standalone.html?highlight=contract#creating-crud-resources-M
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/b5dd965d-4d4b-48a1-b6c5-fe60eae13c57%40googlegroups.com.
This might or might not work, but looks complicated enough for me not to know if there is a possible bug in it, that I'll just stick with has_permission and duplicated values in templates.
Thanks a lot! I'm thinking about it, since I'm quite close (in Budapest).
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/5824f9e68e02ab00000d2412%40polymail.io.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/33BD1C53-1461-409E-B260-E00B31FCB168%40gmail.com.
def route_allowed(request, route_name):
from zope.interface import providedBy
from pyramid.interfaces import IRouteRequest
from pyramid.interfaces import IRequest
from pyramid.view import _find_views
reg = request.registry
request_iface = reg.queryUtility(IRouteRequest, name=route_name, default=IRequest)
context_iface = providedBy(request.context)
views = _find_views(reg, request_iface, context_iface, '')
assert len(views) == 1
view = views[0]
permission = view.__permission__
return bool(request.has_permission(permission))--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/648eb04e-4036-4161-985a-c7317e8cd653%40googlegroups.com.To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.