[PLT-6838] Restrict channel delete option per permission policy even for last channel member (#6706)

* channel delete option is hidden from the menu unless there is appropriate permissions as set in the policy page

* apply to public channel only and add restriction to API layer

* updated channel deletion
Этот коммит содержится в:
Saturnino Abril
2017-07-05 06:32:27 +08:00
коммит произвёл GitHub
родитель 6d6ed309b9
Коммит 8f8a978e84
6 изменённых файлов: 45 добавлений и 37 удалений

Просмотреть файл

@@ -428,7 +428,7 @@ func getDeletedChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Reques
return return
} }
if channels, err := app.GetDeletedChannels(c.Params.TeamId, c.Params.Page * c.Params.PerPage, c.Params.PerPage); err != nil { if channels, err := app.GetDeletedChannels(c.Params.TeamId, c.Params.Page*c.Params.PerPage, c.Params.PerPage); err != nil {
c.Err = err c.Err = err
return return
} else { } else {
@@ -540,17 +540,15 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
// Allow delete if user is the only member left in channel if channel.Type == model.CHANNEL_OPEN && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_DELETE_PUBLIC_CHANNEL) {
if memberCount > 1 { c.SetPermissionError(model.PERMISSION_DELETE_PUBLIC_CHANNEL)
if channel.Type == model.CHANNEL_OPEN && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_DELETE_PUBLIC_CHANNEL) { return
c.SetPermissionError(model.PERMISSION_DELETE_PUBLIC_CHANNEL) }
return
}
if channel.Type == model.CHANNEL_PRIVATE && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_DELETE_PRIVATE_CHANNEL) { // Allow delete if there's only one member left in a private channel
c.SetPermissionError(model.PERMISSION_DELETE_PRIVATE_CHANNEL) if memberCount > 1 && channel.Type == model.CHANNEL_PRIVATE && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_DELETE_PRIVATE_CHANNEL) {
return c.SetPermissionError(model.PERMISSION_DELETE_PRIVATE_CHANNEL)
} return
} }
err = app.DeleteChannel(channel, c.Session.UserId) err = app.DeleteChannel(channel, c.Session.UserId)

Просмотреть файл

