2016-12-19 17:19:50 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-02-03 05:37:05 +00:00
|
|
|
from rest_framework import permissions
|
2016-12-19 17:19:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IsValidUser(permissions.IsAuthenticated, permissions.BasePermission):
|
|
|
|
"""Allows access to valid user, is active and not expired"""
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
return super(IsValidUser, self).has_permission(request, view) \
|
2017-02-03 05:37:05 +00:00
|
|
|
and request.user.is_valid
|
2016-12-19 17:19:50 +00:00
|
|
|
|
|
|
|
|
2017-03-31 03:25:25 +00:00
|
|
|
class IsAppUser(IsValidUser):
|
2016-12-19 17:19:50 +00:00
|
|
|
"""Allows access only to app user """
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
2016-12-25 05:15:28 +00:00
|
|
|
return super(IsAppUser, self).has_permission(request, view) \
|
2017-02-03 05:37:05 +00:00
|
|
|
and request.user.is_app
|
2016-12-19 17:19:50 +00:00
|
|
|
|
|
|
|
|
2017-03-31 03:25:25 +00:00
|
|
|
class IsSuperUser(IsValidUser):
|
2016-12-19 17:19:50 +00:00
|
|
|
"""Allows access only to superuser"""
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
return super(IsSuperUser, self).has_permission(request, view) \
|
2017-02-03 05:37:05 +00:00
|
|
|
and request.user.is_superuser
|
2016-12-19 17:19:50 +00:00
|
|
|
|
|
|
|
|
2017-03-31 03:25:25 +00:00
|
|
|
class IsSuperUserOrAppUser(IsValidUser):
|
2016-12-19 17:19:50 +00:00
|
|
|
"""Allows access between superuser and app user"""
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
2016-12-25 05:15:28 +00:00
|
|
|
return super(IsSuperUserOrAppUser, self).has_permission(request, view) \
|
2017-02-03 05:37:05 +00:00
|
|
|
and (request.user.is_superuser or request.user.is_app)
|
|
|
|
|
2016-12-19 17:19:50 +00:00
|
|
|
|
2017-03-31 03:25:25 +00:00
|
|
|
class IsSuperUserOrAppUserOrUserReadonly(IsSuperUserOrAppUser):
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
if IsValidUser.has_permission(self, request, view) \
|
|
|
|
and request.method in permissions.SAFE_METHODS:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return IsSuperUserOrAppUser.has_permission(self, request, view)
|
2016-12-19 17:19:50 +00:00
|
|
|
|
2017-03-31 03:25:25 +00:00
|
|
|
|
|
|
|
class IsCurrentUserOrReadOnly(permissions.BasePermission):
|
2017-02-03 05:37:05 +00:00
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
if request.method in permissions.SAFE_METHODS:
|
|
|
|
return True
|
|
|
|
return obj == request.user
|