2018-09-03 03:24:25 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.contrib.auth import logout
|
2018-09-07 04:40:26 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
from rest_framework import generics
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2019-08-12 09:05:01 +00:00
|
|
|
from rest_framework.serializers import ValidationError
|
2018-09-03 03:24:25 +00:00
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
|
|
|
|
2019-03-11 02:06:45 +00:00
|
|
|
from common.permissions import (
|
2019-07-02 06:17:56 +00:00
|
|
|
IsOrgAdmin, IsCurrentUserOrReadOnly, IsOrgAdminOrAppUser,
|
2019-07-12 10:31:55 +00:00
|
|
|
CanUpdateDeleteSuperUser,
|
2019-03-11 02:06:45 +00:00
|
|
|
)
|
2019-05-21 08:24:01 +00:00
|
|
|
from common.mixins import IDInCacheFilterMixin
|
2018-11-23 02:25:35 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from orgs.utils import current_org
|
2019-08-21 12:27:21 +00:00
|
|
|
from .. import serializers
|
2018-09-03 03:24:25 +00:00
|
|
|
from ..models import User
|
2019-01-10 03:50:08 +00:00
|
|
|
from ..signals import post_user_create
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
__all__ = [
|
|
|
|
'UserViewSet', 'UserChangePasswordApi', 'UserUpdateGroupApi',
|
|
|
|
'UserResetPasswordApi', 'UserResetPKApi', 'UserUpdatePKApi',
|
|
|
|
'UserUnblockPKApi', 'UserProfileApi', 'UserResetOTPApi',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
class UserViewSet(IDInCacheFilterMixin, BulkModelViewSet):
|
2018-10-24 06:55:04 +00:00
|
|
|
filter_fields = ('username', 'email', 'name', 'id')
|
|
|
|
search_fields = filter_fields
|
2018-11-23 08:24:30 +00:00
|
|
|
queryset = User.objects.exclude(role=User.ROLE_APP)
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2019-07-12 10:31:55 +00:00
|
|
|
permission_classes = (IsOrgAdmin, CanUpdateDeleteSuperUser)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
def send_created_signal(self, users):
|
|
|
|
if not isinstance(users, list):
|
|
|
|
users = [users]
|
|
|
|
for user in users:
|
|
|
|
post_user_create.send(self.__class__, user=user)
|
|
|
|
|
2019-01-10 03:50:08 +00:00
|
|
|
def perform_create(self, serializer):
|
2019-05-21 08:24:01 +00:00
|
|
|
users = serializer.save()
|
2019-06-25 06:32:25 +00:00
|
|
|
if isinstance(users, User):
|
|
|
|
users = [users]
|
|
|
|
if current_org and current_org.is_real():
|
|
|
|
current_org.users.add(*users)
|
2019-05-21 08:24:01 +00:00
|
|
|
self.send_created_signal(users)
|
2019-01-10 03:50:08 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
def get_queryset(self):
|
2019-06-25 03:22:17 +00:00
|
|
|
queryset = current_org.get_org_users().prefetch_related('groups')
|
2018-09-03 03:24:25 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def get_permissions(self):
|
2019-08-21 12:27:21 +00:00
|
|
|
if self.action in ["retrieve", "list"]:
|
2018-09-03 03:24:25 +00:00
|
|
|
self.permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
return super().get_permissions()
|
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
def _deny_permission(self, instance):
|
|
|
|
"""
|
|
|
|
check current user has permission to handle instance
|
|
|
|
(update, destroy, bulk_update, bulk destroy)
|
|
|
|
"""
|
2019-08-12 09:05:01 +00:00
|
|
|
if instance.is_superuser and not self.request.user.is_superuser:
|
2019-07-05 05:59:38 +00:00
|
|
|
return True
|
|
|
|
return False
|
2019-04-25 02:11:50 +00:00
|
|
|
|
|
|
|
def _bulk_deny_permission(self, instances):
|
|
|
|
deny_instances = [i for i in instances if self._deny_permission(i)]
|
|
|
|
if len(deny_instances) > 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
def allow_bulk_destroy(self, qs, filtered):
|
2019-08-21 12:27:21 +00:00
|
|
|
return False
|
2018-12-18 09:28:45 +00:00
|
|
|
|
2019-08-12 09:05:01 +00:00
|
|
|
def perform_bulk_update(self, serializer):
|
|
|
|
users_ids = [d.get("id") or d.get("pk") for d in serializer.validated_data]
|
|
|
|
users = User.objects.filter(id__in=users_ids)
|
|
|
|
deny_instances = [str(i.id) for i in users if self._deny_permission(i)]
|
|
|
|
if deny_instances:
|
|
|
|
msg = "{} can't be update".format(deny_instances)
|
|
|
|
raise ValidationError({"id": msg})
|
|
|
|
return super().perform_bulk_update(serializer)
|
2019-04-25 02:11:50 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
class UserChangePasswordApi(generics.RetrieveUpdateAPIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
queryset = User.objects.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.ChangeUserPasswordSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
user = self.get_object()
|
|
|
|
user.password_raw = serializer.validated_data["password"]
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
|
|
|
class UserUpdateGroupApi(generics.RetrieveUpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserUpdateGroupSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
|
|
|
|
class UserResetPasswordApi(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
# Note: we are not updating the user object here.
|
|
|
|
# We just do the reset-password stuff.
|
|
|
|
from ..utils import send_reset_password_mail
|
|
|
|
user = self.get_object()
|
|
|
|
user.password_raw = str(uuid.uuid4())
|
|
|
|
user.save()
|
|
|
|
send_reset_password_mail(user)
|
|
|
|
|
|
|
|
|
|
|
|
class UserResetPKApi(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
from ..utils import send_reset_ssh_key_mail
|
|
|
|
user = self.get_object()
|
|
|
|
user.is_public_key_valid = False
|
|
|
|
user.save()
|
|
|
|
send_reset_ssh_key_mail(user)
|
|
|
|
|
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
# 废弃
|
2018-09-03 03:24:25 +00:00
|
|
|
class UserUpdatePKApi(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserPKUpdateSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
permission_classes = (IsCurrentUserOrReadOnly,)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
user = self.get_object()
|
2019-06-25 06:32:25 +00:00
|
|
|
user.public_key = serializer.validated_data['public_key']
|
2018-09-03 03:24:25 +00:00
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
|
|
|
class UserUnblockPKApi(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
key_prefix_limit = "_LOGIN_LIMIT_{}_{}"
|
|
|
|
key_prefix_block = "_LOGIN_BLOCK_{}"
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
user = self.get_object()
|
|
|
|
username = user.username if user else ''
|
|
|
|
key_limit = self.key_prefix_limit.format(username, '*')
|
|
|
|
key_block = self.key_prefix_block.format(username)
|
|
|
|
cache.delete_pattern(key_limit)
|
|
|
|
cache.delete(key_block)
|
|
|
|
|
|
|
|
|
|
|
|
class UserProfileApi(generics.RetrieveAPIView):
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
|
|
|
|
2019-08-08 07:14:08 +00:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
age = request.session.get_expiry_age()
|
|
|
|
request.session.set_expiry(age)
|
|
|
|
return super().retrieve(request, *args, **kwargs)
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
class UserResetOTPApi(generics.RetrieveAPIView):
|
|
|
|
queryset = User.objects.all()
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = serializers.ResetOTPSerializer
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
user = self.get_object() if kwargs.get('pk') else request.user
|
|
|
|
if user == request.user:
|
|
|
|
msg = _("Could not reset self otp, use profile reset instead")
|
2018-09-07 04:40:26 +00:00
|
|
|
return Response({"error": msg}, status=401)
|
2018-09-03 03:24:25 +00:00
|
|
|
if user.otp_enabled and user.otp_secret_key:
|
|
|
|
user.otp_secret_key = ''
|
|
|
|
user.save()
|
|
|
|
logout(request)
|
2019-05-21 08:24:01 +00:00
|
|
|
return Response({"msg": "success"})
|