Skip to content

Commit

Permalink
Better validation and error handling for room rename. Fixes #566
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Aug 24, 2015
1 parent 9729bb2 commit e9bb5cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client/views/app/room.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,10 @@ Template.room.onRendered ->
RoomHistoryManager.getMoreIfIsEmpty this.data._id

renameRoom = (rid, name) ->
name = name?.toLowerCase().trim()
console.log 'room renameRoom' if window.rocketDebug
room = Session.get('roomData' + rid)
if room.name == name
if room.name is name
Session.set('editRoomTitle', false)
return false

Expand All @@ -723,6 +724,15 @@ renameRoom = (rid, name) ->

toastr.success t('Room_name_changed_successfully')
if error
if error.error is 'name-invalid'
toastr.error t('Invalid_room_name', name)
return
if error.error is 'duplicate-name'
if room.t is 'c'
toastr.error t('Duplicate_channel_name', name)
else
toastr.error t('Duplicate_private_group_name', name)
return
toastr.error error.reason

toggleAddUser = ->
Expand Down
7 changes: 7 additions & 0 deletions server/methods/saveRoomName.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ Meteor.methods
if room.u._id isnt Meteor.userId() or room.t not in ['c', 'p']
throw new Meteor.Error 403, 'Not allowed'

if not /^[0-9a-z-_]+$/.test name
throw new Meteor.Error 'name-invalid'

name = _.slugify name

if name is room.name
return

# avoid duplicate names
if ChatRoom.findOne({name:name})
throw new Meteor.Error 'duplicate-name'

ChatRoom.update rid,
$set:
name: name
Expand Down

0 comments on commit e9bb5cb

Please sign in to comment.