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-12-16 11:32:05 +00:00
|
|
|
import base64
|
2016-09-26 13:22:48 +00:00
|
|
|
|
2016-10-31 10:58:23 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.conf import settings
|
2016-11-25 14:45:47 +00:00
|
|
|
from rest_framework import generics, viewsets
|
2016-09-26 13:22:48 +00:00
|
|
|
from rest_framework.response import Response
|
2016-10-31 10:58:23 +00:00
|
|
|
from rest_framework.views import APIView
|
2016-11-25 14:45:47 +00:00
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
2016-11-24 11:36:49 +00:00
|
|
|
from django_filters.rest_framework import DjangoFilterBackend
|
2016-08-23 16:11:13 +00:00
|
|
|
|
2016-11-25 03:00:51 +00:00
|
|
|
from common.mixins import IDInFilterMixin
|
2016-10-15 08:04:54 +00:00
|
|
|
from common.utils import get_logger
|
2016-12-19 17:19:50 +00:00
|
|
|
from .utils import check_user_valid, get_or_refresh_token
|
2016-08-24 09:14:21 +00:00
|
|
|
from .models import User, UserGroup
|
2016-11-10 15:54:21 +00:00
|
|
|
from .hands import write_login_log_async
|
2016-12-25 05:15:28 +00:00
|
|
|
from .permissions import IsSuperUser, IsAppUser, IsValidUser, IsSuperUserOrAppUser
|
2016-11-10 15:54:21 +00:00
|
|
|
from . import serializers
|
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
|
|
|
|
|
|
|
|
2016-11-25 03:00:51 +00:00
|
|
|
class UserViewSet(IDInFilterMixin, BulkModelViewSet):
|
2016-08-24 09:14:21 +00:00
|
|
|
queryset = User.objects.all()
|
2016-11-09 11:29:15 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2016-10-15 09:14:56 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2016-11-24 11:36:49 +00:00
|
|
|
filter_backends = (DjangoFilterBackend,)
|
2016-11-25 00:39:24 +00:00
|
|
|
filter_fields = ('username', 'email', 'name', 'id')
|
2016-11-24 09:08:20 +00:00
|
|
|
|
2016-08-24 09:14:21 +00:00
|
|
|
|
2016-11-14 16:48:48 +00:00
|
|
|
class UserUpdateGroupApi(generics.RetrieveUpdateAPIView):
|
2016-11-09 15:49:10 +00:00
|
|
|
queryset = User.objects.all()
|
2016-11-14 16:48:48 +00:00
|
|
|
serializer_class = serializers.UserUpdateGroupSerializer
|
2016-11-09 15:49:10 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2016-09-13 13:45:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserResetPasswordApi(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
2016-11-09 11:29:15 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2016-09-13 13:45:10 +00:00
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
# Note: we are not updating the user object here.
|
|
|
|
# We just do the reset-password staff.
|
2016-09-15 08:54:00 +00:00
|
|
|
import uuid
|
2016-11-09 11:29:15 +00:00
|
|
|
from .utils import send_reset_password_mail
|
|
|
|
user = self.get_object()
|
2016-09-15 08:54:00 +00:00
|
|
|
user.password_raw = str(uuid.uuid4())
|
|
|
|
user.save()
|
2016-09-13 13:45:10 +00:00
|
|
|
send_reset_password_mail(user)
|
|
|
|
|
|
|
|
|
2016-11-09 15:49:10 +00:00
|
|
|
class UserResetPKApi(generics.UpdateAPIView):
|
2016-09-13 13:45:10 +00:00
|
|
|
queryset = User.objects.all()
|
2016-11-09 11:29:15 +00:00
|
|
|
serializer_class = serializers.UserSerializer
|
2016-09-13 13:45:10 +00:00
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
2016-11-09 11:29:15 +00:00
|
|
|
from .utils import send_reset_ssh_key_mail
|
2016-09-13 13:45:10 +00:00
|
|
|
user = self.get_object()
|
2016-09-18 06:28:34 +00:00
|
|
|
user.is_public_key_valid = False
|
2016-09-13 13:45:10 +00:00
|
|
|
user.save()
|
2016-09-15 08:54:00 +00:00
|
|
|
send_reset_ssh_key_mail(user)
|
2016-09-18 06:28:34 +00:00
|
|
|
|
2016-11-09 15:49:10 +00:00
|
|
|
|
|
|
|
class UserUpdatePKApi(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.all()
|
|
|
|
serializer_class = serializers.UserPKUpdateSerializer
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
user = self.get_object()
|
|
|
|
user.public_key = serializer.validated_data['_public_key']
|
|
|
|
user.save()
|
|
|
|
|
2016-11-14 16:48:48 +00:00
|
|
|
|
2016-11-25 14:45:47 +00:00
|
|
|
class UserGroupViewSet(IDInFilterMixin, BulkModelViewSet):
|
2016-11-14 16:48:48 +00:00
|
|
|
queryset = UserGroup.objects.all()
|
|
|
|
serializer_class = serializers.UserGroupSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupUpdateUserApi(generics.RetrieveUpdateAPIView):
|
|
|
|
queryset = UserGroup.objects.all()
|
|
|
|
serializer_class = serializers.UserGroupUpdateMemeberSerializer
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
2016-10-15 09:14:56 +00:00
|
|
|
|
2016-12-16 11:32:05 +00:00
|
|
|
class UserToken(APIView):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
if not request.user:
|
|
|
|
return Response({'error': 'unauthorized'})
|
2016-12-19 17:19:50 +00:00
|
|
|
token = get_token(request)
|
2016-12-16 11:32:05 +00:00
|
|
|
return Response({'token': token})
|
|
|
|
|
|
|
|
|
|
|
|
class UserProfile(APIView):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return Response(request.user.to_json())
|
|
|
|
|
|
|
|
|
2016-11-09 11:29:15 +00:00
|
|
|
class UserAuthApi(APIView):
|
2016-10-31 10:58:23 +00:00
|
|
|
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', '')
|
2016-11-10 15:54:21 +00:00
|
|
|
remote_addr = request.data.get('remote_addr', '')
|
|
|
|
terminal = request.data.get('terminal', '')
|
|
|
|
login_type = request.data.get('login_type', 'T')
|
2016-10-31 10:58:23 +00:00
|
|
|
user = check_user_valid(username=username, password=password, public_key=public_key)
|
2016-11-09 11:29:15 +00:00
|
|
|
|
2016-10-31 10:58:23 +00:00
|
|
|
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-10 15:54:21 +00:00
|
|
|
write_login_log_async.delay(user.username, name=user.name, terminal=terminal,
|
|
|
|
login_ip=remote_addr, login_type=login_type)
|
2016-12-16 11:32:05 +00:00
|
|
|
return Response({'token': token, 'id': user.id, 'username': user.username,
|
|
|
|
'name': user.name, 'is_active': user.is_active})
|
2016-10-31 10:58:23 +00:00
|
|
|
else:
|
2016-11-07 08:59:52 +00:00
|
|
|
return Response({'msg': 'Invalid password or public key or user is not active or expired'}, status=401)
|