Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable renaming public rooms if the current user is not an admin or the creator of the linked workspace #15688

Merged
merged 14 commits into from
Mar 12, 2023
Merged
12 changes: 12 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ function isChatRoom(report) {
return isUserCreatedPolicyRoom(report) || isDefaultRoom(report);
}

/**
* Whether the provided report is a public room
* @param {Object} report
* @param {String} report.visibility
* @returns {Boolean}
*/
function isPublicRoom(report) {
const visibility = lodashGet(report, 'visibility', '');
return visibility === CONST.REPORT.VISIBILITY.PUBLIC || visibility === CONST.REPORT.VISIBILITY.PUBLIC_ANNOUNCE;
}

/**
* Get the policy type from a given report
* @param {Object} report
Expand Down Expand Up @@ -1635,6 +1646,7 @@ export {
getPolicyName,
getPolicyType,
isArchivedRoom,
isPublicRoom,
isConciergeChatReport,
hasAutomatedExpensifyEmails,
hasExpensifyGuidesEmails,
Expand Down
11 changes: 11 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ function isAdminOfFreePolicy(policies) {
&& policy.role === CONST.POLICY.ROLE.ADMIN);
}

/**
* Is the user the owner of the given policy?
*
* @param {Object} policy
* @returns {Boolean}
*/
function isPolicyOwner(policy) {
return policy.owner === sessionEmail;
}

/**
* Check if the user has any active free policies (aka workspaces)
*
Expand Down Expand Up @@ -1060,5 +1070,6 @@ export {
openWorkspaceMembersPage,
openWorkspaceInvitePage,
removeWorkspace,
isPolicyOwner,
leaveRoom,
};
18 changes: 17 additions & 1 deletion src/pages/ReportSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import styles from '../styles/styles';
import compose from '../libs/compose';
import Navigation from '../libs/Navigation/Navigation';
import * as Report from '../libs/actions/Report';
import * as Policy from '../libs/actions/Policy';
import * as ReportUtils from '../libs/ReportUtils';
import HeaderWithCloseButton from '../components/HeaderWithCloseButton';
import ScreenWrapper from '../components/ScreenWrapper';
Expand Down Expand Up @@ -65,6 +66,19 @@ class ReportSettingsPage extends Component {
];
}

/**
* @param {Object} report - the given report we're viewing settings for
* @param {Object|null} policy - the workspace the report is on, null if the user isn't a member of the workspace
* @returns {Boolean}
*/
shouldDisablePublicRoomRename(report, policy) {
if (!policy || !ReportUtils.isPublicRoom(report)) {
return true;
}
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved

return !Policy.isPolicyOwner(policy) && policy.role !== CONST.POLICY.ROLE.ADMIN;
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {Object} values - form input values passed by the Form component
*/
Expand Down Expand Up @@ -110,8 +124,10 @@ class ReportSettingsPage extends Component {

render() {
const shouldShowRoomName = !ReportUtils.isPolicyExpenseChat(this.props.report);
const shouldDisableRename = ReportUtils.isDefaultRoom(this.props.report) || ReportUtils.isArchivedRoom(this.props.report);
const linkedWorkspace = _.find(this.props.policies, policy => policy && policy.id === this.props.report.policyID);
const shouldDisableRename = ReportUtils.isDefaultRoom(this.props.report)
|| ReportUtils.isArchivedRoom(this.props.report)
|| this.shouldDisablePublicRoomRename(this.props.report, linkedWorkspace);
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved

return (
<ScreenWrapper>
Expand Down