From 34f134d58dcf73098d7841f3dd745b27295185f4 Mon Sep 17 00:00:00 2001 From: sahil839 Date: Sun, 11 Apr 2021 11:08:09 +0530 Subject: [PATCH] users: Add role field to user objects returned by format_user_row. This commit modifies the user objects returned by 'GET /users', 'GET /users/me', 'GET /users/{user_id}' and 'GET /users/{email}' endpoints to include role field. We also include role field in the page_params['realm_users'] dict and in the person object sent in (type="realm_user", op="add") event. --- frontend_tests/node_tests/lib/events.js | 1 + templates/zerver/api/changelog.md | 8 ++++ version.py | 2 +- zerver/lib/event_schema.py | 1 + zerver/lib/users.py | 1 + zerver/openapi/zulip.yaml | 47 ++++++++++++++++++++++++ zerver/tests/test_custom_profile_data.py | 3 ++ zerver/tests/test_home.py | 3 ++ zerver/tests/test_users.py | 13 ++++++- 9 files changed, 77 insertions(+), 2 deletions(-) diff --git a/frontend_tests/node_tests/lib/events.js b/frontend_tests/node_tests/lib/events.js index e09b5679df..cf9650e00b 100644 --- a/frontend_tests/node_tests/lib/events.js +++ b/frontend_tests/node_tests/lib/events.js @@ -489,6 +489,7 @@ exports.fixtures = { is_admin: false, is_active: true, is_owner: false, + role: 400, is_bot: false, is_guest: false, profile_data: {}, diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md index 26d490e0a9..2a003c7bed 100644 --- a/templates/zerver/api/changelog.md +++ b/templates/zerver/api/changelog.md @@ -12,6 +12,14 @@ below features are supported. **Feature level 59** +* [`GET /users`](/api/get-users), [`GET /users/{user_id}`](/api/get-user), + [`GET /users/{email}`](/api/get-user-by-email) and + [`GET /users/me`](/api/get-own-user): Added `role` field to returned + user objects. +* [`GET /events`](/api/get-events): Added `role` field to + user objects sent in `realm_user` events. +* [`POST /register`](/api/register-queue): Added `role` field + in the user objects returned in the `realm_users` field. * [`GET /events`](/api/get-events): Added new `zulip_version` and `zulip_feature_level` fields to the `restart` event. diff --git a/version.py b/version.py index ef3a9f759e..5589ccbe2e 100644 --- a/version.py +++ b/version.py @@ -30,7 +30,7 @@ DESKTOP_WARNING_VERSION = "5.2.0" # # Changes should be accompanied by documentation explaining what the # new level means in templates/zerver/api/changelog.md. -API_FEATURE_LEVEL = 58 +API_FEATURE_LEVEL = 59 # Bump the minor PROVISION_VERSION to indicate that folks should provision # only when going from an old version of the code to a newer version. Bump diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py index c83a2a91ee..13559d12b5 100644 --- a/zerver/lib/event_schema.py +++ b/zerver/lib/event_schema.py @@ -1013,6 +1013,7 @@ realm_user_type = DictType( ("is_owner", bool), ("is_bot", bool), ("is_guest", bool), + ("role", EnumType(UserProfile.ROLE_TYPES)), ("is_active", bool), ("profile_data", StringDictType(dict)), ("timezone", str), diff --git a/zerver/lib/users.py b/zerver/lib/users.py index f148da9354..a07cddedfe 100644 --- a/zerver/lib/users.py +++ b/zerver/lib/users.py @@ -374,6 +374,7 @@ def format_user_row( is_admin=is_admin, is_owner=is_owner, is_guest=is_guest, + role=row["role"], is_bot=is_bot, full_name=row["full_name"], timezone=canonicalize_timezone(row["timezone"]), diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 9e92fbc966..c95b6d6dfc 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -814,6 +814,7 @@ paths: "is_admin": false, "is_owner": false, "is_guest": false, + "role": 400, "is_bot": false, "full_name": "full name", "timezone": "", @@ -5036,6 +5037,7 @@ paths: "email": "AARON@zulip.com", "is_admin": false, "is_owner": false, + "role": 400, "avatar_url": "https://secure.gravatar.com/avatar/818c212b9f8830dfef491b3f7da99a14?d=identicon&version=1", "bot_type": null, "timezone": "", @@ -5079,6 +5081,7 @@ paths: "timezone": "", "is_admin": false, "is_owner": false, + "role": 400, "avatar_url": "https://secure.gravatar.com/avatar/6d8cad0fd00256e7b40691d27ddfd466?d=identicon&version=1", "is_active": true, "email": "hamlet@zulip.com", @@ -5093,6 +5096,7 @@ paths: "avatar_url": "https://secure.gravatar.com/avatar/7328586831cdbb1627649bd857b1ee8c?d=identicon&version=1", "is_admin": false, "is_owner": false, + "role": 400, "user_id": 23, "bot_type": 1, "timezone": "", @@ -5337,6 +5341,25 @@ paths: **Changes**: New in Zulip 3.0 (feature level 8). example: false + role: + type: integer + enum: + - 100 + - 200 + - 300 + - 400 + - 600 + description: | + [Organization-level role](/help/roles-and-permissions)) of the user. + Poosible values are: + + * Organization owner => 100 + * Organization administrator => 200 + * Organization moderator => 300 + * Member => 400 + * Guest => 600 + + **Changes**: New in Zulip 4.0 (feature level 59). is_guest: type: boolean description: | @@ -5405,6 +5428,7 @@ paths: "full_name": "Iago", "is_admin": true, "is_owner": false, + "role": 200, "is_guest": false, "is_bot": false, "is_active": true, @@ -6612,6 +6636,7 @@ paths: "timezone": "", "is_admin": false, "is_owner": false, + "role": 400, "avatar_url": "https://secure.gravatar.com/avatar/6d8cad0fd00256e7b40691d27ddfd466?d=identicon&version=1", "is_active": true, "email": "hamlet@zulip.com", @@ -6686,6 +6711,7 @@ paths: "timezone": "", "is_admin": false, "is_owner": false, + "role": 400, "avatar_url": "https://secure.gravatar.com/avatar/6d8cad0fd00256e7b40691d27ddfd466?d=identicon&version=1", "is_active": true, "email": "hamlet@zulip.com", @@ -8969,6 +8995,7 @@ paths: full_name: {} is_admin: {} is_owner: {} + role: {} bot_type: {} user_id: {} bot_owner_id: {} @@ -11635,6 +11662,7 @@ components: full_name: {} is_admin: {} is_owner: {} + role: {} bot_type: {} user_id: {} bot_owner_id: {} @@ -11692,6 +11720,25 @@ components: If true, is_admin will also be true. **Changes**: New in Zulip 3.0 (feature level 8). + role: + type: integer + enum: + - 100 + - 200 + - 300 + - 400 + - 600 + description: | + [Organization-level role](/help/roles-and-permissions)) of the user. + Poosible values are: + + * Organization owner => 100 + * Organization administrator => 200 + * Organization moderator => 300 + * Member => 400 + * Guest => 600 + + **Changes**: New in Zulip 4.0 (feature level 59). bot_type: type: integer nullable: true diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py index dfab822ac1..d3ace5bb42 100644 --- a/zerver/tests/test_custom_profile_data.py +++ b/zerver/tests/test_custom_profile_data.py @@ -782,6 +782,7 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase): "is_guest", "is_bot", "is_owner", + "role", "full_name", "timezone", "is_active", @@ -801,6 +802,7 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase): "is_guest", "is_bot", "is_owner", + "role", "full_name", "timezone", "is_active", @@ -832,6 +834,7 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase): "is_bot", "is_admin", "is_owner", + "role", "profile_data", "avatar_version", "timezone", diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index 2901cfb3da..d5b16d09ba 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -631,6 +631,7 @@ class HomeTest(ZulipTestCase): is_bot=True, is_admin=False, is_owner=False, + role=email_gateway_bot.role, is_cross_realm_bot=True, is_guest=False, ), @@ -645,6 +646,7 @@ class HomeTest(ZulipTestCase): is_bot=True, is_admin=False, is_owner=False, + role=notification_bot.role, is_cross_realm_bot=True, is_guest=False, ), @@ -659,6 +661,7 @@ class HomeTest(ZulipTestCase): is_bot=True, is_admin=False, is_owner=False, + role=welcome_bot.role, is_cross_realm_bot=True, is_guest=False, ), diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index a98b80f514..ee50398062 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -1709,7 +1709,7 @@ class GetProfileTest(ZulipTestCase): hamlet = self.example_user("hamlet") iago = self.example_user("iago") desdemona = self.example_user("desdemona") - + shiva = self.example_user("shiva") self.login("hamlet") result = orjson.loads(self.client_get("/json/users/me").content) self.assertEqual(result["email"], hamlet.email) @@ -1719,6 +1719,7 @@ class GetProfileTest(ZulipTestCase): self.assertFalse(result["is_admin"]) self.assertFalse(result["is_owner"]) self.assertFalse(result["is_guest"]) + self.assertEqual(result["role"], UserProfile.ROLE_MEMBER) self.assertFalse("delivery_email" in result) self.login("iago") result = orjson.loads(self.client_get("/json/users/me").content) @@ -1728,6 +1729,7 @@ class GetProfileTest(ZulipTestCase): self.assertTrue(result["is_admin"]) self.assertFalse(result["is_owner"]) self.assertFalse(result["is_guest"]) + self.assertEqual(result["role"], UserProfile.ROLE_REALM_ADMINISTRATOR) self.login("desdemona") result = orjson.loads(self.client_get("/json/users/me").content) self.assertEqual(result["email"], desdemona.email) @@ -1735,6 +1737,15 @@ class GetProfileTest(ZulipTestCase): self.assertTrue(result["is_admin"]) self.assertTrue(result["is_owner"]) self.assertFalse(result["is_guest"]) + self.assertEqual(result["role"], UserProfile.ROLE_REALM_OWNER) + self.login("shiva") + result = orjson.loads(self.client_get("/json/users/me").content) + self.assertEqual(result["email"], shiva.email) + self.assertFalse(result["is_bot"]) + self.assertFalse(result["is_admin"]) + self.assertFalse(result["is_owner"]) + self.assertFalse(result["is_guest"]) + self.assertEqual(result["role"], UserProfile.ROLE_MODERATOR) # Tests the GET ../users/{id} API endpoint. user = self.example_user("hamlet")