Created
July 28, 2025 06:42
-
-
Save vijayanant/218affe1ef2bca3a60841d6b07efb20b to your computer and use it in GitHub Desktop.
Duplication Vs Abstraction
This file contains hidden or 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
def can_user_act_on_document(user, document, action): | |
if user.is_admin: | |
return True | |
if action == "view": | |
return document.owner_id == user.id | |
if action == "edit": | |
return document.owner_id == user.id | |
return False # unknown action | |
# growing conditionals and nested logic |
This file contains hidden or 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
def can_user_view_document(user, document): | |
return document.owner_id == user.id or user.is_admin | |
def can_user_edit_document(user, document): | |
return document.owner_id == user.id |
This file contains hidden or 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
def can_user_act_on_document(user, document, action, context=None): | |
if user.is_admin: | |
return True | |
if action == "view": | |
if document.shared_with and user.id in document.shared_with: | |
return True | |
if document.status == "published" and document.is_public: | |
return True | |
return document.owner_id == user.id | |
if action == "edit": | |
if document.workspace_id in user.collaborating_workspaces: | |
return True | |
if context and context.get("override"): | |
log_override(user, document, action) | |
return True | |
return document.owner_id == user.id | |
log_permission_denied(user, document, action) | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment