Passed
Push — master ( 02b342...4fc171 )
by Jesus
04:50
created

AdminsController.delete_role()   A

Complexity

Conditions 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
dl 0
loc 17
rs 9.0833
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]
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 "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
  # SITE SETTINGS
110
111
  # POST /admins/branding
112
  def branding
113
    @settings.update_value("Branding Image", params[:url])
114
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
115
  end
116
117
  # POST /admins/color
118
  def coloring
119
    @settings.update_value("Primary Color", params[:color])
120
    @settings.update_value("Primary Color Lighten", color_lighten(params[:color]))
121
    @settings.update_value("Primary Color Darken", color_darken(params[:color]))
122
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
123
  end
124
125 View Code Duplication
  def coloring_lighten
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
126
    @settings.update_value("Primary Color Lighten", params[:color])
127
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
128
  end
129
130 View Code Duplication
  def coloring_darken
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
131
    @settings.update_value("Primary Color Darken", params[:color])
132
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
133
  end
134
135
  # POST /admins/room_authentication
136
  def room_authentication
137
    @settings.update_value("Room Authentication", params[:value])
138
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
139
  end
140
141
  # POST /admins/registration_method/:method
142
  def registration_method
143
    new_method = Rails.configuration.registration_methods[params[:method].to_sym]
144
145
    # Only allow change to Join by Invitation if user has emails enabled
146
    if !Rails.configuration.enable_email_verification && new_method == Rails.configuration.registration_methods[:invite]
147
      redirect_to admin_site_settings_path,
148
        flash: { alert: I18n.t("administrator.flash.invite_email_verification") }
149
    else
150
      @settings.update_value("Registration Method", new_method)
151
      redirect_to admin_site_settings_path,
152
        flash: { success: I18n.t("administrator.flash.registration_method_updated") }
153
    end
154
  end
155
156
  # POST /admins/room_limit
157
  def room_limit
158
    @settings.update_value("Room Limit", params[:limit])
159
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
160
  end
161
162
  # POST /admins/default_recording_visibility
163
  def default_recording_visibility
164
    @settings.update_value("Default Recording Visibility", params[:visibility])
165
    redirect_to admin_site_settings_path, flash: {
166
      success: I18n.t("administrator.flash.settings") + ". " +
167
               I18n.t("administrator.site_settings.recording_visibility.warning")
168
    }
169
  end
170
171
  # ROLES
172
173
  # GET /admins/roles
174
  def roles
175
    @roles = Role.editable_roles(@user_domain)
176
177
    if @roles.count.zero?
178
      Role.create_default_roles(@user_domain)
179
      @roles = Role.editable_roles(@user_domain)
180
    end
181
182
    @selected_role = if params[:selected_role].nil?
183
                        @roles.find_by(name: 'user')
184
                      else
185
                        @roles.find(params[:selected_role])
186
                     end
187
  end
188
189
  # POST /admin/role
190
  # This method creates a new role scope to the users provider
191
  def new_role
192
    new_role_name = params[:role][:name]
193
194
    # Make sure that the role name isn't a duplicate or a reserved name like super_admin
195
    if Role.duplicate_name(new_role_name, @user_domain)
196
      flash[:alert] = I18n.t("administrator.roles.duplicate_name")
197
198
      return redirect_to admin_roles_path
199
    end
200
201
    # Make sure the role name isn't empty
202
    if new_role_name.strip.empty?
203
      flash[:alert] = I18n.t("administrator.roles.empty_name")
204
205
      return redirect_to admin_roles_path
206
    end
207
208
    # Create the new role with the second highest priority
209
    # This means that it will only be more important than the user role
210
    # This also updates the user role to have the highest priority
211
    new_role = Role.create(name: new_role_name, provider: @user_domain)
212
    user_role = Role.find_by(name: 'user', provider: @user_domain)
213
214
    new_role.priority = user_role.priority
215
    user_role.priority += 1
216
217
    new_role.save!
218
    user_role.save!
219
220
    redirect_to admin_roles_path(selected_role: new_role.id)
221
  end
222
223
  # PATCH /admin/roles/order
224
  # This updates the priority of a site's roles
225
  # Note: A lower priority role will always get used before a higher priority one
226
  def change_role_order
227
    user_role = Role.find_by(name: "user", provider: @user_domain)
228
    admin_role = Role.find_by(name: "admin", provider: @user_domain)
229
230
    current_user_role = current_user.highest_priority_role
231
232
    # Users aren't allowed to update the priority of the admin or user roles
233
    if params[:role].include?(user_role.id.to_s) || params[:role].include?(admin_role.id.to_s)
