permissions.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_hide_own_threads = forms.TypedChoiceField(
  32. label=_("Can hide own threads"),
  33. help_text=_("Only threads started within time limit and "
  34. "with no replies can be hidden."),
  35. coerce=int,
  36. initial=0,
  37. choices=(
  38. (0, _("No")),
  39. (1, _("Hide threads")),
  40. (2, _("Delete threads"))
  41. ))
  42. thread_edit_time = forms.IntegerField(
  43. label=_("Min. time for own thread edit, in minutes"),
  44. help_text=_("Enter 0 to don't limit time for editing own threads."),
  45. initial=0,
  46. min_value=0)
  47. can_hide_threads = forms.TypedChoiceField(
  48. label=_("Can hide threads"),
  49. coerce=int,
  50. initial=0,
  51. choices=(
  52. (0, _("No")),
  53. (1, _("Hide threads")),
  54. (2, _("Delete threads"))
  55. ))
  56. can_edit_replies = forms.TypedChoiceField(
  57. label=_("Can edit replies"),
  58. coerce=int,
  59. initial=0,
  60. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  61. can_hide_own_replies = forms.TypedChoiceField(
  62. label=_("Can hide own replies"),
  63. help_text=_("Only last replies to thread made within "
  64. "edit time limit can be hidden."),
  65. coerce=int,
  66. initial=0,
  67. choices=(
  68. (0, _("No")),
  69. (1, _("Hide replies")),
  70. (2, _("Delete replies"))
  71. ))
  72. reply_edit_time = forms.IntegerField(
  73. label=_("Min. time for own reply edit, in minutes"),
  74. help_text=_("Enter 0 to don't limit time for editing own replies."),
  75. initial=0,
  76. min_value=0)
  77. can_hide_replies = forms.TypedChoiceField(
  78. label=_("Can hide replies"),
  79. coerce=int,
  80. initial=0,
  81. choices=(
  82. (0, _("No")),
  83. (1, _("Hide replies")),
  84. (2, _("Delete replies"))
  85. ))
  86. can_protect_posts = forms.YesNoSwitch(
  87. label=_("Can protect posts"),
  88. help_text=_("Only users with this permission "
  89. "can edit protected posts."))
  90. can_change_threads_prefix = forms.TypedChoiceField(
  91. label=_("Can change threads prefix"), coerce=int, initial=0,
  92. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  93. can_change_threads_weight = forms.TypedChoiceField(
  94. label=_("Can change threads weight"), coerce=int, initial=0,
  95. choices=(
  96. (0, _("No")),
  97. (1, _("Pin threads")),
  98. (2, _("Make announcements")),
  99. ))
  100. can_close_threads = forms.TypedChoiceField(
  101. label=_("Can close threads"),
  102. coerce=int,
  103. initial=0,
  104. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  105. can_review_moderated_content = forms.YesNoSwitch(
  106. label=_("Can review moderated content"),
  107. help_text=_("Will see and be able to accept moderated content."))
  108. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  109. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  110. def change_permissions_form(role):
  111. if isinstance(role, ForumRole):
  112. return PermissionsForm
  113. else:
  114. return None
  115. """
  116. ACL Builder
  117. """
  118. def build_acl(acl, roles, key_name):
  119. acl['moderated_forums'] = []
  120. forums_roles = get_forums_roles(roles)
  121. for forum in Forum.objects.all_forums():
  122. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  123. if forum_acl['can_browse']:
  124. acl['forums'][forum.pk] = build_forum_acl(
  125. forum_acl, forum, forums_roles, key_name)
  126. return acl
  127. def build_forum_acl(acl, forum, forums_roles, key_name):
  128. forum_roles = forums_roles.get(forum.pk, [])
  129. final_acl = {
  130. 'can_see_all_threads': 0,
  131. 'can_start_threads': 0,
  132. 'can_reply_threads': 0,
  133. 'can_edit_threads': 0,
  134. 'can_edit_replies': 0,
  135. 'can_hide_own_threads': 0,
  136. 'can_hide_own_replies': 0,
  137. 'thread_edit_time': 0,
  138. 'reply_edit_time': 0,
  139. 'can_hide_threads': 0,
  140. 'can_hide_replies': 0,
  141. 'can_protect_posts': 0,
  142. 'can_change_threads_prefix': 0,
  143. 'can_change_threads_weight': 0,
  144. 'can_close_threads': 0,
  145. 'can_review_moderated_content': 0,
  146. 'can_report_content': 0,
  147. 'can_see_reports': 0,
  148. }
  149. final_acl.update(acl)
  150. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  151. can_see_all_threads=algebra.greater,
  152. can_start_threads=algebra.greater,
  153. can_reply_threads=algebra.greater,
  154. can_edit_threads=algebra.greater,
  155. can_edit_replies=algebra.greater,
  156. can_hide_threads=algebra.greater,
  157. can_hide_replies=algebra.greater,
  158. can_hide_own_threads=algebra.greater,
  159. can_hide_own_replies=algebra.greater,
  160. thread_edit_time=algebra.greater_or_zero,
  161. reply_edit_time=algebra.greater_or_zero,
  162. can_protect_posts=algebra.greater,
  163. can_change_threads_prefix=algebra.greater,
  164. can_change_threads_weight=algebra.greater,
  165. can_close_threads=algebra.greater,
  166. can_review_moderated_content=algebra.greater,
  167. can_report_content=algebra.greater,
  168. can_see_reports=algebra.greater,
  169. )
  170. return final_acl
  171. """
  172. ACL's for targets
  173. """
  174. def add_acl_to_target(user, target):
  175. if isinstance(target, Forum):
  176. add_acl_to_forum(user, target)
  177. if isinstance(target, Thread):
  178. add_acl_to_thread(user, target)
  179. if isinstance(target, Post):
  180. add_acl_to_post(user, target)
  181. def add_acl_to_forum(user, forum):
  182. forum_acl = user.acl['forums'].get(forum.pk, {})
  183. forum.acl.update({
  184. 'can_see_all_threads': 0,
  185. 'can_start_threads': 0,
  186. 'can_reply_threads': 0,
  187. 'can_edit_threads': 0,
  188. 'can_edit_replies': 0,
  189. 'can_hide_own_threads': 0,
  190. 'can_hide_own_replies': 0,
  191. 'thread_edit_time': 0,
  192. 'reply_edit_time': 0,
  193. 'can_hide_threads': 0,
  194. 'can_hide_replies': 0,
  195. 'can_protect_posts': 0,
  196. 'can_change_threads_prefix': 0,
  197. 'can_change_threads_weight': 0,
  198. 'can_close_threads': 0,
  199. 'can_review_moderated_content': 0,
  200. 'can_report_content': 0,
  201. 'can_see_reports': 0,
  202. })
  203. if user.is_authenticated():
  204. algebra.sum_acls(forum.acl, acls=[forum_acl],
  205. can_see_all_threads=algebra.greater,
  206. can_start_threads=algebra.greater,
  207. can_reply_threads=algebra.greater,
  208. can_edit_threads=algebra.greater,
  209. can_edit_replies=algebra.greater,
  210. can_hide_threads=algebra.greater,
  211. can_hide_replies=algebra.greater,
  212. can_hide_own_threads=algebra.greater,
  213. can_hide_own_replies=algebra.greater,
  214. thread_edit_time=algebra.greater_or_zero,
  215. reply_edit_time=algebra.greater_or_zero,
  216. can_protect_posts=algebra.greater,
  217. can_change_threads_prefix=algebra.greater,
  218. can_change_threads_weight=algebra.greater,
  219. can_close_threads=algebra.greater,
  220. can_review_moderated_content=algebra.greater,
  221. can_report_content=algebra.greater,
  222. can_see_reports=algebra.greater,
  223. )
  224. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  225. def add_acl_to_thread(user, thread):
  226. pass
  227. def add_acl_to_post(user, post):
  228. pass
  229. """
  230. ACL tests
  231. """
  232. def allow_see_thread(user, target):
  233. forum_acl = user.acl['forums'].get(target.forum_id, {})
  234. if not forum_acl.get('can_see_all_threads'):
  235. if user.is_anonymous() or user.pk != target.starter_id:
  236. message = _("You can't see other users threads in this forum.")
  237. raise PermissionDenied(user)
  238. can_see_thread = return_boolean(allow_see_thread)
  239. def allow_start_thread(user, target):
  240. if target.is_closed:
  241. message = _("This forum is closed. You can't start new threads in it.")
  242. raise PermissionDenied(message)
  243. if user.is_anonymous():
  244. raise PermissionDenied(_("You have to sign in to start new thread."))
  245. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  246. raise PermissionDenied(_("You don't have permission to start "
  247. "new threads in this forum."))
  248. can_start_thread = return_boolean(allow_start_thread)