permissions.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from django.core.exceptions import PermissionDenied
  2. from django.http import Http404
  3. from django.utils.translation import ugettext_lazy as _
  4. from misago.acl import algebra
  5. from misago.acl.decorators import return_boolean
  6. from misago.core import forms
  7. from misago.forums.models import Forum, RoleForumACL, ForumRole
  8. from misago.forums.permissions import get_forums_roles
  9. from misago.threads.models import Thread, Post
  10. """
  11. Admin Permissions Form
  12. """
  13. class PermissionsForm(forms.Form):
  14. legend = _("Threads")
  15. can_see_all_threads = forms.TypedChoiceField(
  16. label=_("Can see threads"),
  17. coerce=int,
  18. initial=0,
  19. choices=((0, _("Started threads")), (1, _("All threads"))))
  20. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  21. can_reply_threads = forms.TypedChoiceField(
  22. label=_("Can reply to threads"),
  23. coerce=int,
  24. initial=0,
  25. choices=((0, _("No")), (1, _("Open threads")), (2, _("All threads"))))
  26. can_edit_threads = forms.TypedChoiceField(
  27. label=_("Can edit threads"),
  28. coerce=int,
  29. initial=0,
  30. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  31. can_edit_replies = forms.TypedChoiceField(
  32. label=_("Can edit replies"),
  33. coerce=int,
  34. initial=0,
  35. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  36. def change_permissions_form(role):
  37. if isinstance(role, ForumRole):
  38. return PermissionsForm
  39. else:
  40. return None
  41. """
  42. ACL Builder
  43. """
  44. def build_acl(acl, roles, key_name):
  45. forums_roles = get_forums_roles(roles)
  46. for forum in Forum.objects.all_forums():
  47. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  48. if forum_acl['can_browse']:
  49. acl['forums'][forum.pk] = build_forum_acl(
  50. forum_acl, forum, forums_roles, key_name)
  51. return acl
  52. def build_forum_acl(acl, forum, forums_roles, key_name):
  53. forum_roles = forums_roles.get(forum.pk, [])
  54. final_acl = {
  55. 'can_see_all_threads': 0,
  56. 'can_start_threads': 0,
  57. }
  58. final_acl.update(acl)
  59. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  60. can_see_all_threads=algebra.greater,
  61. can_start_threads=algebra.greater
  62. )
  63. return final_acl
  64. """
  65. ACL's for targets
  66. """
  67. def add_acl_to_target(user, target):
  68. if isinstance(target, Forum):
  69. add_acl_to_forum(user, target)
  70. if isinstance(target, Thread):
  71. add_acl_to_thread(user, target)
  72. if isinstance(target, Post):
  73. add_acl_to_post(user, target)
  74. def add_acl_to_forum(user, forum):
  75. forum_acl = user.acl['forums'].get(forum.pk, {})
  76. forum.acl['can_see_all_threads'] = forum_acl.get('can_see_all_threads', 0)
  77. if user.is_authenticated():
  78. forum.acl['can_start_threads'] = forum_acl.get('can_start_threads', 0)
  79. else:
  80. forum.acl['can_start_threads'] = 0
  81. def add_acl_to_thread(user, thread):
  82. pass
  83. def add_acl_to_post(user, post):
  84. pass
  85. """
  86. ACL tests
  87. """
  88. def allow_see_thread(user, target):
  89. raise NotImplementedError()
  90. can_see_thread = return_boolean(allow_see_thread)
  91. def allow_start_thread(user, target):
  92. if target.is_closed:
  93. message = _("This forum is closed. You can't start new threads in it.")
  94. raise PermissionDenied(message)
  95. if user.is_anonymous():
  96. raise PermissionDenied(_("You have to sign in to start new thread."))
  97. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  98. raise PermissionDenied(_("You don't have permission to start "
  99. "new threads in this forum."))
  100. can_start_thread = return_boolean(allow_start_thread)