Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion flask_appbuilder/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ class ExampleApi(BaseApi):
'delete': 'write'
}
"""
strict_method_mapping = False
"""
Enforcing that a permission mapping is defined for each and every methods
This can help preventing sprawling permissions in larger applications
"""
previous_method_permission_name: Optional[Dict[str, str]] = None
"""
Use same structure as method_permission_name. If set security converge
Expand Down Expand Up @@ -728,7 +733,10 @@ def get_method_permission(self, method_name: str) -> str:
Returns the permission name for a method
"""
if self.method_permission_name:
return self.method_permission_name.get(method_name, method_name)
perm = self.method_permission_name.get(method_name)
if self.strict_method_mapping and not perm:
raise FABException(f"Method {method_name} does not have a permission mapping")
return perm or method_name
else:
if hasattr(getattr(self, method_name), "_permission_name"):
return getattr(getattr(self, method_name), "_permission_name")
Expand Down
7 changes: 7 additions & 0 deletions flask_appbuilder/baseviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ class MyView(ModelView):
'delete': 'write'
}
"""
strict_method_mapping = False
"""
Enforcing that a permission mapping is defined for each and every methods
This can help preventing sprawling permissions in larger applications
"""
previous_method_permission_name = None
"""
Use same structure as method_permission_name. If set security converge
Expand Down Expand Up @@ -411,6 +416,8 @@ def get_method_permission(self, method_name: str) -> str:
Returns the permission name for a method
"""
permission = self.method_permission_name.get(method_name)
if self.strict_method_mapping and not permission:
raise FABException(f"Method {method_name} does not have a permission mapping")
if permission:
return permission
else:
Expand Down