api.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. Module functions for ACLs
  3. Workflow for ACLs in Misago is simple:
  4. First, you get user ACL. Its directory that you can introspect to find out user
  5. permissions, or if you have objects, you can use this acl to make those objects
  6. aware of their ACLs. This gives objects themselves special "acl" attribute with
  7. properties defined by ACL providers within their "add_acl_to_target"
  8. """
  9. import copy
  10. from misago.core import threadstore
  11. from misago.core.cache import cache
  12. from . import version
  13. from .buildacl import build_acl
  14. from .providers import providers
  15. def get_user_acl(user):
  16. """get ACL for User"""
  17. acl_key = 'acl_%s' % user.acl_key
  18. acl_cache = threadstore.get(acl_key)
  19. if not acl_cache:
  20. acl_cache = cache.get(acl_key)
  21. if acl_cache and version.is_valid(acl_cache.get('_acl_version')):
  22. return acl_cache
  23. else:
  24. new_acl = build_acl(user.get_roles())
  25. new_acl['_acl_version'] = version.get_version()
  26. threadstore.set(acl_key, new_acl)
  27. cache.set(acl_key, new_acl)
  28. return new_acl
  29. def add_acl(user_acl, target):
  30. """add valid ACL to target (iterable of objects or single object)"""
  31. if hasattr(target, '__iter__'):
  32. for item in target:
  33. _add_acl_to_target(user_acl, item)
  34. else:
  35. _add_acl_to_target(user_acl, target)
  36. def _add_acl_to_target(user_acl, target):
  37. """add valid ACL to single target, helper for add_acl function"""
  38. target.acl = {}
  39. for annotator in providers.get_obj_type_annotators(target):
  40. annotator(user_acl, target)
  41. def serialize_acl(target):
  42. """serialize authenticated user's ACL"""
  43. serialized_acl = copy.deepcopy(target.acl_cache)
  44. for serializer in providers.get_obj_type_serializers(target):
  45. serializer(serialized_acl)
  46. return serialized_acl