[FIX] Check permissions on team channels action sheet (#3155)

* [IMPROVEMENT] Show only the option that user can manage in TeamChannelsView

* Refactor the showActionSheet function

* Added remove team channel permission

* Cleanup

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-05-27 15:07:21 -03:00 committed by GitHub
parent 981b3688f1
commit 6e32d1dc6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 33 deletions

View File

@ -32,6 +32,12 @@ import { CustomIcon } from '../lib/Icons';
import { themes } from '../constants/colors'; import { themes } from '../constants/colors';
const API_FETCH_COUNT = 25; const API_FETCH_COUNT = 25;
const PERMISSION_DELETE_C = 'delete-c';
const PERMISSION_DELETE_P = 'delete-p';
const PERMISSION_EDIT_TEAM_CHANNEL = 'edit-team-channel';
const PERMISSION_REMOVE_TEAM_CHANNEL = 'remove-team-channel';
const PERMISSION_ADD_TEAM_CHANNEL = 'add-team-channel';
const getItemLayout = (data, index) => ({ const getItemLayout = (data, index) => ({
length: data.length, length: data.length,
@ -51,7 +57,10 @@ class TeamChannelsView extends React.Component {
width: PropTypes.number, width: PropTypes.number,
StoreLastMessage: PropTypes.bool, StoreLastMessage: PropTypes.bool,
addTeamChannelPermission: PropTypes.array, addTeamChannelPermission: PropTypes.array,
editTeamChannelPermission: PropTypes.array,
removeTeamChannelPermission: PropTypes.array, removeTeamChannelPermission: PropTypes.array,
deleteCPermission: PropTypes.array,
deletePPermission: PropTypes.array,
showActionSheet: PropTypes.func, showActionSheet: PropTypes.func,
deleteRoom: PropTypes.func deleteRoom: PropTypes.func
} }
@ -291,33 +300,6 @@ class TeamChannelsView extends React.Component {
} }
}, 1000, true); }, 1000, true);
options = (item) => {
const { theme } = this.props;
const isAutoJoinChecked = item.teamDefault;
const autoJoinIcon = isAutoJoinChecked ? 'checkbox-checked' : 'checkbox-unchecked';
const autoJoinIconColor = isAutoJoinChecked ? themes[theme].tintActive : themes[theme].auxiliaryTintColor;
return ([
{
title: I18n.t('Auto-join'),
icon: item.t === 'p' ? 'channel-private' : 'channel-public',
onPress: () => this.toggleAutoJoin(item),
right: () => <CustomIcon name={autoJoinIcon} size={20} color={autoJoinIconColor} />
},
{
title: I18n.t('Remove_from_Team'),
icon: 'close',
danger: true,
onPress: () => this.remove(item)
},
{
title: I18n.t('Delete'),
icon: 'delete',
danger: true,
onPress: () => this.delete(item)
}
]);
}
toggleAutoJoin = async(item) => { toggleAutoJoin = async(item) => {
try { try {
const { data } = this.state; const { data } = this.state;
@ -391,13 +373,49 @@ class TeamChannelsView extends React.Component {
showChannelActions = async(item) => { showChannelActions = async(item) => {
logEvent(events.ROOM_SHOW_BOX_ACTIONS); logEvent(events.ROOM_SHOW_BOX_ACTIONS);
const { showActionSheet, removeTeamChannelPermission } = this.props; const {
showActionSheet, editTeamChannelPermission, deleteCPermission, deletePPermission, theme, removeTeamChannelPermission
} = this.props;
const isAutoJoinChecked = item.teamDefault;
const autoJoinIcon = isAutoJoinChecked ? 'checkbox-checked' : 'checkbox-unchecked';
const autoJoinIconColor = isAutoJoinChecked ? themes[theme].tintActive : themes[theme].auxiliaryTintColor;
const permissions = await RocketChat.hasPermission([removeTeamChannelPermission], this.team.rid); const options = [];
if (!permissions[0]) {
const permissionsTeam = await RocketChat.hasPermission([editTeamChannelPermission], this.team.rid);
if (permissionsTeam[0]) {
options.push({
title: I18n.t('Auto-join'),
icon: item.t === 'p' ? 'channel-private' : 'channel-public',
onPress: () => this.toggleAutoJoin(item),
right: () => <CustomIcon name={autoJoinIcon} size={20} color={autoJoinIconColor} />
});
}
const permissionsRemoveTeam = await RocketChat.hasPermission([removeTeamChannelPermission], this.team.rid);
if (permissionsRemoveTeam[0]) {
options.push({
title: I18n.t('Remove_from_Team'),
icon: 'close',
danger: true,
onPress: () => this.remove(item)
});
}
const permissionsChannel = await RocketChat.hasPermission([item.t === 'c' ? deleteCPermission : deletePPermission], item._id);
if (permissionsChannel[0]) {
options.push({
title: I18n.t('Delete'),
icon: 'delete',
danger: true,
onPress: () => this.delete(item)
});
}
if (options.length === 0) {
return; return;
} }
showActionSheet({ options: this.options(item) }); showActionSheet({ options });
} }
renderItem = ({ item }) => { renderItem = ({ item }) => {
@ -481,8 +499,11 @@ const mapStateToProps = state => ({
useRealName: state.settings.UI_Use_Real_Name, useRealName: state.settings.UI_Use_Real_Name,
isMasterDetail: state.app.isMasterDetail, isMasterDetail: state.app.isMasterDetail,
StoreLastMessage: state.settings.Store_Last_Message, StoreLastMessage: state.settings.Store_Last_Message,
addTeamChannelPermission: state.permissions['add-team-channel'], addTeamChannelPermission: state.permissions[PERMISSION_ADD_TEAM_CHANNEL],
removeTeamChannelPermission: state.permissions['remove-team-channel'] editTeamChannelPermission: state.permissions[PERMISSION_EDIT_TEAM_CHANNEL],
removeTeamChannelPermission: state.permissions[PERMISSION_REMOVE_TEAM_CHANNEL],
deleteCPermission: state.permissions[PERMISSION_DELETE_C],
deletePPermission: state.permissions[PERMISSION_DELETE_P]
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({