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.
Passed
Pull Request — v2.4-alpha (#765)
by Ahmad
04:48
created

RoomsController.join_room()   B

Complexity

Conditions 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 27
rs 8.7653
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
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, except: [:create, :show, :join, :logout, :login, :join_specific_room]
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
  # PATCH /:room_uid
83
  def update
84
    update_room_attributes("name")
85
86
    redirect_back fallback_location: room_path(@room)
87
  end
88
89
  # POST /:room_uid
90
  def join
91
    return redirect_to root_path,
92
      flash: { alert: I18n.t("administrator.site_settings.authentication.user-info") } if auth_required
93
94
    unless @room.owned_by?(current_user)
95
      # Don't allow users to join unless they have a valid access code or the room doesn't have an access code
96
      if @room.access_code && [email protected]_code.empty? && @room.access_code != session[:access_code]
97
        return redirect_to room_path(room_uid: params[:room_uid]), flash: { alert: I18n.t("room.access_code_required") }
98
      end
99
100
      # Join name not passed.
101
      return unless params[:join_name]
102
103
      # Assign join name if passed.
104
      @join_name = params[@room.invite_path][:join_name] if params[@room.invite_path]
105
    end
106
107
    # create or update cookie with join name
108
    cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name
109
110
    save_recent_rooms
111
112
    logger.info "Support: #{current_user.present? ? current_user.email : @join_name} is joining room #{@room.uid}"
113
    join_room(default_meeting_options)
114
  end
115
116
  # DELETE /:room_uid
117
  def destroy
118
    # Don't delete the users home room.
119
    if @room.owned_by?(current_user) && @room != current_user.main_room
120
      @room.destroy
121
      delete_all_recordings(@room.bbb_id)
122
    end
123
124
    redirect_to current_user.main_room
125
  end
126
127
  # POST /room/join
128
  def join_specific_room
129
    room_uid = params[:join_room][:url].split('/').last
130
131
    begin
132
      @room = Room.find_by!(uid: room_uid)
133
    rescue ActiveRecord::RecordNotFound
134
      return redirect_to current_user.main_room, alert: I18n.t("room.no_room.invalid_room_uid")
135
    end
136
137
    redirect_to room_path(@room)
138
  end
139
140
  # POST /:room_uid/start
141
  def start
142
    logger.info "Support: #{current_user.email} is starting room #{@room.uid}"
143
144
    # Join the user in and start the meeting.
145
    opts = default_meeting_options
146
    opts[:user_is_moderator] = true
147
148
    # Include the user's choices for the room settings
149
    room_settings = JSON.parse(@room[:room_settings])
150
    opts[:mute_on_start] = room_settings["muteOnStart"] if room_settings["muteOnStart"]
151
    opts[:require_moderator_approval] = room_settings["requireModeratorApproval"]
152
153
    begin
154
      redirect_to join_path(@room, current_user.name, opts, current_user.uid)
155
    rescue BigBlueButton::BigBlueButtonException => e
156
      logger.error("Support: #{@room.uid} start failed: #{e}")
157
158
      redirect_to room_path, alert: I18n.t(e.key.to_s.underscore, default: I18n.t("bigbluebutton_exception"))
159
    end
160
161
    # Notify users that the room has started.
162
    # Delay 5 seconds to allow for server start, although the request will retry until it succeeds.
163
    NotifyUserWaitingJob.set(wait: 5.seconds).perform_later(@room)
164
  end
165
166
  # POST /:room_uid/update_settings
167
  def update_settings
168
    begin
169
      raise "Room name can't be blank" if room_params[:name].empty?
170
171
      @room = Room.find_by!(uid: params[:room_uid])
172
      # Update the rooms settings
173
      update_room_attributes("settings")
174
      # Update the rooms name if it has been changed
175
      update_room_attributes("name") if @room.name != room_params[:name]
176
      # Update the room's access code if it has changed
177
      update_room_attributes("access_code") if @room.access_code != room_params[:access_code]
178
179
      flash[:success] = I18n.t("room.update_settings_success")
180
    rescue => e
181
      logger.error "Support: Error in updating room settings: #{e}"
182
      flash[:alert] = I18n.t("room.update_settings_error")
183
    end
184
185
    redirect_to room_path
186
  end
187
188
  # GET /:room_uid/logout
189
  def logout
190
    logger.info "Support: #{current_user.present? ? current_user.email : 'Guest'} has left room #{@room.uid}"
191
192
    # Redirect the correct page.
193
    redirect_to @room
194
  end
195
196
  # POST /:room_uid/login
197
  def login
198
    session[:access_code] = room_params[:access_code]
199
200
    flash[:alert] = I18n.t("room.access_code_required") if session[:access_code] != @room.access_code
201
202
    redirect_to room_path(@room.uid)
203
  end
204
205
  private
206
207
  def update_room_attributes(update_type)
208
    if @room.owned_by?(current_user) && @room != current_user.main_room
209
      if update_type.eql? "name"
210
        @room.update_attributes(name: params[:room_name] || room_params[:name])
211
      elsif update_type.eql? "settings"
212
        room_settings_string = create_room_settings_string(room_params)
213
        @room.update_attributes(room_settings: room_settings_string)
214
      elsif update_type.eql? "access_code"
215
        @room.update_attributes(access_code: room_params[:access_code])
216
      end
217
    end
218
  end
219
220
  def create_room_settings_string(options)
221
    room_settings = {}
222
    room_settings["muteOnStart"] = options[:mute_on_join] == "1"
223
224
    room_settings["requireModeratorApproval"] = options[:require_moderator_approval] == "1"
225
226
    room_settings["anyoneCanStart"] = options[:anyone_can_start] == "1"
227
228
    room_settings["joinModerator"] = options[:all_join_moderator] == "1"
229
230
    room_settings.to_json
231
  end
232
233
  def room_params
234
    params.require(:room).permit(:name, :auto_join, :mute_on_join, :access_code,
235
      :require_moderator_approval, :anyone_can_start, :all_join_moderator)
236
  end
237
238
  # Find the room from the uid.
239
  def find_room
240
    @room = Room.find_by!(uid: params[:room_uid])
241
  end
242
243
  # Ensure the user is logged into the room they are accessing.
244
  def verify_room_ownership
245
    unless @room.owned_by?(current_user)
246
      # Redirect authenticated users to their room.
247
      return redirect_to room_path(current_user.main_room) if current_user
248
249
      # Redirect unauthenticated users to root.
250
      redirect_to root_path
251
    end
252
  end
253
254
  def validate_accepted_terms
255
    redirect_to terms_path if current_user && !current_user&.accepted_terms
256
  end
257
258
  def validate_verified_email
259
    redirect_to account_activation_path(current_user) if current_user && !current_user&.activated?
260
  end
261
262
  def verify_room_owner_verified
263
    unless @room.owner.activated?
264
      flash[:alert] = t("room.unavailable")
265
266
      return redirect_to current_user.main_room if current_user && [email protected]_by?(current_user)
267
268
      redirect_to root_path
269
270
    end
271
  end
272
273
  def verify_user_not_admin
274
    redirect_to admins_path if current_user&.has_role?(:super_admin)
275
  end
276
277
  def auth_required
278
    @settings.get_value("Room Authentication") == "true" && current_user.nil?
279
  end
280
281
  def room_limit_exceeded
282
    limit = @settings.get_value("Room Limit").to_i
283
284
    # Does not apply to admin or users that aren't signed in
285
    # 15+ option is used as unlimited
286
    return false if current_user&.has_role?(:admin) || limit == 15
287
288
    current_user.rooms.length >= limit
289
  end
290
  helper_method :room_limit_exceeded
291
end
292