GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#709)
by
unknown
04:37
created

AdminsController.find_user()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
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]
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 admins_path, flash: { success: I18n.t("administrator.flash.settings") + ". " +
166
                                               I18n.t("administrator.site_settings.recording_visibility.warning") }
167
  end
168
169
  # ROLES
170
171
  # GET /admins/roles
172
  def roles
173
    @roles = Role.editable_roles(@user_domain)
174
175
    if @roles.count.zero?
176
      Role.create_default_roles(@user_domain)
177
      @roles = Role.editable_roles(@user_domain)
178
    end
179
180
    @selected_role = if params[:selected_role].nil?
181
                        @roles.find_by(name: 'user')
182
                      else
183
                        @roles.find(params[:selected_role])
184
                     end
185
  end
186
187
  # POST /admin/role
188
  # This method creates a new role scope to the users provider
189
  def new_role
190
    new_role_name = params[:role][:name]
191
192
    # Make sure that the role name isn't a duplicate or a reserved name like super_admin
193
    if Role.duplicate_name(new_role_name, @user_domain)
194
      flash[:alert] = I18n.t("administrator.roles.duplicate_name")
195
196
      return redirect_to admin_roles_path
197
    end
198
199
    # Make sure the role name isn't empty
200
    if new_role_name.strip.empty?
201
      flash[:alert] = I18n.t("administrator.roles.empty_name")
202
203
      return redirect_to admin_roles_path
204
    end
205
206
    # Create the new role with the second highest priority
207
    # This means that it will only be more important than the user role
208
    # This also updates the user role to have the highest priority
209
    new_role = Role.create(name: new_role_name, provider: @user_domain)
210
    user_role = Role.find_by(name: 'user', provider: @user_domain)
211
212
    new_role.priority = user_role.priority
213
    user_role.priority += 1
214
215
    new_role.save!
216
    user_role.save!
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
    # Make sure if the user is updating the role name that the role name is valid
285
    if role.name != role_params[:name] && !Role.duplicate_name(role_params[:name], @user_domain) &&
286
       !role_params[:name].strip.empty?
287
      role.name = role_params[:name]
288
    elsif role.name != role_params[:name]
289
      flash[:alert] = I18n.t("administrator.roles.duplicate_name")
290
291
      return redirect_to admin_roles_path(selected_role: role.id)
292
    end
293
294
    role.update(permission_params)
295
296
    role.save!
297
298
    redirect_to admin_roles_path(selected_role: role.id)
299
  end
300
301
  # DELETE admins/role/:role_id
302
  # This deletes a role
303
  def delete_role
304
    role = Role.find(params[:role_id])
305
306
    # Make sure no users are assigned to the role and the role isn't a reserved role
307
    # before deleting
308
    if role.users.count.positive?
309
      flash[:alert] = I18n.t("administrator.roles.role_has_users", user_count: role.users.count)
310
      return redirect_to admin_roles_path(selected_role: role.id)
311
    elsif Role::RESERVED_ROLE_NAMES.include?(role) || role.provider != @user_domain ||
312
          role.priority <= current_user.highest_priority_role.priority
313
      return redirect_to admin_roles_path(selected_role: role.id)
314
    else
315
      role.delete
316
    end
317
318
    redirect_to admin_roles_path
319
  end
320
321
  private
322
323
  def find_user
324
    @user = User.where(uid: params[:user_uid]).includes(:roles).first
325
  end
326
327
  def find_setting
328
    @settings = Setting.find_or_create_by!(provider: user_settings_provider)
329
  end
330
331
  def verify_admin_of_user
332
    redirect_to admins_path,
333
      flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user)
334
  end
335
336
  # Gets the list of users based on your configuration
337
  def user_list
338
    initial_list = if current_user.has_role? :super_admin
339
      User.where.not(id: current_user.id)
340
    else
341
      User.without_role(:super_admin).where.not(id: current_user.id)
342
    end
343
344
    if Rails.configuration.loadbalanced_configuration
345
      initial_list.where(provider: user_settings_provider)
346
                  .admins_search(@search, @role)
347
                  .admins_order(@order_column, @order_direction)
348
    else
349
      initial_list.admins_search(@search, @role)
350
                  .admins_order(@order_column, @order_direction)
351
    end
352
  end
353
354
  # Creates the invite if it doesn't exist, or updates the updated_at time if it does
355
  def create_or_update_invite(email)
356
    invite = Invitation.find_by(email: email, provider: @user_domain)
357
358
    # Invite already exists
359
    if invite.present?
360
      # Updates updated_at to now
361
      invite.touch
362
    else
363
      # Creates invite
364
      invite = Invitation.create(email: email, provider: @user_domain)
365
    end
366
367
    invite
368
  end
369
end
370