Passed
Push — master ( 6bc43d...2da2ee )
by Ahmad
07:04
created

RoomsController.room_limit_exceeded()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
1
# frozen_string_literal: true
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
class RoomsController < ApplicationController
20
  include Pagy::Backend
21
  include Recorder
22
  include Joiner
23
  include Populator
24
25
  before_action :validate_accepted_terms, unless: -> { !Rails.configuration.terms }
26
  before_action :validate_verified_email, except: [:show, :join],
27
                unless: -> { !Rails.configuration.enable_email_verification }
28
  before_action :find_room, except: [:create, :join_specific_room]
29
  before_action :verify_room_ownership_or_admin_or_shared, only: [:start, :shared_access]
30
  before_action :verify_room_ownership_or_admin, only: [:update_settings, :destroy]
31
  before_action :verify_room_ownership_or_shared, only: [:remove_shared_access]
32
  before_action :verify_room_owner_verified, only: [:show, :join],
33
                unless: -> { !Rails.configuration.enable_email_verification }
34
  before_action :verify_room_owner_valid, only: [:show, :join]
35
  before_action :verify_user_not_admin, only: [:show]
36
37
  # POST /
38
  def create
39
    # Return to root if user is not signed in
40
    return redirect_to root_path unless current_user
41
42
    # Check if the user has not exceeded the room limit
43
    return redirect_to current_user.main_room, flash: { alert: I18n.t("room.room_limit") } if room_limit_exceeded
44
45
    # Create room
46
    @room = Room.new(name: room_params[:name], access_code: room_params[:access_code])
47
    @room.owner = current_user
48
    @room.room_settings = create_room_settings_string(room_params)
49
50
    # Save the room and redirect if it fails
51
    return redirect_to current_user.main_room, flash: { alert: I18n.t("room.create_room_error") } unless @room.save
52
53
    logger.info "Support: #{current_user.email} has created a new room #{@room.uid}."
54
55
    # Redirect to room is auto join was not turned on
56
    return redirect_to @room,
57
      flash: { success: I18n.t("room.create_room_success") } unless room_params[:auto_join] == "1"
58
59
    # Start the room if auto join was turned on
60
    start
61
  end
62
63
  # GET /:room_uid
64
  def show
65
    @anyone_can_start = JSON.parse(@room[:room_settings])["anyoneCanStart"]
66
    @room_running = room_running?(@room.bbb_id)
67
    @shared_room = room_shared_with_user
68
69
    # If its the current user's room
70
    if current_user && (@room.owned_by?(current_user) || @shared_room)
71
      if current_user.highest_priority_role.get_permission("can_create_rooms")
72
        # User is allowed to have rooms
73
        @search, @order_column, @order_direction, recs =
74
          recordings(@room.bbb_id, params.permit(:search, :column, :direction), true)
75
76
        @user_list = shared_user_list if shared_access_allowed
77
78
        @pagy, @recordings = pagy_array(recs)
79
      else
80
        # Render view for users that cant create rooms
81
        @recent_rooms = Room.where(id: cookies.encrypted["#{current_user.uid}_recently_joined_rooms"])
82
        render :cant_create_rooms
83
      end
84
    else
85
      return redirect_to root_path, flash: { alert: I18n.t("room.invalid_provider") } if incorrect_user_domain
86
87
      show_user_join
88
    end
89
  end
90
91
  # POST /:room_uid
92
  def join
93
    return redirect_to root_path,
94
      flash: { alert: I18n.t("administrator.site_settings.authentication.user-info") } if auth_required
95
96
    unless @room.owned_by?(current_user)
97
      # Don't allow users to join unless they have a valid access code or the room doesn't have an access code
98
      if @room.access_code && [email protected]_code.empty? && @room.access_code != session[:access_code]
99
        return redirect_to room_path(room_uid: params[:room_uid]), flash: { alert: I18n.t("room.access_code_required") }
100
      end
101
102
      # Assign join name if passed.
103
      if params[@room.invite_path]
104
        @join_name = params[@room.invite_path][:join_name]
105
      elsif !params[:join_name]
106
        # Join name not passed.
107
        return redirect_to root_path
108
      end
109
    end
110
111
    # create or update cookie with join name
112
    cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name
113
114
    save_recent_rooms
