Skip to content

Commit

Permalink
[FIX] Missing proper permissions on Teams Channels (#21946)
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Fabris authored May 12, 2021
1 parent aa3d161 commit e55a7a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
29 changes: 20 additions & 9 deletions client/views/teams/contextualBar/channels/RoomActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import React, { useMemo } from 'react';

import { roomTypes } from '../../../../../app/utils/client';
import { usePermission } from '../../../../contexts/AuthorizationContext';
import { useSetModal } from '../../../../contexts/ModalContext';
import { useToastMessageDispatch } from '../../../../contexts/ToastMessagesContext';
import { useTranslation } from '../../../../contexts/TranslationContext';
Expand All @@ -23,8 +24,15 @@ const useReactModal = (Component, props) => {

const RoomActions = ({ room, reload }) => {
const t = useTranslation();
const rid = room._id;
const type = room.t;

const dispatchToastMessage = useToastMessageDispatch();

const canDeleteTeamChannel = usePermission(type === 'c' ? 'delete-c' : 'delete-p', rid);
const canEditTeamChannel = usePermission('edit-team-channel');
const canRemoveTeamChannel = usePermission('remove-team-channel');

const updateRoomEndpoint = useEndpointActionExperimental('POST', 'teams.updateRoom');
const removeRoomEndpoint = useEndpointActionExperimental(
'POST',
Expand Down Expand Up @@ -83,7 +91,7 @@ const RoomActions = ({ room, reload }) => {
const AutoJoinAction = async () => {
try {
await updateRoomEndpoint({
roomId: room._id,
roomId: rid,
isDefault: !room.teamDefault,
});
} catch (error) {
Expand All @@ -94,38 +102,41 @@ const RoomActions = ({ room, reload }) => {
};

return [
{
canEditTeamChannel && {
label: {
label: t('Team_Auto-join'),
icon: room.t === 'c' ? 'hash' : 'hashtag-lock',
icon: type === 'c' ? 'hash' : 'hashtag-lock',
},
action: AutoJoinAction,
},
{
canRemoveTeamChannel && {
label: {
label: t('Team_Remove_from_team'),
icon: 'cross',
},
action: RemoveFromTeamAction,
},
{
canDeleteTeamChannel && {
label: {
label: t('Delete'),
icon: 'trash',
},
action: DeleteChannelAction,
},
];
].filter(Boolean);
}, [
DeleteChannelAction,
RemoveFromTeamAction,
room._id,
room.t,
rid,
type,
room.teamDefault,
t,
updateRoomEndpoint,
reload,
dispatchToastMessage,
canDeleteTeamChannel,
canRemoveTeamChannel,
canEditTeamChannel,
]);

return (
Expand All @@ -144,7 +155,7 @@ const RoomActions = ({ room, reload }) => {
</Box>
)
}
options={menuOptions}
options={(canEditTeamChannel || canRemoveTeamChannel || canDeleteTeamChannel) && menuOptions}
/>
);
};
Expand Down
24 changes: 17 additions & 7 deletions client/views/teams/contextualBar/channels/TeamsChannelItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import React, { useState } from 'react';

import { roomTypes } from '../../../../../app/utils/client';
import RoomAvatar from '../../../../components/avatar/RoomAvatar';
import { usePermission } from '../../../../contexts/AuthorizationContext';
import { useTranslation } from '../../../../contexts/TranslationContext';
import { usePreventProgation } from '../../../../hooks/usePreventProgation';
import RoomActions from './RoomActions';

const TeamsChannelItem = ({ room, onClickView, reload }) => {
const t = useTranslation();
const rid = room._id;
const type = room.t;

const [showButton, setShowButton] = useState();

const canRemoveTeamChannel = usePermission('remove-team-channel');
const canEditTeamChannel = usePermission('edit-team-channel');
const canDeleteTeamChannel = usePermission(type === 'c' ? 'delete-c' : 'delete-p', rid);

const isReduceMotionEnabled = usePrefersReducedMotion();
const handleMenuEvent = {
[isReduceMotionEnabled ? 'onMouseEnter' : 'onTransitionEnd']: setShowButton,
Expand Down Expand Up @@ -39,13 +47,15 @@ const TeamsChannelItem = ({ room, onClickView, reload }) => {
)}
</Box>
</Option.Content>
<Option.Menu onClick={onClick}>
{showButton ? (
<RoomActions room={room} reload={reload} />
) : (
<ActionButton ghost tiny icon='kebab' />
)}
</Option.Menu>
{(canRemoveTeamChannel || canEditTeamChannel || canDeleteTeamChannel) && (
<Option.Menu onClick={onClick}>
{showButton ? (
<RoomActions room={room} reload={reload} />
) : (
<ActionButton ghost tiny icon='kebab' />
)}
</Option.Menu>
)}
</Option>
);
};
Expand Down

0 comments on commit e55a7a7

Please sign in to comment.