Passed
Push — master ( beadd4...2775b1 )
by Jesus
05:37 queued 11s
created

ApplicationController.handle_readonly_error()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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