Completed
Branch more-refac (5b2fc3)
by Ahmad
03:46
created

ApplicationController.check_provider_exists()   B

Complexity

Conditions 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 32
rs 8.1786
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.where(id: session[:user_id]).includes(:roles).first
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.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 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
    I18n.locale = locale.tr('-', '_') unless locale.nil?
103
  end
104
105
  # Checks to make sure that the admin has changed his password from the default
106
  def check_admin_password
107
    if current_user&.has_role?(:admin) && current_user.email == "[email protected]" &&
108
       current_user&.greenlight_account? && current_user&.authenticate(Rails.configuration.admin_password_default)
109
110
      flash.now[:alert] = I18n.t("default_admin",
111
        edit_link: edit_user_path(user_uid: current_user.uid) + "?setting=password").html_safe
112
    end
113
  end
114
115
  # Checks if the user is banned and logs him out if he is
116
  def check_user_role
117
    if current_user&.has_role? :denied
118
      session.delete(:user_id)
119
      redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
120
    elsif current_user&.has_role? :pending
121
      session.delete(:user_id)
122
      redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
123
    end
124
  end
125
126
  # Relative root helper (when deploying to subdirectory).
127
  def relative_root
128
    Rails.configuration.relative_url_root || ""
129
  end
130
  helper_method :relative_root
131
132
  # Determines if the BigBlueButton endpoint is configured (or set to default).
133
  def bigbluebutton_endpoint_default?
134
    return false if Rails.configuration.loadbalanced_configuration
135
    Rails.configuration.bigbluebutton_endpoint_default == Rails.configuration.bigbluebutton_endpoint
136
  end
137
  helper_method :bigbluebutton_endpoint_default?
138
139
  def allow_greenlight_accounts?
140
    return Rails.configuration.allow_user_signup unless Rails.configuration.loadbalanced_configuration
141
    return false unless @user_domain && !@user_domain.empty? && Rails.configuration.allow_user_signup
142
    return false if @user_domain == "greenlight"
143
    # Proceed with retrieving the provider info
144
    begin
145
      provider_info = retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
146
      provider_info['provider'] == 'greenlight'
147
    rescue => e
148
      logger.error "Error in checking if greenlight accounts are allowed: #{e}"
149
      false
150
    end
151
  end
152
  helper_method :allow_greenlight_accounts?
153
154
  # Determine if Greenlight is configured to allow user signups.
155
  def allow_user_signup?
156
    Rails.configuration.allow_user_signup
157
  end
158
  helper_method :allow_user_signup?
159
160
  # Gets all configured omniauth providers.
161
  def configured_providers
162
    Rails.configuration.providers.select do |provider|
163
      Rails.configuration.send("omniauth_#{provider}")
164
    end
165
  end
166
  helper_method :configured_providers
167
168
  # Parses the url for the user domain
169 View Code Duplication
  def parse_user_domain(hostname)
170
    return hostname.split('.').first if Rails.configuration.url_host.empty?
171
    Rails.configuration.url_host.split(',').each do |url_host|
172
      return hostname.chomp(url_host).chomp('.') if hostname.include?(url_host)
173
    end
174
    ''
175
  end
176
177
  # Include user domain in lograge logs
178
  def append_info_to_payload(payload)
179
    super
180
    payload[:host] = @user_domain
181
  end
182
183
  # Manually Handle BigBlueButton errors
184
  def handle_bigbluebutton_error
185
    render "errors/bigbluebutton_error"
186
  end
187
188
  # Manually deal with 401 errors
189
  rescue_from CanCan::AccessDenied do |_exception|
190
    render "errors/greenlight_error"
191
  end
192
193
  private
194
195
  def check_provider_exists
196
    # Checks to see if the user exists
197
    begin
198
      # Check if the session has already checked that the user exists
199
      # and return true if they did for this domain
200
      return if session[:provider_exists] == @user_domain
201
202
      retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
203
204
      # Add a session variable if the provider exists
205
      session[:provider_exists] = @user_domain
206
    rescue => e
207
      logger.error "Error in retrieve provider info: #{e}"
208
      # Use the default site settings
209
      @user_domain = "greenlight"
210
211
      if e.message.eql? "No user with that id exists"
212
        render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_not_found.message"),
213
          help: I18n.t("errors.not_found.user_not_found.help") }
214
      elsif e.message.eql? "Provider not included."
215
        render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_missing.message"),
216
          help: I18n.t("errors.not_found.user_missing.help") }
217
      elsif e.message.eql? "That user has no configured provider."
218
        render "errors/greenlight_error", locals: { status_code: 501,
219
          message: I18n.t("errors.no_provider.message"),
220
          help: I18n.t("errors.no_provider.help") }
221
      else
222
        render "errors/greenlight_error", locals: { status_code: 500, message: I18n.t("errors.internal.message"),
223
          help: I18n.t("errors.internal.help"), display_back: true }
224
      end
225
    end
226
  end
227
end
228