Passed
Push — master ( 921f25...cdf465 )
by Jesus
07:01
created

AdminsController.reset()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 7
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
class AdminsController < ApplicationController
20
  include Pagy::Backend
21
  include Themer
22
  include Emailer
23
  include Recorder
24
25
  manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve, :reset]
26
  site_settings = [:branding, :coloring, :coloring_lighten, :coloring_darken,
27
                   :registration_method, :room_authentication, :room_limit, :default_recording_visibility]
28
29
  authorize_resource class: false
30
  before_action :find_user, only: manage_users
31
  before_action :verify_admin_of_user, only: manage_users
32
  before_action :find_setting, only: site_settings
33
34
  # GET /admins
35
  def index
36
    @search = params[:search] || ""
37
    @order_column = params[:column] && params[:direction] != "none" ? params[:column] : "created_at"
38
    @order_direction = params[:direction] && params[:direction] != "none" ? params[:direction] : "DESC"
39
40
    @role = params[:role] ? Role.find_by(name: params[:role], provider: @user_domain) : nil
41
42
    @pagy, @users = pagy(user_list)
43
  end
44
45
  # GET /admins/site_settings
46
  def site_settings
47
  end
48
49
  # GET /admins/server_recordings
50
  def server_recordings
51
    server_rooms = if Rails.configuration.loadbalanced_configuration
52
      Room.includes(:owner).where(users: { provider: user_settings_provider }).pluck(:bbb_id)
53
    else
54
      Room.pluck(:bbb_id)
55
    end
56
57
    @search, @order_column, @order_direction, recs =
58
      all_recordings(server_rooms, @user_domain, params.permit(:search, :column, :direction), true, true)
59
    @pagy, @recordings = pagy_array(recs)
60
  end
61
62
  # MANAGE USERS
63
64
  # GET /admins/edit/:user_uid
65
  def edit_user
66
  end
67
68
  # POST /admins/ban/:user_uid
69
  def ban_user
70
    @user.roles = []
71
    @user.add_role :denied
72
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.banned") }
73
  end
74
75
  # POST /admins/unban/:user_uid
76
  def unban_user
77
    @user.remove_role :denied
78
    @user.add_role :user
79
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.unbanned") }
80
  end
81
82
  # POST /admins/approve/:user_uid
83
  def approve
84
    @user.remove_role :pending
85
86
    send_user_approved_email(@user)
87
88
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.approved") }
89
  end
90
91
  # POST /admins/invite
92
  def invite
93
    email = params[:invite_user][:email]
94
95
    begin
96
      invitation = create_or_update_invite(email)
97
98
      send_invitation_email(current_user.name, email, invitation.invite_token)
99
    rescue => e
100
      logger.error "Support: Error in email delivery: #{e}"
101
      flash[:alert] = I18n.t(params[:message], default: I18n.t("delivery_error"))
102
    else
103
      flash[:success] = I18n.t("administrator.flash.invite", email: email)
104
    end
105
106
    redirect_to admins_path
107
  end
108
109
  # GET /admins/reset
110
  def reset
111
    @user.create_reset_digest
112
113
    send_password_reset_email(@user)
114
115
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.reset_password") }
116
  end
117
  # SITE SETTINGS
118
119
  # POST /admins/branding
120
  def branding
121
    @settings.update_value("Branding Image", params[:url])
122
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
123
  end
124
125
  # POST /admins/color
126
  def coloring
127
    @settings.update_value("Primary Color", params[:color])
128
    @settings.update_value("Primary Color Lighten", color_lighten(params[:color]))
129
    @settings.update_value("Primary Color Darken", color_darken(params[:color]))
130
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
131
  end
132
133 View Code Duplication
  def coloring_lighten
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
134
    @settings.update_value("Primary Color Lighten", params[:color])
135
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
136
  end
137
138 View Code Duplication
  def coloring_darken
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
139
    @settings.update_value("Primary Color Darken", params[:color])
140
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
141
  end
142
143
  # POST /admins/room_authentication
144
  def room_authentication
