access_control.py 3.3 KB

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