Passed
Push — master ( 71f35c...b1a999 )
by Ahmad
06:52 queued 10s
created

ApplicationController.handle_bigbluebutton_error()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
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 ApplicationController < ActionController::Base
20
  include BbbServer
21
22
  before_action :redirect_to_https, :set_user_domain, :set_user_settings, :maintenance_mode?, :migration_error?,
23
    :user_locale, :check_admin_password, :check_user_role
24
25
  protect_from_forgery with: :exceptions
26
27
  # Retrieves the current user.
28
  def current_user
29
    @current_user ||= User.includes(:roles, :main_room).find_by(id: session[:user_id])
30
31
    if Rails.configuration.loadbalanced_configuration
32
      if @current_user && !@current_user.has_role?(:super_admin) &&
33
         @current_user.provider != @user_domain
34
        @current_user = nil
35
        session.clear
36
      end
37
    end
38
39
    @current_user
40
  end
41
  helper_method :current_user
42
43
  def bbb_server
44
    @bbb_server ||= Rails.configuration.loadbalanced_configuration ? bbb(@user_domain) : bbb("greenlight")
45
  end
46
47
  # Force SSL
48
  def redirect_to_https
49
    if Rails.configuration.loadbalanced_configuration && request.headers["X-Forwarded-Proto"] == "http"
50
      redirect_to protocol: "https://"
51
    end
52
  end
53
54
  # Sets the user domain variable
55
  def set_user_domain
56
    if Rails.env.test? || !Rails.configuration.loadbalanced_configuration
57
      @user_domain = "greenlight"
58
    else
59
      @user_domain = parse_user_domain(request.host)
60
61
      check_provider_exists
62
    end
63
  end
64
65
  # Sets the settinfs variable
66
  def set_user_settings
67
    @settings = Setting.includes(:features).find_or_create_by(provider: @user_domain)
68
  end
69
70
  # Redirects the user to a Maintenance page if turned on
71
  def maintenance_mode?
72
    if ENV["MAINTENANCE_MODE"] == "true"
73
      render "errors/greenlight_error", status: 503, formats: :html,
74
        locals: {
75
          status_code: 503,
76
          message: I18n.t("errors.maintenance.message"),
77
          help: I18n.t("errors.maintenance.help"),
78
        }
79
    end
80
    if Rails.configuration.maintenance_window.present?
81
      unless cookies[:maintenance_window] == Rails.configuration.maintenance_window
82
        flash.now[:maintenance] = Rails.configuration.maintenance_window
83
      end
84
    end
85
  end
86
87
  # Show an information page when migration fails and there is a version error.
88
  def migration_error?
89
    render :migration_error, status: 500 unless ENV["DB_MIGRATE_FAILED"].blank?
90
  end
91
92
  # Sets the appropriate locale.
93
  def user_locale(user = current_user)
94
    locale = if user && user.language != 'default'
95
      user.language
96
    else
97
      http_accept_language.language_region_compatible_from(I18n.available_locales)
98
    end
99
100
    begin
101
      I18n.locale = locale.tr('-', '_') unless locale.nil?
102
    rescue
103
      # Default to English if there are any issues in language
104
      logger.error("Support: User locale is not supported (#{locale}")
105
      I18n.locale = "en"
106
    end
107
  end
108
109
  # Checks to make sure that the admin has changed his password from the default
110
  def check_admin_password
111
    if current_user&.has_role?(:admin) && current_user.email == "[email protected]" &&
112
       current_user&.greenlight_account? && current_user&.authenticate(Rails.configuration.admin_password_default)
113
114
      flash.now[:alert] = I18n.t("default_admin",
115
        edit_link: edit_user_path(user_uid: current_user.uid) + "?setting=password").html_safe
116
    end
117
  end
118
119
  # Checks if the user is banned and logs him out if he is
120
  def check_user_role
121
    if current_user&.has_role? :denied
122
      session.delete(:user_id)
123
      redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
124
    elsif current_user&.has_role? :pending
125
      session.delete(:user_id)
126
      redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
127
    end
128
  end
129
130
  # Relative root helper (when deploying to subdirectory).
131
  def relative_root
132
    Rails.configuration.relative_url_root || ""
133
  end
134
  helper_method :relative_root
135
136
  # Determines if the BigBlueButton endpoint is configured (or set to default).
137
  def bigbluebutton_endpoint_default?
