permissions.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.utils.permissions
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~
  5. A place for all permission checks
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. def check_perm(user, perm, forum, post_user_id=None):
  10. """Checks if the `user` has a specified `perm` in the `forum`
  11. If post_user_id is provided, it will also check if the user
  12. has created the post
  13. :param user: The user for whom we should check the permission
  14. :param perm: The permission. You can find a full list of available
  15. permissions here: <INSERT LINK TO DOCS>
  16. :param forum: The forum where we should check the permission against
  17. :param post_user_id: If post_user_id is given, it will also perform an
  18. check if the user is the owner of this topic or post.
  19. """
  20. if can_moderate(user=user, forum=forum):
  21. return True
  22. if post_user_id and user.is_authenticated():
  23. return user.permissions[perm] and user.id == post_user_id
  24. return not user.permissions['banned'] and user.permissions[perm]
  25. def is_moderator(user):
  26. """Returns ``True`` if the user is in a moderator or super moderator group.
  27. :param user: The user who should be checked.
  28. """
  29. return user.permissions['mod'] or user.permissions['super_mod']
  30. def is_admin(user):
  31. """Returns ``True`` if the user is a administrator.
  32. :param user: The user who should be checked.
  33. """
  34. return user.permissions['admin']
  35. def is_admin_or_moderator(user):
  36. """Returns ``True`` if the user is either a admin or in a moderator group
  37. :param user: The user who should be checked.
  38. """
  39. return is_admin(user) or is_moderator(user)
  40. def can_moderate(user, forum=None, perm=None):
  41. """Checks if a user can moderate a forum or a user.
  42. He needs to be super moderator or a moderator of the
  43. specified forum.
  44. :param user: The user for whom we should check the permission.
  45. :param forum: The forum that should be checked. If no forum is specified
  46. it will check if the user has at least moderator permissions
  47. and then it will perform another permission check for ``mod``
  48. permissions (they start with ``mod_``).
  49. :param perm: Optional - Check if the user also has the permission to do
  50. certain things in the forum. There are a few permissions
  51. where you need to be at least a moderator (or anything higher)
  52. in the forum and therefore you can pass a permission and
  53. it will check if the user has it. Those special permissions
  54. are documented here: <INSERT LINK TO DOCS>
  55. """
  56. # Check if the user has moderator specific permissions (mod_ prefix)
  57. if is_admin_or_moderator(user) and forum is None:
  58. if perm is not None and perm.startswith("mod_"):
  59. return user.permissions[perm]
  60. # If no permission is definied, return False
  61. return False
  62. # check if the user is a moderation and is moderating the forum
  63. if user.permissions['mod'] and user in forum.moderators:
  64. return True
  65. # if the user is a super_mod or admin, he can moderate all forums
  66. return user.permissions['super_mod'] or user.permissions['admin']
  67. def can_edit_post(user, post):
  68. """Check if the post can be edited by the user"""
  69. topic = post.topic
  70. if can_moderate(user, topic.forum):
  71. return True
  72. if topic.locked or topic.forum.locked:
  73. return False
  74. return check_perm(user=user, perm='editpost', forum=post.topic.forum,
  75. post_user_id=post.user_id)
  76. def can_delete_post(user, post):
  77. """Check if the post can be deleted by the user"""
  78. return check_perm(user=user, perm='deletepost', forum=post.topic.forum,
  79. post_user_id=post.user_id)
  80. def can_delete_topic(user, topic):
  81. """Check if the topic can be deleted by the user"""
  82. return check_perm(user=user, perm='deletetopic', forum=topic.forum,
  83. post_user_id=topic.user_id)
  84. def can_post_reply(user, topic):
  85. """Check if the user is allowed to post in the forum"""
  86. if can_moderate(user, topic.forum):
  87. return True
  88. if topic.locked or topic.forum.locked:
  89. return False
  90. return check_perm(user=user, perm='postreply', forum=topic.forum)
  91. def can_post_topic(user, forum):
  92. """Checks if the user is allowed to create a new topic in the forum"""
  93. return check_perm(user=user, perm='posttopic', forum=forum)
  94. # Moderator permission checks
  95. def can_edit_user(user):
  96. """Check if the user is allowed to edit another users profile.
  97. Requires at least ``mod`` permissions.
  98. """
  99. return can_moderate(user=user, perm="mod_edituser")
  100. def can_ban_user(user):
  101. """Check if the user is allowed to ban another user.
  102. Requires at least ``mod`` permissions.
  103. """
  104. return can_moderate(user=user, perm="mod_banuser")