permissions.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, 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 user.permissions[perm]
  25. def can_moderate(user, forum, perm=None):
  26. """Checks if a user can moderate a forum
  27. He needs to be super moderator or a moderator of the
  28. specified forum
  29. :param user: The user for whom we should check the permission.
  30. :param forum: The forum that should be checked.
  31. :param perm: Optional - Check if the user also has the permission to do
  32. certain things in the forum. There are a few permissions
  33. where you need to be at least a moderator (or anything higher)
  34. in the forum and therefore you can pass a permission and
  35. it will check if the user has it. Those special permissions
  36. are documented here: <INSERT LINK TO DOCS>
  37. """
  38. if user.permissions['mod'] and user in forum.moderators:
  39. if perm is not None:
  40. return user.permissions[perm]
  41. return True
  42. return user.permissions['super_mod'] or user.permissions['admin']
  43. def can_edit_post(user, post_user_id, forum):
  44. """Check if the post can be edited by the user"""
  45. return check_perm(user=user, perm='editpost', forum=forum,
  46. post_user_id=post_user_id)
  47. def can_delete_post(user, post_user_id, forum):
  48. """Check if the post can be deleted by the user"""
  49. return check_perm(user=user, perm='deletepost', forum=forum,
  50. post_user_id=post_user_id)
  51. def can_delete_topic(user, post_user_id, forum):
  52. """Check if the topic can be deleted by the user"""
  53. return check_perm(user=user, perm='deletetopic', forum=forum,
  54. post_user_id=post_user_id)
  55. def can_post_reply(user, forum):
  56. """Check if the user is allowed to post in the forum"""
  57. return check_perm(user=user, perm='postreply', forum=forum)
  58. def can_post_topic(user, forum):
  59. """Checks if the user is allowed to create a new topic in the forum"""
  60. return check_perm(user=user, perm='posttopic', forum=forum)
  61. def can_lock_topic(user, forum):
  62. """Check if the user is allowed to lock a topic in the forum
  63. Returns True if the user can moderate the forum and has the permission
  64. to do it.
  65. """
  66. return can_moderate(user, forum, "locktopic")
  67. def can_move_topic(user, forum):
  68. """Check if the user is allowed to move a topic in the forum
  69. Returns True if the user can moderate the forum and has the permission
  70. to do it."""
  71. return can_moderate(user, forum, "movetopic")
  72. def can_merge_topic(user, forum):
  73. """Check if the user is allowed to merge a topic in the forum.
  74. Returns True if the user can moderate the forum and has the permission
  75. to do it.
  76. """
  77. return can_moderate(user, forum, "mergetopic")