permissions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. from django.core.exceptions import PermissionDenied
  2. from django.db.models import Q
  3. from django.http import Http404
  4. from django.utils.translation import ugettext_lazy as _
  5. from misago.acl import algebra
  6. from misago.acl.decorators import return_boolean
  7. from misago.core import forms
  8. from misago.forums.models import Forum, RoleForumACL, ForumRole
  9. from misago.forums.permissions import get_forums_roles
  10. from misago.threads.models import Thread, Post, Event
  11. """
  12. Admin Permissions Form
  13. """
  14. class PermissionsForm(forms.Form):
  15. legend = _("Threads")
  16. can_see_all_threads = forms.TypedChoiceField(
  17. label=_("Can see threads"),
  18. coerce=int,
  19. initial=0,
  20. choices=((0, _("Started threads")), (1, _("All threads"))))
  21. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  22. can_reply_threads = forms.TypedChoiceField(
  23. label=_("Can reply to threads"),
  24. coerce=int,
  25. initial=0,
  26. choices=((0, _("No")), (1, _("Open threads")), (2, _("All threads"))))
  27. can_edit_threads = forms.TypedChoiceField(
  28. label=_("Can edit threads"),
  29. coerce=int,
  30. initial=0,
  31. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  32. can_hide_own_threads = forms.TypedChoiceField(
  33. label=_("Can hide own threads"),
  34. help_text=_("Only threads started within time limit and "
  35. "with no replies can be hidden."),
  36. coerce=int,
  37. initial=0,
  38. choices=(
  39. (0, _("No")),
  40. (1, _("Hide threads")),
  41. (2, _("Delete threads"))
  42. ))
  43. thread_edit_time = forms.IntegerField(
  44. label=_("Min. time for own thread edit, in minutes"),
  45. help_text=_("Enter 0 to don't limit time for editing own threads."),
  46. initial=0,
  47. min_value=0)
  48. can_hide_threads = forms.TypedChoiceField(
  49. label=_("Can hide threads"),
  50. coerce=int,
  51. initial=0,
  52. choices=(
  53. (0, _("No")),
  54. (1, _("Hide threads")),
  55. (2, _("Delete threads"))
  56. ))
  57. can_edit_replies = forms.TypedChoiceField(
  58. label=_("Can edit replies"),
  59. coerce=int,
  60. initial=0,
  61. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  62. can_hide_own_replies = forms.TypedChoiceField(
  63. label=_("Can hide own replies"),
  64. help_text=_("Only last replies to thread made within "
  65. "edit time limit can be hidden."),
  66. coerce=int,
  67. initial=0,
  68. choices=(
  69. (0, _("No")),
  70. (1, _("Hide replies")),
  71. (2, _("Delete replies"))
  72. ))
  73. reply_edit_time = forms.IntegerField(
  74. label=_("Min. time for own reply edit, in minutes"),
  75. help_text=_("Enter 0 to don't limit time for editing own replies."),
  76. initial=0,
  77. min_value=0)
  78. can_hide_replies = forms.TypedChoiceField(
  79. label=_("Can hide replies"),
  80. coerce=int,
  81. initial=0,
  82. choices=(
  83. (0, _("No")),
  84. (1, _("Hide replies")),
  85. (2, _("Delete replies"))
  86. ))
  87. can_protect_posts = forms.YesNoSwitch(
  88. label=_("Can protect posts"),
  89. help_text=_("Only users with this permission "
  90. "can edit protected posts."))
  91. can_change_threads_labels = forms.TypedChoiceField(
  92. label=_("Can change threads labels"), coerce=int, initial=0,
  93. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  94. can_change_threads_weight = forms.TypedChoiceField(
  95. label=_("Can change threads weight"), coerce=int, initial=0,
  96. choices=(
  97. (0, _("No")),
  98. (1, _("Pin threads")),
  99. (2, _("Make announcements")),
  100. ))
  101. can_close_threads = forms.TypedChoiceField(
  102. label=_("Can close threads"),
  103. coerce=int,
  104. initial=0,
  105. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  106. can_review_moderated_content = forms.YesNoSwitch(
  107. label=_("Can review moderated content"),
  108. help_text=_("Will see and be able to accept moderated content."))
  109. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  110. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  111. can_hide_events = forms.TypedChoiceField(
  112. label=_("Can hide events"),
  113. coerce=int,
  114. initial=0,
  115. choices=(
  116. (0, _("No")),
  117. (1, _("Hide events")),
  118. (2, _("Delete events"))
  119. ))
  120. def change_permissions_form(role):
  121. if isinstance(role, ForumRole):
  122. return PermissionsForm
  123. else:
  124. return None
  125. """
  126. ACL Builder
  127. """
  128. def build_acl(acl, roles, key_name):
  129. acl['moderated_forums'] = []
  130. forums_roles = get_forums_roles(roles)
  131. for forum in Forum.objects.all_forums():
  132. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  133. if forum_acl['can_browse']:
  134. acl['forums'][forum.pk] = build_forum_acl(
  135. forum_acl, forum, forums_roles, key_name)
  136. return acl
  137. def build_forum_acl(acl, forum, forums_roles, key_name):
  138. forum_roles = forums_roles.get(forum.pk, [])
  139. final_acl = {
  140. 'can_see_all_threads': 0,
  141. 'can_start_threads': 0,
  142. 'can_reply_threads': 0,
  143. 'can_edit_threads': 0,
  144. 'can_edit_replies': 0,
  145. 'can_hide_own_threads': 0,
  146. 'can_hide_own_replies': 0,
  147. 'thread_edit_time': 0,
  148. 'reply_edit_time': 0,
  149. 'can_hide_threads': 0,
  150. 'can_hide_replies': 0,
  151. 'can_protect_posts': 0,
  152. 'can_change_threads_labels': 0,
  153. 'can_change_threads_weight': 0,
  154. 'can_close_threads': 0,
  155. 'can_review_moderated_content': 0,
  156. 'can_report_content': 0,
  157. 'can_see_reports': 0,
  158. 'can_can_hide_events': 0,
  159. }
  160. final_acl.update(acl)
  161. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  162. can_see_all_threads=algebra.greater,
  163. can_start_threads=algebra.greater,
  164. can_reply_threads=algebra.greater,
  165. can_edit_threads=algebra.greater,
  166. can_edit_replies=algebra.greater,
  167. can_hide_threads=algebra.greater,
  168. can_hide_replies=algebra.greater,
  169. can_hide_own_threads=algebra.greater,
  170. can_hide_own_replies=algebra.greater,
  171. thread_edit_time=algebra.greater_or_zero,
  172. reply_edit_time=algebra.greater_or_zero,
  173. can_protect_posts=algebra.greater,
  174. can_change_threads_labels=algebra.greater,
  175. can_change_threads_weight=algebra.greater,
  176. can_close_threads=algebra.greater,
  177. can_review_moderated_content=algebra.greater,
  178. can_report_content=algebra.greater,
  179. can_see_reports=algebra.greater,
  180. can_can_hide_events=algebra.greater,
  181. )
  182. return final_acl
  183. """
  184. ACL's for targets
  185. """
  186. def add_acl_to_target(user, target):
  187. if isinstance(target, Forum):
  188. add_acl_to_forum(user, target)
  189. if isinstance(target, Thread):
  190. add_acl_to_thread(user, target)
  191. if isinstance(target, Post):
  192. add_acl_to_post(user, target)
  193. if isinstance(target, Event):
  194. add_acl_to_event(user, target)
  195. def add_acl_to_forum(user, forum):
  196. forum_acl = user.acl['forums'].get(forum.pk, {})
  197. forum.acl.update({
  198. 'can_see_all_threads': 0,
  199. 'can_start_threads': 0,
  200. 'can_reply_threads': 0,
  201. 'can_edit_threads': 0,
  202. 'can_edit_replies': 0,
  203. 'can_hide_own_threads': 0,
  204. 'can_hide_own_replies': 0,
  205. 'thread_edit_time': 0,
  206. 'reply_edit_time': 0,
  207. 'can_hide_threads': 0,
  208. 'can_hide_replies': 0,
  209. 'can_protect_posts': 0,
  210. 'can_change_threads_labels': 0,
  211. 'can_change_threads_weight': 0,
  212. 'can_close_threads': 0,
  213. 'can_review_moderated_content': 0,
  214. 'can_report_content': 0,
  215. 'can_see_reports': 0,
  216. 'can_can_hide_events': 0,
  217. })
  218. algebra.sum_acls(forum.acl, acls=[forum_acl],
  219. can_see_all_threads=algebra.greater)
  220. if user.is_authenticated():
  221. algebra.sum_acls(forum.acl, acls=[forum_acl],
  222. can_start_threads=algebra.greater,
  223. can_reply_threads=algebra.greater,
  224. can_edit_threads=algebra.greater,
  225. can_edit_replies=algebra.greater,
  226. can_hide_threads=algebra.greater,
  227. can_hide_replies=algebra.greater,
  228. can_hide_own_threads=algebra.greater,
  229. can_hide_own_replies=algebra.greater,
  230. thread_edit_time=algebra.greater_or_zero,
  231. reply_edit_time=algebra.greater_or_zero,
  232. can_protect_posts=algebra.greater,
  233. can_change_threads_labels=algebra.greater,
  234. can_change_threads_weight=algebra.greater,
  235. can_close_threads=algebra.greater,
  236. can_review_moderated_content=algebra.greater,
  237. can_report_content=algebra.greater,
  238. can_see_reports=algebra.greater,
  239. can_can_hide_events=algebra.greater,
  240. )
  241. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  242. def add_acl_to_thread(user, thread):
  243. pass
  244. def add_acl_to_post(user, post):
  245. pass
  246. def add_acl_to_event(user, event):
  247. forum_acl = user.acl['forums'].get(event.forum_id, {})
  248. can_hide_events = forum_acl.get('can_can_hide_events', 0)
  249. event.acl['can_hide'] = can_hide_events > 0
  250. event.acl['can_delete'] = can_hide_events == 2
  251. """
  252. ACL tests
  253. """
  254. def allow_see_thread(user, target):
  255. forum_acl = user.acl['forums'].get(target.forum_id, {})
  256. if not forum_acl.get('can_see_all_threads'):
  257. if user.is_anonymous() or user.pk != target.starter_id:
  258. message = _("You can't see other users threads in this forum.")
  259. raise PermissionDenied(user)
  260. can_see_thread = return_boolean(allow_see_thread)
  261. def allow_start_thread(user, target):
  262. if target.is_closed:
  263. message = _("This forum is closed. You can't start new threads in it.")
  264. raise PermissionDenied(message)
  265. if user.is_anonymous():
  266. raise PermissionDenied(_("You have to sign in to start new thread."))
  267. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  268. raise PermissionDenied(_("You don't have permission to start "
  269. "new threads in this forum."))
  270. can_start_thread = return_boolean(allow_start_thread)
  271. """
  272. Queryset helpers
  273. """
  274. def exclude_invisible_threads(user, forum, queryset):
  275. if user.is_authenticated():
  276. condition_author = Q(starter_id=user.id)
  277. can_mod = forum.acl['can_review_moderated_content']
  278. can_hide = forum.acl['can_hide_threads']
  279. if not can_mod and not can_hide:
  280. condition = Q(is_moderated=False) & Q(is_hidden=False)
  281. queryset = queryset.filter(condition_author | condition)
  282. elif not can_mod:
  283. condition = Q(is_moderated=False)
  284. queryset = queryset.filter(condition_author | condition)
  285. elif not can_hide:
  286. condition = Q(is_hidden=False)
  287. queryset = queryset.filter(condition_author | condition)
  288. else:
  289. if not forum.acl['can_review_moderated_content']:
  290. queryset = queryset.filter(is_moderated=False)
  291. if not forum.acl['can_hide_threads']:
  292. queryset = queryset.filter(is_hidden=False)
  293. return queryset
  294. def exclude_invisible_postss(user, forum, queryset):
  295. if user.is_authenticated():
  296. if not forum.acl['can_review_moderated_content']:
  297. condition_author = Q(starter_id=user.id)
  298. condition = Q(is_moderated=False)
  299. queryset = queryset.filter(condition_author | condition)
  300. elif not forum.acl['can_review_moderated_content']:
  301. queryset = queryset.filter(is_moderated=False)
  302. return queryset