Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

api-docs: Document endpoints for allowed email address domains.

Jay Sonif5d15ae1f9e5
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index 5f271632d3f..b575039a8b2 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -3190,9 +3190,11 @@ user's profile.
`emails_restricted_to_domains`, `invite_required`, and
`waiting_period_threshold` settings can no longer be changed by
organization administrators who are not owners.
-* `PATCH /realm/domains`, `POST /realm/domains`, `DELETE
- /realm/domains`: Organization administrators who are not owners can
- no longer access these endpoints.
+* [`POST /realm/domains`](/api/add-realm-domain),
+ [`PATCH /realm/domains/{domain}`](/api/patch-realm-domain),
+ [`DELETE /realm/domains/{domain}`](/api/delete-realm-domain):
+ Organization administrators who are not owners can no longer access
+ these endpoints.
**Feature level 142**
@@ -3809,8 +3811,8 @@ No changes; feature level used for Zulip 4.0 release.
parameter `default_language`.
* `POST /users/me/tutorial_status`: Removed unnecessary JSON-encoding of string
parameter `status`.
-* `POST /realm/domains`: Removed unnecessary JSON-encoding of string
- parameter `domain`.
+* [`POST /realm/domains`](/api/add-realm-domain): Removed unnecessary
+ JSON-encoding of string parameter `domain`.
* `PATCH /default_stream_groups/{user_id}`: Removed unnecessary JSON-encoding of string
parameters `new_group_name` and `new_description`.
* `POST /users/me/hotspots`: Removed unnecessary JSON-encoding of string
diff --git a/api_docs/include/api-owner-only.md b/api_docs/include/api-owner-only.md
new file mode 100644
index 00000000000..b8a632c3f53
--- /dev/null
+++ b/api_docs/include/api-owner-only.md
@@ -0,0 +1,2 @@
+!!! warn ""
+ This endpoint is only available to organization owners.
diff --git a/api_docs/include/rest-endpoints.md b/api_docs/include/rest-endpoints.md
index 4e0c48beebd..b37c1ff5117 100644
--- a/api_docs/include/rest-endpoints.md
+++ b/api_docs/include/rest-endpoints.md
@@ -148,6 +148,10 @@
* [Reorder custom profile fields](/api/reorder-custom-profile-fields)
* [Create a custom profile field](/api/create-custom-profile-field)
* [Update realm-level defaults of user settings](/api/update-realm-user-settings-defaults)
+* [Get allowed domains](/api/get-realm-domains)
+* [Add an allowed domain](/api/add-realm-domain)
+* [Update an allowed domain](/api/patch-realm-domain)
+* [Remove an allowed domain](/api/delete-realm-domain)
* [Get all data exports](/api/get-realm-exports)
* [Create a data export](/api/export-realm)
* [Get data export consent state](/api/get-realm-export-consents)
diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py
index 86c137c8310..9f91ac0b321 100644
--- a/zerver/openapi/curl_param_value_generators.py
+++ b/zerver/openapi/curl_param_value_generators.py
@@ -16,6 +16,7 @@ from zerver.actions.channel_folders import check_add_channel_folder
from zerver.actions.create_user import do_create_user
from zerver.actions.presence import update_user_presence
from zerver.actions.reactions import do_add_reaction
+from zerver.actions.realm_domains import do_add_realm_domain
from zerver.actions.realm_linkifiers import do_add_linkifier
from zerver.actions.realm_playgrounds import check_add_realm_playground
from zerver.lib.events import do_events_register
@@ -470,3 +471,31 @@ def check_thumbnail_status_for_uploaded_file() -> dict[str, object]:
realm_id = upload_path_parts[1]
filename = upload_path_parts[2]
return {"realm_id_str": realm_id, "filename": filename}
+
+
+@openapi_param_value_generator(["/realm/domains:post"])
+def add_realm_domain_owner_auth() -> dict[str, object]:
+ # This endpoint requires organization owner permissions.
+ owner = helpers.example_user("desdemona")
+ AUTHENTICATION_LINE[0] = f"{owner.email}:{owner.api_key}"
+ return {}
+
+
+@openapi_param_value_generator(["/realm/domains/{domain}:patch"])
+def patch_realm_domain_owner_auth() -> dict[str, object]:
+ # This endpoint requires organization owner permissions.
+ owner = helpers.example_user("desdemona")
+ AUTHENTICATION_LINE[0] = f"{owner.email}:{owner.api_key}"
+
+ do_add_realm_domain(owner.realm, "patch-domain-example.com", False, acting_user=None)
+ return {"domain": "patch-domain-example.com"}
+
+
+@openapi_param_value_generator(["/realm/domains/{domain}:delete"])
+def delete_realm_domain_owner_auth() -> dict[str, object]:
+ # This endpoint requires organization owner permissions.
+ owner = helpers.example_user("desdemona")
+ AUTHENTICATION_LINE[0] = f"{owner.email}:{owner.api_key}"
+
+ do_add_realm_domain(owner.realm, "delete-domain-example.com", False, acting_user=None)
+ return {"domain": "delete-domain-example.com"}
diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.py
index 1946413bc9c..2a4f48104e4 100644
--- a/zerver/openapi/markdown_extension.py
+++ b/zerver/openapi/markdown_extension.py
@@ -27,6 +27,7 @@ from zerver.openapi.openapi import (
Parameter,
check_additional_imports,
check_requires_administrator,
+ check_requires_owner,
generate_openapi_fixture,
get_curl_include_exclude,
get_openapi_description,
@@ -521,6 +522,7 @@ class APIHeaderPreprocessor(BasePreprocessor):
return [
*("# " + line for line in raw_title.splitlines()),
*(["{!api-admin-only.md!}"] if check_requires_administrator(path, method) else []),
+ *(["{!api-owner-only.md!}"] if check_requires_owner(path, method) else []),
"",
f"`{method.upper()} {self.api_url}/v1{path}`",
"",
diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.py
index 0ecd11ff414..f2234d908ea 100644
--- a/zerver/openapi/openapi.py
+++ b/zerver/openapi/openapi.py
@@ -239,6 +239,11 @@ def check_requires_administrator(endpoint: str, method: str) -> bool:
)
+def check_requires_owner(endpoint: str, method: str) -> bool:
+ """Fetch if the endpoint requires owner config."""
+ return openapi_spec.openapi()["paths"][endpoint][method.lower()].get("x-requires-owner", False)
+
+
def check_additional_imports(endpoint: str, method: str) -> list[str] | None:
"""Fetch the additional imports required for an endpoint."""
return openapi_spec.openapi()["paths"][endpoint][method.lower()].get(
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index efc68ea3fdc..b4ac074b21a 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -4401,9 +4401,9 @@ paths:
- type: object
additionalProperties: false
description: |
- Event sent to all users in a Zulip organization when the set of
- [allowed domains for new users](/help/restrict-account-creation#configuring-email-domain-restrictions)
- has changed.
+ Event sent to all users in a Zulip organization when a new domain has been
+ added to the set of [allowed domains for account creation and email
+ changes](/help/restrict-account-creation#configuring-email-domain-restrictions).
properties:
id:
$ref: "#/components/schemas/EventIdSchema"
@@ -4417,7 +4417,10 @@ paths:
enum:
- add
realm_domain:
- $ref: "#/components/schemas/RealmDomain"
+ allOf:
+ - $ref: "#/components/schemas/RealmDomain"
+ - description: |
+ Object containing details of the newly added domain.
example:
{
"type": "realm_domains",
@@ -4433,7 +4436,8 @@ paths:
additionalProperties: false
description: |
Event sent to all users in a Zulip organization when the set of
- [allowed domains for new users](/help/restrict-account-creation#configuring-email-domain-restrictions)
+ [allowed domains for account creation and email
+ changes](/help/restrict-account-creation#configuring-email-domain-restrictions)
has changed.
properties:
id:
@@ -4448,19 +4452,10 @@ paths:
enum:
- change
realm_domain:
- type: object
- additionalProperties: false
- description: |
- Object containing details of the edited domain.
- properties:
- domain:
- type: string
- description: |
- The domain whose settings have changed.
- allow_subdomains:
- type: boolean
- description: |
- Whether subdomains are allowed for this domain.
+ allOf:
+ - $ref: "#/components/schemas/RealmDomain"
+ - description: |
+ Object containing details of the edited domain.
example:
{
"type": "realm_domains",
@@ -4475,9 +4470,9 @@ paths:
- type: object
additionalProperties: false
description: |
- Event sent to all users in a Zulip organization when the set of
- [allowed domains for new users](/help/restrict-account-creation#configuring-email-domain-restrictions)
- has changed.
+ Event sent to all users in a Zulip organization when a domain has been
+ removed from the set of [allowed domains for account creation and email
+ changes](/help/restrict-account-creation#configuring-email-domain-restrictions).
properties:
id:
$ref: "#/components/schemas/EventIdSchema"
@@ -14295,6 +14290,174 @@ paths:
"result": "success",
"server_timestamp": 1656958539.6287155,
}
+ /realm/domains:
+ get:
+ operationId: get-realm-domains
+ summary: Get allowed domains
+ tags: ["server_and_organizations"]
+ description: |
+ Get the set of allowed domains configured in the organization for user
+ account email addresses.
+
+ As each Zulip user account is associated with an email address, organization
+ owners can [restrict new account creation (and email
+ changes)](/help/restrict-account-creation) to email addresses with these
+ domains.
+ responses:
+ "200":
+ description: Success.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonSuccessBase"
+ - additionalProperties: false
+ properties:
+ result: {}
+ msg: {}
+ ignored_parameters_unsupported: {}
+ domains:
+ type: array
+ description: |
+ A list of objects containing details of each domain allowed
+ for user account email addresses.
+ items:
+ $ref: "#/components/schemas/RealmDomain"
+ example:
+ {
+ "result": "success",
+ "msg": "",
+ "domains":
+ [
+ {
+ "domain": "example.com",
+ "allow_subdomains": false,
+ },
+ ],
+ }
+ post:
+ operationId: add-realm-domain
+ summary: Add an allowed domain
+ tags: ["server_and_organizations"]
+ description: |
+ Add a domain to the set of allowed domains configured in the organization
+ for [user account email addresses](/help/restrict-account-creation).
+
+ **Changes**: Prior to Zulip 6.0 (feature level 143), organization
+ administrators who were not owners could access this endpoint.
+ x-requires-owner: true
+ requestBody:
+ required: true
+ content:
+ application/x-www-form-urlencoded:
+ schema:
+ type: object
+ properties:
+ domain:
+ description: |
+ The new domain.
+
+ **Changes**: In Zulip 4.0 (feature level 63), the unnecessary
+ JSON-encoding of this parameter was removed.
+ type: string
+ example: "example.com"
+ allow_subdomains:
+ description: |
+ Whether subdomains are allowed for this domain.
+ type: boolean
+ example: false
+ required:
+ - domain
+ - allow_subdomains
+ responses:
+ "200":
+ description: Success.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonSuccessBase"
+ - additionalProperties: false
+ properties:
+ result: {}
+ msg: {}
+ ignored_parameters_unsupported: {}
+ new_domain:
+ type: array
+ description: |
+ An array containing the ID of the newly added domain
+ and the submitted domain name string.
+ items:
+ oneOf:
+ - type: integer
+ - type: string
+ example:
+ {
+ "result": "success",
+ "msg": "",
+ "new_domain": [1, "example.com"],
+ }
+ /realm/domains/{domain}:
+ patch:
+ operationId: patch-realm-domain
+ summary: Update an allowed domain
+ tags: ["server_and_organizations"]
+ description: |
+ Update whether subdomains are allowed in [user account email
+ addresses](/help/restrict-account-creation) for the specified domain.
+
+ **Changes**: Prior to Zulip 6.0 (feature level 143), organization
+ administrators who were not owners could access this endpoint.
+ x-requires-owner: true
+ parameters:
+ - name: domain
+ in: path
+ description: |
+ The domain to update.
+ schema:
+ type: string
+ example: "example.com"
+ required: true
+ requestBody:
+ required: true
+ content:
+ application/x-www-form-urlencoded:
+ schema:
+ type: object
+ properties:
+ allow_subdomains:
+ description: |
+ Whether subdomains are allowed for this domain.
+ type: boolean
+ example: true
+ required:
+ - allow_subdomains
+ responses:
+ "200":
+ $ref: "#/components/responses/SimpleSuccess"
+ delete:
+ operationId: delete-realm-domain
+ summary: Remove an allowed domain
+ tags: ["server_and_organizations"]
+ description: |
+ Remove the specified domain from the set of allowed domains configured in
+ the organization for [user account email addresses](/help/restrict-account-creation).
+
+ **Changes**: Prior to Zulip 6.0 (feature level 143), organization
+ administrators who were not owners could access this endpoint.
+ x-requires-owner: true
+ parameters:
+ - name: domain
+ in: path
+ description: |
+ The domain to remove.
+ schema:
+ type: string
+ example: "example.com"
+ required: true
+ responses:
+ "200":
+ $ref: "#/components/responses/SimpleSuccess"
/realm/profile_fields:
get:
operationId: get-custom-profile-fields
@@ -17903,8 +18066,8 @@ paths:
description: |
Present if `realm_domains` is present in `fetch_event_types`.
- An array of dictionaries where each dictionary describes a domain within
- which users can join the organization without and invitation.
+ An array of dictionaries where each dictionary describes an allowed domain
+ for [user account email addresses](/help/restrict-account-creation).
items:
$ref: "#/components/schemas/RealmDomain"
realm_emoji:
@@ -27009,17 +27172,18 @@ components:
RealmDomain:
type: object
additionalProperties: false
- description: |
- Object containing details of the newly added domain.
properties:
domain:
type: string
description: |
- The new allowed domain.
+ The allowed domain.
allow_subdomains:
type: boolean
description: |
Whether subdomains are allowed for this domain.
+ required:
+ - domain
+ - allow_subdomains
RealmPlayground:
type: object
additionalProperties: false
diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py
index c50578283ce..87f99e3913b 100644
--- a/zerver/tests/test_openapi.py
+++ b/zerver/tests/test_openapi.py
@@ -232,8 +232,6 @@ class OpenAPIArgumentsTest(ZulipTestCase):
"/zcommand",
#### These "organization settings" endpoint have modest value to document:
"/realm",
- "/realm/domains",
- "/realm/domains/{domain}",
"/bots",
"/bots/{bot_id}",
#### These "organization settings" endpoints have low value to document: