Delete clan

This commit is contained in:
Matt Lott 2015-04-02 11:44:18 -07:00
parent d91a7d8d1a
commit df120bdea3
4 changed files with 95 additions and 33 deletions

View file

@ -8,11 +8,11 @@ block content
| ) | )
div div
if isOwner if isOwner
button.btn.btn-sm.btn-warning Delete Clan button.btn.btn-sm.btn-warning.delete-clan-btn Delete Clan
else if isMember else if isMember
button.btn.btn-sm.btn-warning.leave-clan-btn(data-id="#{clan.id}") Leave Clan button.btn.btn-sm.btn-warning.leave-clan-btn Leave Clan
else else
button.btn.btn-sm.btn-success.join-clan-btn(data-id="#{clan.id}") Join Clan button.btn.btn-sm.btn-success.join-clan-btn Join Clan
if clan.get('members') if clan.get('members')
h3 Clan Members (#{clan.get('members').length}) h3 Clan Members (#{clan.get('members').length})

View file

@ -4,6 +4,7 @@ RootView = require 'views/core/RootView'
template = require 'templates/clans/clan-details' template = require 'templates/clans/clan-details'
Clan = require 'models/Clan' Clan = require 'models/Clan'
# TODO: Message for clan not found
# TODO: join/leave mostly duped from clans view # TODO: join/leave mostly duped from clans view
module.exports = class ClanDetailsView extends RootView module.exports = class ClanDetailsView extends RootView
@ -11,6 +12,7 @@ module.exports = class ClanDetailsView extends RootView
template: template template: template
events: events:
'click .delete-clan-btn': 'onDeleteClan'
'click .join-clan-btn': 'onJoinClan' 'click .join-clan-btn': 'onJoinClan'
'click .leave-clan-btn': 'onLeaveClan' 'click .leave-clan-btn': 'onLeaveClan'
'click .remove-member-btn': 'onRemoveMember' 'click .remove-member-btn': 'onRemoveMember'
@ -27,36 +29,42 @@ module.exports = class ClanDetailsView extends RootView
context.isMember = _.find(@clan.get('members'), (m) -> m.id is me.id) ? false context.isMember = _.find(@clan.get('members'), (m) -> m.id is me.id) ? false
context context
onDeleteClan: (e) ->
return @openModalView(new AuthModal()) if me.isAnonymous()
options =
url: "/db/clan/#{@clanID}"
method: 'DELETE'
error: (model, response, options) =>
console.error 'Error joining clan', response
success: (model, response, options) =>
app.router.navigate "/clans"
window.location.reload()
@supermodel.addRequestResource( 'delete_clan', options).load()
onJoinClan: (e) -> onJoinClan: (e) ->
return @openModalView(new AuthModal()) if me.isAnonymous() return @openModalView(new AuthModal()) if me.isAnonymous()
if clanID = $(e.target).data('id') options =
options = url: "/db/clan/#{@clanID}/join"
url: "/db/clan/#{clanID}/join" method: 'PUT'
method: 'PUT' error: (model, response, options) =>
error: (model, response, options) => console.error 'Error joining clan', response
console.error 'Error joining clan', response success: (model, response, options) =>
success: (model, response, options) => @listenToOnce @clan, 'sync', =>
@listenToOnce @clan, 'sync', => @render?()
@render?() @clan.fetch cache: false
@clan.fetch cache: false @supermodel.addRequestResource( 'join_clan', options).load()
@supermodel.addRequestResource( 'join_clan', options).load()
else
console.error "No clan ID attached to join button."
onLeaveClan: (e) -> onLeaveClan: (e) ->
if clanID = $(e.target).data('id') options =
options = url: "/db/clan/#{@clanID}/leave"
url: "/db/clan/#{clanID}/leave" method: 'PUT'
method: 'PUT' error: (model, response, options) =>
error: (model, response, options) => console.error 'Error leaving clan', response
console.error 'Error leaving clan', response success: (model, response, options) =>
success: (model, response, options) => @listenToOnce @clan, 'sync', =>
@listenToOnce @clan, 'sync', => @render?()
@render?() @clan.fetch cache: false
@clan.fetch cache: false @supermodel.addRequestResource( 'leave_clan', options).load()
@supermodel.addRequestResource( 'leave_clan', options).load()
else
console.error "No clan ID attached to leave button."
onRemoveMember: (e) -> onRemoveMember: (e) ->
if memberID = $(e.target).data('id') if memberID = $(e.target).data('id')

View file

@ -17,6 +17,7 @@ ClanHandler = class ClanHandler extends Handler
method = (method or req.method).toLowerCase() method = (method or req.method).toLowerCase()
return true if req.user?.isAdmin() return true if req.user?.isAdmin()
return true if method is 'get' return true if method is 'get'
return true if document.get('ownerID')?.equals req.user._id
false false
makeNewInstance: (req) -> makeNewInstance: (req) ->
@ -29,6 +30,15 @@ ClanHandler = class ClanHandler extends Handler
] ]
instance instance
delete: (req, res, clanID) ->
@getDocumentForIdOrSlug clanID, (err, clan) =>
return @sendDatabaseError res, err if err
return @sendNotFoundError res unless clan
return @sendForbiddenError res unless @hasAccessToDocument(req, clan)
Clan.remove {_id: clan.get('_id')}, (err) =>
return @sendDatabaseError res, err if err
@sendNoContent(res)
getByRelationship: (req, res, args...) -> getByRelationship: (req, res, args...) ->
return @joinClan(req, res, args[0]) if args[1] is 'join' return @joinClan(req, res, args[0]) if args[1] is 'join'
return @leaveClan(req, res, args[0]) if args[1] is 'leave' return @leaveClan(req, res, args[0]) if args[1] is 'leave'
@ -62,10 +72,10 @@ ClanHandler = class ClanHandler extends Handler
clanID = mongoose.Types.ObjectId(clanID) clanID = mongoose.Types.ObjectId(clanID)
memberID = mongoose.Types.ObjectId(memberID) memberID = mongoose.Types.ObjectId(memberID)
catch err catch err
return @sendBadInputError(res, err) return @sendNotFoundError(res, err)
Clan.findById clanID, (err, clan) => Clan.findById clanID, (err, clan) =>
return @sendDatabaseError(res, err) if err return @sendDatabaseError(res, err) if err
return @sendForbiddenError(res) unless clan.get('ownerID')?.equals req.user._id return @sendForbiddenError res unless @hasAccessToDocument(req, clan)
return @sendForbiddenError(res) if clan.get('ownerID').equals memberID return @sendForbiddenError(res) if clan.get('ownerID').equals memberID
Clan.update {_id: clanID}, {$pull: {members: {id: memberID}}}, (err) => Clan.update {_id: clanID}, {$pull: {members: {id: memberID}}}, (err) =>
return @sendDatabaseError(res, err) if err return @sendDatabaseError(res, err) if err

