def create(self, request): """ Create User Create new user """ serializer = UserCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): username = serializer.validated_data.get("username") role = serializer.validated_data.get("role") organization = serializer.validated_data.get("organization") password = serializer.validated_data.get("password") email = serializer.validated_data.get("email") user_count = UserProfile.objects.filter( Q(username=username) | Q(email=email)).count() if user_count > 0: raise ResourceExists( detail="User name or email already exists") user = UserProfile( username=username, role=role, email=email, organization=organization, ) user.set_password(password) user.save() response = UserIDSerializer(data={"id": user.id}) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_201_CREATED)
def create(self, request): """ Create User Create new user """ serializer = UserCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): username = serializer.validated_data.get("username") role = serializer.validated_data.get("role") organization = serializer.validated_data.get("organization") password = serializer.validated_data.get("password") email = serializer.validated_data.get("email") user_count = UserProfile.objects.filter( Q(username=username) | Q(email=email) ).count() if user_count > 0: raise ResourceExists( detail="User name or email already exists" ) user = UserProfile( username=username, role=role, email=email, organization=organization, ) user.set_password(password) user.save() response = UserIDSerializer(data={"id": user.id}) if response.is_valid(raise_exception=True): return Response( response.validated_data, status=status.HTTP_201_CREATED )
def _add_user(request, pk=None): serializer = UserIDSerializer(data=request.data) if serializer.is_valid(raise_exception=True): user_id = serializer.validated_data.get("id") try: organization = Organization.objects.get(id=pk) user = UserProfile.objects.get(id=user_id) if user.organization: raise ResourceInUse except ObjectDoesNotExist: raise ResourceNotFound else: user.organization = organization user.save() return Response(status=status.HTTP_202_ACCEPTED)
def create(self, request): """ Create User Create new user """ serializer = UserCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") role = serializer.validated_data.get("role") govern = serializer.validated_data.get("govern") password = serializer.validated_data.get("password") keycloak_client = KeyCloakClient() user_exists = keycloak_client.get_user(username=name) if user_exists: raise ResourceExists create_user_body = { "username": name, "requiredActions": [], "enabled": True, } keycloak_client.create_user(create_user_body) user_id = keycloak_client.get_user_id(username=name) keycloak_client.reset_user_password(user_id, password) user_attr = {"role": role} if govern: user_attr.update({"govern": str(govern.id)}) keycloak_client.update_user(user_id, body={"attributes": user_attr}) user, _ = UserModel.objects.get_or_create(id=user_id, name=name, role=role, govern=govern) response = UserIDSerializer(data={"id": user_id}) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_201_CREATED)
def _add_user(request, pk=None): serializer = UserIDSerializer(data=request.data) if serializer.is_valid(raise_exception=True): user_id = serializer.validated_data.get("id") try: organization = Organization.objects.get(id=pk) user = UserProfile.objects.get(id=user_id) if user.govern: raise ResourceInUse except ObjectDoesNotExist: raise ResourceNotFound else: keycloak_client = KeyCloakClient() user_info = keycloak_client.get_user(user.name) user_attr = user_info.get("attributes", {}) user_attr.update({"organization": pk}) keycloak_client.update_user(user_id, body={"attributes": user_attr}) user.organization = organization user.save() return Response(status=status.HTTP_202_ACCEPTED)
def _add_user(request, pk=None): serializer = UserIDSerializer(data=request.data) if serializer.is_valid(raise_exception=True): user_id = serializer.validated_data.get("id") try: organization = Organization.objects.get(id=pk) user = UserProfile.objects.get(id=user_id) if user.govern: raise ResourceInUse except ObjectDoesNotExist: raise ResourceNotFound else: keycloak_client = KeyCloakClient() user_info = keycloak_client.get_user(user.name) user_attr = user_info.get("attributes", {}) user_attr.update({"organization": pk}) keycloak_client.update_user( user_id, body={"attributes": user_attr} ) user.organization = organization user.save() return Response(status=status.HTTP_202_ACCEPTED)