2020-03-20 10:21:27 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from rest_framework import generics
|
2021-03-02 06:57:48 +00:00
|
|
|
from common.permissions import IsOrgAdmin
|
2020-03-20 10:21:27 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2020-06-24 02:52:05 +00:00
|
|
|
from django.conf import settings
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
from common.permissions import (
|
|
|
|
IsCurrentUserOrReadOnly
|
|
|
|
)
|
|
|
|
from .. import serializers
|
|
|
|
from ..models import User
|
2020-10-14 11:59:05 +00:00
|
|
|
from ..utils import send_reset_password_success_mail
|
2020-03-20 10:21:27 +00:00
|
|
|
from .mixins import UserQuerysetMixin
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserResetPasswordApi', 'UserResetPKApi',
|
|
|
|
'UserProfileApi', 'UserUpdatePKApi',
|
2020-05-28 08:10:28 +00:00
|
|
|
'UserPasswordApi', 'UserPublicKeyApi'
|
2020-03-20 10:21:27 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UserResetPasswordApi(UserQuerysetMixin, generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
|
|
|
serializer_class = serializers.UserSerializer
|
2021-03-02 06:57:48 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
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(UserQuerysetMixin, generics.UpdateAPIView):
|
|
|
|
serializer_class = serializers.UserSerializer
|
2021-03-02 06:57:48 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
from ..utils import send_reset_ssh_key_mail
|
|
|
|
user = self.get_object()
|
|
|
|
user.public_key = None
|
|
|
|
user.save()
|
|
|
|
send_reset_ssh_key_mail(user)
|
|
|
|
|
|
|
|
|
|
|
|
# 废弃
|
|
|
|
class UserUpdatePKApi(UserQuerysetMixin, generics.UpdateAPIView):
|
|
|
|
serializer_class = serializers.UserPKUpdateSerializer
|
|
|
|
permission_classes = (IsCurrentUserOrReadOnly,)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
user = self.get_object()
|
|
|
|
user.public_key = serializer.validated_data['public_key']
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2020-05-28 07:02:53 +00:00
|
|
|
class UserProfileApi(generics.RetrieveUpdateAPIView):
|
2020-03-20 10:21:27 +00:00
|
|
|
permission_classes = (IsAuthenticated,)
|
2020-04-09 02:33:20 +00:00
|
|
|
serializer_class = serializers.UserProfileSerializer
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
|
|
|
|
2020-05-28 08:10:28 +00:00
|
|
|
|
|
|
|
class UserPasswordApi(generics.RetrieveUpdateAPIView):
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
serializer_class = serializers.UserUpdatePasswordSerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
|
|
|
class UserPublicKeyApi(generics.RetrieveUpdateAPIView):
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
serializer_class = serializers.UserUpdatePublicKeySerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
2020-10-14 11:59:05 +00:00
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
super().perform_update(serializer)
|
|
|
|
send_reset_password_success_mail(self.request, self.get_object())
|