Completed
Branch more-refac (5b2fc3)
by Ahmad
03:46
created

RoomsController.room_params()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 1
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
24
  before_action :validate_accepted_terms, unless: -> { !Rails.configuration.terms }
25
  before_action :validate_verified_email, except: [:show, :join],
26
                unless: -> { !Rails.configuration.enable_email_verification }
27
  before_action :find_room, except: [:create, :join_specific_room]
28
  before_action :verify_room_ownership, only: [:destroy, :start, :update_settings]
29
  before_action :verify_room_owner_verified, only: [:show, :join],
30
                unless: -> { !Rails.configuration.enable_email_verification }
31
  before_action :verify_user_not_admin, only: [:show]
32
33
  # POST /
34
  def create
35
    # Return to root if user is not signed in
36
    return redirect_to root_path unless current_user
37
38
    # Check if the user has not exceeded the room limit
39
    return redirect_to current_user.main_room, flash: { alert: I18n.t("room.room_limit") } if room_limit_exceeded
40
41
    # Create room
42
    @room = Room.new(name: room_params[:name], access_code: room_params[:access_code])
43
    @room.owner = current_user
44
    @room.room_settings = create_room_settings_string(room_params)
45
46
    # Save the room and redirect if it fails
47
    return redirect_to current_user.main_room, flash: { alert: I18n.t("room.create_room_error") } unless @room.save
48
49
    logger.info "Support: #{current_user.email} has created a new room #{@room.uid}."
50
51
    # Redirect to room is auto join was not turned on
52
    return redirect_to @room,
53
      flash: { success: I18n.t("room.create_room_success") } unless room_params[:auto_join] == "1"
54
55
    # Start the room if auto join was turned on
56
    start
57
  end
58
59
  # GET /:room_uid
60
  def show
61
    @anyone_can_start = JSON.parse(@room[:room_settings])["anyoneCanStart"]
62
    @room_running = room_running?(@room.bbb_id)
63
64
    # If its the current user's room
65
    if current_user && @room.owned_by?(current_user)
66
      if current_user.highest_priority_role.can_create_rooms
67
        # User is allowed to have rooms
68
        @search, @order_column, @order_direction, recs =
69
          recordings(@room.bbb_id, params.permit(:search, :column, :direction), true)
70
71
        @pagy, @recordings = pagy_array(recs)
72
      else
73
        # Render view for users that cant create rooms
74
        @recent_rooms = Room.where(id: cookies.encrypted["#{current_user.uid}_recently_joined_rooms"])
75
        render :cant_create_rooms
76
      end
77
    else
78
      show_user_join
79
    end
80
  end
81
82
  # POST /:room_uid
83
  def join
84
    return redirect_to root_path,
85
      flash: { alert: I18n.t("administrator.site_settings.authentication.user-info") } if auth_required
86
87
    unless @room.owned_by?(current_user)
88
      # Don't allow users to join unless they have a valid access code or the room doesn't have an access code
89
      if @room.access_code && [email protected]_code.empty? && @room.access_code != session[:access_code]
90
        return redirect_to room_path(room_uid: params[:room_uid]), flash: { alert: I18n.t("room.access_code_required") }
91
      end
92
93
      # Join name not passed.
94
      return unless params[:join_name]
95
96
      # Assign join name if passed.
97
      @join_name = params[@room.invite_path][:join_name] if params[@room.invite_path]
98
    end
99
100
    # create or update cookie with join name
101
    cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name
102
103
    save_recent_rooms
104
105
    logger.info "Support: #{current_user.present? ? current_user.email : @join_name} is joining room #{@room.uid}"
106
    join_room(default_meeting_options)
107
  end
108
109
  # DELETE /:room_uid
110
  def destroy
111
    # Don't delete the users home room.
112
    if @room.owned_by?(current_user) && @room != current_user.main_room
113
      @room.destroy
114
      delete_all_recordings(@room.bbb_id)
115
    end
116
117
    redirect_to current_user.main_room
118
  end
119
120
  # POST /room/join
121
  def join_specific_room
122
    room_uid = params[:join_room][:url].split('/').last
123
124
    begin
125
      @room = Room.find_by!(uid: room_uid)
126
    rescue ActiveRecord::RecordNotFound
127
      return redirect_to current_user.main_room, alert: I18n.t("room.no_room.invalid_room_uid")
128
    end
129
130
    redirect_to room_path(@room)
131
  end
132
133
  # POST /:room_uid/start
134
  def start
135
    logger.info "Support: #{current_user.email} is starting room #{@room.uid}"
