Completed
Branch v2.4-alpha (b4736b)
by Ahmad
05:54
created

ApplicationController.allow_greenlight_accounts?   B

Complexity

Conditions 7

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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