api.py 1.6 KB

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