For the purpose of this discussion, I'm going to suggest that you are using SQLAlchemy to interact with your database.
If you have config.add_route('pages', '/pages/{id}')
in __init__.py
, you can add a custom factory to replace / add the default ACL. For instance:
The current ACL might look like this:
class RootFactory(object): __acl__ = [ (Allow, Everyone, 'view'), (Allow, Authenticated, 'auth'), ] def __init__(self, request): self.request = request
This will allow Authenticated users to access any view with the permission of "auth" and anyone who visits your site to access any view with the permission of "view".
Using a custom factory , you can either bypass RootFactory or add it.
To work around , change the original config.add_route file to β config.add_route('pages', '/pages/{id}', factory=PageFactory)
and create the PageFactory class as follows:
class PageFactory(object): __acl__ = [ (Allow, Everyone, 'view'), (Allow, Authenticated, 'auth'), ] def __init__(self, request): self.request = request from pyramid.security import authenticated_userid user_id = authenticated_userid(self.request) thispage = DBSession.query(Page).filter(Page.id==self.request.matchdict['id']).first() if thispage.user_id == user_id:
This assumes your view has permission='edit'
as one of its parameters.
Now, if you want to use RootFactory and the add- on with your custom factory , so you donβt need to repeat yourself, just leave you RootFactory, as I showed at the beginning of this post, and inherit from the RootFactory class as such:
class PageFactory(RootFactory): @property def __acl__(self): acl = super(PageFactory, self).__acl__[:]
groupfinder is very useful, by the way, because then you can just place users in groups, such as "admin", and everything in the admin group can access the views using permission='whatever'
or permission='whateverelse'
that you may need, and Factory is not required, only a groupfinder that returns a list of groups for the current user. Alas, I digress, because this is not what you wanted to do. Hope this answers your question.