-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.py
36 lines (27 loc) · 1.29 KB
/
permissions.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
from rest_framework import permissions
from .models import UserRole
class IsAdminOrSuperUser(permissions.BasePermission):
"""Права доступа для администратора"""
def has_permission(self, request, view):
if not request.user.is_authenticated:
return False
if request.user.is_staff or request.user.role == UserRole.ADMIN:
return True
class IsAdminOrReadOnly(permissions.BasePermission):
def has_permission(self, request, view):
if request.method in permissions.SAFE_METHODS:
return True
if request.user.is_authenticated:
return bool(request.user.is_staff or request.user.role == 'admin')
class ReviewCommentPermissions(permissions.BasePermission):
"""Права доступа для комментариев и отзывов"""
def has_object_permission(self, request, view, obj):
if request.method == 'POST':
return not request.user.is_anonymous()
if request.method in ('PATCH', 'DELETE'):
return (request.user == obj.author or
request.user.role == UserRole.ADMIN or
request.user.role == UserRole.MODERATOR)
if request.method in permissions.SAFE_METHODS:
return True
return False