permissions.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. from django.core.exceptions import PermissionDenied
  2. from django.db.models import Q
  3. from django.http import Http404
  4. from django.utils import timezone
  5. from django.utils.translation import ugettext_lazy as _
  6. from misago.acl import add_acl, algebra
  7. from misago.acl.decorators import return_boolean
  8. from misago.core import forms
  9. from misago.forums.models import Forum, RoleForumACL, ForumRole
  10. from misago.forums.permissions import get_forums_roles
  11. from misago.threads.models import Thread, Post, Event
  12. """
  13. Admin Permissions Form
  14. """
  15. class PermissionsForm(forms.Form):
  16. legend = _("Threads")
  17. can_see_all_threads = forms.TypedChoiceField(
  18. label=_("Can see threads"),
  19. coerce=int,
  20. initial=0,
  21. choices=((0, _("Started threads")), (1, _("All threads"))))
  22. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  23. can_reply_threads = forms.TypedChoiceField(
  24. label=_("Can reply to threads"),
  25. coerce=int,
  26. initial=0,
  27. choices=((0, _("No")), (1, _("Open threads")), (2, _("All threads"))))
  28. can_edit_threads = forms.TypedChoiceField(
  29. label=_("Can edit threads"),
  30. coerce=int,
  31. initial=0,
  32. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  33. can_hide_own_threads = forms.TypedChoiceField(
  34. label=_("Can hide own threads"),
  35. help_text=_("Only threads started within time limit and "
  36. "with no replies can be hidden."),
  37. coerce=int,
  38. initial=0,
  39. choices=(
  40. (0, _("No")),
  41. (1, _("Hide threads")),
  42. (2, _("Delete threads"))
  43. ))
  44. thread_edit_time = forms.IntegerField(
  45. label=_("Min. time for own thread edit, in minutes"),
  46. help_text=_("Enter 0 to don't limit time for editing own threads."),
  47. initial=0,
  48. min_value=0)
  49. can_hide_threads = forms.TypedChoiceField(
  50. label=_("Can hide threads"),
  51. coerce=int,
  52. initial=0,
  53. choices=(
  54. (0, _("No")),
  55. (1, _("Hide threads")),
  56. (2, _("Delete threads"))
  57. ))
  58. can_edit_replies = forms.TypedChoiceField(
  59. label=_("Can edit replies"),
  60. coerce=int,
  61. initial=0,
  62. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  63. can_hide_own_replies = forms.TypedChoiceField(
  64. label=_("Can hide own replies"),
  65. help_text=_("Only last replies to thread made within "
  66. "edit time limit can be hidden."),
  67. coerce=int,
  68. initial=0,
  69. choices=(
  70. (0, _("No")),
  71. (1, _("Hide replies")),
  72. (2, _("Delete replies"))
  73. ))
  74. reply_edit_time = forms.IntegerField(
  75. label=_("Min. time for own reply edit, in minutes"),
  76. help_text=_("Enter 0 to don't limit time for editing own replies."),
  77. initial=0,
  78. min_value=0)
  79. can_hide_replies = forms.TypedChoiceField(
  80. label=_("Can hide replies"),
  81. coerce=int,
  82. initial=0,
  83. choices=(
  84. (0, _("No")),
  85. (1, _("Hide replies")),
  86. (2, _("Delete replies"))
  87. ))
  88. can_protect_posts = forms.YesNoSwitch(
  89. label=_("Can protect posts"),
  90. help_text=_("Only users with this permission "
  91. "can edit protected posts."))
  92. can_move_posts = forms.YesNoSwitch(
  93. label=_("Can move posts"))
  94. can_merge_posts = forms.YesNoSwitch(
  95. label=_("Can merge posts"))
  96. can_change_threads_labels = forms.TypedChoiceField(
  97. label=_("Can change threads labels"), coerce=int, initial=0,
  98. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  99. can_pin_threads = forms.YesNoSwitch(
  100. label=_("Can pin threads"))
  101. can_close_threads = forms.YesNoSwitch(label=_("Can close threads"))
  102. can_move_threads = forms.YesNoSwitch(
  103. label=_("Can move threads"))
  104. can_merge_threads = forms.YesNoSwitch(
  105. label=_("Can merge threads"))
  106. can_split_threads = forms.YesNoSwitch(
  107. label=_("Can split threads"))
  108. can_review_moderated_content = forms.YesNoSwitch(
  109. label=_("Can review moderated content"),
  110. help_text=_("Will see and be able to accept moderated content."))
  111. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  112. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  113. can_hide_events = forms.TypedChoiceField(
  114. label=_("Can hide events"),
  115. coerce=int,
  116. initial=0,
  117. choices=(
  118. (0, _("No")),
  119. (1, _("Hide events")),
  120. (2, _("Delete events"))
  121. ))
  122. def change_permissions_form(role):
  123. if isinstance(role, ForumRole):
  124. return PermissionsForm
  125. else:
  126. return None
  127. """
  128. ACL Builder
  129. """
  130. def build_acl(acl, roles, key_name):
  131. acl['moderated_forums'] = []
  132. forums_roles = get_forums_roles(roles)
  133. for forum in Forum.objects.all_forums():
  134. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  135. if forum_acl['can_browse']:
  136. acl['forums'][forum.pk] = build_forum_acl(
  137. forum_acl, forum, forums_roles, key_name)
  138. return acl
  139. def build_forum_acl(acl, forum, forums_roles, key_name):
  140. forum_roles = forums_roles.get(forum.pk, [])
  141. final_acl = {
  142. 'can_see_all_threads': 0,
  143. 'can_start_threads': 0,
  144. 'can_reply_threads': 0,
  145. 'can_edit_threads': 0,
  146. 'can_edit_replies': 0,
  147. 'can_hide_own_threads': 0,
  148. 'can_hide_own_replies': 0,
  149. 'thread_edit_time': 0,
  150. 'reply_edit_time': 0,
  151. 'can_hide_threads': 0,
  152. 'can_hide_replies': 0,
  153. 'can_protect_posts': 0,
  154. 'can_move_posts': 0,
  155. 'can_merge_posts': 0,
  156. 'can_change_threads_labels': 0,
  157. 'can_pin_threads': 0,
  158. 'can_close_threads': 0,
  159. 'can_move_threads': 0,
  160. 'can_merge_threads': 0,
  161. 'can_split_threads': 0,
  162. 'can_review_moderated_content': 0,
  163. 'can_report_content': 0,
  164. 'can_see_reports': 0,
  165. 'can_hide_events': 0,
  166. }
  167. final_acl.update(acl)
  168. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  169. can_see_all_threads=algebra.greater,
  170. can_start_threads=algebra.greater,
  171. can_reply_threads=algebra.greater,
  172. can_edit_threads=algebra.greater,
  173. can_edit_replies=algebra.greater,
  174. can_hide_threads=algebra.greater,
  175. can_hide_replies=algebra.greater,
  176. can_hide_own_threads=algebra.greater,
  177. can_hide_own_replies=algebra.greater,
  178. thread_edit_time=algebra.greater_or_zero,
  179. reply_edit_time=algebra.greater_or_zero,
  180. can_protect_posts=algebra.greater,
  181. can_move_posts=algebra.greater,
  182. can_merge_posts=algebra.greater,
  183. can_change_threads_labels=algebra.greater,
  184. can_pin_threads=algebra.greater,
  185. can_close_threads=algebra.greater,
  186. can_move_threads=algebra.greater,
  187. can_merge_threads=algebra.greater,
  188. can_split_threads=algebra.greater,
  189. can_review_moderated_content=algebra.greater,
  190. can_report_content=algebra.greater,
  191. can_see_reports=algebra.greater,
  192. can_hide_events=algebra.greater,
  193. )
  194. return final_acl
  195. """
  196. ACL's for targets
  197. """
  198. def add_acl_to_target(user, target):
  199. if isinstance(target, Forum):
  200. add_acl_to_forum(user, target)
  201. if isinstance(target, Thread):
  202. add_acl_to_thread(user, target)
  203. if isinstance(target, Post):
  204. add_acl_to_post(user, target)
  205. if isinstance(target, Event):
  206. add_acl_to_event(user, target)
  207. def add_acl_to_forum(user, forum):
  208. forum_acl = user.acl['forums'].get(forum.pk, {})
  209. forum.acl.update({
  210. 'can_see_all_threads': 0,
  211. 'can_start_threads': 0,
  212. 'can_reply_threads': 0,
  213. 'can_edit_threads': 0,
  214. 'can_edit_replies': 0,
  215. 'can_hide_own_threads': 0,
  216. 'can_hide_own_replies': 0,
  217. 'thread_edit_time': 0,
  218. 'reply_edit_time': 0,
  219. 'can_hide_threads': 0,
  220. 'can_hide_replies': 0,
  221. 'can_protect_posts': 0,
  222. 'can_move_posts': 0,
  223. 'can_merge_posts': 0,
  224. 'can_change_threads_labels': 0,
  225. 'can_pin_threads': 0,
  226. 'can_close_threads': 0,
  227. 'can_move_threads': 0,
  228. 'can_merge_threads': 0,
  229. 'can_split_threads': 0,
  230. 'can_review_moderated_content': 0,
  231. 'can_report_content': 0,
  232. 'can_see_reports': 0,
  233. 'can_hide_events': 0,
  234. })
  235. algebra.sum_acls(forum.acl, acls=[forum_acl],
  236. can_see_all_threads=algebra.greater)
  237. if user.is_authenticated():
  238. algebra.sum_acls(forum.acl, acls=[forum_acl],
  239. can_start_threads=algebra.greater,
  240. can_reply_threads=algebra.greater,
  241. can_edit_threads=algebra.greater,
  242. can_edit_replies=algebra.greater,
  243. can_hide_threads=algebra.greater,
  244. can_hide_replies=algebra.greater,
  245. can_hide_own_threads=algebra.greater,
  246. can_hide_own_replies=algebra.greater,
  247. thread_edit_time=algebra.greater_or_zero,
  248. reply_edit_time=algebra.greater_or_zero,
  249. can_protect_posts=algebra.greater,
  250. can_move_posts=algebra.greater,
  251. can_merge_posts=algebra.greater,
  252. can_change_threads_labels=algebra.greater,
  253. can_pin_threads=algebra.greater,
  254. can_close_threads=algebra.greater,
  255. can_move_threads=algebra.greater,
  256. can_merge_threads=algebra.greater,
  257. can_split_threads=algebra.greater,
  258. can_review_moderated_content=algebra.greater,
  259. can_report_content=algebra.greater,
  260. can_see_reports=algebra.greater,
  261. can_hide_events=algebra.greater,
  262. )
  263. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  264. def add_acl_to_thread(user, thread):
  265. forum_acl = user.acl['forums'].get(thread.forum_id, {})
  266. thread.acl.update({
  267. 'can_reply': can_reply_thread(user, thread),
  268. 'can_hide': forum_acl.get('can_hide_threads'),
  269. 'can_change_label': forum_acl.get('can_change_threads_labels') == 2,
  270. 'can_pin': forum_acl.get('can_pin_threads'),
  271. 'can_close': forum_acl.get('can_close_threads'),
  272. 'can_move': forum_acl.get('can_move_threads'),
  273. 'can_review': forum_acl.get('can_review_moderated_content'),
  274. 'can_report': forum_acl.get('can_report'),
  275. 'can_see_reports': forum_acl.get('can_see_reports')
  276. })
  277. if can_change_owned_thread(user, thread):
  278. if not thread.acl['can_change_label']:
  279. can_change_label = forum_acl.get('can_change_threads_labels') == 1
  280. thread.acl['can_change_label'] = can_change_label
  281. if not thread.acl['can_hide']:
  282. thread.acl['can_hide'] = forum_acl.get('can_hide_own_threads')
  283. def add_acl_to_post(user, post):
  284. pass
  285. def add_acl_to_event(user, event):
  286. forum_acl = user.acl['forums'].get(event.forum_id, {})
  287. can_hide_events = forum_acl.get('can_hide_events', 0)
  288. event.acl['can_hide'] = can_hide_events > 0
  289. event.acl['can_delete'] = can_hide_events == 2
  290. """
  291. ACL tests
  292. """
  293. def allow_see_thread(user, target):
  294. forum_acl = user.acl['forums'].get(target.forum_id, {})
  295. if not forum_acl.get('can_see_all_threads'):
  296. if user.is_anonymous() or user.pk != target.starter_id:
  297. raise Http404()
  298. can_see_thread = return_boolean(allow_see_thread)
  299. def allow_start_thread(user, target):
  300. if target.is_closed:
  301. message = _("This forum is closed. You can't start new threads in it.")
  302. raise PermissionDenied(message)
  303. if user.is_anonymous():
  304. raise PermissionDenied(_("You have to sign in to start new thread."))
  305. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  306. raise PermissionDenied(_("You don't have permission to start "
  307. "new threads in this forum."))
  308. can_start_thread = return_boolean(allow_start_thread)
  309. def allow_reply_thread(user, target):
  310. if target.forum.is_closed:
  311. message = _("This forum is closed.")
  312. raise PermissionDenied(message)
  313. if user.is_anonymous():
  314. raise PermissionDenied(_("You have to sign in to reply threads."))
  315. reply_thread = user.acl['forums'].get(target.id, {'can_reply_threads': 0})
  316. if reply_thread == 0:
  317. raise PermissionDenied(_("You can't reply to threads in this forum."))
  318. if target.is_closed and reply_thread < 2:
  319. raise PermissionDenied(
  320. _("You can't reply to closed threads in this forum."))
  321. can_reply_thread = return_boolean(allow_reply_thread)
  322. def can_change_owned_thread(user, target):
  323. forum_acl = user.acl['forums'].get(target.forum_id, {})
  324. if user.is_anonymous() or user.pk != target.starter_id:
  325. return False
  326. if target.forum.is_closed or target.is_closed:
  327. return False
  328. if target.first_post.is_protected:
  329. return False
  330. if forum_acl.get('thread_edit_time'):
  331. diff = timezone.now() - target.started_on
  332. diff_minutes = int(diff.total_seconds() / 60)
  333. if diff_minutes > forum_acl.get('thread_edit_time'):
  334. return False
  335. return True
  336. def allow_see_post(user, target):
  337. if target.is_moderated:
  338. forum_acl = user.acl['forums'].get(target.forum_id, {})
  339. if not forum_acl.get('can_review_moderated_content'):
  340. if user.is_anonymous() or user.pk != target.poster_id:
  341. raise Http404()
  342. can_see_post = return_boolean(allow_see_post)
  343. """
  344. Queryset helpers
  345. """
  346. def exclude_invisible_threads(queryset, user, forum=None):
  347. if forum:
  348. return exclude_invisible_forum_threads(queryset, user, forum)
  349. else:
  350. return exclude_all_invisible_threads(queryset, user)
  351. def exclude_invisible_forum_threads(queryset, user, forum):
  352. if user.is_authenticated():
  353. condition_author = Q(starter_id=user.id)
  354. can_mod = forum.acl['can_review_moderated_content']
  355. can_hide = forum.acl['can_hide_threads']
  356. if not can_mod and not can_hide:
  357. condition = Q(is_moderated=False) & Q(is_hidden=False)
  358. queryset = queryset.filter(condition_author | condition)
  359. elif not can_mod:
  360. condition = Q(is_moderated=False)
  361. queryset = queryset.filter(condition_author | condition)
  362. elif not can_hide:
  363. condition = Q(is_hidden=False)
  364. queryset = queryset.filter(condition_author | condition)
  365. else:
  366. if not forum.acl['can_review_moderated_content']:
  367. queryset = queryset.filter(is_moderated=False)
  368. if not forum.acl['can_hide_threads']:
  369. queryset = queryset.filter(is_hidden=False)
  370. return queryset
  371. def exclude_all_invisible_threads(queryset, user):
  372. forums_in = []
  373. conditions = None
  374. for forum in Forum.objects.all_forums():
  375. add_acl(user, forum)
  376. condition_forum = Q(forum=forum)
  377. condition_author = Q(starter_id=user.id)
  378. # can see all threads?
  379. if forum.acl['can_see_all_threads']:
  380. can_mod = forum.acl['can_review_moderated_content']
  381. can_hide = forum.acl['can_hide_threads']
  382. if not can_mod or not can_hide:
  383. if not can_mod and not can_hide:
  384. condition = Q(is_moderated=False) & Q(is_hidden=False)
  385. elif not can_mod:
  386. condition = Q(is_moderated=False)
  387. elif not can_hide:
  388. condition = Q(is_hidden=False)
  389. visibility_condition = condition_author | condition
  390. visibility_condition = condition_forum & visibility_condition
  391. else:
  392. # user can see everything so don't bother with rest of routine
  393. forums_in.append(forum.pk)
  394. continue
  395. else:
  396. # show all threads in forum made by user
  397. visibility_condition = condition_forum & condition_author
  398. if conditions:
  399. conditions = conditions | visibility_condition
  400. else:
  401. conditions = visibility_condition
  402. if conditions and forums_in:
  403. return queryset.filter(Q(forum_id__in=forums_in) | conditions)
  404. elif conditions:
  405. return queryset.filter(conditions)
  406. elif forums_in:
  407. return queryset.filter(forum_id__in=forums_in)
  408. else:
  409. return Thread.objects.none()
  410. def exclude_invisible_posts(queryset, user, forum):
  411. if not forum.acl['can_review_moderated_content']:
  412. if user.is_authenticated():
  413. condition_author = Q(poster=user.id)
  414. condition = Q(is_moderated=False)
  415. queryset = queryset.filter(condition_author | condition)
  416. else:
  417. queryset = queryset.filter(is_moderated=False)
  418. return queryset