feat: Retrieve user's trust bridge information

This commit is contained in:
aldrin 2021-10-21 11:53:44 +02:00
parent b4c686bfaa
commit 818b19cf6b
7 changed files with 53 additions and 25 deletions

View file

@ -97,7 +97,14 @@ class User(PermissionsMixin, AbstractBaseUser):
raise PasswordMismatch("The given password does not match the user's password.")
return ldap.create_account(self.username, raw_password)
def get_or_create_trust_bridge(self):
try:
return self.trust_bridge
except TrustBridge.DoesNotExist:
return TrustBridge.objects.create(trust_taker=self)
class TrustBridge(models.Model):
user = models.OneToOneField("User", on_delete=models.CASCADE, related_name="trust_bridge")
is_trusted = models.BooleanField(default=False)
trust_giver = models.ForeignKey("User", on_delete=models.SET_NULL, null=True)
trust_taker = models.OneToOneField("User", on_delete=models.CASCADE, related_name="trust_bridge")

View file

@ -1,30 +1,9 @@
from rest_framework import serializers
from userausfall.models import TrustBridge, User
from userausfall.models import TrustBridge
class TrustBridgeSerializer(serializers.ModelSerializer):
class Meta:
model = TrustBridge
fields = ["is_trusted"]
class ActivateUserSerializer(serializers.Serializer):
password = serializers.CharField()
class RetrieveUserSerializer(serializers.ModelSerializer):
trust_bridge = TrustBridgeSerializer(required=False, read_only=True)
class Meta:
model = User
fields = ["pk", "username", "trust_bridge"]
class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("username", "password")
def create(self, validated_data):
return User.objects.create_user(**validated_data)
fields = ["is_trusted", "trust_giver"]

View file

@ -1 +1,2 @@
from .auth import * # noqa: F401, F403
from .trust_bridges import * # noqa: F401, F403

View file

@ -0,0 +1,22 @@
from rest_framework import status
from userausfall.rest_api.tests import UserausfallAPITestCase
from userausfall.tests import UserMixin
class TrustBridgeTestCase(UserMixin, UserausfallAPITestCase):
def test_retrieve_trust_bridge(self):
"""
Retrieve the trust bridge information of a user without an ldap account.
"""
url = "/trust-bridge/"
self.authenticate_user()
response = self.client.get(self.get_api_url(url))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data,
{
"is_trusted": False,
"trust_giver": None,
},
)

View file

@ -2,6 +2,8 @@ from django.urls import include, path
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
from rest_framework import routers
from userausfall.rest_api.views import TrustBridgeView
router = routers.SimpleRouter()
urlpatterns = [
@ -10,4 +12,5 @@ urlpatterns = [
path("schema/", SpectacularAPIView.as_view(), name="schema"),
path("schema/swagger-ui/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
path("schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
path("trust-bridge/", TrustBridgeView.as_view()),
]

View file

@ -1,8 +1,17 @@
from rest_framework import status, viewsets
from rest_framework import generics, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from userausfall.models import MissingUserAttribute, PasswordMismatch, User
from userausfall.rest_api.serializers import TrustBridgeSerializer
from userausfall.views import get_authenticated_user
class TrustBridgeView(generics.RetrieveAPIView):
serializer_class = TrustBridgeSerializer
def get_object(self):
return get_authenticated_user(self.request).get_or_create_trust_bridge()
class UserViewSet(viewsets.GenericViewSet):

7
userausfall/views.py Normal file
View file

@ -0,0 +1,7 @@
from userausfall.models import User
def get_authenticated_user(request) -> User:
if request is not None and request.user.is_authenticated:
return request.user
return None