Skip to content

Commit

Permalink
Add loggers to permission denied attempts (#571)
Browse files Browse the repository at this point in the history
* Add loggers to permission denied attempts

* fix lint
  • Loading branch information
ashwin1111 authored Feb 23, 2024
1 parent 5f0b9c0 commit 5099454
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apps/workspaces/permissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from cryptography.fernet import Fernet
from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -8,18 +9,25 @@

User = get_user_model()

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class WorkspacePermissions(permissions.BasePermission):
"""
Permission check for users <> workspaces
"""

def validate_and_cache(self, workspace_users, user: User, workspace_id: str, cache_users: bool = False):
def validate_and_cache(self, workspace_users, user: User, workspace_id: str, payload: dict, cache_users: bool = False):
if user.id in workspace_users:
if cache_users:
cache.set(workspace_id, workspace_users, 172800)
return True

logger.error(f'User {user.id} is not allowed to access workspace {workspace_id}')
logger.info(f'Permission was cached earlier: {not cache_users}')
logger.info(f'Allowed users: {workspace_users}')
logger.info(f'Payload: {payload}')
return False

def has_permission(self, request, view):
Expand All @@ -28,10 +36,10 @@ def has_permission(self, request, view):
workspace_users = cache.get(workspace_id)

if workspace_users:
return self.validate_and_cache(workspace_users, user, workspace_id)
return self.validate_and_cache(workspace_users, user, workspace_id, request.data)
else:
workspace_users = Workspace.objects.filter(pk=workspace_id).values_list('user', flat=True)
return self.validate_and_cache(workspace_users, user, workspace_id, True)
return self.validate_and_cache(workspace_users, user, workspace_id, request.data, True)


class IsAuthenticatedForTest(permissions.BasePermission):
Expand Down

0 comments on commit 5099454

Please sign in to comment.