145
    @settings.update_value("Room Authentication", params[:value])
146
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
147
  end
148
149
  # POST /admins/registration_method/:method
150
  def registration_method
151
    new_method = Rails.configuration.registration_methods[params[:method].to_sym]
152
153
    # Only allow change to Join by Invitation if user has emails enabled
154
    if !Rails.configuration.enable_email_verification && new_method == Rails.configuration.registration_methods[:invite]
155
      redirect_to admin_site_settings_path,
156
        flash: { alert: I18n.t("administrator.flash.invite_email_verification") }
157
    else
158
      @settings.update_value("Registration Method", new_method)
159
      redirect_to admin_site_settings_path,
160
        flash: { success: I18n.t("administrator.flash.registration_method_updated") }
161
    end
162
  end
163
164
  # POST /admins/room_limit
165
  def room_limit
166
    @settings.update_value("Room Limit", params[:limit])
167
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
168
  end
169
170
  # POST /admins/default_recording_visibility
171
  def default_recording_visibility
172
    @settings.update_value("Default Recording Visibility", params[:visibility])
173
    redirect_to admin_site_settings_path, flash: {
174
      success: I18n.t("administrator.flash.settings") + ". " +
175
               I18n.t("administrator.site_settings.recording_visibility.warning")
176
    }
177
  end
178
179
  # ROLES
180
181
  # GET /admins/roles
182
  def roles
183
    @roles = Role.editable_roles(@user_domain)
184
185
    if @roles.count.zero?
186
      Role.create_default_roles(@user_domain)
187
      @roles = Role.editable_roles(@user_domain)
188
    end
189
190
    @selected_role = if params[:selected_role].nil?
191
                        @roles.find_by(name: 'user')
192
                      else
193
                        @roles.find(params[:selected_role])
194
                     end
195
  end
196
197
  # POST /admin/role
198
  # This method creates a new role scope to the users provider
199
  def new_role
200
    new_role_name = params[:role][:name]
201
202
    # Make sure that the role name isn't a duplicate or a reserved name like super_admin
203
    if Role.duplicate_name(new_role_name, @user_domain)
204
      flash[:alert] = I18n.t("administrator.roles.duplicate_name")
205
206
      return redirect_to admin_roles_path
207
    end
208
209
    # Make sure the role name isn't empty
210
    if new_role_name.strip.empty?
211
      flash[:alert] = I18n.t("administrator.roles.empty_name")
212
213
      return redirect_to admin_roles_path
214
    end
215
216
    new_role = Role.create_new_role(new_role_name, @user_domain)
217
218
    redirect_to admin_roles_path(selected_role: new_role.id)
219
  end
220
221
  # PATCH /admin/roles/order
222
  # This updates the priority of a site's roles
223
  # Note: A lower priority role will always get used before a higher priority one
224
  def change_role_order
225
    user_role = Role.find_by(name: "user", provider: @user_domain)
226
    admin_role = Role.find_by(name: "admin", provider: @user_domain)
227
228
    current_user_role = current_user.highest_priority_role
229
230
    # Users aren't allowed to update the priority of the admin or user roles
231
    if params[:role].include?(user_role.id.to_s) || params[:role].include?(admin_role.id.to_s)
232
      flash[:alert] = I18n.t("administrator.roles.invalid_order")
233
234
      return redirect_to admin_roles_path
235
    end
236
237
    # Restrict users to only updating the priority for roles in their domain with a higher
238
    # priority
239
    params[:role].each do |id|
240
      role = Role.find(id)
241
      if role.priority <= current_user_role.priority || role.provider != @user_domain
242
        flash[:alert] = I18n.t("administrator.roles.invalid_update")
243
        return redirect_to admin_roles_path
244
      end
245
    end
246
247
    # Update the roles priority including the user role
248
    top_priority = 0
249
250
    params[:role].each_with_index do |id, index|
251
      new_priority = index + [current_user_role.priority, 0].max + 1
252
      top_priority = new_priority
253
      Role.where(id: id).update_all(priority: new_priority)
