Passed
Push — master ( 670efe...d1e50f )
by Jesus
05:17
created

ApplicationController.maintenance_mode?   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 10
rs 9.9
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
require 'bigbluebutton_api'
20
21
class ApplicationController < ActionController::Base
22
  include ApplicationHelper
23
  include SessionsHelper
24
  include ThemingHelper
25
26
  # Force SSL for loadbalancer configurations.
27
  before_action :redirect_to_https
28
29
  before_action :set_user_domain
30
  before_action :maintenance_mode?
31
  before_action :migration_error?
32
  before_action :set_locale
33
  before_action :check_admin_password
34
  before_action :check_user_role
35
36
  # Manually handle BigBlueButton errors
37
  rescue_from BigBlueButton::BigBlueButtonException, with: :handle_bigbluebutton_error
38
39
  # Manually Handle errors when application is in readonly mode
40
  rescue_from ActiveRecord::ReadOnlyRecord, with: :handle_readonly_error
41
42
  protect_from_forgery with: :exception
43
44
  MEETING_NAME_LIMIT = 90
45
  USER_NAME_LIMIT = 32
46
47
  # Show an information page when migration fails and there is a version error.
48
  def migration_error?
49
    render :migration_error unless ENV["DB_MIGRATE_FAILED"].blank?
50
  end
51
52
  def maintenance_mode?
53
    if ENV["MAINTENANCE_MODE"] == "full"
54
      render "errors/greenlight_error", status: 503, formats: :html,
55
        locals: {
56
          status_code: 503,
57
          message: I18n.t("errors.maintenance.message"),
58
          help: I18n.t("errors.maintenance.help"),
59
        }
60
    end
61
  end
62
63
  # Sets the appropriate locale.
64
  def set_locale
65
    update_locale(current_user)
66
  end
67
68
  def update_locale(user)
69
    locale = if user && user.language != 'default'
70
      user.language
71
    else
72
      http_accept_language.language_region_compatible_from(I18n.available_locales)
73
    end
74
    I18n.locale = locale.tr('-', '_') unless locale.nil?
75
  end
76
77
  def meeting_name_limit
78
    MEETING_NAME_LIMIT
79
  end
80
  helper_method :meeting_name_limit
81
82
  def user_name_limit
83
    USER_NAME_LIMIT
84
  end
85
  helper_method :user_name_limit
86
87
  # Relative root helper (when deploying to subdirectory).
88
  def relative_root
89
    Rails.configuration.relative_url_root || ""
90
  end
91
  helper_method :relative_root
92
93
  # Determines if the BigBlueButton endpoint is configured (or set to default).
94
  def bigbluebutton_endpoint_default?
95
    return false if Rails.configuration.loadbalanced_configuration
96
    Rails.configuration.bigbluebutton_endpoint_default == Rails.configuration.bigbluebutton_endpoint
97
  end
98
  helper_method :bigbluebutton_endpoint_default?
99
100
  def recording_thumbnails?
101
    Rails.configuration.recording_thumbnails
102
  end
103
  helper_method :recording_thumbnails?
104
105
  def allow_greenlight_users?
106
    allow_greenlight_accounts?
107
  end
108
  helper_method :allow_greenlight_users?
109
110
  # Determines if a form field needs the is-invalid class.
111
  def form_is_invalid?(obj, key)
112
    'is-invalid' unless obj.errors.messages[key].empty?
113
  end
114
  helper_method :form_is_invalid?
115
116
  # Default, unconfigured meeting options.
117
  def default_meeting_options
118
    invite_msg = I18n.t("invite_message")
119
    {
120
      user_is_moderator: false,
121
      meeting_logout_url: request.base_url + logout_room_path(@room),
122
      meeting_recorded: true,
123
      moderator_message: "#{invite_msg}\n\n#{request.base_url + room_path(@room)}",
124
    }
125
  end
126
127
  # Manually deal with 401 errors
128
  rescue_from CanCan::AccessDenied do |_exception|
129
    render "errors/greenlight_error"
130
  end
131
132
  # Checks to make sure that the admin has changed his password from the default
133
  def check_admin_password
134
    if current_user&.has_role?(:admin) && current_user&.greenlight_account? &&
135
       current_user&.authenticate(Rails.configuration.admin_password_default)
136
137
      flash.now[:alert] = I18n.t("default_admin",
138
        edit_link: edit_user_path(user_uid: current_user.uid) + "?setting=password").html_safe
139
    end
140
  end
141
142
  def redirect_to_https
143
    if Rails.configuration.loadbalanced_configuration && request.headers["X-Forwarded-Proto"] == "http"
144
      redirect_to protocol: "https://"
145
    end
146
  end
147
148
  def set_user_domain
149
    if Rails.env.test? || !Rails.configuration.loadbalanced_configuration
150
      @user_domain = "greenlight"
151
    else
152
      @user_domain = parse_user_domain(request.host)
153
154
      # Checks to see if the user exists
155
      begin
156
        retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
157
      rescue => e
158
        # Use the default site settings
159
        @user_domain = "greenlight"
160
161
        if e.message.eql? "No user with that id exists"
162
          render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_not_found.message"),
163
            help: I18n.t("errors.not_found.user_not_found.help") }
164
        elsif e.message.eql? "Provider not included."
165
          render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_missing.message"),
166
            help: I18n.t("errors.not_found.user_missing.help") }
167
        elsif e.message.eql? "That user has no configured provider."
168
          render "errors/greenlight_error", locals: { status_code: 501,
169
            message: I18n.t("errors.no_provider.message"),
170
            help: I18n.t("errors.no_provider.help") }
171
        else
172
          render "errors/greenlight_error", locals: { status_code: 500, message: I18n.t("errors.internal.message"),
173
            help: I18n.t("errors.internal.help"), display_back: true }
174
        end
175
      end
176
    end
177
  end
178
  helper_method :set_user_domain
179
180
  # Checks if the user is banned and logs him out if he is
181
  def check_user_role
182
    if current_user&.has_role? :denied
183
      session.delete(:user_id)
184
      redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
185
    elsif current_user&.has_role? :pending
186
      session.delete(:user_id)
187
      redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
188
    end
189
  end
190
  helper_method :check_user_role
191
192
  # Manually Handle BigBlueButton errors
193
  def handle_bigbluebutton_error
194
    render "errors/bigbluebutton_error"
195
  end
196
197
  # Manually Handle errors when application is in readonly mode
198
  def handle_readonly_error
199
    flash.clear
200
    redirect_to request.referrer, flash: { alert: I18n.t("errors.maintenance.readonly") }
201
  end
202
end
203