2020-03-20 10:21:27 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from rest_framework import generics
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2022-07-19 07:57:02 +00:00
|
|
|
from common.permissions import IsValidUserOrConnectionToken
|
|
|
|
from common.utils import get_object_or_none
|
|
|
|
from orgs.utils import tmp_to_root_org
|
|
|
|
from authentication.models import ConnectionToken
|
2020-03-20 10:21:27 +00:00
|
|
|
|
2021-11-12 07:33:33 +00:00
|
|
|
from users.notifications import (
|
|
|
|
ResetPasswordMsg, ResetPasswordSuccessMsg, ResetSSHKeyMsg,
|
|
|
|
ResetPublicKeySuccessMsg,
|
|
|
|
)
|
2022-02-17 12:13:31 +00:00
|
|
|
|
2020-03-20 10:21:27 +00:00
|
|
|
from .. import serializers
|
|
|
|
from ..models import User
|
|
|
|
from .mixins import UserQuerysetMixin
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserResetPasswordApi', 'UserResetPKApi',
|
2022-02-17 12:13:31 +00:00
|
|
|
'UserProfileApi', 'UserPasswordApi',
|
|
|
|
'UserSecretKeyApi', 'UserPublicKeyApi'
|
2020-03-20 10:21:27 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UserResetPasswordApi(UserQuerysetMixin, generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
|
|
|
serializer_class = serializers.UserSerializer
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
# Note: we are not updating the user object here.
|
|
|
|
# We just do the reset-password stuff.
|
|
|
|
user = self.get_object()
|
|
|
|
user.password_raw = str(uuid.uuid4())
|
|
|
|
user.save()
|
2021-08-24 06:20:54 +00:00
|
|
|
ResetPasswordMsg(user).publish_async()
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserResetPKApi(UserQuerysetMixin, generics.UpdateAPIView):
|
|
|
|
serializer_class = serializers.UserSerializer
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
user = self.get_object()
|
|
|
|
user.public_key = None
|
|
|
|
user.save()
|
2021-08-24 06:20:54 +00:00
|
|
|
ResetSSHKeyMsg(user).publish_async()
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
|
2020-05-28 07:02:53 +00:00
|
|
|
class UserProfileApi(generics.RetrieveUpdateAPIView):
|
2022-07-19 07:57:02 +00:00
|
|
|
permission_classes = (IsValidUserOrConnectionToken,)
|
2020-04-09 02:33:20 +00:00
|
|
|
serializer_class = serializers.UserProfileSerializer
|
2020-03-20 10:21:27 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
2022-07-19 07:57:02 +00:00
|
|
|
if self.request.user.is_anonymous:
|
|
|
|
user = self.get_connection_token_user()
|
|
|
|
if user:
|
|
|
|
return user
|
2020-03-20 10:21:27 +00:00
|
|
|
return self.request.user
|
|
|
|
|
2022-07-19 07:57:02 +00:00
|
|
|
def get_connection_token_user(self):
|
|
|
|
token_id = self.request.query_params.get('token')
|
|
|
|
if not token_id:
|
|
|
|
return
|
|
|
|
with tmp_to_root_org():
|
|
|
|
token = get_object_or_none(ConnectionToken, id=token_id)
|
|
|
|
if not token:
|
|
|
|
return
|
|
|
|
return token.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
|
|
|
|
|
2021-09-13 02:56:55 +00:00
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
resp = super().update(request, *args, **kwargs)
|
|
|
|
ResetPasswordSuccessMsg(self.request.user, request).publish_async()
|
|
|
|
return resp
|
|
|
|
|
2020-05-28 08:10:28 +00:00
|
|
|
|
2021-12-03 07:50:04 +00:00
|
|
|
class UserSecretKeyApi(generics.RetrieveUpdateAPIView):
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
serializer_class = serializers.UserUpdateSecretKeySerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
2020-05-28 08:10:28 +00:00
|
|
|
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)
|
2021-11-12 07:33:33 +00:00
|
|
|
ResetPublicKeySuccessMsg(self.get_object(), self.request).publish_async()
|