Skip to content

Commit 462aefc

Browse files
authored
feat: fix enforce param name from enforceId to enforcerId (#111)
1 parent 23f80a4 commit 462aefc

4 files changed

Lines changed: 84 additions & 30 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

src/.DS_Store

6 KB
Binary file not shown.

src/casdoor/async_main.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,43 @@
2525
from .user import User
2626

2727

28+
def _build_enforce_params(
29+
permission_id: str,
30+
model_id: str,
31+
resource_id: str,
32+
enforce_id: str,
33+
owner: str,
34+
) -> Dict[str, str]:
35+
"""
36+
Build and validate parameters for enforce API calls.
37+
38+
Exactly one of the parameters must be provided and non-empty.
39+
40+
:return: Dictionary with exactly one parameter set
41+
:raises ValueError: If zero or multiple parameters are provided
42+
"""
43+
params: Dict[str, str] = {}
44+
if permission_id:
45+
params["permissionId"] = permission_id
46+
if model_id:
47+
params["modelId"] = model_id
48+
if resource_id:
49+
params["resourceId"] = resource_id
50+
if enforce_id:
51+
params["enforcerId"] = enforce_id
52+
if owner:
53+
params["owner"] = owner
54+
55+
if len(params) != 1:
56+
raise ValueError(
57+
"Exactly one of (permission_id, model_id, resource_id, enforce_id, owner) "
58+
"must be provided and non-empty. "
59+
f"Got {len(params)} parameters: {list(params.keys())}"
60+
)
61+
62+
return params
63+
64+
2865
class AioHttpClient:
2966
def __init__(self, base_url):
3067
self.base_url = base_url
@@ -277,19 +314,13 @@ async def enforce(
277314
:param permission_id: the permission id (i.e. organization name/permission name)
278315
:param model_id: the model id
279316
:param resource_id: the resource id
280-
:param enforce_id: the enforce id
317+
:param enforce_id: the enforcer id (note: uses 'enforcerId' parameter in API)
281318
:param owner: the owner of the permission
282319
:param casbin_request: a list containing the request data (i.e. sub, obj, act)
283320
:return: a boolean value indicating whether the request is allowed
284321
"""
285322
url = "/api/enforce"
286-
params: Dict[str, str] = {
287-
"permissionId": permission_id,
288-
"modelId": model_id,
289-
"resourceId": resource_id,
290-
"enforceId": enforce_id,
291-
"owner": owner,
292-
}
323+
params = _build_enforce_params(permission_id, model_id, resource_id, enforce_id, owner)
293324

294325
async with self._session as session:
295326
response = await session.post(
@@ -328,18 +359,13 @@ async def batch_enforce(
328359
329360
:param permission_id: the permission id (i.e. organization name/permission name)
330361
:param model_id: the model id
331-
:param enforce_id: the enforce id
362+
:param enforce_id: the enforcer id (note: uses 'enforcerId' parameter in API)
332363
:param owner: the owner of the permission
333364
:param casbin_request: a list of lists containing the request data
334365
:return: a list of boolean values indicating whether each request is allowed
335366
"""
336367
url = "/api/batch-enforce"
337-
params = {
338-
"permissionId": permission_id,
339-
"modelId": model_id,
340-
"enforceId": enforce_id,
341-
"owner": owner,
342-
}
368+
params = _build_enforce_params(permission_id, model_id, "", enforce_id, owner)
343369

344370
async with self._session as session:
345371
response = await session.post(

src/casdoor/main.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,43 @@
4343
from .webhook import _WebhookSDK
4444

4545

46+
def _build_enforce_params(
47+
permission_id: str,
48+
model_id: str,
49+
resource_id: str,
50+
enforce_id: str,
51+
owner: str,
52+
) -> Dict[str, str]:
53+
"""
54+
Build and validate parameters for enforce API calls.
55+
56+
Exactly one of the parameters must be provided and non-empty.
57+
58+
:return: Dictionary with exactly one parameter set
59+
:raises ValueError: If zero or multiple parameters are provided
60+
"""
61+
params = {}
62+
if permission_id:
63+
params["permissionId"] = permission_id
64+
if model_id:
65+
params["modelId"] = model_id
66+
if resource_id:
67+
params["resourceId"] = resource_id
68+
if enforce_id:
69+
params["enforcerId"] = enforce_id
70+
if owner:
71+
params["owner"] = owner
72+
73+
if len(params) != 1:
74+
raise ValueError(
75+
"Exactly one of (permission_id, model_id, resource_id, enforce_id, owner) "
76+
"must be provided and non-empty. "
77+
f"Got {len(params)} parameters: {list(params.keys())}"
78+
)
79+
80+
return params
81+
82+
4683
class CasdoorSDK(
4784
_UserSDK,
4885
_AdapterSDK,
@@ -281,19 +318,14 @@ def enforce(
281318
:param permission_id: the permission id (i.e. organization name/permission name)
282319
:param model_id: the model id
283320
:param resource_id: the resource id
284-
:param enforce_id: the enforce id
321+
:param enforce_id: the enforcer id (note: uses 'enforcerId' parameter in API)
285322
:param owner: the owner of the permission
286323
:param casbin_request: a list containing the request data (i.e. sub, obj, act)
287324
:return: a boolean value indicating whether the request is allowed
288325
"""
289326
url = self.endpoint + "/api/enforce"
290-
params = {
291-
"permissionId": permission_id,
292-
"modelId": model_id,
293-
"resourceId": resource_id,
294-
"enforceId": enforce_id,
295-
"owner": owner,
296-
}
327+
params = _build_enforce_params(permission_id, model_id, resource_id, enforce_id, owner)
328+
297329
r = requests.post(
298330
url,
299331
params=params,
@@ -332,18 +364,14 @@ def batch_enforce(
332364
333365
:param permission_id: the permission id (i.e. organization name/permission name)
334366
:param model_id: the model id
335-
:param enforce_id: the enforce id
367+
:param enforce_id: the enforcer id (note: uses 'enforcerId' parameter in API)
336368
:param owner: the owner of the permission
337369
:param casbin_request: a list of lists containing the request data
338370
:return: a list of boolean values indicating whether each request is allowed
339371
"""
340372
url = self.endpoint + "/api/batch-enforce"
341-
params = {
342-
"permissionId": permission_id,
343-
"modelId": model_id,
344-
"enforceId": enforce_id,
345-
"owner": owner,
346-
}
373+
params = _build_enforce_params(permission_id, model_id, "", enforce_id, owner)
374+
347375
r = requests.post(
348376
url,
349377
params=params,

0 commit comments

Comments
 (0)