GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#912)
by Ahmad
05:05
created

RoomsController   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 307
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
dl 0
loc 307
rs 3.2
c 4
b 2
f 0
wmc 65

13 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 24 5
B show() 0 30 5
A room_params() 0 4 1
A room_limit_exceeded() 0 9 3
A verify_user_not_admin() 0 3 2
A remove_shared_access() 0 11 2
A login() 0 7 2
A shared_users() 0 6 1
A destroy() 0 6 3
A shared_access() 0 26 3
A verify_room_ownership_or_shared() 0 3 3
A auth_required() 0 3 1
A join_specific_room() 0 11 2

How to fix   Complexity   

Complex Class

Complex classes like RoomsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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, :update_settings, :shared_access]
29
  before_action :verify_room_owner_verified, only: [:show, :join],
30
                unless: -> { !Rails.configuration.enable_email_verification }
31
  before_action :verify_room_ownership_or_shared, only: [:start, :remove_shared_access]
32
  before_action :verify_user_not_admin, only: [:show]
33
34
  # POST /
35
  def create
36
    # Return to root if user is not signed in
37
    return redirect_to root_path unless current_user
38
39
    # Check if the user has not exceeded the room limit
40
    return redirect_to current_user.main_room, flash: { alert: I18n.t("room.room_limit") } if room_limit_exceeded
41
42
    # Create room
43
    @room = Room.new(name: room_params[:name], access_code: room_params[:access_code])
44
    @room.owner = current_user
45
    @room.room_settings = create_room_settings_string(room_params)
46
47
    # Save the room and redirect if it fails
48
    return redirect_to current_user.main_room, flash: { alert: I18n.t("room.create_room_error") } unless @room.save
49
50
    logger.info "Support: #{current_user.email} has created a new room #{@room.uid}."
51
52
    # Redirect to room is auto join was not turned on
53
    return redirect_to @room,
54
      flash: { success: I18n.t("room.create_room_success") } unless room_params[:auto_join] == "1"
55
56
    # Start the room if auto join was turned on
57
    start
58
  end
59
60
  # GET /:room_uid
61
  def show
62
    @anyone_can_start = JSON.parse(@room[:room_settings])["anyoneCanStart"]
63
    @room_running = room_running?(@room.bbb_id)
64
    @shared_room = @room.shared_with?(current_user)
65
66
    # If its the current user's room
67
    if current_user && (@room.owned_by?(current_user) || @shared_room)
68
      if current_user.highest_priority_role.get_permission("can_create_rooms")
69
        # User is allowed to have rooms
70
        @search, @order_column, @order_direction, recs =
71
          recordings(@room.bbb_id, params.permit(:search, :column, :direction), true)
72
73
        @user_list = if Rails.configuration.loadbalanced_configuration
74
          User.where.not(uid: current_user.uid).where(provider: @user_domain).without_role(:super_admin)
75
        else
76
          User.where.not(uid: current_user.uid)
77
        end
78
79
        @pagy, @recordings = pagy_array(recs)
80
      else
81
        # Render view for users that cant create rooms
82
        @recent_rooms = Room.where(id: cookies.encrypted["#{current_user.uid}_recently_joined_rooms"])
83
        render :cant_create_rooms
84
      end
85
    else
86
      return redirect_to root_path, flash: { alert: I18n.t("room.invalid_provider") } if incorrect_user_domain
87
88
      show_user_join
89
    end
90
  end
91
92
  # POST /:room_uid
93
  def join
94
    return redirect_to root_path,
95
      flash: { alert: I18n.t("administrator.site_settings.authentication.user-info") } if auth_required
96
97
    unless @room.owned_by?(current_user)
98
      # Don't allow users to join unless they have a valid access code or the room doesn't have an access code
99
      if @room.access_code && [email protected]_code.empty? && @room.access_code != session[:access_code]
100
        return redirect_to room_path(room_uid: params[:room_uid]), flash: { alert: I18n.t("room.access_code_required") }
101
      end
102
103
      # Assign join name if passed.
104
      if params[@room.invite_path]
105
        @join_name = params[@room.invite_path][:join_name]
106
      elsif !params[:join_name]
107
        # Join name not passed.
108
        return redirect_to root_path
109
      end
110
    end
111
112
    # create or update cookie with join name
113
    cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name
114
115
    save_recent_rooms
116
117
    logger.info "Support: #{current_user.present? ? current_user.email : @join_name} is joining room #{@room.uid}"
118
    join_room(default_meeting_options)
119
  end
120
121
  # DELETE /:room_uid
122
  def destroy
123
    # Don't delete the users home room.
124
    @room.destroy if @room.owned_by?(current_user) && @room != current_user.main_room
125
126
    redirect_to current_user.main_room
127
  end
128
129
  # POST /room/join
130
  def join_specific_room
131
    room_uid = params[:join_room][:url].split('/').last
132
133
    begin
134
      @room = Room.find_by!(uid: room_uid)
135
    rescue ActiveRecord::RecordNotFound
136
      return redirect_to current_user.main_room, alert: I18n.t("room.no_room.invalid_room_uid")
137
    end
138
139
    redirect_to room_path(@room)
140
  end
141
142
  # POST /:room_uid/start
143
  def start
144
    logger.info "Support: #{current_user.email} is starting room #{@room.uid}"
145
146
    # Join the user in and start the meeting.
147
    opts = default_meeting_options
148
    opts[:user_is_moderator] = true
149
150
    # Include the user's choices for the room settings
151
    room_settings = JSON.parse(@room[:room_settings])
152
    opts[:mute_on_start] = room_settings["muteOnStart"]
153
    opts[:require_moderator_approval] = room_settings["requireModeratorApproval"]
154
155
    begin
