feat: Add API permissions
This commit is contained in:
parent
d656370aef
commit
04c2ec8ec2
4 changed files with 29 additions and 23 deletions
|
@ -50,7 +50,7 @@ export class User {
|
||||||
* Activate the corresponding LDAP account.
|
* Activate the corresponding LDAP account.
|
||||||
*/
|
*/
|
||||||
async activate(): Promise<void> {
|
async activate(): Promise<void> {
|
||||||
await api_request("POST", "users/activate", 204, {
|
await api_request("POST", `users/${this.pk}/activate`, 204, {
|
||||||
password: this.password,
|
password: this.password,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,17 @@ from rest_framework import permissions
|
||||||
|
|
||||||
class UserPermission(permissions.BasePermission):
|
class UserPermission(permissions.BasePermission):
|
||||||
def has_permission(self, request, view):
|
def has_permission(self, request, view):
|
||||||
if request.method == "POST":
|
result = False
|
||||||
return True
|
if view.action == "activate":
|
||||||
return False
|
result = True
|
||||||
|
elif view.action == "create":
|
||||||
|
result = True
|
||||||
|
elif view.action == "retrieve_authenticated":
|
||||||
|
result = request.user.is_authenticated
|
||||||
|
return result
|
||||||
|
|
||||||
def has_object_permission(self, request, view, obj):
|
def has_object_permission(self, request, view, obj):
|
||||||
return False
|
result = False
|
||||||
|
if view.action == "activate":
|
||||||
|
result = request.user == obj
|
||||||
|
return result
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
from userausfall.rest_api.views import UserViewSet, ConfidantConfirmationView
|
from userausfall.rest_api.views import UserViewSet
|
||||||
|
|
||||||
router = routers.DefaultRouter(trailing_slash=True)
|
router = routers.DefaultRouter(trailing_slash=True)
|
||||||
router.register(r'users', UserViewSet, basename="user")
|
router.register(r'users', UserViewSet, basename="user")
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("confirm/confidant/", ConfidantConfirmationView.as_view())
|
# path("confirm/confidant/", ConfidantConfirmationView.as_view())
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns += router.urls
|
urlpatterns += router.urls
|
||||||
|
|
|
@ -2,36 +2,30 @@ from rest_framework import viewsets, status
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from djeveric import ConfirmationView
|
|
||||||
from userausfall.models import User, MissingUserAttribute, PasswordMismatch
|
from userausfall.models import User, MissingUserAttribute, PasswordMismatch
|
||||||
from userausfall.confirmations import ConfidantConfirmation
|
from userausfall.rest_api.permissions import UserPermission
|
||||||
from userausfall.rest_api.serializers import (
|
from userausfall.rest_api.serializers import (
|
||||||
ActivateUserSerializer,
|
ActivateUserSerializer,
|
||||||
CreateUserSerializer,
|
CreateUserSerializer,
|
||||||
TrustBridgeSerializer, RetrieveUserSerializer,
|
RetrieveUserSerializer,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ConfidantConfirmationView(ConfirmationView):
|
|
||||||
confirmation_class = ConfidantConfirmation
|
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
# permission_classes = [UserPermission]
|
permission_classes = [UserPermission]
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
|
|
||||||
@action(detail=False)
|
@action(detail=False, url_path="me")
|
||||||
def me(self, request):
|
def retrieve_authenticated(self, request):
|
||||||
"""Retrieve user data for logged in user."""
|
"""Retrieve user data for logged in user."""
|
||||||
user = request.user
|
serializer = self.get_serializer(request.user)
|
||||||
serializer = RetrieveUserSerializer(user)
|
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
@action(detail=False, methods=["post"])
|
@action(detail=True, methods=["post"])
|
||||||
def activate(self, request, pk=None):
|
def activate(self, request, pk=None):
|
||||||
"""Create the corresponding LDAP account."""
|
"""Create the corresponding LDAP account."""
|
||||||
user: User = request.user # self.get_object()
|
user: User = self.get_object()
|
||||||
serializer = ActivateUserSerializer(data=request.data)
|
serializer = self.get_serializer(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
try:
|
try:
|
||||||
# We prevent untrusted user accounts from being activated via API.
|
# We prevent untrusted user accounts from being activated via API.
|
||||||
|
@ -46,5 +40,9 @@ class UserViewSet(viewsets.ModelViewSet):
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
def get_serializer_class(self):
|
def get_serializer_class(self):
|
||||||
if self.action == "create":
|
if self.action == "activate":
|
||||||
|
return ActivateUserSerializer
|
||||||
|
elif self.action == "create":
|
||||||
return CreateUserSerializer
|
return CreateUserSerializer
|
||||||
|
elif self.action == "retrieve_authenticated":
|
||||||
|
return RetrieveUserSerializer
|
||||||
|
|
Reference in a new issue