Passed
Push — master ( 4fc171...e42d2d )
by Jesus
03:46
created

ApplicationController   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 195
rs 9.1199
wmc 41

7 Methods

Rating   Name   Duplication   Size   Complexity  
A maintenance_mode? 0 10 2
A append_info_to_payload() 0 4 1
A set_locale() 0 3 1
A check_admin_password() 0 8 5
A handle_bigbluebutton_error() 0 3 1
B check_provider_exists() 0 31 6
A check_user_role() 0 9 3

How to fix   Complexity   

Complex Class

Complex classes like ApplicationController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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_role?(:admin) && current_user.email == "[email protected]" &&
141
       current_user&.greenlight_account? && 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
      check_provider_exists
161
    end
162
  end
163
  helper_method :set_user_domain
164
165
  # Checks if the user is banned and logs him out if he is
166
  def check_user_role
167
    if current_user&.has_role? :denied
168
      session.delete(:user_id)
169
      redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
170
    elsif current_user&.has_role? :pending
171
      session.delete(:user_id)
172
      redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
173
    end
174
  end
175
  helper_method :check_user_role
176
177
  # Manually Handle BigBlueButton errors
178
  def handle_bigbluebutton_error
179
    render "errors/bigbluebutton_error"
180
  end
181
182
  private
183
184
  def check_provider_exists
185
    # Checks to see if the user exists
186
    begin
187
      # Check if the session has already checked that the user exists
188
      # and return true if they did for this domain
189
      return if session[:provider_exists] == @user_domain
190
191
      retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
192
193
      # Add a session variable if the provider exists
194
      session[:provider_exists] = @user_domain
195
    rescue => e
196
      # Use the default site settings
197
      @user_domain = "greenlight"
198
199
      if e.message.eql? "No user with that id exists"
200
        render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_not_found.message"),
201
          help: I18n.t("errors.not_found.user_not_found.help") }
202
      elsif e.message.eql? "Provider not included."
203
        render "errors/greenlight_error", locals: { message: I18n.t("errors.not_found.user_missing.message"),
204
          help: I18n.t("errors.not_found.user_missing.help") }
205
      elsif e.message.eql? "That user has no configured provider."
206
        render "errors/greenlight_error", locals: { status_code: 501,
207
          message: I18n.t("errors.no_provider.message"),
208
          help: I18n.t("errors.no_provider.help") }
209
      else
210
        render "errors/greenlight_error", locals: { status_code: 500, message: I18n.t("errors.internal.message"),
211
          help: I18n.t("errors.internal.help"), display_back: true }
212
      end
213
    end
214
  end
215
end
216