-
Notifications
You must be signed in to change notification settings - Fork 148
Closed
Description
I'm trying to check if a user posting a discussion has access to the topic they are trying to associate the discussion with. However when the user posts, the second argument in the predicate, object
is always None
- I need to access the post body to check the topic
attribute and compare to the users groups.
How do I access the post object? Is it available in the predicate context somewhere if I bind the predicate?
This is the logic I have so far which works for reading discussion objects, but it looks like I need different logic for POST requests due to the object being null:
@rules.predicate
def is_allowed(user, content):
"""
| Accepts a user and content object and checks if the user is allowed to view it.
:param user:
:param content:
:return:
"""
if content is None or not (hasattr(content, 'topic') or not hasattr(content, 'group')):
LOGGER.debug('Content was null or did not have topic')
LOGGER.debug('Content: %s', repr(content))
LOGGER.debug('User: %s', repr(user))
return False
if hasattr(content, 'group'):
if content.group is None or not hasattr(content.group, 'name'):
LOGGER.debug('Topic group was none or did not have name')
return True
LOGGER.debug('Checking if member is in group %s', content.group.name)
if content.group.name in user.groups.all():
return True
if hasattr(content, 'topic'):
if content.topic.group is None or not hasattr(content.topic.group, 'name'):
LOGGER.debug('Topic group was none or did not have name')
return True
LOGGER.debug('Checking if member is in group %s', content.topic.group.name)
if content.topic.group.name in user.groups.all():
return True
return False
Metadata
Metadata
Assignees
Labels
No labels