Skip to content

Commit

Permalink
Merge pull request #22316 from hungvu193/fix-21498
Browse files Browse the repository at this point in the history
fix rename room permission
  • Loading branch information
Julesssss authored Jul 19, 2023
2 parents 16a83ac + 00e18fc commit 568aab2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
28 changes: 28 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Onyx.connect({
callback: (val) => (allPolicies = val),
});

let loginList;
Onyx.connect({
key: ONYXKEYS.LOGIN_LIST,
callback: (val) => (loginList = val),
});

function getChatType(report) {
return report ? report.chatType : '';
}
Expand Down Expand Up @@ -2530,6 +2536,27 @@ function getReportOfflinePendingActionAndErrors(report) {
return {addWorkspaceRoomOrChatPendingAction, addWorkspaceRoomOrChatErrors};
}

/**
* @param {Object|null} report
* @param {Object|null} policy - the workspace the report is on, null if the user isn't a member of the workspace
* @returns {Boolean}
*/
function shouldDisableRename(report, policy) {
if (isDefaultRoom(report) || isArchivedRoom(report)) {
return true;
}

// if the linked workspace is null, that means the person isn't a member of the workspace the report is in
// which means this has to be a public room we want to disable renaming for
if (!policy) {
return true;
}

// If there is a linked workspace, that means the user is a member of the workspace the report is in.
// Still, we only want policy owners and admins to be able to modify the name.
return !_.keys(loginList).includes(policy.owner) && policy.role !== CONST.POLICY.ROLE.ADMIN;
}

export {
getReportParticipantsTitle,
isReportMessageAttachment,
Expand Down Expand Up @@ -2635,4 +2662,5 @@ export {
getOriginalReportID,
canAccessReport,
getReportOfflinePendingActionAndErrors,
shouldDisableRename,
};
28 changes: 1 addition & 27 deletions src/pages/settings/Report/ReportSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 HeaderWithBackButton from '../../../components/HeaderWithBackButton';
import ScreenWrapper from '../../../components/ScreenWrapper';
Expand Down Expand Up @@ -56,31 +55,6 @@ const defaultProps = {
};

class ReportSettingsPage extends Component {
/**
* @param {Object|null} linkedWorkspace - the workspace the report is on, null if the user isn't a member of the workspace
* @returns {Boolean}
*/
shouldDisableRename(linkedWorkspace) {
if (ReportUtils.isDefaultRoom(this.props.report) || ReportUtils.isArchivedRoom(this.props.report)) {
return true;
}

// The remaining checks only apply to public rooms
if (!ReportUtils.isPublicRoom(this.props.report)) {
return false;
}

// if the linked workspace is null, that means the person isn't a member of the workspace the report is in
// which means this has to be a public room we want to disable renaming for
if (!linkedWorkspace) {
return true;
}

// If there is a linked workspace, that means the user is a member of the workspace the report is in.
// Still, we only want policy owners and admins to be able to modify the name.
return !Policy.isPolicyOwner(linkedWorkspace) && linkedWorkspace.role !== CONST.POLICY.ROLE.ADMIN;
}

/**
* We only want policy owners and admins to be able to modify the welcome message.
*
Expand All @@ -94,7 +68,7 @@ class ReportSettingsPage extends Component {
render() {
const shouldShowRoomName = !ReportUtils.isPolicyExpenseChat(this.props.report) && !ReportUtils.isChatThread(this.props.report);
const linkedWorkspace = _.find(this.props.policies, (policy) => policy && policy.id === this.props.report.policyID);
const shouldDisableRename = this.shouldDisableRename(linkedWorkspace) || ReportUtils.isChatThread(this.props.report);
const shouldDisableRename = ReportUtils.shouldDisableRename(this.props.report, linkedWorkspace) || ReportUtils.isChatThread(this.props.report);
const notificationPreference = this.props.translate(`notificationPreferencesPage.notificationPreferences.${this.props.report.notificationPreference}`);
const shouldDisableWelcomeMessage = this.shouldDisableWelcomeMessage(linkedWorkspace);
const writeCapability = ReportUtils.isAdminRoom(this.props.report)
Expand Down
58 changes: 36 additions & 22 deletions src/pages/settings/Report/RoomNamePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import reportPropTypes from '../../reportPropTypes';
import ROUTES from '../../../ROUTES';
import * as Report from '../../../libs/actions/Report';
import RoomNameInput from '../../../components/RoomNameInput';
import * as ReportUtils from '../../../libs/ReportUtils';
import FullPageNotFoundView from '../../../components/BlockingViews/FullPageNotFoundView';

const propTypes = {
...withLocalizePropTypes,
Expand All @@ -27,12 +29,20 @@ const propTypes = {

/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes),

/** Policy of the report for which the name is being edited */
policy: PropTypes.shape({
role: PropTypes.string,
owner: PropTypes.string,
}),
};
const defaultProps = {
reports: {},
policy: {},
};

function RoomNamePage(props) {
const policy = props.policy;
const report = props.report;
const reports = props.reports;
const translate = props.translate;
Expand Down Expand Up @@ -69,30 +79,31 @@ function RoomNamePage(props) {

return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
// Room name input autofocusing may block screen transition on Safari
onEntryTransitionEnd={() => roomNameInputRef.current && roomNameInputRef.current.focus()}
includeSafeAreaPaddingBottom={false}
>
<HeaderWithBackButton
title={translate('newRoomPage.roomName')}
onBackButtonPress={() => Navigation.goBack(ROUTES.getReportSettingsRoute(report.reportID))}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.ROOM_NAME_FORM}
onSubmit={(values) => Report.updatePolicyRoomNameAndNavigate(report, values.roomName)}
validate={validate}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<RoomNameInput
ref={(ref) => (roomNameInputRef.current = ref)}
inputID="roomName"
defaultValue={report.reportName}
/>
</View>
</Form>
<FullPageNotFoundView shouldShow={ReportUtils.shouldDisableRename(report, policy)}>
<HeaderWithBackButton
title={translate('newRoomPage.roomName')}
onBackButtonPress={() => Navigation.goBack(ROUTES.getReportSettingsRoute(report.reportID))}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.ROOM_NAME_FORM}
onSubmit={(values) => Report.updatePolicyRoomNameAndNavigate(report, values.roomName)}
validate={validate}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<RoomNameInput
ref={(ref) => (roomNameInputRef.current = ref)}
inputID="roomName"
defaultValue={report.reportName}
/>
</View>
</Form>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
Expand All @@ -108,5 +119,8 @@ export default compose(
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`,
},
}),
)(RoomNamePage);

0 comments on commit 568aab2

Please sign in to comment.