Completed
Branch more-refac (5b2fc3)
by Ahmad
03:46
created

AdminsController.update_role()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
  include Rolify
25
26
  manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve, :reset]
27
28
  authorize_resource class: false
29
  before_action :find_user, only: manage_users
30
  before_action :verify_admin_of_user, only: manage_users
31
32
  # GET /admins
33
  def index
34
    # Initializa the data manipulation variables
35
    @search = params[:search] || ""
36
    @order_column = params[:column] && params[:direction] != "none" ? params[:column] : "created_at"
37
    @order_direction = params[:direction] && params[:direction] != "none" ? params[:direction] : "DESC"
38
39
    @role = params[:role] ? Role.find_by(name: params[:role], provider: @user_domain) : nil
40
41
    @pagy, @users = pagy(user_list)
42
  end
43
44
  # GET /admins/site_settings
45
  def site_settings
46
  end
47
48
  # GET /admins/server_recordings
49
  def server_recordings
50
    server_rooms = if Rails.configuration.loadbalanced_configuration
51
      Room.includes(:owner).where(users: { provider: @user_domain }).pluck(:bbb_id)
52
    else
53
      Room.pluck(:bbb_id)
54
    end
55
56
    @search, @order_column, @order_direction, recs =
57
      all_recordings(server_rooms, params.permit(:search, :column, :direction), true, true)
58
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
    invitation = create_or_update_invite(email)
96
97
    send_invitation_email(current_user.name, email, invitation.invite_token)
98
99
    redirect_to admins_path
100
  end
101
102
  # GET /admins/reset
103
  def reset
104
    @user.create_reset_digest
105
106
    send_password_reset_email(@user)
107
108
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.reset_password") }
109
  end
110
  # SITE SETTINGS
111
112
  # POST /admins/update_settings
113
  def update_settings
114
    @settings.update_value(params[:setting], params[:value])
115
116
    flash_message = I18n.t("administrator.flash.settings")
117
118
    if params[:value] == "Default Recording Visibility"
119
      flash_message += ". " + I18n.t("administrator.site_settings.recording_visibility.warning")
120
    end
121
122
    redirect_to admin_site_settings_path, flash: { success: flash_message }
123
  end
124
125
  # POST /admins/color
126
  def coloring
127
    @settings.update_value("Primary Color", params[:value])
128
    @settings.update_value("Primary Color Lighten", color_lighten(params[:value]))
129
    @settings.update_value("Primary Color Darken", color_darken(params[:value]))
130
    redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") }
131
  end
132
133
  # POST /admins/registration_method/:method
134
  def registration_method
135
    new_method = Rails.configuration.registration_methods[params[:value].to_sym]
136
137
    # Only allow change to Join by Invitation if user has emails enabled
138
    if !Rails.configuration.enable_email_verification && new_method == Rails.configuration.registration_methods[:invite]
139
      redirect_to admin_site_settings_path,
140
        flash: { alert: I18n.t("administrator.flash.invite_email_verification") }
141
    else
142
      @settings.update_value("Registration Method", new_method)
143
      redirect_to admin_site_settings_path,
144
        flash: { success: I18n.t("administrator.flash.registration_method_updated") }
145
    end
146
  end
147
148
  # ROLES
149
150
  # GET /admins/roles
151
  def roles
152
    @roles = all_roles(params[:selected_role])
153
  end
154
155
  # POST /admins/role
156
  # This method creates a new role scoped to the users provider
157
  def new_role
158
    new_role = create_role(params[:role][:name])
159
160
    return redirect_to admin_roles_path, flash: { alert: I18n.t("administrator.roles.invalid_create") } if new_role.nil?
161
162
    redirect_to admin_roles_path(selected_role: new_role.id)
163
  end
164
165
  # PATCH /admin/roles/order
166
  # This updates the priority of a site's roles
167
  # Note: A lower priority role will always get used before a higher priority one
168
  def change_role_order
169
    unless update_priority(params[:role])
170
      redirect_to admin_roles_path, flash: { alert: I18n.t("administrator.roles.invalid_order") }
171
    end
172
  end
173
174
  # POST /admin/role/:role_id
175
  # This method updates the permissions assigned to a role
176
  def update_role
177
    role = Role.find(params[:role_id])
178
    flash[:alert] = I18n.t("administrator.roles.invalid_update") unless update_permissions(role)
179
    redirect_to admin_roles_path(selected_role: role.id)
180
  end
181
182
  # DELETE admins/role/:role_id
183
  # This deletes a role
184
  def delete_role
185
    role = Role.find(params[:role_id])
186
187
    # Make sure no users are assigned to the role and the role isn't a reserved role
188
    # before deleting
189
    if role.users.count.positive?
190
      flash[:alert] = I18n.t("administrator.roles.role_has_users", user_count: role.users.count)
191
      return redirect_to admin_roles_path(selected_role: role.id)
192
    elsif Role::RESERVED_ROLE_NAMES.include?(role) || role.provider != @user_domain ||
193
          role.priority <= current_user.highest_priority_role.priority
194
      return redirect_to admin_roles_path(selected_role: role.id)
195
    else
196
      role.delete
197
    end
198
199
    redirect_to admin_roles_path
200
  end
201
202
  private
203
204
  def find_user
205
    @user = User.where(uid: params[:user_uid]).includes(:roles).first
206
  end
207
208
  # Verifies that admin is an administrator of the user in the action
209
  def verify_admin_of_user
210
    redirect_to admins_path,
211
      flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user)
212
  end
213
214
  # Gets the list of users based on your configuration
215
  def user_list
216
    initial_list = if current_user.has_role? :super_admin
217
      User.where.not(id: current_user.id)
218
    else
219
      User.without_role(:super_admin).where.not(id: current_user.id)
220
    end
221
222
    if Rails.configuration.loadbalanced_configuration
223
      initial_list.where(provider: @user_domain)
224
                  .admins_search(@search, @role)
225
                  .admins_order(@order_column, @order_direction)
226
    else
227
      initial_list.admins_search(@search, @role)
228
                  .admins_order(@order_column, @order_direction)
229
    end
230
  end
231
232
  # Creates the invite if it doesn't exist, or updates the updated_at time if it does
233
  def create_or_update_invite(email)
234
    invite = Invitation.find_by(email: email, provider: @user_domain)
235
236
    # Invite already exists
237
    if invite.present?
238
      # Updates updated_at to now
239
      invite.touch
240
    else
241
      # Creates invite
242
      invite = Invitation.create(email: email, provider: @user_domain)
243
    end
244
245
    invite
246
  end
247
end
248