jumpserver/apps/users/api/profile.py

88 lines
2.6 KiB
Python
Raw Normal View History

2020-03-20 10:21:27 +00:00
# ~*~ coding: utf-8 ~*~
import uuid
from rest_framework import generics
from common.permissions import IsOrgAdmin
2020-03-20 10:21:27 +00:00
from rest_framework.permissions import IsAuthenticated
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
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',
'UserPasswordApi', 'UserPublicKeyApi'
2020-03-20 10:21:27 +00:00
]
class UserResetPasswordApi(UserQuerysetMixin, generics.UpdateAPIView):
queryset = User.objects.all()
serializer_class = serializers.UserSerializer
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
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
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
def perform_update(self, serializer):
super().perform_update(serializer)
send_reset_password_success_mail(self.request, self.get_object())