users: Add support for changing user role to moderator in api.

This commit adds backend support for changing a user role to
moderator and also to change role from moderator to any other
role.
This commit is contained in:
sahil839 2021-04-19 22:11:20 +05:30 committed by Tim Abbott
parent 34f134d58d
commit dc771f3a14
4 changed files with 43 additions and 1 deletions

View File

@ -10,6 +10,13 @@ below features are supported.
## Changes in Zulip 4.0 ## Changes in Zulip 4.0
**Feature level 60**
* [`PATCH /users/{user_id}`](/api/update-user): Added support for
changing a user's organization-level role to moderator.
* API endpoints that return `role` values can now return `300`, the
encoding of the moderator role.
**Feature level 59** **Feature level 59**
* [`GET /users`](/api/get-users), [`GET /users/{user_id}`](/api/get-user), * [`GET /users`](/api/get-users), [`GET /users/{user_id}`](/api/get-user),

View File

@ -1186,6 +1186,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
ROLE_TYPES = [ ROLE_TYPES = [
ROLE_REALM_OWNER, ROLE_REALM_OWNER,
ROLE_REALM_ADMINISTRATOR, ROLE_REALM_ADMINISTRATOR,
ROLE_MODERATOR,
ROLE_MEMBER, ROLE_MEMBER,
ROLE_GUEST, ROLE_GUEST,
] ]

View File

@ -390,6 +390,7 @@ paths:
enum: enum:
- 100 - 100
- 200 - 200
- 300
- 400 - 400
- 600 - 600
- type: object - type: object
@ -6750,6 +6751,7 @@ paths:
* Organization owner: 100 * Organization owner: 100
* Organization administrator: 200 * Organization administrator: 200
* Organization moderator: 300
* Member: 400 * Member: 400
* Guest: 600 * Guest: 600
@ -6758,7 +6760,8 @@ paths:
The owner role cannot be removed from the only organization owner. The owner role cannot be removed from the only organization owner.
**Changes**: New in Zulip 3.0 (feature level 8), replacing the previous **Changes**: New in Zulip 3.0 (feature level 8), replacing the previous
pair of `is_admin` and `is_guest` boolean parameters. pair of `is_admin` and `is_guest` boolean parameters. Organization moderator
role added in Zulip 4.0 (feature level 60).
schema: schema:
type: integer type: integer
example: 400 example: 400

View File

@ -447,17 +447,27 @@ class PermissionTest(ZulipTestCase):
user_profile.is_realm_admin user_profile.is_realm_admin
and not user_profile.is_guest and not user_profile.is_guest
and not user_profile.is_realm_owner and not user_profile.is_realm_owner
and not user_profile.is_moderator
) )
elif role == UserProfile.ROLE_REALM_OWNER: elif role == UserProfile.ROLE_REALM_OWNER:
return ( return (
user_profile.is_realm_owner user_profile.is_realm_owner
and user_profile.is_realm_admin and user_profile.is_realm_admin
and not user_profile.is_moderator
and not user_profile.is_guest
)
elif role == UserProfile.ROLE_MODERATOR:
return (
user_profile.is_moderator
and not user_profile.is_realm_owner
and not user_profile.is_realm_admin
and not user_profile.is_guest and not user_profile.is_guest
) )
if role == UserProfile.ROLE_MEMBER: if role == UserProfile.ROLE_MEMBER:
return ( return (
not user_profile.is_guest not user_profile.is_guest
and not user_profile.is_moderator
and not user_profile.is_realm_admin and not user_profile.is_realm_admin
and not user_profile.is_realm_owner and not user_profile.is_realm_owner
) )
@ -465,6 +475,7 @@ class PermissionTest(ZulipTestCase):
assert role == UserProfile.ROLE_GUEST assert role == UserProfile.ROLE_GUEST
return ( return (
user_profile.is_guest user_profile.is_guest
and not user_profile.is_moderator
and not user_profile.is_realm_admin and not user_profile.is_realm_admin
and not user_profile.is_realm_owner and not user_profile.is_realm_owner
) )
@ -524,6 +535,26 @@ class PermissionTest(ZulipTestCase):
do_change_user_role(iago, UserProfile.ROLE_REALM_OWNER, acting_user=None) do_change_user_role(iago, UserProfile.ROLE_REALM_OWNER, acting_user=None)
self.check_user_role_change("iago", UserProfile.ROLE_REALM_ADMINISTRATOR) self.check_user_role_change("iago", UserProfile.ROLE_REALM_ADMINISTRATOR)
def test_change_owner_to_moderator(self) -> None:
iago = self.example_user("iago")
do_change_user_role(iago, UserProfile.ROLE_REALM_OWNER, acting_user=None)
self.check_user_role_change("iago", UserProfile.ROLE_MODERATOR)
def test_change_moderator_to_owner(self) -> None:
self.check_user_role_change("shiva", UserProfile.ROLE_REALM_OWNER)
def test_change_admin_to_moderator(self) -> None:
self.check_user_role_change("iago", UserProfile.ROLE_MODERATOR)
def test_change_moderator_to_admin(self) -> None:
self.check_user_role_change("shiva", UserProfile.ROLE_REALM_ADMINISTRATOR)
def test_change_guest_to_moderator(self) -> None:
self.check_user_role_change("polonius", UserProfile.ROLE_MODERATOR)
def test_change_moderator_to_guest(self) -> None:
self.check_user_role_change("shiva", UserProfile.ROLE_GUEST)
def test_admin_user_can_change_profile_data(self) -> None: def test_admin_user_can_change_profile_data(self) -> None:
realm = get_realm("zulip") realm = get_realm("zulip")
self.login("iago") self.login("iago")