254
    end
255
256
    user_role.priority = top_priority + 1
257
    user_role.save!
258
  end
259
260
  # POST /admin/role/:role_id
261
  # This method updates the permissions assigned to a role
262
  def update_role
263
    role = Role.find(params[:role_id])
264
    current_user_role = current_user.highest_priority_role
265
266
    # Checks that it is valid for the provider to update the role
267
    if role.priority <= current_user_role.priority || role.provider != @user_domain
268
      flash[:alert] = I18n.t("administrator.roles.invalid_update")
269
      return redirect_to admin_roles_path(selected_role: role.id)
270
    end
271
272
    role_params = params.require(:role).permit(:name)
273
    permission_params = params.require(:role)
274
                              .permit(
275
                                :can_create_rooms,
276
                                :send_promoted_email,
277
                                :send_demoted_email,
278
                                :can_edit_site_settings,
279
                                :can_edit_roles,
280
                                :can_manage_users,
281
                                :colour
282
                              )
283
284
    # Role is a default role so users can't change the name
285
    role_params[:name] = role.name if Role::RESERVED_ROLE_NAMES.include?(role.name)
286
287
    # Make sure if the user is updating the role name that the role name is valid
288
    if role.name != role_params[:name] && !Role.duplicate_name(role_params[:name], @user_domain) &&
289
       !role_params[:name].strip.empty?
290
      role.name = role_params[:name]
291
    elsif role.name != role_params[:name]
292
      flash[:alert] = I18n.t("administrator.roles.duplicate_name")
293
294
      return redirect_to admin_roles_path(selected_role: role.id)
295
    end
296
297
    role.update(permission_params)
298
299
    role.save!
300
301
    redirect_to admin_roles_path(selected_role: role.id)
302
  end
303
304
  # DELETE admins/role/:role_id
305
  # This deletes a role
306
  def delete_role
307
    role = Role.find(params[:role_id])
308
309
    # Make sure no users are assigned to the role and the role isn't a reserved role
310
    # before deleting
311
    if role.users.count.positive?
312
      flash[:alert] = I18n.t("administrator.roles.role_has_users", user_count: role.users.count)
313
      return redirect_to admin_roles_path(selected_role: role.id)
314
    elsif Role::RESERVED_ROLE_NAMES.include?(role) || role.provider != @user_domain ||
315
          role.priority <= current_user.highest_priority_role.priority
316
      return redirect_to admin_roles_path(selected_role: role.id)
317
    else
318
      role.delete
319
    end
320
321
    redirect_to admin_roles_path
322
  end
323
324
  private
325
326
  def find_user
327
    @user = User.where(uid: params[:user_uid]).includes(:roles).first
328
  end
329
330
  def find_setting
331
    @settings = Setting.find_or_create_by!(provider: user_settings_provider)
332
  end
333
334
  def verify_admin_of_user
335
    redirect_to admins_path,
336
      flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user)
337
  end
338
339
  # Gets the list of users based on your configuration
340
  def user_list
341
    initial_list = if current_user.has_role? :super_admin
342
      User.where.not(id: current_user.id)
343
    else
344
      User.without_role(:super_admin).where.not(id: current_user.id)
345
    end
346
347
    if Rails.configuration.loadbalanced_configuration
348
      initial_list.where(provider: user_settings_provider)
349
                  .admins_search(@search, @role)
350
                  .admins_order(@order_column, @order_direction)
351
    else
352
      initial_list.admins_search(@search, @role)
353
                  .admins_order(@order_column, @order_direction)
354
    end
355
  end
356
357
  # Creates the invite if it doesn't exist, or updates the updated_at time if it does
358
  def create_or_update_invite(email)
359
    invite = Invitation.find_by(email: email, provider: @user_domain)
360
361
    # Invite already exists
362
    if invite.present?
363
      # Updates updated_at to now
364
      invite.touch
365
    else
366
      # Creates invite
367
      invite = Invitation.create(email: email, provider: @user_domain)
368
    end
369
370
    invite
371
  end
372
end
373