136
137
    # Join the user in and start the meeting.
138
    opts = default_meeting_options
139
    opts[:user_is_moderator] = true
140
141
    # Include the user's choices for the room settings
142
    room_settings = JSON.parse(@room[:room_settings])
143
    opts[:mute_on_start] = room_settings["muteOnStart"] if room_settings["muteOnStart"]
144
    opts[:require_moderator_approval] = room_settings["requireModeratorApproval"]
145
146
    begin
147
      redirect_to join_path(@room, current_user.name, opts, current_user.uid)
148
    rescue BigBlueButton::BigBlueButtonException => e
149
      logger.error("Support: #{@room.uid} start failed: #{e}")
150
151
      redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception"))
152
    end
153
154
    # Notify users that the room has started.
155
    # Delay 5 seconds to allow for server start, although the request will retry until it succeeds.
156
    NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room)
157
  end
158
159
  # POST /:room_uid/update_settings
160
  def update_settings
161
    begin
162
      raise "Room name can't be blank" if room_params[:name].empty?
163
      raise "Unauthorized Request" if [email protected]_by?(current_user) || @room == current_user.main_room
164
165
      # Update the rooms settings
166
      if room_params
167
        room_settings_string = create_room_settings_string(room_params)
168
        @room.update_attributes(room_settings: room_settings_string)
169
      end
170
171
      # Update the rooms name if it has been changed
172
      @room.update_attributes(name: params[:room_name] || room_params[:name]) if @room.name != room_params[:name]
173
      # Update the room's access code if it has changed
174
      @room.update_attributes(access_code: room_params[:access_code]) if @room.access_code != room_params[:access_code]
175
176
      flash[:success] = I18n.t("room.update_settings_success")
177
    rescue => e
178
      logger.error "Support: Error in updating room settings: #{e}"
179
      flash[:alert] = I18n.t("room.update_settings_error")
180
    end
181
182
    redirect_to room_path
183
  end
184
185
  # GET /:room_uid/logout
186
  def logout
187
    logger.info "Support: #{current_user.present? ? current_user.email : 'Guest'} has left room #{@room.uid}"
188
189
    # Redirect the correct page.
190
    redirect_to @room
191
  end
192
193
  # POST /:room_uid/login
194
  def login
195
    session[:access_code] = room_params[:access_code]
196
197
    flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code
198
199
    redirect_to room_path(@room.uid)
200
  end
201
202
  private
203
204
  def create_room_settings_string(options)
205
    room_settings = {}
206
    room_settings["muteOnStart"] = options[:mute_on_join] == "1"
207
208
    room_settings["requireModeratorApproval"] = options[:require_moderator_approval] == "1"
209
210
    room_settings["anyoneCanStart"] = options[:anyone_can_start] == "1"
211
212
    room_settings["joinModerator"] = options[:all_join_moderator] == "1"
213
214
    room_settings.to_json
215
  end
216
217
  def room_params
218
    params.require(:room).permit(:name, :auto_join, :mute_on_join, :access_code,
219
      :require_moderator_approval, :anyone_can_start, :all_join_moderator)
220
  end
221
222
  # Find the room from the uid.
223
  def find_room
224
    @room = Room.find_by!(uid: params[:room_uid])
225
  end
226
227
  # Ensure the user is logged into the room they are accessing.
228
  def verify_room_ownership
229
    return redirect_to root_path unless @room.owned_by?(current_user)
230
  end
231
232
  def validate_accepted_terms
233
    redirect_to terms_path if current_user && !current_user&.accepted_terms
234
  end
235
236
  def validate_verified_email
237
    redirect_to account_activation_path(current_user) if current_user && !current_user&.activated?
238
  end
239
240
  def verify_room_owner_verified
241
    flash[:alert] = t("room.unavailable")
242
    redirect_to root_path unless @room.owner.activated?
243
  end
244
245
  def verify_user_not_admin
246
    redirect_to admins_path if current_user&.has_role?(:super_admin)
247
  end
248
249
  def auth_required
250
    @settings.get_value("Room Authentication") == "true" && current_user.nil?
251
  end
252
253
  def room_limit_exceeded
254
    limit = @settings.get_value("Room Limit").to_i
255
256
    # Does not apply to admin or users that aren't signed in
257
    # 15+ option is used as unlimited
258
    return false if current_user&.has_role?(:admin) || limit == 15
259
260
    current_user.rooms.length >= limit
261
  end
262
  helper_method :room_limit_exceeded
263
end
264