View file

@ -194,12 +194,12 @@ describe 'Clans', ->
expect(clan1.get('members')[0].id).toEqual(user1.get('_id')) expect(clan1.get('members')[0].id).toEqual(user1.get('_id'))
done() done()
it 'Remove invalid memberID 422', (done) -> it 'Remove invalid memberID 404', (done) ->
loginNewUser (user1) -> loginNewUser (user1) ->
createClan 'public', (clan1) -> createClan 'public', (clan1) ->
request.put {uri: "#{clanURL}/#{clan1.id}/remove/123" }, (err, res, body) -> request.put {uri: "#{clanURL}/#{clan1.id}/remove/123" }, (err, res, body) ->
expect(err).toBeNull() expect(err).toBeNull()
expect(res.statusCode).toBe(422) expect(res.statusCode).toBe(404)
done() done()
it 'Remove member, not in clan 403', (done) -> it 'Remove member, not in clan 403', (done) ->
@ -238,3 +238,47 @@ describe 'Clans', ->
expect(err).toBeNull() expect(err).toBeNull()
expect(res.statusCode).toBe(403) expect(res.statusCode).toBe(403)
done() done()
it 'Delete clan', (done) ->
loginNewUser (user1) ->
createClan 'public', (clan) ->
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(204)
done()
it 'Delete clan anonymous 401', (done) ->
loginNewUser (user1) ->
createClan 'public', (clan) ->
logoutUser ->
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(401)
done()
it 'Delete clan not owner 403', (done) ->
loginNewUser (user1) ->
createClan 'public', (clan) ->
loginNewUser (user2) ->
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(403)
done()
it 'Delete clan no longer exists 404', (done) ->
loginNewUser (user1) ->
createClan 'public', (clan) ->
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(204)
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(404)
done()
it 'Delete clan invalid ID 404', (done) ->
loginNewUser (user1) ->
request.del {uri: "#{clanURL}/1234" }, (err, res, body) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(404)
done()