Skip to content

Instantly share code, notes, and snippets.

@vijayanant
Created July 28, 2025 06:42
Show Gist options
  • Save vijayanant/218affe1ef2bca3a60841d6b07efb20b to your computer and use it in GitHub Desktop.
Save vijayanant/218affe1ef2bca3a60841d6b07efb20b to your computer and use it in GitHub Desktop.
Duplication Vs Abstraction
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
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
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