115
116
    logger.info "Support: #{current_user.present? ? current_user.email : @join_name} is joining room #{@room.uid}"
117
    join_room(default_meeting_options)
118
  end
119
120
  # DELETE /:room_uid
121
  def destroy
122
    begin
123
      # Don't delete the users home room.
124
      raise I18n.t("room.delete.home_room") if @room == @room.owner.main_room
125
      @room.destroy
126
    rescue => e
127
      flash[:alert] = I18n.t("room.delete.fail", error: e)
128
    else
129
      flash[:success] = I18n.t("room.delete.success")
130
    end
131
132
    # Redirect to home room if the redirect_back location is the deleted room
133
    return redirect_to @current_user.main_room if request.referer == room_url(@room)
134
135
    # Redirect to the location that the user deleted the room from
136
    redirect_back fallback_location: current_user.main_room
137
  end
138
139
  # POST /room/join
140
  def join_specific_room
141
    room_uid = params[:join_room][:url].split('/').last
142
143
    begin
144
      @room = Room.find_by!(uid: room_uid)
145
    rescue ActiveRecord::RecordNotFound
146
      return redirect_to current_user.main_room, alert: I18n.t("room.no_room.invalid_room_uid")
147
    end
148
149
    redirect_to room_path(@room)
150
  end
151
152
  # POST /:room_uid/start
153
  def start
154
    logger.info "Support: #{current_user.email} is starting room #{@room.uid}"
155
156
    # Join the user in and start the meeting.
157
    opts = default_meeting_options
158
    opts[:user_is_moderator] = true
159
160
    # Include the user's choices for the room settings
161
    room_settings = JSON.parse(@room[:room_settings])
162
    opts[:mute_on_start] = room_settings["muteOnStart"]
163
    opts[:require_moderator_approval] = room_settings["requireModeratorApproval"]
164
165
    begin
166
      redirect_to join_path(@room, current_user.name, opts, current_user.uid)
167
    rescue BigBlueButton::BigBlueButtonException => e
168
      logger.error("Support: #{@room.uid} start failed: #{e}")
169
170
      redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception"))
171
    end
172
173
    # Notify users that the room has started.
174
    # Delay 5 seconds to allow for server start, although the request will retry until it succeeds.
175
    NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room)
176
  end
177
178
  # POST /:room_uid/update_settings
179
  def update_settings
180
    begin
181
      options = params[:room].nil? ? params : params[:room]
182
      raise "Room name can't be blank" if options[:name].blank?
183
184
      # Update the rooms values
185
      room_settings_string = create_room_settings_string(options)
186
187
      @room.update_attributes(
188
        name: options[:name],
189
        room_settings: room_settings_string,
190
        access_code: options[:access_code]
191
      )
192
193
      flash[:success] = I18n.t("room.update_settings_success")
194
    rescue => e
195
      logger.error "Support: Error in updating room settings: #{e}"
196
      flash[:alert] = I18n.t("room.update_settings_error")
197
    end
198
199
    redirect_back fallback_location: room_path(@room)
200
  end
201
202
  # POST /:room_uid/update_shared_access
203
  def shared_access
204
    begin
205
      current_list = @room.shared_users.pluck(:id)
206
      new_list = User.where(uid: params[:add]).pluck(:id)
207
208
      # Get the list of users that used to be in the list but were removed
209
      users_to_remove = current_list - new_list
210
      # Get the list of users that are in the new list but not in the current list
211
      users_to_add = new_list - current_list
212
213
      # Remove users that are removed
214
      SharedAccess.where(room_id: @room.id, user_id: users_to_remove).delete_all unless users_to_remove.empty?
215
216
      # Add users that are added
217
      users_to_add.each do |id|
218
        SharedAccess.create(room_id: @room.id, user_id: id)
219
      end
220
221
      flash[:success] = I18n.t("room.shared_access_success")
222
    rescue => e
223
      logger.error "Support: Error in updating room shared access: #{e}"
224
      flash[:alert] = I18n.t("room.shared_access_error")
225
    end
226
227
    redirect_back fallback_location: room_path
228
  end
229
230
  # POST /:room_uid/remove_shared_access
231
  def remove_shared_access
232
    begin
233
      SharedAccess.find_by!(room_id: @room.id, user_id: params[:user_id]).destroy
