-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Permission class added for dynamics #58
Conversation
WalkthroughThe update to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ? TipsChat with CodeRabbit Bot (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (1)
- ms_business_central_api/settings.py (1 hunks)
Files skipped from review due to trivial changes (1)
- ms_business_central_api/settings.py
|
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 2
Configuration used: CodeRabbit UI
Files selected for processing (1)
- apps/workspaces/permissions.py (1 hunks)
Additional comments: 1
apps/workspaces/permissions.py (1)
- 1-52: Verify that the PR title accurately reflects the changes made. The title suggests a new permission class has been added, but the provided hunk and summary indicate the modification and removal of existing classes.
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): | ||
if user.id in workspace_users: | ||
if cache_users: | ||
cache.set(workspace_id, workspace_users, 172800) | ||
return True | ||
|
||
return False | ||
|
||
def has_permission(self, request, view): | ||
workspace_id = str(view.kwargs.get('workspace_id')) | ||
user = request.user | ||
workspace_users = cache.get(workspace_id) | ||
|
||
if workspace_users: | ||
return self.validate_and_cache(workspace_users, user, workspace_id) | ||
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The WorkspacePermissions
class introduces caching logic for workspace users. Ensure that the cache key is unique and cannot collide with other cache entries. Also, consider handling potential exceptions when interacting with the cache or the database.
+ try:
+ # Existing code for caching and database interaction
+ except SomeSpecificException as e:
+ # Handle exceptions appropriately
+ pass
Committable suggestion
❗ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
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): | |
if user.id in workspace_users: | |
if cache_users: | |
cache.set(workspace_id, workspace_users, 172800) | |
return True | |
return False | |
def has_permission(self, request, view): | |
workspace_id = str(view.kwargs.get('workspace_id')) | |
user = request.user | |
workspace_users = cache.get(workspace_id) | |
if workspace_users: | |
return self.validate_and_cache(workspace_users, user, workspace_id) | |
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) | |
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): | |
try: | |
if user.id in workspace_users: | |
if cache_users: | |
cache.set(workspace_id, workspace_users, 172800) | |
return True | |
return False | |
except SomeSpecificException as e: | |
# Handle exceptions appropriately | |
pass | |
def has_permission(self, request, view): | |
try: | |
workspace_id = str(view.kwargs.get('workspace_id')) | |
user = request.user | |
workspace_users = cache.get(workspace_id) | |
if workspace_users: | |
return self.validate_and_cache(workspace_users, user, workspace_id) | |
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) | |
except SomeSpecificException as e: | |
# Handle exceptions appropriately | |
pass |
class IsAuthenticatedForTest(permissions.BasePermission): | ||
""" | ||
Custom auth for preparing a workspace for e2e tests | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
# Client sends a token in the header, which we decrypt and compare with the Client Secret | ||
cipher_suite = Fernet(settings.ENCRYPTION_KEY) | ||
try: | ||
decrypted_password = cipher_suite.decrypt(request.headers['X-E2E-Tests-Client-ID'].encode('utf-8')).decode('utf-8') | ||
if decrypted_password == settings.E2E_TESTS_CLIENT_SECRET: | ||
return True | ||
except Exception: | ||
return False | ||
|
||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The IsAuthenticatedForTest
class uses encryption keys to decrypt a client ID. Ensure that the ENCRYPTION_KEY
and E2E_TESTS_CLIENT_SECRET
are securely managed and that the decryption process does not introduce security vulnerabilities. Additionally, catching a generic Exception
is not a best practice; catch specific exceptions instead.
[SECURITY]
- except Exception:
+ except (Fernet.InvalidToken, KeyError, AttributeError) as e:
+ # Log the error for debugging purposes
+ logger.error(f"Error during decryption: {e}")
Committable suggestion
❗ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
class IsAuthenticatedForTest(permissions.BasePermission): | |
""" | |
Custom auth for preparing a workspace for e2e tests | |
""" | |
def has_permission(self, request, view): | |
# Client sends a token in the header, which we decrypt and compare with the Client Secret | |
cipher_suite = Fernet(settings.ENCRYPTION_KEY) | |
try: | |
decrypted_password = cipher_suite.decrypt(request.headers['X-E2E-Tests-Client-ID'].encode('utf-8')).decode('utf-8') | |
if decrypted_password == settings.E2E_TESTS_CLIENT_SECRET: | |
return True | |
except Exception: | |
return False | |
return False | |
class IsAuthenticatedForTest(permissions.BasePermission): | |
""" | |
Custom auth for preparing a workspace for e2e tests | |
""" | |
def has_permission(self, request, view): | |
# Client sends a token in the header, which we decrypt and compare with the Client Secret | |
cipher_suite = Fernet(settings.ENCRYPTION_KEY) | |
try: | |
decrypted_password = cipher_suite.decrypt(request.headers['X-E2E-Tests-Client-ID'].encode('utf-8')).decode('utf-8') | |
if decrypted_password == settings.E2E_TESTS_CLIENT_SECRET: | |
return True | |
except (Fernet.InvalidToken, KeyError, AttributeError) as e: | |
# Log the error for debugging purposes | |
logger.error(f"Error during decryption: {e}") | |
return False | |
return False |
Summary by CodeRabbit
DEFAULT_PERMISSION_CLASSES
insettings.py
.WorkspacePermissions
class inpermissions.py
for workspace permission checks.IsAuthenticatedForTest
class inpermissions.py
for custom authentication during end-to-end tests.