138
    return false if Rails.configuration.loadbalanced_configuration
139
    Rails.configuration.bigbluebutton_endpoint_default == Rails.configuration.bigbluebutton_endpoint
140
  end
141
  helper_method :bigbluebutton_endpoint_default?
142
143
  def allow_greenlight_accounts?
144
    return Rails.configuration.allow_user_signup unless Rails.configuration.loadbalanced_configuration
145
    return false unless @user_domain && !@user_domain.empty? && Rails.configuration.allow_user_signup
146
    return false if @user_domain == "greenlight"
147
    # Proceed with retrieving the provider info
148
    begin
149
      provider_info = retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
150
      provider_info['provider'] == 'greenlight'
151
    rescue => e
152
      logger.error "Error in checking if greenlight accounts are allowed: #{e}"
153
      false
154
    end
155
  end
156
  helper_method :allow_greenlight_accounts?
157
158
  # Determine if Greenlight is configured to allow user signups.
159
  def allow_user_signup?
160
    Rails.configuration.allow_user_signup
161
  end
162
  helper_method :allow_user_signup?
163
164
  # Gets all configured omniauth providers.
165
  def configured_providers
166
    Rails.configuration.providers.select do |provider|
167
      Rails.configuration.send("omniauth_#{provider}")
168
    end
169
  end
170
  helper_method :configured_providers
171
172
  # Indicates whether users are allowed to share rooms
173
  def shared_access_allowed
174
    @settings.get_value("Shared Access") == "true"
175
  end
176
  helper_method :shared_access_allowed
177
178
  # Parses the url for the user domain
179 View Code Duplication
  def parse_user_domain(hostname)
180
    return hostname.split('.').first if Rails.configuration.url_host.empty?
181
    Rails.configuration.url_host.split(',').each do |url_host|
182
      return hostname.chomp(url_host).chomp('.') if hostname.include?(url_host)
183
    end
184
    ''
185
  end
186
187
  # Include user domain in lograge logs
188
  def append_info_to_payload(payload)
189
    super
190
    payload[:host] = @user_domain
191
  end
192
193
  # Manually handle BigBlueButton errors
194
  rescue_from BigBlueButton::BigBlueButtonException do |ex|
195
    logger.error "BigBlueButtonException: #{ex}"
196
    render "errors/bigbluebutton_error"
197
  end
198
199
  # Manually deal with 401 errors
200
  rescue_from CanCan::AccessDenied do |_exception|
201
    if current_user
202
      render "errors/greenlight_error"
203
    else
204
      # Store the current url as a cookie to redirect to after sigining in
205
      cookies[:return_to] = request.url
206
207
      # Get the correct signin path
208
      path = if allow_greenlight_accounts?
209
        signin_path
210
      elsif Rails.configuration.loadbalanced_configuration
211
        omniauth_login_url(:bn_launcher)
212
      else
213
        signin_path
214
      end
215
216
      redirect_to path
217
    end
218
  end
219
220
  private
221
222
  def check_provider_exists
223
    # Checks to see if the user exists
224
    begin
225
      # Check if the session has already checked that the user exists
226
      # and return true if they did for this domain
227
      return if session[:provider_exists] == @user_domain
228
229
      retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
230
231
      # Add a session variable if the provider exists
232
      session[:provider_exists] = @user_domain
233
    rescue => e
234
      logger.error "Error in retrieve provider info: #{e}"
235
      # Use the default site settings
236
      @user_domain = "greenlight"
237
      @settings = Setting.find_or_create_by(provider: @user_domain)
238
239
      if e.message.eql? "No user with that id exists"
240
        render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_not_found.message"),
241
          help: I18n.t("errors.not_found.user_not_found.help") }
242
      elsif e.message.eql? "Provider not included."
243
        render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_missing.message"),
244
          help: I18n.t("errors.not_found.user_missing.help") }
245
      elsif e.message.eql? "That user has no configured provider."
246
        render "errors/greenlight_error", locals: { status_code: 501,
247
          message: I18n.t("errors.no_provider.message"),
248
          help: I18n.t("errors.no_provider.help") }
249
      else
250
        render "errors/greenlight_error", locals: { status_code: 500, message: I18n.t("errors.internal.message"),
251
          help: I18n.t("errors.internal.help"), display_back: true }
252
      end
253
    end
254
  end
255
end
256