Skip to content

Commit

Permalink
Missing permission option added
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarans committed Jun 7, 2024
1 parent 482b7e5 commit b55483d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .config/pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.11
python: python3.12
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand All @@ -9,7 +9,7 @@ repos:
exclude: test_scraper_.*\.json
- id: check-ast
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
rev: v0.4.8
hooks:
# Run the linter.
- id: ruff
Expand All @@ -18,7 +18,7 @@ repos:
- id: ruff-format
args: [--config, .config/ruff.toml]
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.1.38
rev: 0.2.9
hooks:
# Run the pip compile
- id: pip-compile
Expand Down
32 changes: 26 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ chardet==5.2.0
charset-normalizer==3.3.2
# via requests
ckanapi==4.8
# via hdx-python-api (pyproject.toml)
click==8.1.7
# via typer
coverage==7.5.3
# via pytest-cov
cryptography==42.0.8
# via pyopenssl
defopt==6.4.0
# via hdx-python-api (pyproject.toml)
distlib==0.3.8
# via virtualenv
dnspython==2.6.1
Expand All @@ -39,22 +41,27 @@ docopt==0.6.2
docutils==0.21.2
# via defopt
email-validator==2.1.1
# via hdx-python-api (pyproject.toml)
et-xmlfile==1.1.0
# via openpyxl
filelock==3.14.0
# via virtualenv
frictionless==5.17.0
# via hdx-python-utilities
google-auth==2.29.0
google-auth==2.30.0
# via
# google-auth-oauthlib
# gspread
google-auth-oauthlib==1.2.0
# via gspread
gspread==6.1.2
# via hdx-python-api (pyproject.toml)
hdx-python-country==3.7.2
# via hdx-python-api (pyproject.toml)
hdx-python-utilities==3.6.9
# via hdx-python-country
# via
# hdx-python-api (pyproject.toml)
# hdx-python-country
humanize==4.9.0
# via frictionless
identify==2.5.36
Expand All @@ -63,7 +70,7 @@ idna==3.7
# via
# email-validator
# requests
ijson==3.2.3
ijson==3.3.0
# via hdx-python-utilities
inflect==7.2.1
# via quantulum3
Expand All @@ -84,10 +91,13 @@ jsonschema==4.22.0
jsonschema-specifications==2023.12.1
# via jsonschema
libhxl==5.2.1
# via hdx-python-country
# via
# hdx-python-api (pyproject.toml)
# hdx-python-country
loguru==0.7.2
# via hdx-python-utilities
makefun==1.15.2
# via hdx-python-api (pyproject.toml)
markdown-it-py==3.0.0
# via rich
marko==2.0.3
Expand All @@ -99,6 +109,7 @@ mdurl==0.1.2
more-itertools==10.2.0
# via inflect
ndg-httpsclient==0.5.1
# via hdx-python-api (pyproject.toml)
nodeenv==1.9.1
# via pre-commit
num2words==0.5.13
Expand All @@ -122,8 +133,10 @@ ply==3.11
pockets==0.9.1
# via sphinxcontrib-napoleon
pre-commit==3.7.1
# via hdx-python-api (pyproject.toml)
pyasn1==0.6.0
# via
# hdx-python-api (pyproject.toml)
# ndg-httpsclient
# pyasn1-modules
# rsa
Expand All @@ -138,12 +151,17 @@ pydantic-core==2.18.4
pygments==2.18.0
# via rich
pyopenssl==24.1.0
# via ndg-httpsclient
# via
# hdx-python-api (pyproject.toml)
# ndg-httpsclient
pyphonetics==0.5.3
# via hdx-python-country
pytest==8.2.2
# via pytest-cov
# via
# hdx-python-api (pyproject.toml)
# pytest-cov
pytest-cov==5.0.0
# via hdx-python-api (pyproject.toml)
python-dateutil==2.8.2
# via
# frictionless
Expand All @@ -161,6 +179,7 @@ pyyaml==6.0.1
# pre-commit
# tableschema-to-template
quantulum3==0.9.1
# via hdx-python-api (pyproject.toml)
ratelimit==2.2.1
# via hdx-python-utilities
referencing==0.35.1
Expand All @@ -169,6 +188,7 @@ referencing==0.35.1
# jsonschema-specifications
requests==2.32.3
# via
# hdx-python-api (pyproject.toml)
# ckanapi
# frictionless
# libhxl
Expand Down
20 changes: 15 additions & 5 deletions src/hdx/data/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,21 @@ def get_organizations(
organizations.append(org)
return organizations

def check_organization_access(self, organization: str) -> bool:
def check_organization_access(
self, organization: str, permission: str = "read"
) -> bool:
"""Check user is a member of a given organization.
Args:
organization (str): Organization id or name.
permission (str): Permission to check for. Defaults to 'read'.
Returns:
bool: True if the logged in user is a member of the organization.
"""
for organization_dict in self.get_organization_dicts():
for organization_dict in self.get_organization_dicts(
permission=permission
):
if organization_dict["id"] == organization:
return True
if organization_dict["name"] == organization:
Expand All @@ -323,7 +328,7 @@ def get_current_user_organization_dicts(
user = User(configuration=configuration)
try:
return user.configuration.call_remoteckan(
cls.actions()["listorgs"]
cls.actions()["listorgs"], {"permission": permission}
)
except Exception:
return []
Expand Down Expand Up @@ -355,16 +360,21 @@ def get_current_user_organizations(
return organizations

@classmethod
def check_current_user_organization_access(cls, organization: str) -> bool:
def check_current_user_organization_access(
cls, organization: str, permission: str = "read"
) -> bool:
"""Check logged in user is a member of a given organization.
Args:
organization (str): Organization id or name.
permission (str): Permission to check for. Defaults to 'read'.
Returns:
bool: True if the logged in user is a member of the organization.
"""
for organization_dict in cls.get_current_user_organization_dicts():
for organization_dict in cls.get_current_user_organization_dicts(
permission=permission
):
if organization_dict["id"] == organization:
return True
if organization_dict["name"] == organization:
Expand Down

0 comments on commit b55483d

Please sign in to comment.