Completed
Branch master (ce2c9c)
by Jesus
04:32
created

ApplicationController.check_admin_password()   A

Complexity

Conditions 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
dl 0
loc 8
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
  before_action :migration_error?
27
  before_action :set_locale
28
  before_action :check_admin_password
29
  before_action :set_user_domain
30
  before_action :check_user_role
31
32
  # Manually handle BigBlueButton errors
33
  rescue_from BigBlueButton::BigBlueButtonException, with: :handle_bigbluebutton_error
34
35
  # Force SSL for loadbalancer configurations.
36
  before_action :redirect_to_https
37
38
  protect_from_forgery with: :exception
39
40
  MEETING_NAME_LIMIT = 90
41
  USER_NAME_LIMIT = 32
42
43
  # Show an information page when migration fails and there is a version error.
44
  def migration_error?
45
    render :migration_error unless ENV["DB_MIGRATE_FAILED"].blank?
46
  end
47
48
  # Sets the appropriate locale.
49
  def set_locale
50
    update_locale(current_user)
51
  end
52
53
  def update_locale(user)
54
    locale = if user && user.language != 'default'
55
      user.language
56
    else
57
      http_accept_language.language_region_compatible_from(I18n.available_locales)
58
    end
59
    I18n.locale = locale.tr('-', '_') unless locale.nil?
60
  end
61
62
  def meeting_name_limit
63
    MEETING_NAME_LIMIT
64
  end
65
  helper_method :meeting_name_limit
66
67
  def user_name_limit
68
    USER_NAME_LIMIT
69
  end
70
  helper_method :user_name_limit
71
72
  # Relative root helper (when deploying to subdirectory).
73
  def relative_root
74
    Rails.configuration.relative_url_root || ""
75
  end
76
  helper_method :relative_root
77
78
  # Determines if the BigBlueButton endpoint is configured (or set to default).
79
  def bigbluebutton_endpoint_default?
80
    return false if Rails.configuration.loadbalanced_configuration
81
    Rails.configuration.bigbluebutton_endpoint_default == Rails.configuration.bigbluebutton_endpoint
82
  end
83
  helper_method :bigbluebutton_endpoint_default?
84
85
  def recording_thumbnails?
86
    Rails.configuration.recording_thumbnails
87
  end
88
  helper_method :recording_thumbnails?
89
90
  def allow_greenlight_users?
91
    allow_greenlight_accounts?
92
  end
93
  helper_method :allow_greenlight_users?
94
95
  # Determines if a form field needs the is-invalid class.
96
  def form_is_invalid?(obj, key)
97
    'is-invalid' unless obj.errors.messages[key].empty?
98
  end
99
  helper_method :form_is_invalid?
100
101
  # Default, unconfigured meeting options.
102
  def default_meeting_options
103
    invite_msg = I18n.t("invite_message")
104
    {
105
      user_is_moderator: false,
106
      meeting_logout_url: request.base_url + logout_room_path(@room),
107
      meeting_recorded: true,
108
      moderator_message: "#{invite_msg}\n\n#{request.base_url + room_path(@room)}",
109
    }
110
  end
111
112
  # Manually deal with 401 errors
113
  rescue_from CanCan::AccessDenied do |_exception|
114
    render "errors/not_found"
115
  end
116
117
  # Checks to make sure that the admin has changed his password from the default
118
  def check_admin_password
119
    if current_user&.has_role?(:admin) && current_user&.greenlight_account? &&
120
       current_user&.authenticate(Rails.configuration.admin_password_default)
121
122
      flash.now[:alert] = I18n.t("default_admin",
123
        edit_link: edit_user_path(user_uid: current_user.uid) + "?setting=password").html_safe
124
    end
125
  end
126
127
  def redirect_to_https
128
    if Rails.configuration.loadbalanced_configuration && request.headers["X-Forwarded-Proto"] == "http"
129
      redirect_to protocol: "https://"
130
    end
131
  end
132
133
  def set_user_domain
134
    @user_domain = if Rails.env.test? || !Rails.configuration.loadbalanced_configuration
135
      "greenlight"
136
    else
137
      parse_user_domain(request.host)
138
    end
139
  end
140
  helper_method :set_user_domain
141
142
  # Checks if the user is banned and logs him out if he is
143
  def check_user_role
144
    if current_user&.has_role? :denied
145
      session.delete(:user_id)
146
      redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") }
147
    elsif current_user&.has_role? :pending
148
      session.delete(:user_id)
149
      redirect_to root_path, flash: { alert: I18n.t("registration.approval.fail") }
150
    end
151
  end
152
  helper_method :check_user_role
153
154
  # Manually Handle BigBlueButton errors
155
  def handle_bigbluebutton_error
156
    render "errors/bigbluebutton_error"
157
  end
158
end
159