jumpserver/apps/users/api.py

144 lines
4.9 KiB
Python
Raw Normal View History

2016-08-09 09:27:37 +00:00
# ~*~ coding: utf-8 ~*~
2016-08-23 16:11:13 +00:00
#
2016-08-09 09:27:37 +00:00
2016-10-31 10:58:23 +00:00
import base64
2016-10-31 10:58:23 +00:00
from django.shortcuts import get_object_or_404
from django.core.cache import cache
from django.conf import settings
from rest_framework import generics, status
from rest_framework.response import Response
2016-10-31 10:58:23 +00:00
from rest_framework.views import APIView
2016-09-21 07:48:21 +00:00
from rest_framework_bulk import ListBulkCreateUpdateDestroyAPIView
2016-10-31 10:58:23 +00:00
from rest_framework import authentication
2016-08-23 16:11:13 +00:00
2016-10-15 08:04:54 +00:00
from common.mixins import BulkDeleteApiMixin
from common.utils import get_logger
2016-10-31 10:58:23 +00:00
from .utils import check_user_valid, token_gen
2016-08-24 09:14:21 +00:00
from .models import User, UserGroup
from .serializers import UserDetailSerializer, UserAndGroupSerializer, \
GroupDetailSerializer, UserPKUpdateSerializer, UserBulkUpdateSerializer, GroupBulkUpdateSerializer
2016-10-17 07:24:41 +00:00
from .backends import IsSuperUser, IsTerminalUser, IsValidUser, IsSuperUserOrTerminalUser
2016-08-23 16:11:13 +00:00
2016-10-07 15:54:29 +00:00
logger = get_logger(__name__)
2016-08-26 08:56:50 +00:00
class UserDetailApi(generics.RetrieveUpdateDestroyAPIView):
2016-08-24 09:14:21 +00:00
queryset = User.objects.all()
serializer_class = UserDetailSerializer
2016-10-15 09:14:56 +00:00
permission_classes = (IsSuperUser,)
2016-08-24 09:14:21 +00:00
class UserAndGroupEditApi(generics.RetrieveUpdateAPIView):
2016-08-23 16:11:13 +00:00
queryset = User.objects.all()
serializer_class = UserAndGroupSerializer
2016-10-15 09:14:56 +00:00
permission_classes = (IsSuperUser,)
class UserResetPasswordApi(generics.UpdateAPIView):
queryset = User.objects.all()
serializer_class = UserDetailSerializer
def perform_update(self, serializer):
# Note: we are not updating the user object here.
# We just do the reset-password staff.
user = self.get_object()
import uuid
user.password_raw = str(uuid.uuid4())
user.save()
from .utils import send_reset_password_mail
send_reset_password_mail(user)
class UserResetPKApi(generics.UpdateAPIView):
queryset = User.objects.all()
serializer_class = UserDetailSerializer
def perform_update(self, serializer):
user = self.get_object()
2016-09-18 06:28:34 +00:00
user.is_public_key_valid = False
user.save()
from .utils import send_reset_ssh_key_mail
send_reset_ssh_key_mail(user)
2016-09-18 06:28:34 +00:00
class UserUpdatePKApi(generics.UpdateAPIView):
queryset = User.objects.all()
serializer_class = UserPKUpdateSerializer
def perform_update(self, serializer):
user = self.get_object()
user.private_key = serializer.validated_data['_public_key']
user.save()
2016-09-19 07:47:58 +00:00
class GroupDetailApi(generics.RetrieveUpdateDestroyAPIView):
2016-09-19 07:47:58 +00:00
queryset = UserGroup.objects.all()
serializer_class = GroupDetailSerializer
2016-09-21 07:48:21 +00:00
2016-09-29 08:41:55 +00:00
def perform_update(self, serializer):
users = serializer.validated_data.get('users')
if users:
2016-09-29 08:41:55 +00:00
group = self.get_object()
# Note: use `list` method to force hitting the db.
group_users = list(group.users.all())
serializer.save()
group.users.set(users + group_users)
2016-09-29 08:41:55 +00:00
group.save()
return
serializer.save()
2016-09-29 08:41:55 +00:00
2016-09-21 07:48:21 +00:00
class UserListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView):
2016-09-21 07:48:21 +00:00
queryset = User.objects.all()
serializer_class = UserBulkUpdateSerializer
2016-10-17 07:24:41 +00:00
permission_classes = (IsSuperUserOrTerminalUser,)
2016-10-15 08:04:54 +00:00
2016-10-16 14:12:13 +00:00
# def get(self, request, *args, **kwargs):
# return super(UserListUpdateApi, self).get(request, *args, **kwargs)
2016-09-23 08:30:59 +00:00
class GroupListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView):
queryset = UserGroup.objects.all()
serializer_class = GroupBulkUpdateSerializer
class DeleteUserFromGroupApi(generics.DestroyAPIView):
queryset = UserGroup.objects.all()
serializer_class = GroupDetailSerializer
def destroy(self, request, *args, **kwargs):
group = self.get_object()
self.perform_destroy(group, **kwargs)
return Response(status=status.HTTP_204_NO_CONTENT)
def perform_destroy(self, instance, **kwargs):
user_id = kwargs.get('uid')
user = get_object_or_404(User, id=user_id)
instance.users.remove(user)
2016-10-15 09:14:56 +00:00
2016-10-31 10:58:23 +00:00
class UserTokenApi(APIView):
permission_classes = ()
expiration = settings.CONFIG.TOKEN_EXPIRATION or 3600
def post(self, request, *args, **kwargs):
username = request.data.get('username', '')
password = request.data.get('password', '')
public_key = request.data.get('public_key', '')
remote_addr = request.META.get('REMOTE_ADDR', '')
remote_addr = base64.b64encode(remote_addr).replace('=', '')
user = check_user_valid(username=username, password=password, public_key=public_key)
if user:
token = cache.get('%s_%s' % (user.id, remote_addr))
if not token:
token = token_gen(user)
cache.set(token, user.id, self.expiration)
cache.set('%s_%s' % (user.id, remote_addr), token, self.expiration)
2016-11-01 09:21:16 +00:00
return Response({'token': token, 'id': user.id, 'username': user.username, 'name': user.name})
2016-10-31 10:58:23 +00:00
else:
return Response({'msg': 'Invalid password or public key or user is not active or expired'})