From f06ae3f686a133171c5bb9005b4e6c4c279f5664 Mon Sep 17 00:00:00 2001 From: Azat Abubakirov Date: Sat, 15 Apr 2023 20:36:28 +0300 Subject: [PATCH] feat: add get_user_count() to sdk (#42) --- README.md | 2 +- src/casdoor/async_main.py | 23 +++++++++++++++++++++++ src/casdoor/main.py | 23 +++++++++++++++++++++++ src/tests/test_async_oauth.py | 10 ++++++++++ src/tests/test_oauth.py | 10 ++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f154a3..0140d34 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/casdoor/async_main.py b/src/casdoor/async_main.py index 70e507b..578adbe 100644 --- a/src/casdoor/async_main.py +++ b/src/casdoor/async_main.py @@ -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 diff --git a/src/casdoor/main.py b/src/casdoor/main.py index 54403d4..c1a856a 100644 --- a/src/casdoor/main.py +++ b/src/casdoor/main.py @@ -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 diff --git a/src/tests/test_async_oauth.py b/src/tests/test_async_oauth.py index f0e2d5f..be32f12 100644 --- a/src/tests/test_async_oauth.py +++ b/src/tests/test_async_oauth.py @@ -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() diff --git a/src/tests/test_oauth.py b/src/tests/test_oauth.py index 05a08b7..274c03b 100644 --- a/src/tests/test_oauth.py +++ b/src/tests/test_oauth.py @@ -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")