234
      flash[:success] = I18n.t("room.remove_shared_access_success")
235
    rescue => e
236
      logger.error "Support: Error in removing room shared access: #{e}"
237
      flash[:alert] = I18n.t("room.remove_shared_access_error")
238
    end
239
240
    redirect_to current_user.main_room
241
  end
242
243
  # GET /:room_uid/shared_users
244
  def shared_users
245
    # Respond with JSON object of users that have access to the room
246
    respond_to do |format|
247
      format.json { render body: @room.shared_users.to_json }
248
    end
249
  end
250
251
  # GET /:room_uid/room_settings
252
  def room_settings
253
    # Respond with JSON object of the room_settings
254
    respond_to do |format|
255
      format.json { render body: @room.room_settings.to_json }
256
    end
257
  end
258
259
  # GET /:room_uid/logout
260
  def logout
261
    logger.info "Support: #{current_user.present? ? current_user.email : 'Guest'} has left room #{@room.uid}"
262
263
    # Redirect the correct page.
264
    redirect_to @room
265
  end
266
267
  # POST /:room_uid/login
268
  def login
269
    session[:access_code] = room_params[:access_code]
270
271
    flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code
272
273
    redirect_to room_path(@room.uid)
274
  end
275
276
  private
277
278
  def create_room_settings_string(options)
279
    room_settings = {
280
      "muteOnStart": options[:mute_on_join] == "1",
281
      "requireModeratorApproval": options[:require_moderator_approval] == "1",
282
      "anyoneCanStart": options[:anyone_can_start] == "1",
283
      "joinModerator": options[:all_join_moderator] == "1",
284
    }
285
286
    room_settings.to_json
287
  end
288
289
  def room_params
290
    params.require(:room).permit(:name, :auto_join, :mute_on_join, :access_code,
291
      :require_moderator_approval, :anyone_can_start, :all_join_moderator)
292
  end
293
294
  # Find the room from the uid.
295
  def find_room
296
    @room = Room.includes(:owner).find_by!(uid: params[:room_uid])
297
  end
298
299
  # Ensure the user either owns the room or is an admin of the room owner or the room is shared with him
300
  def verify_room_ownership_or_admin_or_shared
301
    return redirect_to root_path unless @room.owned_by?(current_user) ||
302
                                        room_shared_with_user ||
303
                                        current_user&.admin_of?(@room.owner)
304
  end
305
306
  # Ensure the user either owns the room or is an admin of the room owner
307
  def verify_room_ownership_or_admin
308
    return redirect_to root_path if [email protected]_by?(current_user) && !current_user&.admin_of?(@room.owner)
309
  end
310
311
  # Ensure the user owns the room or is allowed to start it
312
  def verify_room_ownership_or_shared
313
   return redirect_to root_path unless @room.owned_by?(current_user) || room_shared_with_user
314
  end
315
316
  def validate_accepted_terms
317
    redirect_to terms_path if current_user && !current_user&.accepted_terms
318
  end
319
320
  def validate_verified_email
321
    redirect_to account_activation_path(current_user) if current_user && !current_user&.activated?
322
  end
323
324
  def verify_room_owner_verified
325
    redirect_to root_path, alert: t("room.unavailable") unless @room.owner.activated?
326
  end
327
328
  # Check to make sure the room owner is not pending or banned
329
  def verify_room_owner_valid
330
    redirect_to root_path, alert: t("room.owner_banned") if @room.owner.has_role?(:pending) || @room.owner.has_role?(:denied)
331
  end
332
333
  def verify_user_not_admin
334
    redirect_to admins_path if current_user&.has_role?(:super_admin)
335
  end
336
337
  def auth_required
338
    @settings.get_value("Room Authentication") == "true" && current_user.nil?
339
  end
340
341
  # Checks if the room is shared with the user and room sharing is enabled
342
  def room_shared_with_user
343
    shared_access_allowed ? @room.shared_with?(current_user) : false
344
  end
345
346
  def room_limit_exceeded
347
    limit = @settings.get_value("Room Limit").to_i
348
349
    # Does not apply to admin or users that aren't signed in
350
    # 15+ option is used as unlimited
351
    return false if current_user&.has_role?(:admin) || limit == 15
352
353
    current_user.rooms.length >= limit
354
  end
355
  helper_method :room_limit_exceeded
356
end
357