Skip to content

Commit

Permalink
custom apis for permissioning
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Apr 9, 2024
1 parent 71fd942 commit db3c49c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/urban/upmigrate/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from .views import UpdateUserPassword, UpdateResourceOwner

urlpatterns = [
path('api/v2/extra/admin-set-user-password/', view=UpdateUserPassword.as_view()),
path('api/v2/extra/admin-set-resource-owner/', view=UpdateResourceOwner.as_view()),
]
45 changes: 45 additions & 0 deletions src/urban/upmigrate/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers, status, permissions

from geonode.base.models import ResourceBase

from django.contrib.auth import get_user_model

User = get_user_model()

class UserPasswordSerializer(serializers.Serializer):
username = serializers.CharField(required=True)
password = serializers.CharField(required=True)


class ChangeResourceOwnershipSerializer(serializers.Serializer):
username = serializers.CharField(required=True)
resource_id = serializers.IntegerField(required=True)


class UpdateUserPassword(APIView):
permission_classes = [permissions.IsAdminUser]

def post(self, request):
serializer = UserPasswordSerializer(data=request.data)
if serializer.is_valid():
u = User.objects.get(username=serializer.data['username'])
u.set_password(serializer.data['password'])
u.save()
return Response({'success': True})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class UpdateResourceOwner(APIView):
permission_classes = [permissions.IsAdminUser]

def post(self, request):
serializer = ChangeResourceOwnershipSerializer(data=request.data)
if serializer.is_valid():
u = User.objects.get(username=serializer.data['username'])
r = ResourceBase.objects.get(id=serializer.data['resource_id'])
r.owner = u
r.save()
return Response({'success': True})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
11 changes: 4 additions & 7 deletions src/urban/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
#
#########################################################################

from django.urls import path, include
from geonode.urls import urlpatterns

"""
# You can register your own urlpatterns here

urlpatterns = [
url(r'^/?$',
homepage,
name='home'),
] + urlpatterns
"""
path('', include('upmigrate')),
] + urlpatterns

0 comments on commit db3c49c

Please sign in to comment.