Passed
Push — master ( 23b088...27bc68 )
by Ahmad
06:31
created

ApplicationController.can_edit_user?   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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