234
      flash[:alert] = I18n.t("administrator.roles.invalid_order")
235
236
      return redirect_to admin_roles_path
237
    end
238
239
    # Restrict users to only updating the priority for roles in their domain with a higher
240
    # priority
241
    params[:role].each do |id|
242
      role = Role.find(id)
243
      if role.priority <= current_user_role.priority || role.provider != @user_domain
244
        flash[:alert] = I18n.t("administrator.roles.invalid_update")
245
        return redirect_to admin_roles_path
246
      end
247
    end
248
249
    # Update the roles priority including the user role
250
    top_priority = 0
251
252
    params[:role].each_with_index do |id, index|
253
      new_priority = index + [current_user_role.priority, 0].max + 1
254
      top_priority = new_priority
255
      Role.where(id: id).update_all(priority: new_priority)
256
    end
257
258
    user_role.priority = top_priority + 1
259
    user_role.save!
260
  end
261
262
  # POST /admin/role/:role_id
263
  # This method updates the permissions assigned to a role
264
  def update_role
265
    role = Role.find(params[:role_id])
266
    current_user_role = current_user.highest_priority_role
267
268
    # Checks that it is valid for the provider to update the role
269
    if role.priority <= current_user_role.priority || role.provider != @user_domain
270
      flash[:alert] = I18n.t("administrator.roles.invalid_update")
271
      return redirect_to admin_roles_path(selected_role: role.id)
272
    end
273
274
    role_params = params.require(:role).permit(:name)
275
    permission_params = params.require(:role)
276
                              .permit(
277
                                :can_create_rooms,
278
                                :send_promoted_email,
279
                                :send_demoted_email,
280
                                :can_edit_site_settings,
281
                                :can_edit_roles,
282
                                :can_manage_users,
283
                                :colour
284
                              )
285
286
    # Make sure if the user is updating the role name that the role name is valid
287
    if role.name != role_params[:name] && !Role.duplicate_name(role_params[:name], @user_domain) &&
288
       !role_params[:name].strip.empty?
289
      role.name = role_params[:name]
290
    elsif role.name != role_params[:name]
291
      flash[:alert] = I18n.t("administrator.roles.duplicate_name")
292
293
      return redirect_to admin_roles_path(selected_role: role.id)
294
    end
295
296
    role.update(permission_params)
297
298
    role.save!
299
300
    redirect_to admin_roles_path(selected_role: role.id)
301
  end
302
303
  # DELETE admins/role/:role_id
304
  # This deletes a role
305
  def delete_role
306
    role = Role.find(params[:role_id])
307
308
    # Make sure no users are assigned to the role and the role isn't a reserved role
309
    # before deleting
310
    if role.users.count.positive?
311
      flash[:alert] = I18n.t("administrator.roles.role_has_users", user_count: role.users.count)
312
      return redirect_to admin_roles_path(selected_role: role.id)
313
    elsif Role::RESERVED_ROLE_NAMES.include?(role) || role.provider != @user_domain ||
314
          role.priority <= current_user.highest_priority_role.priority
315
      return redirect_to admin_roles_path(selected_role: role.id)
316
    else
317
      role.delete
318
    end
319
320
    redirect_to admin_roles_path
321
  end
322
323
  private
324
325
  def find_user
326
    @user = User.where(uid: params[:user_uid]).includes(:roles).first
327
  end
328
329
  def find_setting
330
    @settings = Setting.find_or_create_by!(provider: user_settings_provider)
331
  end
332
333
  def verify_admin_of_user
334
    redirect_to admins_path,
335
      flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user)
336
  end
337
338
  # Gets the list of users based on your configuration
339
  def user_list
340
    initial_list = if current_user.has_role? :super_admin
341
      User.where.not(id: current_user.id)
342
    else
343
      User.without_role(:super_admin).where.not(id: current_user.id)
344
    end
345
346
    if Rails.configuration.loadbalanced_configuration
347
      initial_list.where(provider: user_settings_provider)
348
                  .admins_search(@search, @role)
349
                  .admins_order(@order_column, @order_direction)
350
    else
351
      initial_list.admins_search(@search, @role)
352
                  .admins_order(@order_column, @order_direction)
353
    end
354
  end
355
356
  # Creates the invite if it doesn't exist, or updates the updated_at time if it does
357
  def create_or_update_invite(email)
358
    invite = Invitation.find_by(email: email, provider: @user_domain)
359
360
    # Invite already exists
361
    if invite.present?
362
      # Updates updated_at to now
363
      invite.touch
364
    else
365
      # Creates invite
366
      invite = Invitation.create(email: email, provider: @user_domain)
367
    end
368
369
    invite
370
  end
371
end
372