mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-09-24 09:14:55 +09:00
Scope room lookup to the type each controller administers
Rooms::DirectsController relaxes ensure_can_administer to true, because every participant in a direct room may administer it. set_room was inherited unscoped, though, so that relaxation applied to any room the caller was merely a member of: DELETE /rooms/directs/<id> destroyed open and closed rooms and all their messages. The same unscoped lookup let a direct room be loaded by the opens and closeds controllers, where force_room_type promoted it. Promoting a DM to open grants every user on the account membership and republishes the whole conversation, including the other participant's messages; converting it to closed lets the initiator revise who is in it and lock the other participant out. Each controller now narrows room_scope to the types it may act on. Opens and closeds keep reach into each other, since converting between them is a feature. Neither can reach a direct room, and directs can only reach directs. Room also refuses to change type away from Rooms::Direct, so the invariant holds for any future caller of becomes! rather than only these two controllers.
This commit is contained in:
@@ -42,6 +42,12 @@ class Rooms::ClosedsController < RoomsController
|
||||
@room = @room.becomes!(Rooms::Closed)
|
||||
end
|
||||
|
||||
# Open and closed rooms convert into each other, so both are in reach here. Direct
|
||||
# rooms never are: converting one would let its creator revise who's in it.
|
||||
def room_scope
|
||||
Current.user.rooms.without_directs
|
||||
end
|
||||
|
||||
def grantees
|
||||
User.where(id: grantee_ids)
|
||||
end
|
||||
|
||||
@@ -29,8 +29,13 @@ class Rooms::DirectsController < RoomsController
|
||||
end
|
||||
end
|
||||
|
||||
# All users in a direct room can administer it
|
||||
# All users in a direct room can administer it. Only direct rooms, though: this
|
||||
# relaxation is why room_scope below has to keep every other type out of reach.
|
||||
def ensure_can_administer
|
||||
true
|
||||
end
|
||||
|
||||
def room_scope
|
||||
Current.user.rooms.directs
|
||||
end
|
||||
end
|
||||
|
||||
@@ -40,6 +40,12 @@ class Rooms::OpensController < RoomsController
|
||||
@room = @room.becomes!(Rooms::Open)
|
||||
end
|
||||
|
||||
# Open and closed rooms convert into each other, so both are in reach here. Direct
|
||||
# rooms never are: promoting one would republish its history to the whole account.
|
||||
def room_scope
|
||||
Current.user.rooms.without_directs
|
||||
end
|
||||
|
||||
def broadcast_create_room(room)
|
||||
broadcast_prepend_to :rooms, target: :shared_rooms, partial: "users/sidebars/rooms/shared", locals: { room: room }
|
||||
end
|
||||
|
||||
@@ -20,13 +20,19 @@ class RoomsController < ApplicationController
|
||||
|
||||
private
|
||||
def set_room
|
||||
if room = Current.user.rooms.find_by(id: params[:room_id] || params[:id])
|
||||
if room = room_scope.find_by(id: params[:room_id] || params[:id])
|
||||
@room = room
|
||||
else
|
||||
redirect_to root_url, alert: "Room not found or inaccessible"
|
||||
end
|
||||
end
|
||||
|
||||
# Subclasses narrow this to the room types they're allowed to act on, so that one
|
||||
# room namespace can't be used to reach another's rooms.
|
||||
def room_scope
|
||||
Current.user.rooms
|
||||
end
|
||||
|
||||
def ensure_can_administer
|
||||
head :forbidden unless Current.user.can_administer?(@room)
|
||||
end
|
||||
|
||||
@@ -22,6 +22,8 @@ class Room < ApplicationRecord
|
||||
|
||||
belongs_to :creator, class_name: "User", default: -> { Current.user }
|
||||
|
||||
validate :direct_rooms_keep_their_type, on: :update
|
||||
|
||||
scope :opens, -> { where(type: "Rooms::Open") }
|
||||
scope :closeds, -> { where(type: "Rooms::Closed") }
|
||||
scope :directs, -> { where(type: "Rooms::Direct") }
|
||||
@@ -65,6 +67,15 @@ class Room < ApplicationRecord
|
||||
end
|
||||
|
||||
private
|
||||
# Open and closed rooms convert into each other freely. A direct room can't become
|
||||
# either: its participants agreed to a private conversation, not to one whose
|
||||
# audience someone else gets to widen afterwards.
|
||||
def direct_rooms_keep_their_type
|
||||
if type_changed? && type_was == "Rooms::Direct"
|
||||
errors.add :type, "can't be changed for a direct room"
|
||||
end
|
||||
end
|
||||
|
||||
def unread_memberships(message)
|
||||
memberships.visible.disconnected.where.not(user: message.creator).update_all(unread_at: message.created_at, updated_at: Time.current)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user