-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user permissions for chat and chat messages
- Loading branch information
Showing
13 changed files
with
332 additions
and
40 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
babblebox/babblebox/api/migrations/0002_participant_chat_participants_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 4.2.9 on 2024-03-06 01:53 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("api", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Participant", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("is_owner", models.BooleanField(default=False)), | ||
("has_read_access", models.BooleanField(default=True)), | ||
("has_write_access", models.BooleanField(default=True)), | ||
("last_updated", models.DateTimeField(auto_now=True)), | ||
("chat", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="api.chat")), | ||
("user", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name="chat", | ||
name="participants", | ||
field=models.ManyToManyField(through="api.Participant", to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddIndex( | ||
model_name="participant", | ||
index=models.Index(fields=["chat"], name="chat_participant_idx"), | ||
), | ||
migrations.AddIndex( | ||
model_name="participant", | ||
index=models.Index(fields=["user"], name="user_participant_idx"), | ||
), | ||
migrations.AddIndex( | ||
model_name="participant", | ||
index=models.Index(fields=["chat", "user"], name="chat_user_participant_idx"), | ||
), | ||
] |
77 changes: 77 additions & 0 deletions
77
...lebox/api/migrations/0003_chatparticipant_remove_chat_participants_chat_owner_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Generated by Django 4.2.9 on 2024-03-06 16:57 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("api", "0002_participant_chat_participants_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="ChatParticipant", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("has_read_access", models.BooleanField(default=True)), | ||
("has_write_access", models.BooleanField(default=True)), | ||
("last_updated", models.DateTimeField(auto_now=True)), | ||
], | ||
), | ||
migrations.RemoveField( | ||
model_name="chat", | ||
name="participants", | ||
), | ||
migrations.AddField( | ||
model_name="chat", | ||
name="owner", | ||
field=models.ForeignKey( | ||
default=3, | ||
editable=False, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="chat_owner", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name="chatmessage", | ||
name="owner", | ||
field=models.ForeignKey( | ||
default=3, | ||
editable=False, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="chat_message_owner", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.DeleteModel( | ||
name="Participant", | ||
), | ||
migrations.AddField( | ||
model_name="chatparticipant", | ||
name="chat", | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="api.chat"), | ||
), | ||
migrations.AddField( | ||
model_name="chatparticipant", | ||
name="user", | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddIndex( | ||
model_name="chatparticipant", | ||
index=models.Index(fields=["chat"], name="chat_participant_idx"), | ||
), | ||
migrations.AddIndex( | ||
model_name="chatparticipant", | ||
index=models.Index(fields=["user"], name="user_participant_idx"), | ||
), | ||
migrations.AddIndex( | ||
model_name="chatparticipant", | ||
index=models.Index(fields=["chat", "user"], name="chat_user_participant_idx"), | ||
), | ||
] |
19 changes: 19 additions & 0 deletions
19
babblebox/babblebox/api/migrations/0004_chat_participants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.9 on 2024-03-06 16:58 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("api", "0003_chatparticipant_remove_chat_participants_chat_owner_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="chat", | ||
name="participants", | ||
field=models.ManyToManyField(through="api.ChatParticipant", to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.9 on 2024-03-06 17:35 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("api", "0004_chat_participants"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="chat", | ||
name="is_public", | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from rest_framework import permissions | ||
|
||
class IsOwnerOrReadOnly(permissions.BasePermission): | ||
""" | ||
Custom permission to only allow owners of an object to edit it. | ||
""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
# Read permissions are allowed to any request, | ||
# so we'll always allow GET, HEAD or OPTIONS requests. | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
# Write permissions are only allowed to the owner of the chat. | ||
return obj.owner == request.user | ||
|
||
|
||
class IsParticipantOrOwner(permissions.BasePermission): | ||
""" | ||
Custom permission to only allow participants or the owner of the chat to view it. | ||
""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if request.user == obj.owner: | ||
return True | ||
return obj.participants.filter(id=request.user.id).exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
|
||
from .views import AudioFileViewSet, ChatViewSet, ChatMessageViewSet, ImageFileViewSet | ||
from .views import AudioFileViewSet, ChatViewSet, ChatMessageViewSet, ImageFileViewSet, ParticipantViewSet | ||
|
||
router = DefaultRouter() | ||
router.register(r'AudioFile', AudioFileViewSet) | ||
router.register(r'ImageFile', ImageFileViewSet) | ||
router.register(r'Chat', ChatViewSet) | ||
router.register(r'ChatMessage', ChatMessageViewSet) | ||
router.register(r'Participant', ParticipantViewSet) | ||
|
||
urlpatterns = [ | ||
path('', include(router.urls)), | ||
] | ||
] |
Oops, something went wrong.