settings_data: Add 'user_can_invite_others_to_realm' helper.

This commit adds 'user_can_invite_others_to_realm' function
in settings_data.js which replaces all the instances of
'page_params.can_invite_others_to_realm'.

This change makes it possible to remove the complex logic of
updating 'page_params.can_invite_others_to_realm' on
'realm_update' event as we are using above added helper instead
of using page_params object and thus we always use the updated
value to check whether user can invite others or not.

The 'user_has_permission' helper will be used by functions added
for create_stream_policy and invite_to_stream_policy in further
commits and will help in avoiding code duplication.
This commit is contained in:
sahil839
2021-04-29 00:24:03 +05:30
committed by Tim Abbott
parent 21dd589f32
commit 38a4105744
4 changed files with 65 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const {page_params} = require("../zjsunit/zpage_params");
const people = zrequire("people");
const settings_data = zrequire("settings_data");
const settings_config = zrequire("settings_config");
@@ -19,7 +20,10 @@ const settings_config = zrequire("settings_config");
const isaac = {
email: "isaac@example.com",
delivery_email: "isaac-delivery@example.com",
user_id: 30,
full_name: "Isaac",
};
people.add_active_user(isaac);
run_test("email_for_user_settings", () => {
const email = settings_data.email_for_user_settings;
@@ -102,3 +106,33 @@ run_test("user_can_change_logo", () => {
page_params.zulip_plan_is_not_limited = true;
assert.equal(can_change_logo(), false);
});
run_test("user_can_invite_others_to_realm", () => {
const can_invite_others_to_realm = settings_data.user_can_invite_others_to_realm;
page_params.is_admin = true;
page_params.realm_invite_to_realm_policy =
settings_config.common_policy_values.by_admins_only.code;
assert.equal(can_invite_others_to_realm(), true);
page_params.is_admin = false;
assert.equal(can_invite_others_to_realm(), false);
page_params.is_guest = true;
page_params.realm_invite_to_realm_policy = settings_config.common_policy_values.by_members.code;
assert.equal(can_invite_others_to_realm(), false);
page_params.is_guest = false;
assert.equal(can_invite_others_to_realm(), true);
page_params.realm_invite_to_realm_policy =
settings_config.common_policy_values.by_full_members.code;
page_params.user_id = 30;
people.add_active_user(isaac);
isaac.date_joined = new Date(Date.now());
page_params.realm_waiting_period_threshold = 10;
assert.equal(can_invite_others_to_realm(), false);
isaac.date_joined = new Date(Date.now() - 20 * 86400000);
assert.equal(can_invite_others_to_realm(), true);
});

View File

@@ -124,7 +124,7 @@ export function build_page() {
msg_delete_limit_dropdown_values: settings_config.msg_delete_limit_dropdown_values,
bot_creation_policy_values: settings_bots.bot_creation_policy_values,
email_address_visibility_values: settings_config.email_address_visibility_values,
can_invite_others_to_realm: page_params.can_invite_others_to_realm,
can_invite_others_to_realm: settings_data.user_can_invite_others_to_realm(),
...settings_org.get_organization_settings_options(),
};

View File

@@ -231,11 +231,6 @@ export function dispatch_normal_event(event) {
page_params.can_invite_to_stream =
page_params.is_admin ||
page_params.realm_invite_to_stream_policy === 1;
} else if (event.property === "invite_to_realm_policy") {
// TODO: Add waiting period threshold logic here.
page_params.can_invite_others_to_realm =
page_params.is_admin ||
page_params.realm_invite_to_realm_policy === 1;
}
if (event.property === "name" && window.electron_bridge !== undefined) {

View File

@@ -1,4 +1,5 @@
import {page_params} from "./page_params";
import * as people from "./people";
import * as settings_config from "./settings_config";
/*
@@ -80,3 +81,32 @@ export function user_can_change_avatar() {
export function user_can_change_logo() {
return page_params.is_admin && page_params.zulip_plan_is_not_limited;
}
function user_has_permission(policy_value) {
if (page_params.is_admin) {
return true;
}
if (page_params.is_guest) {
return false;
}
if (policy_value === settings_config.common_policy_values.by_admins_only.code) {
return false;
}
if (policy_value === settings_config.common_policy_values.by_members.code) {
return true;
}
const person = people.get_by_user_id(page_params.user_id);
const current_datetime = new Date(Date.now());
const person_date_joined = new Date(person.date_joined);
const days = (current_datetime - person_date_joined) / 1000 / 86400;
return days >= page_params.realm_waiting_period_threshold;
}
export function user_can_invite_others_to_realm() {
return user_has_permission(page_params.realm_invite_to_realm_policy);
}