2024-07-01 18:10:40 +02:00
|
|
|
"""Permission handlers for the Meet core app."""
|
2024-07-30 16:48:26 +02:00
|
|
|
|
2024-11-08 10:32:50 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
from rest_framework import permissions
|
|
|
|
|
|
2024-06-25 00:21:36 +02:00
|
|
|
from ..models import RoleChoices
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
ACTION_FOR_METHOD_TO_PERMISSION = {
|
|
|
|
|
"versions_detail": {"DELETE": "versions_destroy", "GET": "versions_retrieve"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IsAuthenticated(permissions.BasePermission):
|
|
|
|
|
"""
|
|
|
|
|
Allows access only to authenticated users. Alternative method checking the presence
|
|
|
|
|
of the auth token to avoid hitting the database.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
|
return bool(request.auth) or request.user.is_authenticated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IsAuthenticatedOrSafe(IsAuthenticated):
|
|
|
|
|
"""Allows access to authenticated users (or anonymous users but only on safe methods)."""
|
|
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
|
if request.method in permissions.SAFE_METHODS:
|
|
|
|
|
return True
|
|
|
|
|
return super().has_permission(request, view)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IsSelf(IsAuthenticated):
|
|
|
|
|
"""
|
|
|
|
|
Allows access only to authenticated users. Alternative method checking the presence
|
|
|
|
|
of the auth token to avoid hitting the database.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
|
"""Write permissions are only allowed to the user itself."""
|
|
|
|
|
return obj == request.user
|
2024-06-25 00:21:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RoomPermissions(permissions.BasePermission):
|
|
|
|
|
"""
|
|
|
|
|
Permissions applying to the room API endpoint.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
|
"""Only allow authenticated users for unsafe methods."""
|
|
|
|
|
if request.method in permissions.SAFE_METHODS:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return request.user.is_authenticated
|
|
|
|
|
|
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
|
"""Object permissions are only given to administrators of the room."""
|
|
|
|
|
|
|
|
|
|
if request.method in permissions.SAFE_METHODS:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
|
|
|
|
|
|
if request.method == "DELETE":
|
|
|
|
|
return obj.is_owner(user)
|
|
|
|
|
|
2025-06-23 17:17:02 +02:00
|
|
|
return obj.is_administrator_or_owner(user)
|
2024-06-25 00:21:36 +02:00
|
|
|
|
|
|
|
|
|
2024-11-06 17:00:23 +01:00
|
|
|
class ResourceAccessPermission(IsAuthenticated):
|
2024-06-25 00:21:36 +02:00
|
|
|
"""
|
|
|
|
|
Permissions for a room that can only be updated by room administrators.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
|
"""
|
|
|
|
|
Check that the logged-in user is administrator of the linked room.
|
|
|
|
|
"""
|
|
|
|
|
user = request.user
|
|
|
|
|
if request.method == "DELETE" and obj.role == RoleChoices.OWNER:
|
|
|
|
|
return obj.user == user
|
|
|
|
|
|
2025-06-23 17:17:02 +02:00
|
|
|
return obj.resource.is_administrator_or_owner(user)
|
2024-11-06 17:00:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class HasAbilityPermission(IsAuthenticated):
|
|
|
|
|
"""Permission class for access objects."""
|
|
|
|
|
|
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
|
"""Check permission for a given object."""
|
|
|
|
|
return obj.get_abilities(request.user).get(view.action, False)
|
2024-11-08 10:32:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class HasPrivilegesOnRoom(IsAuthenticated):
|
|
|
|
|
"""Check if user has privileges on a given room."""
|
|
|
|
|
|
2025-04-16 10:10:07 +02:00
|
|
|
message = "You must have privileges on room to perform this action."
|
2024-11-08 10:32:50 +01:00
|
|
|
|
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
|
"""Determine if user has privileges on room."""
|
2025-06-23 17:17:02 +02:00
|
|
|
return obj.is_administrator_or_owner(request.user)
|
2024-11-08 10:32:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class IsRecordingEnabled(permissions.BasePermission):
|
|
|
|
|
"""Check if the recording feature is enabled."""
|
|
|
|
|
|
|
|
|
|
message = "Access denied, recording is disabled."
|
|
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
|
"""Determine if access is allowed based on settings."""
|
|
|
|
|
return settings.RECORDING_ENABLE
|
2024-11-08 17:01:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class IsStorageEventEnabled(permissions.BasePermission):
|
|
|
|
|
"""Check if the storage event feature is enabled."""
|
|
|
|
|
|
|
|
|
|
message = "Access denied, storage event is disabled."
|
|
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
|
"""Determine if access is allowed based on settings."""
|
|
|
|
|
return settings.RECORDING_STORAGE_EVENT_ENABLE
|