Passed
Push — master ( 453e6b...beadd4 )
by Jesus
04:22
created

ApplicationController.append_info_to_payload()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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