Skip to content

Commit

Permalink
feat: add get_user_count() to sdk (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirovVerst authored Apr 15, 2023
1 parent b25d1a9 commit f06ae3f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ casdoor-python-sdk support basic user operations, like:
- `modify_user(method: str, user: User)/add_user(user: User)/update_user(user: User)/delete_user(user: User)`, write user to database.
- `refresh_token_request(refresh_token: str, scope: str)`, refresh access token
- `enforce(self, permission_model_name: str, sub: str, obj: str, act: str)`, check permission from model

- `get_user_count(is_online: bool = None)`, get user count.

## Resource Owner Password Credentials Grant

Expand Down
23 changes: 23 additions & 0 deletions src/casdoor/async_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,29 @@ async def get_user(self, user_id: str) -> dict:
user = await request.json()
return user

async def get_user_count(self, is_online: bool = None) -> int:
"""
Get the count of filtered users for an organization
:param is_online: True for online users, False for offline users,
None for all users
:return: the count of filtered users for an organization
"""
url = self.endpoint + "/api/get-user-count"
params = {
"owner": self.org_name,
"clientId": self.client_id,
"clientSecret": self.client_secret,
}

if is_online is None:
params["isOnline"] = ""
else:
params["isOnline"] = "1" if is_online else "0"

async with self._session.get(url, params=params) as request:
count = await request.json()
return count

async def modify_user(self, method: str, user: User) -> dict:
url = self.endpoint + f"/api/{method}"
user.owner = self.org_name
Expand Down
23 changes: 23 additions & 0 deletions src/casdoor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,29 @@ def get_user(self, user_id: str) -> dict:
user = r.json()
return user

def get_user_count(self, is_online: bool = None) -> int:
"""
Get the count of filtered users for an organization
:param is_online: True for online users, False for offline users,
None for all users
:return: the count of filtered users for an organization
"""
url = self.endpoint + "/api/get-user-count"
params = {
"owner": self.org_name,
"clientId": self.client_id,
"clientSecret": self.client_secret,
}

if is_online is None:
params["isOnline"] = ""
else:
params["isOnline"] = "1" if is_online else "0"

r = requests.get(url, params)
count = r.json()
return count

def modify_user(self, method: str, user: User) -> dict:
url = self.endpoint + f"/api/{method}"
user.owner = self.org_name
Expand Down
10 changes: 10 additions & 0 deletions src/tests/test_async_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ async def test_get_user(self):
user = await sdk.get_user("admin")
self.assertIsInstance(user, dict)

async def test_get_user_count(self):
sdk = self.get_sdk()
online_count = await sdk.get_user_count(is_online=True)
offline_count = await sdk.get_user_count(is_online=False)
all_count = await sdk.get_user_count()
self.assertIsInstance(online_count, int)
self.assertIsInstance(offline_count, int)
self.assertIsInstance(all_count, int)
self.assertEqual(online_count + offline_count, all_count)

async def test_modify_user(self):
sdk = self.get_sdk()
user = User()
Expand Down
10 changes: 10 additions & 0 deletions src/tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ def test_get_users(self):
users = sdk.get_users()
self.assertIsInstance(users, list)

def test_get_user_count(self):
sdk = self.get_sdk()
online_count = sdk.get_user_count(is_online=True)
offline_count = sdk.get_user_count(is_online=False)
all_count = sdk.get_user_count()
self.assertIsInstance(online_count, int)
self.assertIsInstance(offline_count, int)
self.assertIsInstance(all_count, int)
self.assertEqual(online_count + offline_count, all_count)

def test_get_user(self):
sdk = self.get_sdk()
user = sdk.get_user("admin")
Expand Down

0 comments on commit f06ae3f

Please sign in to comment.