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.

realm: Check plan when updating workplace_users_group.

Sahil Batra8b950dfd2e65
diff --git a/web/src/admin.ts b/web/src/admin.ts
index 1d9b4b4d8c2..6d0ecc2a51f 100644
--- a/web/src/admin.ts
+++ b/web/src/admin.ts
@@ -315,6 +315,7 @@ export function build_page(): void {
),
default_avatar_source_values: settings_config.default_avatar_source_values,
realm_enable_two_tier_billing: settings_data.two_tier_billing_enabled(),
+ show_two_tier_billing_settings: page_params.non_workplace_pricing_eligible,
};
const rendered_admin_tab = render_admin_tab(options);
diff --git a/web/src/base_page_params.ts b/web/src/base_page_params.ts
index 3d065c02ad0..8f9ab4bd599 100644
--- a/web/src/base_page_params.ts
+++ b/web/src/base_page_params.ts
@@ -57,6 +57,8 @@ const home_params_schema = z.object({
two_fa_enabled: z.boolean(),
two_fa_enabled_user: z.boolean(),
warn_no_email: z.boolean(),
+ non_workplace_pricing_eligible: z.boolean(),
+ is_cloud_realm_with_discounted_plan: z.boolean(),
});
// Sync this with analytics.views.stats.render_stats.
diff --git a/web/src/settings_org.ts b/web/src/settings_org.ts
index d83d1644298..211fac44002 100644
--- a/web/src/settings_org.ts
+++ b/web/src/settings_org.ts
@@ -23,6 +23,7 @@ import {$t, $t_html} from "./i18n.ts";
import * as information_density from "./information_density.ts";
import * as keydown_util from "./keydown_util.ts";
import * as loading from "./loading.ts";
+import {page_params} from "./page_params.ts";
import * as people from "./people.ts";
import * as pygments_data from "./pygments_data.ts";
import * as realm_icon from "./realm_icon.ts";
@@ -465,6 +466,27 @@ function set_welcome_message_custom_text_visibility(): void {
}
export let set_two_tier_billing_settings_visibility = (): void => {
+ if (!page_params.non_workplace_pricing_eligible) {
+ return;
+ }
+
+ if (page_params.is_cloud_realm_with_discounted_plan) {
+ $("input#id_realm_enable_two_tier_billing").prop("checked", false).prop("disabled", true);
+ $("input#id_realm_enable_two_tier_billing")
+ .closest(".input-group")
+ .addClass("control-label-disabled");
+ settings_components.change_element_block_display_property(
+ "id_realm_workplace_users_group",
+ false,
+ );
+ if (current_user.is_owner) {
+ $("input#id_realm_enable_two_tier_billing")
+ .closest(".input-group")
+ .addClass("two-tier-billing-disabled");
+ }
+ return;
+ }
+
const two_tier_billing_enabled = settings_data.two_tier_billing_enabled();
$("input#id_realm_enable_two_tier_billing").prop("checked", two_tier_billing_enabled);
settings_components.change_element_block_display_property(
@@ -1589,6 +1611,13 @@ export let initialize_group_setting_widgets = (): void => {
continue;
}
+ if (
+ setting_name === "workplace_users_group" &&
+ !page_params.non_workplace_pricing_eligible
+ ) {
+ continue;
+ }
+
const opts: {
$pill_container: JQuery;
setting_name: RealmGroupSettingNameSupportingAnonymousGroups;
diff --git a/web/src/tippyjs.ts b/web/src/tippyjs.ts
index f40bcb3de88..3815d986d38 100644
--- a/web/src/tippyjs.ts
+++ b/web/src/tippyjs.ts
@@ -1077,6 +1077,22 @@ export function initialize(): void {
},
});
+ tippy.delegate("body", {
+ target: ".two-tier-billing-disabled",
+ delay: LONG_HOVER_DELAY,
+ onShow(instance) {
+ const content = $t(
+ {defaultMessage: "Contact {sales_email} to enable"},
+ {sales_email: "[email protected]"},
+ );
+ instance.setContent(content);
+ },
+ appendTo: () => document.body,
+ onHidden(instance) {
+ instance.destroy();
+ },
+ });
+
// Show expand / collapse tooltip in inbox view on hover over inbox header.
let collapse_or_expand_tooltip: tippy.Instance | undefined;
let collapse_or_expand_timeout: ReturnType<typeof setTimeout> | undefined;
diff --git a/web/templates/settings/organization_settings_admin.hbs b/web/templates/settings/organization_settings_admin.hbs
index e77b97bdb82..35b661ee2e4 100644
--- a/web/templates/settings/organization_settings_admin.hbs
+++ b/web/templates/settings/organization_settings_admin.hbs
@@ -7,6 +7,7 @@
{{> settings_save_discard_widget section_name="onboarding" show_save_discard_buttons=true }}
</div>
<div class="organization-settings-parent">
+ {{#if show_two_tier_billing_settings}}
{{> settings_checkbox
setting_name="realm_enable_two_tier_billing"
prefix="id_"
@@ -17,6 +18,7 @@
{{> group_setting_value_pill_input
setting_name="realm_workplace_users_group"
label=group_setting_labels.workplace_users_group}}
+ {{/if}}
{{#if is_plan_self_hosted}}
{{> group_setting_value_pill_input
diff --git a/zerver/lib/home.py b/zerver/lib/home.py
index ee10d4bbd7c..9716edd8d34 100644
--- a/zerver/lib/home.py
+++ b/zerver/lib/home.py
@@ -18,6 +18,10 @@ from zerver.lib.i18n import (
from zerver.lib.narrow_helpers import NeverNegatedNarrowTerm
from zerver.lib.realm_description import get_realm_rendered_description
from zerver.lib.request import RequestNotes
+from zerver.lib.workplace_users import (
+ realm_eligible_for_non_workplace_pricing,
+ realm_on_discounted_cloud_plan,
+)
from zerver.models import Message, Realm, Stream, UserProfile
from zerver.views.message_flags import get_latest_update_message_flag_activity
@@ -175,6 +179,8 @@ def build_page_params_for_home_page_load(
# events support for spectators is not implemented yet.
no_event_queue=user_profile is None,
show_try_zulip_modal=show_try_zulip_modal,
+ non_workplace_pricing_eligible=realm_eligible_for_non_workplace_pricing(realm),
+ is_cloud_realm_with_discounted_plan=realm_on_discounted_cloud_plan(realm),
)
page_params["state_data"] = state_data
diff --git a/zerver/lib/workplace_users.py b/zerver/lib/workplace_users.py
index effc8e90b98..38b49f49061 100644
--- a/zerver/lib/workplace_users.py
+++ b/zerver/lib/workplace_users.py
@@ -48,3 +48,49 @@ def validate_workplace_users_group(
"'workplace_users_group' must be a group whose membership can only be managed by organization administrators."
)
)
+
+
+def realm_eligible_for_non_workplace_pricing(realm: Realm) -> bool:
+ if realm.plan_type == Realm.PLAN_TYPE_SELF_HOSTED:
+ # Non-workplace plan pricing is not yet implemented
+ # for self-hosted plans.
+ return False
+
+ if realm.plan_type == Realm.PLAN_TYPE_STANDARD_FREE:
+ # Fully sponsored plans are completely free, so it
+ # would be distracting to offer menu options for
+ # discounted pricing.
+ return False
+
+ if realm.plan_type == Realm.PLAN_TYPE_LIMITED:
+ # We want to allow organizations to enable discounted
+ # pricing for non workplace users before they upgrade.
+ return True
+
+ from corporate.models.plans import get_current_plan_by_realm
+
+ customer_plan = get_current_plan_by_realm(realm)
+ assert customer_plan is not None
+ if customer_plan.fixed_price is not None:
+ # Discounted pricing for non-workplace users is
+ # currently incompatible with a fixed-price plan.
+ return False
+
+ return True
+
+
+def realm_on_discounted_cloud_plan(realm: Realm) -> bool:
+ if realm.plan_type == Realm.PLAN_TYPE_SELF_HOSTED:
+ return False
+
+ if realm.plan_type in {Realm.PLAN_TYPE_LIMITED, Realm.PLAN_TYPE_STANDARD_FREE}:
+ # Realm is on free plan or is fully sponsored.
+ return False
+
+ from corporate.models.customers import get_customer_by_realm
+
+ # We can assume that an active plan will be present
+ # if realm is not on free or fully sponsored plan.
+ customer = get_customer_by_realm(realm)
+ assert customer is not None
+ return customer.monthly_discounted_price > 0 or customer.annual_discounted_price > 0
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 4c189263dc4..042d62b7abc 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -49,12 +49,14 @@ class HomeTest(ZulipTestCase):
"embedded_bots_enabled",
"furthest_read_time",
"insecure_desktop_app",
+ "is_cloud_realm_with_discounted_plan",
"is_spectator",
"language_list",
"login_page",
"narrow",
"narrow_stream",
"no_event_queue",
+ "non_workplace_pricing_eligible",
"page_type",
"presence_history_limit_days_for_web_app",
"promote_sponsoring_zulip",
@@ -381,11 +383,13 @@ class HomeTest(ZulipTestCase):
"embedded_bots_enabled",
"furthest_read_time",
"insecure_desktop_app",
+ "is_cloud_realm_with_discounted_plan",
"is_spectator",
"language_cookie_name",
"language_list",
"login_page",
"no_event_queue",
+ "non_workplace_pricing_eligible",
"page_type",
"presence_history_limit_days_for_web_app",
"promote_sponsoring_zulip",
@@ -483,6 +487,15 @@ class HomeTest(ZulipTestCase):
# Changing the plan_type to Standard grants access to AzureAD, but not SAML:
do_change_realm_plan_type(realm, Realm.PLAN_TYPE_STANDARD, acting_user=None)
+ customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_id")
+ CustomerPlan.objects.create(
+ customer=customer,
+ billing_cycle_anchor=timezone_now(),
+ billing_schedule=CustomerPlan.BILLING_SCHEDULE_ANNUAL,
+ next_invoice_date=timezone_now(),
+ tier=CustomerPlan.TIER_CLOUD_STANDARD,
+ status=CustomerPlan.ACTIVE,
+ )
with self.settings(
AUTHENTICATION_BACKENDS=(
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index f1d4d19c2f9..3c0839fde75 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -19,6 +19,8 @@ from django.utils.timezone import now as timezone_now
from typing_extensions import override
from confirmation.models import Confirmation, create_confirmation_link
+from corporate.models.customers import Customer
+from corporate.models.plans import CustomerPlan
from zerver.actions.create_realm import do_change_realm_subdomain, do_create_realm
from zerver.actions.create_user import do_create_user
from zerver.actions.message_send import (
@@ -2413,6 +2415,9 @@ class RealmAPITest(ZulipTestCase):
def do_test_realm_permission_group_setting_update_api(self, setting_name: str) -> None:
realm = get_realm("zulip")
+ if setting_name == "workplace_users_group":
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
+
all_system_user_groups = NamedUserGroup.objects.filter(
realm_for_sharding=realm,
is_system_group=True,
@@ -2532,6 +2537,9 @@ class RealmAPITest(ZulipTestCase):
name=SystemGroups.MODERATORS, realm_for_sharding=realm, is_system_group=True
)
+ if setting_name == "workplace_users_group":
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
+
result = self.client_patch(
"/json/realm", {setting_name: orjson.dumps({"new": moderators_group.id}).decode()}
)
@@ -3083,6 +3091,8 @@ class RealmAPITest(ZulipTestCase):
iago = self.example_user("iago")
self.login("iago")
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
+
members_group = NamedUserGroup.objects.get(
name=SystemGroups.MEMBERS, realm_for_sharding=realm
)
@@ -3286,6 +3296,95 @@ class RealmAPITest(ZulipTestCase):
realm.refresh_from_db()
self.assertEqual(realm.workplace_users_group_id, hamletcharacters_group.id)
+ def test_updating_workplace_users_group_based_on_realm_plan(self) -> None:
+ realm = get_realm("zulip")
+ self.login("desdemona")
+ moderators_group = NamedUserGroup.objects.get(
+ name=SystemGroups.MODERATORS, realm_for_sharding=realm
+ )
+ members_group = NamedUserGroup.objects.get(
+ name=SystemGroups.MEMBERS, realm_for_sharding=realm
+ )
+
+ # Self hosted organizations cannot enable discounted billing for
+ # non-workplace users.
+ self.assertEqual(realm.plan_type, Realm.PLAN_TYPE_SELF_HOSTED)
+ params = {
+ "workplace_users_group": orjson.dumps({"new": moderators_group.id}).decode(),
+ }
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_error(
+ result, "Organization is not eligible for discounted pricing for non workplace users."
+ )
+
+ # Organizations with 100% sponsorship cannot enable discounted billing for
+ # non-workplace users.
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_STANDARD_FREE, acting_user=None)
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_error(
+ result, "Organization is not eligible for discounted pricing for non workplace users."
+ )
+
+ # Organizations on free plan can enable discounted billing for
+ # non-workplace users.
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_success(result)
+ realm.refresh_from_db()
+ self.assertEqual(realm.workplace_users_group_id, moderators_group.id)
+
+ params = {
+ "workplace_users_group": orjson.dumps({"new": members_group.id}).decode(),
+ }
+
+ # Organizations with a fixed-price plan cannot enable discounted
+ # billing for non-workplace users.
+ do_change_realm_plan_type(realm, Realm.PLAN_TYPE_STANDARD, acting_user=None)
+ customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_id")
+ plan = CustomerPlan.objects.create(
+ customer=customer,
+ billing_cycle_anchor=timezone_now(),
+ billing_schedule=CustomerPlan.BILLING_SCHEDULE_ANNUAL,
+ tier=CustomerPlan.TIER_CLOUD_STANDARD,
+ status=CustomerPlan.ACTIVE,
+ fixed_price=1000,
+ )
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_error(
+ result, "Organization is not eligible for discounted pricing for non workplace users."
+ )
+
+ # Organizations with a discounted price cannot enable discounted billing for
+ # non-workplace users.
+ plan.fixed_price = None
+ plan.price_per_license = 8000
+ plan.save(update_fields=["fixed_price", "price_per_license"])
+ customer.monthly_discounted_price = 7000
+ customer.save(update_fields=["monthly_discounted_price"])
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_error(
+ result,
+ "Discounted pricing for workplace users cannot be enabled with current discounted plan.",
+ )
+
+ customer.monthly_discounted_price = 0
+ customer.annual_discounted_price = 70000
+ customer.save(update_fields=["monthly_discounted_price", "annual_discounted_price"])
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_error(
+ result,
+ "Discounted pricing for workplace users cannot be enabled with current discounted plan.",
+ )
+
+ # Organizations on paid plans with no discount and no fixed price
+ # can enable discounted billing for non-workplace users.
+ customer.annual_discounted_price = 0
+ customer.save(update_fields=["annual_discounted_price"])
+ result = self.client_patch("/json/realm", params)
+ self.assert_json_success(result)
+ realm.refresh_from_db()
+ self.assertEqual(realm.workplace_users_group_id, members_group.id)
+
def test_changing_can_manage_all_groups(self) -> None:
realm = get_realm("zulip")
hamlet = self.example_user("hamlet")
diff --git a/zerver/views/realm.py b/zerver/views/realm.py
index 021f6cb93a1..71893905f09 100644
--- a/zerver/views/realm.py
+++ b/zerver/views/realm.py
@@ -57,7 +57,11 @@ from zerver.lib.user_groups import (
validate_group_setting_value_change,
)
from zerver.lib.validator import check_capped_url, check_string
-from zerver.lib.workplace_users import validate_workplace_users_group
+from zerver.lib.workplace_users import (
+ realm_eligible_for_non_workplace_pricing,
+ realm_on_discounted_cloud_plan,
+ validate_workplace_users_group,
+)
from zerver.models import Realm, RealmReactivationStatus, RealmUserDefault, UserProfile
from zerver.models.groups import SystemGroups
from zerver.models.realms import (
@@ -388,6 +392,19 @@ def update_realm(
data["jitsi_server_url"] = jitsi_server_url
+ if workplace_users_group is not None:
+ if not realm_eligible_for_non_workplace_pricing(realm):
+ raise JsonableError(
+ _("Organization is not eligible for discounted pricing for non workplace users.")
+ )
+
+ if realm_on_discounted_cloud_plan(realm):
+ raise JsonableError(
+ _(
+ "Discounted pricing for workplace users cannot be enabled with current discounted plan."
+ )
+ )
+
# The user of `locals()` here is a bit of a code smell, but it's
# restricted to the elements present in realm.property_types.
#