156
      redirect_to join_path(@room, current_user.name, opts, current_user.uid)
157
    rescue BigBlueButton::BigBlueButtonException => e
158
      logger.error("Support: #{@room.uid} start failed: #{e}")
159
160
      redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception"))
161
    end
162
163
    # Notify users that the room has started.
164
    # Delay 5 seconds to allow for server start, although the request will retry until it succeeds.
165
    NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room)
166
  end
167
168
  # POST /:room_uid/update_settings
169
  def update_settings
170
    begin
171
      options = params[:room].nil? ? params : params[:room]
172
      raise "Room name can't be blank" if options[:name].blank?
173
      raise "Unauthorized Request" if [email protected]_by?(current_user) || @room == current_user.main_room
174
175
      # Update the rooms values
176
      room_settings_string = create_room_settings_string(options)
177
178
      @room.update_attributes(
179
        name: options[:name],
180
        room_settings: room_settings_string,
181
        access_code: options[:access_code]
182
      )
183
184
      flash[:success] = I18n.t("room.update_settings_success")
185
    rescue => e
186
      logger.error "Support: Error in updating room settings: #{e}"
187
      flash[:alert] = I18n.t("room.update_settings_error")
188
    end
189
190
    redirect_to room_path
191
  end
192
193
  # POST /:room_uid/update_shared_access
194
  def shared_access
195
    begin
196
      current_list = @room.shared_users.pluck(:id)
197
      new_list = User.where(uid: params[:add]).pluck(:id)
198
199
      # Get the list of users that used to be in the list but were removed
200
      users_to_remove = current_list - new_list
201
      # Get the list of users that are in the new list but not in the current list
202
      users_to_add = new_list - current_list
203
204
      # Remove users that are removed
205
      SharedAccess.where(room_id: @room.id, user_id: users_to_remove).delete_all unless users_to_remove.empty?
206
207
      # Add users that are added
208
      users_to_add.each do |id|
209
        SharedAccess.create(room_id: @room.id, user_id: id)
210
      end
211
212
      flash[:success] = I18n.t("room.shared_access_success")
213
    rescue => e
214
      logger.error "Support: Error in updating room shared access: #{e}"
215
      flash[:alert] = I18n.t("room.shared_access_error")
216
    end
217
218
    redirect_to room_path
219
  end
220
221
  # POST /:room_uid/remove_shared_access
222
  def remove_shared_access
223
    begin
224
      SharedAccess.find_by!(room_id: @room.id, user_id: params[:user_id]).destroy
225
      flash[:success] = I18n.t("room.remove_shared_access_success")
226
    rescue => e
227
      logger.error "Support: Error in removing room shared access: #{e}"
228
      flash[:alert] = I18n.t("room.remove_shared_access_error")
229
    end
230
231
    redirect_to current_user.main_room
232
  end
233
234
  # GET /:room_uid/shared_users
235
  def shared_users
236
    # Respond with JSON object of users that have access to the room
237
    respond_to do |format|
238
      format.json { render body: @room.shared_users.to_json }
239
    end
240
  end
241
242
  # GET /:room_uid/logout
243
  def logout
244
    logger.info "Support: #{current_user.present? ? current_user.email : 'Guest'} has left room #{@room.uid}"
245
246
    # Redirect the correct page.
247
    redirect_to @room
248
  end
249
250
  # POST /:room_uid/login
251
  def login
252
    session[:access_code] = room_params[:access_code]
253
254
    flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code
255
256
    redirect_to room_path(@room.uid)
257
  end
258
259
  private
260
261
  def create_room_settings_string(options)
262
    room_settings = {
263
      "muteOnStart": options[:mute_on_join] == "1",
264
      "requireModeratorApproval": options[:require_moderator_approval] == "1",
265
      "anyoneCanStart": options[:anyone_can_start] == "1",
266
      "joinModerator": options[:all_join_moderator] == "1",
267
    }
268
269
    room_settings.to_json
270
  end
271
272
  def room_params
273
    params.require(:room).permit(:name, :auto_join, :mute_on_join, :access_code,
274
      :require_moderator_approval, :anyone_can_start, :all_join_moderator)
275
  end
276
277
  # Find the room from the uid.
278
  def find_room
279
    @room = Room.find_by!(uid: params[:room_uid])
280
  end
281
282
  # Ensure the user is logged into the room they are accessing.
283
  def verify_room_ownership
284
    return redirect_to root_path unless @room.owned_by?(current_user)
285
  end
286
287
  # Ensure the user owns the room or is allowed to start it
288
  def verify_room_ownership_or_shared
289
   return redirect_to root_path unless @room.owned_by?(current_user) || @room.shared_with?(current_user)
290
  end
291
292
  def validate_accepted_terms
293
    redirect_to terms_path if current_user && !current_user&.accepted_terms
294
  end
295
296
  def validate_verified_email
297
    redirect_to account_activation_path(current_user) if current_user && !current_user&.activated?
298
  end
299
300
  def verify_room_owner_verified
301
    unless @room.owner.activated?
302
      flash[:alert] = t("room.unavailable")
303
      redirect_to root_path
304
    end
305
  end
306
307
  def verify_user_not_admin
308
    redirect_to admins_path if current_user&.has_role?(:super_admin)
309
  end
310
311
  def auth_required
312
    @settings.get_value("Room Authentication") == "true" && current_user.nil?
313
  end
314
315
  def room_limit_exceeded
316
    limit = @settings.get_value("Room Limit").to_i
317
318
    # Does not apply to admin or users that aren't signed in
319
    # 15+ option is used as unlimited
320
    return false if current_user&.has_role?(:admin) || limit == 15
321
322
    current_user.rooms.length >= limit
323
  end
324
  helper_method :room_limit_exceeded
325
end
326