forked from insin/forum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
executable file
·59 lines (50 loc) · 1.81 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Utility functions related to forum user permissions.
"""
from forum.models import ForumProfile
def is_admin(user):
"""
Shortcut so we don't have to paste this incantation everywhere.
Also provides a single point of change should we ever modify how
user permissions are determined.
"""
return ForumProfile.objects.get_for_user(user).is_admin()
def is_moderator(user):
"""
Shortcut so we don't have to paste this incantation everywhere.
Also provides a single point of change should we ever modify how
user permissions are determined.
"""
return ForumProfile.objects.get_for_user(user).is_moderator()
def user_can_edit_post(user, post, topic=None):
"""
Returns ``True`` if the given User can edit the given Post,
``False`` otherwise.
If the Post's Topic is also given, its ``locked`` status will be
taken into account when determining permissions.
"""
if topic and topic.locked:
return is_moderator(user)
else:
return user.id == post.user_id or is_moderator(user)
def user_can_edit_topic(user, topic):
"""
Returns ``True`` if the given User can edit the given Topic,
``False`` otherwise.
"""
if topic.locked:
return is_moderator(user)
else:
return user.id == topic.user_id or is_moderator(user)
def user_can_edit_user_profile(user, user_to_edit):
"""
Returns ``True`` if the given User can edit the given User's
profile, ``False`` otherwise.
"""
return user.id == user_to_edit.id or is_moderator(user)
def user_can_view_search_results(user, search):
"""
Returns ``True`` if the given User can view the given search results,
``False`` otherwise.
"""
return user.id == search.user_id or is_moderator(user)