attachments.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from misago.acl import algebra
  4. from misago.acl.models import Role
  5. from misago.admin.forms import YesNoSwitch
  6. from misago.threads.models import Attachment
  7. # Admin Permissions Forms
  8. class PermissionsForm(forms.Form):
  9. legend = _("Attachments")
  10. max_attachment_size = forms.IntegerField(
  11. label=_("Max attached file size (in kb)"),
  12. help_text=_("Enter 0 to don't allow uploading end deleting attachments."),
  13. initial=500,
  14. min_value=0,
  15. )
  16. can_download_other_users_attachments = YesNoSwitch(
  17. label=_("Can download other users attachments")
  18. )
  19. can_delete_other_users_attachments = YesNoSwitch(
  20. label=_("Can delete other users attachments")
  21. )
  22. class AnonymousPermissionsForm(forms.Form):
  23. legend = _("Attachments")
  24. can_download_other_users_attachments = YesNoSwitch(
  25. label=_("Can download attachments")
  26. )
  27. def change_permissions_form(role):
  28. if isinstance(role, Role):
  29. if role.special_role != "anonymous":
  30. return PermissionsForm
  31. else:
  32. return AnonymousPermissionsForm
  33. else:
  34. return None
  35. def build_acl(acl, roles, key_name):
  36. new_acl = {
  37. "max_attachment_size": 0,
  38. "can_download_other_users_attachments": False,
  39. "can_delete_other_users_attachments": False,
  40. }
  41. new_acl.update(acl)
  42. return algebra.sum_acls(
  43. new_acl,
  44. roles=roles,
  45. key=key_name,
  46. max_attachment_size=algebra.greater,
  47. can_download_other_users_attachments=algebra.greater,
  48. can_delete_other_users_attachments=algebra.greater,
  49. )
  50. def add_acl_to_attachment(user_acl, attachment):
  51. if user_acl["is_authenticated"] and user_acl["user_id"] == attachment.uploader_id:
  52. attachment.acl.update({"can_delete": True})
  53. else:
  54. user_can_delete = user_acl["can_delete_other_users_attachments"]
  55. attachment.acl.update(
  56. {"can_delete": user_acl["is_authenticated"] and user_can_delete}
  57. )
  58. def register_with(registry):
  59. registry.acl_annotator(Attachment, add_acl_to_attachment)