access_control.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from typing import Optional, Union, List, Dict
  2. from open_webui.apps.webui.models.groups import Groups
  3. def get_permissions(
  4. user_id: str,
  5. default_permissions: Dict[str, bool] = {},
  6. ) -> dict:
  7. """
  8. Get all permissions for a user by combining the permissions of all groups the user is a member of.
  9. If a permission is defined in multiple groups, the most permissive value is used.
  10. """
  11. def merge_permissions(
  12. permissions: Dict[str, bool], new_permissions: Dict[str, bool]
  13. ) -> Dict[str, bool]:
  14. """Merge two permission dictionaries, keeping the most permissive value."""
  15. for key, value in new_permissions.items():
  16. if key not in permissions:
  17. permissions[key] = value
  18. else:
  19. permissions[key] = (
  20. permissions[key] or value
  21. ) # Use the most permissive value
  22. return permissions
  23. user_groups = Groups.get_groups_by_member_id(user_id)
  24. user_permissions = default_permissions.copy()
  25. for group in user_groups:
  26. user_permissions = merge_permissions(user_permissions, group.permissions)
  27. return user_permissions
  28. def has_permission(
  29. user_id: str,
  30. permission_key: str,
  31. default_permissions: Dict[str, bool] = {},
  32. ) -> bool:
  33. """
  34. Check if a user has a specific permission by checking the group permissions
  35. and falls back to default permissions if not found in any group.
  36. Permission keys can be hierarchical and separated by dots ('.').
  37. """
  38. def get_permission(permissions: Dict[str, bool], keys: List[str]) -> bool:
  39. """Traverse permissions dict using a list of keys (from dot-split permission_key)."""
  40. for key in keys:
  41. if key not in permissions:
  42. return False # If any part of the hierarchy is missing, deny access
  43. permissions = permissions[key] # Go one level deeper
  44. return bool(permissions) # Return the boolean at the final level
  45. permission_hierarchy = permission_key.split(".")
  46. # Retrieve user group permissions
  47. user_groups = Groups.get_groups_by_member_id(user_id)
  48. for group in user_groups:
  49. group_permissions = group.permissions
  50. if get_permission(group_permissions, permission_hierarchy):
  51. return True
  52. # Check default permissions afterwards if the group permissions don't allow it
  53. return get_permission(default_permissions, permission_hierarchy)
  54. def has_access(
  55. user_id: str,
  56. type: str = "write",
  57. access_control: Optional[dict] = None,
  58. ) -> bool:
  59. if access_control is None:
  60. return type == "read"
  61. user_groups = Groups.get_groups_by_member_id(user_id)
  62. user_group_ids = [group.id for group in user_groups]
  63. permission_access = access_control.get(type, {})
  64. permitted_group_ids = permission_access.get("group_ids", [])
  65. permitted_user_ids = permission_access.get("user_ids", [])
  66. return user_id in permitted_user_ids or any(
  67. group_id in permitted_group_ids for group_id in user_group_ids
  68. )