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

ApplicationController.check_admin_password()   A

Complexity

Conditions 5

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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