acl.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from django.utils.translation import ugettext_lazy as _
  2. from django import forms
  3. from misago.acl.builder import BaseACL
  4. from misago.forms import YesNoSwitch
  5. def make_forum_form(request, role, form):
  6. form.base_fields['can_see_forum'] = forms.BooleanField(widget=YesNoSwitch,initial=False,required=False)
  7. form.base_fields['can_see_forum_contents'] = forms.BooleanField(widget=YesNoSwitch,initial=False,required=False)
  8. form.layout.append((
  9. _("Forums Permissions"),
  10. (
  11. ('can_see_forum', {'label': _("Can see this forum")}),
  12. ('can_see_forum_contents', {'label': _("Can see this forum's contents")}),
  13. ),
  14. ))
  15. class ForumsACL(BaseACL):
  16. def known_forums(self):
  17. return self.acl['can_see']
  18. def can_see(self, forum):
  19. try:
  20. return forum.pk in self.acl['can_see']
  21. except AttributeError:
  22. return forum in self.acl['can_see']
  23. def can_browse(self, forum):
  24. if self.can_see(forum):
  25. try:
  26. return forum.pk in self.acl['can_see']
  27. except AttributeError:
  28. return forum in self.acl['can_see']
  29. return False
  30. def build_forums(acl, perms, forums, forum_roles):
  31. acl.forums = ForumsACL()
  32. acl.forums.acl['can_see'] = []
  33. acl.forums.acl['can_browse'] = []
  34. for forum in forums:
  35. for perm in perms:
  36. try:
  37. role = forum_roles[perm['forums'][forum.pk]]
  38. if role['can_see_forum'] and forum.pk not in acl.forums.acl['can_see']:
  39. acl.forums.acl['can_see'].append(forum.pk)
  40. if role['can_see_forum_contents'] and forum.pk not in acl.forums.acl['can_browse']:
  41. acl.forums.acl['can_browse'].append(forum.pk)
  42. except KeyError:
  43. pass
  44. def cleanup(acl, perms, forums):
  45. for forum in forums:
  46. if forum.pk in acl.forums.acl['can_browse'] and not forum.pk in acl.forums.acl['can_see']:
  47. # First burp: we can read forum but we cant see forum
  48. del acl.forums.acl['can_browse'][acl.forums.acl['can_browse'].index(forum.pk)]
  49. if forum.level > 1:
  50. if forum.parent_id not in acl.forums.acl['can_see'] or forum.parent_id not in acl.forums.acl['can_browse']:
  51. # Second burp: we cant see or read parent forum
  52. try:
  53. del acl.forums.acl['can_see'][acl.forums.acl['can_see'].index(forum.pk)]
  54. except ValueError:
  55. pass
  56. try:
  57. del acl.forums.acl['can_browse'][acl.forums.acl['can_browse'].index(forum.pk)]
  58. except ValueError:
  59. pass