Skip to content

fix: enforce() should return True if any result is True#119

Closed
adou0510 wants to merge 1 commit intocasdoor:masterfrom
adou0510:fix-enforce-multi-value
Closed

fix: enforce() should return True if any result is True#119
adou0510 wants to merge 1 commit intocasdoor:masterfrom
adou0510:fix-enforce-multi-value

Conversation

@adou0510
Copy link
Copy Markdown

Align Python SDK enforce() behavior with Node.js SDK. When Casdoor returns multiple values, return True if any value is True, instead of only checking the first value.

Align Python SDK enforce() behavior with Node.js SDK.
When Casdoor returns multiple values, return True if any value is True,
instead of only checking the first value.
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Adou seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz requested a review from Copilot April 16, 2026 15:43
@hsluoyz
Copy link
Copy Markdown
Member

hsluoyz commented Apr 16, 2026

@adou0510 fix linter:

image

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns the Python SDK’s enforce() semantics with the Node.js SDK by treating multi-value Casdoor responses as “allowed if any result is True”.

Changes:

  • Update CasdoorSDK.enforce() to return True when any boolean in the returned data list is True.
  • Improve handling of non-list data values (use data directly when present).
  • Reflow a few calls (oauth token payload, JWT cert load, enforce/batch-enforce params) with manual line breaks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/casdoor/main.py
"""
params = self._get_payload_for_access_token_request(code=code, username=username, password=password)
params = self._get_payload_for_access_token_request(
code=code, username=username, password=password)
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These manual line breaks don’t match the repository’s Black formatting (see pre-commit/CI), and Black will likely collapse this call back to a single line (or reflow it with the closing paren on its own line). Please run Black or format this call in a Black-compliant way to avoid CI/pre-commit failures.

Suggested change
code=code, username=username, password=password)
code=code,
username=username,
password=password,
)

Copilot uses AI. Check for mistakes.
Comment thread src/casdoor/main.py
"""
certificate = x509.load_pem_x509_certificate(self.certification, default_backend())
certificate = x509.load_pem_x509_certificate(
self.certification, default_backend())
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call formatting appears non-Black-compliant (multiline without a trailing comma and with the closing parenthesis on the last argument line). Since CI runs Black via pre-commit, please reformat with Black to prevent formatting check failures.

Suggested change
self.certification, default_backend())
self.certification,
default_backend(),
)

Copilot uses AI. Check for mistakes.
Comment thread src/casdoor/main.py
url = self.endpoint + "/api/enforce"
params = _build_enforce_params(permission_id, model_id, resource_id, enforce_id, owner)
params = _build_enforce_params(
permission_id, model_id, resource_id, enforce_id, owner)
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This multiline _build_enforce_params(...) call is unlikely to be Black-compliant (Black will reflow it, typically either to a single line or with a trailing comma + closing paren on its own line). Please run Black or adjust formatting accordingly so CI/pre-commit passes.

Suggested change
permission_id, model_id, resource_id, enforce_id, owner)
permission_id,
model_id,
resource_id,
enforce_id,
owner,
)

Copilot uses AI. Check for mistakes.
Comment thread src/casdoor/main.py
url = self.endpoint + "/api/batch-enforce"
params = _build_enforce_params(permission_id, model_id, "", enforce_id, owner)
params = _build_enforce_params(
permission_id, model_id, "", enforce_id, owner)
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This _build_enforce_params(...) call has the same non-Black-compliant multiline formatting pattern as above. Please run Black or reformat so the pre-commit Black hook in CI doesn’t fail.

Suggested change
permission_id, model_id, "", enforce_id, owner)
permission_id,
model_id,
"",
enforce_id,
owner,
)

Copilot uses AI. Check for mistakes.
Comment thread src/casdoor/main.py
Comment on lines +346 to +353
# Iterate through all results, return True if any is True
for result in data:
if not isinstance(result, bool):
error_str = "Casdoor response error:\n" + r.text
raise ValueError(error_str)
if result:
return True
return False
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CasdoorSDK.enforce() now returns True if any boolean in data is True, but AsyncCasdoorSDK.enforce() (src/casdoor/async_main.py) still returns only data[0]. This creates inconsistent behavior between the sync and async SDKs for the same API; consider updating the async implementation in the same way to keep parity.

Copilot uses AI. Check for mistakes.
Comment thread src/casdoor/main.py
Comment on lines +346 to +353
# Iterate through all results, return True if any is True
for result in data:
if not isinstance(result, bool):
error_str = "Casdoor response error:\n" + r.text
raise ValueError(error_str)
if result:
return True
return False
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated multi-result aggregation behavior (return True if any result is True) isn’t covered by existing tests: current enforce tests only assert the return type. Please add a test that mocks/fixtures the Casdoor response with data: [False, True] (and [False, False]) to verify the new semantics and prevent regressions.

Copilot uses AI. Check for mistakes.
@hsluoyz hsluoyz closed this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants