Skip to content

Commit

Permalink
Fix #2565 - Don't let the last owner leave the room. Warn user.
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloschmidt committed Mar 31, 2016
1 parent 673c6a8 commit cb11441
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT

- Fix #2634. Admins are warned if they have not verified their e-mail and e-mail verification is true.
- Fix #2565 - Don't let the last owner leave the room. Warn user.

## 0.24.0, 2016-Mar-28

Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-lib/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@
"you_are_in_preview_mode_of" : "You are in preview mode of channel #<strong>__room_name__</strong>",
"You_are_logged_in_as" : "You are logged in as",
"You_are_not_authorized_to_view_this_page" : "You are not authorized to view this page.",
"You_are_the_last_owner_Please_set_new_owner_before_leaving_the_room" : "You are the last owner. Please set new owner before leaving the room.",
"You_can_change_a_different_avatar_too" : "You can override the avatar used to post from this integration.",
"You_can_use_an_emoji_as_avatar" : "You can also use an emoji as an avatar.",
"You_have_been_muted" : "You have been muted and cannot speak in this room",
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-lib/i18n/pt.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@
"you_are_in_preview_mode_of" : "Esta é uma prévia do canal #<strong>__room_name__</strong>",
"You_are_logged_in_as" : "Vocês está logado como",
"You_are_not_authorized_to_view_this_page" : "Você não possui permissão para visualizar esta página.",
"You_are_the_last_owner_Please_set_new_owner_before_leaving_the_room" : "Você é o último proprietário da sala. Por favor defina um novo proprietário antes de sair.",
"You_have_successfully_unsubscribed" : "A partir de agora você não está mais cadastrado em nossa lista de e-mails.",
"You_need_confirm_email" : "Você precisa confirmar seu email para logar!",
"You_will_not_be_able_to_recover" : "Você não será capaz de desfazer!",
Expand Down
10 changes: 9 additions & 1 deletion packages/rocketchat-slashcommands-leave/leave.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ else
class Leave
constructor: (command, params, item) ->
if(command == "leave" || command == "part")
Meteor.call 'leaveRoom', item.rid
try
Meteor.call 'leaveRoom', item.rid
catch
RocketChat.Notifications.notifyUser Meteor.userId(), 'message', {
_id: Random.id()
rid: item.rid
ts: new Date
msg: TAPi18n.__('You_are_the_last_owner_Please_set_new_owner_before_leaving_the_room', null, Meteor.user().language)
}

RocketChat.slashCommands.add 'leave', Leave
RocketChat.slashCommands.add 'part', Leave
28 changes: 20 additions & 8 deletions packages/rocketchat-ui-sidenav/side-nav/chatRoomItem.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,24 @@ Template.chatRoomItem.events
confirmButtonColor: '#DD6B55'
confirmButtonText: t('Yes_leave_it')
cancelButtonText: t('Cancel')
closeOnConfirm: true
closeOnConfirm: false
html: false
}, ->
if FlowRouter.getRouteName() in ['channel', 'group', 'direct'] and Session.get('openedRoom') is rid
FlowRouter.go 'home'

RoomManager.close rid

Meteor.call 'leaveRoom', rid
}, (isConfirm) ->
if isConfirm
Meteor.call 'leaveRoom', rid, (err) ->
if err
swal {
title: t('Warning')
text: t("You_are_the_last_owner_Please_set_new_owner_before_leaving_the_room")
type: 'warning'
html: false
}

else
swal.close()
if FlowRouter.getRouteName() in ['channel', 'group', 'direct'] and Session.get('openedRoom') is rid
FlowRouter.go 'home'

RoomManager.close rid
else
swal.close()
15 changes: 6 additions & 9 deletions server/methods/leaveRoom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Meteor.methods
room = RocketChat.models.Rooms.findOneById rid
user = Meteor.user()

# If user is room owner, check if there are other owners. If there isn't anyone else, warn user to set a new owner.
if RocketChat.authz.hasRole(user._id, 'owner', room._id)
numOwners = RocketChat.authz.getUsersInRole('owner', room._id).fetch().length
if numOwners is 1
throw new Meteor.Error 'last-owner', '[methods] leaveRoom -> User is last owner in room.'

RocketChat.callbacks.run 'beforeLeaveRoom', user, room

RocketChat.models.Rooms.removeUsernameById rid, user.username
Expand All @@ -20,15 +26,6 @@ Meteor.methods
if room.t is 'l'
RocketChat.models.Messages.createCommandWithRoomIdAndUser 'survey', rid, user


if room.u?._id is Meteor.userId()
newOwner = _.without(room.usernames, user.username)[0]
if newOwner?
newOwner = RocketChat.models.Users.findOneByUsername newOwner

if newOwner?
RocketChat.models.Rooms.setUserById rid, newOwner

RocketChat.models.Subscriptions.removeByRoomIdAndUserId rid, Meteor.userId()

Meteor.defer ->
Expand Down

0 comments on commit cb11441

Please sign in to comment.