@@ -901,12 +901,14 @@ func TestDeleteChannel(t *testing.T) {
Client = th.Client Client = th.Client
team = th.BasicTeam team = th.BasicTeam
user = th.BasicUser user = th.BasicUser
user2 = th.BasicUser2
// channels created by SystemAdmin // channels created by SystemAdmin
publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN)
privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
app.AddUserToChannel(user, publicChannel6) app.AddUserToChannel(user, publicChannel6)
app.AddUserToChannel(user, privateChannel7) app.AddUserToChannel(user, privateChannel7)
app.AddUserToChannel(user2, privateChannel7)
// successful delete by user // successful delete by user
_, resp = Client.DeleteChannel(publicChannel6.Id) _, resp = Client.DeleteChannel(publicChannel6.Id)
@@ -924,6 +926,7 @@ func TestDeleteChannel(t *testing.T) {
privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
app.AddUserToChannel(user, publicChannel6) app.AddUserToChannel(user, publicChannel6)
app.AddUserToChannel(user, privateChannel7) app.AddUserToChannel(user, privateChannel7)
app.AddUserToChannel(user2, privateChannel7)
// cannot delete by user // cannot delete by user
_, resp = Client.DeleteChannel(publicChannel6.Id) _, resp = Client.DeleteChannel(publicChannel6.Id)
@@ -948,6 +951,7 @@ func TestDeleteChannel(t *testing.T) {
privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
app.AddUserToChannel(user, publicChannel6) app.AddUserToChannel(user, publicChannel6)
app.AddUserToChannel(user, privateChannel7) app.AddUserToChannel(user, privateChannel7)
app.AddUserToChannel(user2, privateChannel7)
// successful delete by team admin // successful delete by team admin
UpdateUserToTeamAdmin(user, team) UpdateUserToTeamAdmin(user, team)
@@ -976,6 +980,7 @@ func TestDeleteChannel(t *testing.T) {
privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
app.AddUserToChannel(user, publicChannel6) app.AddUserToChannel(user, publicChannel6)
app.AddUserToChannel(user, privateChannel7) app.AddUserToChannel(user, privateChannel7)
app.AddUserToChannel(user2, privateChannel7)
// cannot delete by user // cannot delete by user
_, resp = Client.DeleteChannel(publicChannel6.Id) _, resp = Client.DeleteChannel(publicChannel6.Id)
@@ -1017,6 +1022,7 @@ func TestDeleteChannel(t *testing.T) {
privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
app.AddUserToChannel(user, publicChannel6) app.AddUserToChannel(user, publicChannel6)
app.AddUserToChannel(user, privateChannel7) app.AddUserToChannel(user, privateChannel7)
app.AddUserToChannel(user2, privateChannel7)
// cannot delete by user // cannot delete by user
_, resp = Client.DeleteChannel(publicChannel6.Id) _, resp = Client.DeleteChannel(publicChannel6.Id)
@@ -1056,12 +1062,14 @@ func TestDeleteChannel(t *testing.T) {
_, resp = th.SystemAdminClient.DeleteChannel(privateChannel7.Id) _, resp = th.SystemAdminClient.DeleteChannel(privateChannel7.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
// last member of a channel should be able to delete it regardless of required permissions // last member of a public channel should have required permission to delete
publicChannel6 = th.CreateChannelWithClient(th.Client, model.CHANNEL_OPEN) publicChannel6 = th.CreateChannelWithClient(th.Client, model.CHANNEL_OPEN)
privateChannel7 = th.CreateChannelWithClient(th.Client, model.CHANNEL_PRIVATE)
_, resp = Client.DeleteChannel(publicChannel6.Id) _, resp = Client.DeleteChannel(publicChannel6.Id)
CheckNoError(t, resp) CheckForbiddenStatus(t, resp)
// last member of a private channel should be able to delete it regardless of required permissions
privateChannel7 = th.CreateChannelWithClient(th.Client, model.CHANNEL_PRIVATE)
_, resp = Client.DeleteChannel(privateChannel7.Id) _, resp = Client.DeleteChannel(privateChannel7.Id)
CheckNoError(t, resp) CheckNoError(t, resp)

Просмотреть файл

@@ -720,11 +720,7 @@ export default class ChannelHeader extends React.Component {
); );
} }
if (ChannelUtils.showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin)) { if (ChannelUtils.showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin, this.state.userCount)) {
if (!ChannelStore.isDefault(channel)) {
dropdownContents.push(deleteOption);
}
} else if (this.state.userCount === 1) {
dropdownContents.push(deleteOption); dropdownContents.push(deleteOption);
} }

Просмотреть файл

@@ -33,6 +33,7 @@ export default class DeleteChannelModal extends React.Component {
browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/town-square'); browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/town-square');
deleteChannel(this.props.channel.id); deleteChannel(this.props.channel.id);
this.onHide();
} }
onHide() { onHide() {

Просмотреть файл

@@ -529,23 +529,21 @@ export default class Navbar extends React.Component {
); );
} }
if (ChannelUtils.showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin) || this.state.userCount === 1) { if (ChannelUtils.showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin, this.state.userCount)) {
if (!ChannelStore.isDefault(channel)) { deleteChannelOption = (
deleteChannelOption = ( <li role='presentation'>
<li role='presentation'> <ToggleModalButton
<ToggleModalButton role='menuitem'
role='menuitem' dialogType={DeleteChannelModal}
dialogType={DeleteChannelModal} dialogProps={{channel}}
dialogProps={{channel}} >
> <FormattedMessage
<FormattedMessage id='channel_header.delete'
id='channel_header.delete' defaultMessage='Delete Channel'
defaultMessage='Delete Channel' />
/> </ToggleModalButton>
</ToggleModalButton> </li>
</li> );
);
}
} }
const canLeave = channel.type === Constants.PRIVATE_CHANNEL ? this.state.userCount > 1 : true; const canLeave = channel.type === Constants.PRIVATE_CHANNEL ? this.state.userCount > 1 : true;

Просмотреть файл

@@ -190,11 +190,15 @@ export function showManagementOptions(channel, isAdmin, isSystemAdmin, isChannel
return true; return true;
} }
export function showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin) { export function showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin, userCount) {
if (global.window.mm_license.IsLicensed !== 'true') { if (global.window.mm_license.IsLicensed !== 'true') {
return true; return true;
} }
if (ChannelStore.isDefault(channel)) {
return false;
}
if (channel.type === Constants.OPEN_CHANNEL) { if (channel.type === Constants.OPEN_CHANNEL) {
if (global.window.mm_config.RestrictPublicChannelDeletion === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) { if (global.window.mm_config.RestrictPublicChannelDeletion === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false; return false;
@@ -206,6 +210,9 @@ export function showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin
return false; return false;
} }
} else if (channel.type === Constants.PRIVATE_CHANNEL) { } else if (channel.type === Constants.PRIVATE_CHANNEL) {
if (userCount === 1) {
return true;
}
if (global.window.mm_config.RestrictPrivateChannelDeletion === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) { if (global.window.mm_config.RestrictPrivateChannelDeletion === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false; return false;
} }