attachments.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(label=_("Can delete other users attachments"))
  20. class AnonymousPermissionsForm(forms.Form):
  21. legend = _("Attachments")
  22. can_download_other_users_attachments = YesNoSwitch(label=_("Can download attachments"))
  23. def change_permissions_form(role):
  24. if isinstance(role, Role):
  25. if role.special_role != 'anonymous':
  26. return PermissionsForm
  27. else:
  28. return AnonymousPermissionsForm
  29. else:
  30. return None
  31. def build_acl(acl, roles, key_name):
  32. new_acl = {
  33. 'max_attachment_size': 0,
  34. 'can_download_other_users_attachments': False,
  35. 'can_delete_other_users_attachments': False,
  36. }
  37. new_acl.update(acl)
  38. return algebra.sum_acls(
  39. new_acl,
  40. roles=roles,
  41. key=key_name,
  42. max_attachment_size=algebra.greater,
  43. can_download_other_users_attachments=algebra.greater,
  44. can_delete_other_users_attachments=algebra.greater,
  45. )
  46. def add_acl_to_attachment(user_acl, attachment):
  47. if user_acl["is_authenticated"] and user_acl["user_id"] == attachment.uploader_id:
  48. attachment.acl.update({
  49. 'can_delete': True,
  50. })
  51. else:
  52. user_can_delete = user_acl['can_delete_other_users_attachments']
  53. attachment.acl.update({
  54. 'can_delete': user_acl["is_authenticated"] and user_can_delete,
  55. })
  56. def register_with(registry):
  57. registry.acl_annotator(Attachment, add_acl_to_attachment)