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

AdminsController.edit_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
24
  manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve]
25
  site_settings = [:branding, :coloring, :coloring_lighten, :coloring_darken,
26
                   :registration_method, :room_authentication]
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
  before_action :find_setting, only: site_settings
32
33
  # GET /admins
34
  def index
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
    @role = params[:role] || ""
39
40
    @pagy, @users = pagy(user_list)
41
  end
42
43
  # MANAGE USERS
44
45
  # GET /admins/edit/:user_uid
46
  def edit_user
47
    render "admins/index", locals: { setting_id: "account" }
48
  end
49
50
  # POST /admins/promote/:user_uid
51
  def promote
52
    @user.add_role :admin
53
54
    send_user_promoted_email(@user)
55
56
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.promoted") }
57
  end
58
59
  # POST /admins/demote/:user_uid
60
  def demote
61
    @user.remove_role :admin
62
63
    send_user_demoted_email(@user)
64
65
    redirect_to admins_path, flash: { success: I18n.t("administrator.flash.demoted") }
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 admins_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 admins_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 admins_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 admins_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 admins_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 admins_path,
148
        flash: { alert: I18n.t("administrator.flash.invite_email_verification") }
149
    else
150
      @settings.update_value("Registration Method", new_method)
151
      redirect_to admins_path,
152
        flash: { success: I18n.t("administrator.flash.registration_method_updated") }
153
    end
154
  end
155
156
  private
157
158
  def find_user
159
    @user = User.find_by!(uid: params[:user_uid])
160
  end
161
162
  def find_setting
163
    @settings = Setting.find_or_create_by!(provider: user_settings_provider)
164
  end
165
166
  def verify_admin_of_user
167
    redirect_to admins_path,
168
      flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user)
169
  end
170
171
  # Gets the list of users based on your configuration
172
  def user_list
173
    list = if @role.present?
174
      User.with_role(@role.to_sym).where.not(id: current_user.id)
175
    else
176
      User.where.not(id: current_user.id)
177
    end
178
179
    if Rails.configuration.loadbalanced_configuration
180
      list.where(provider: user_settings_provider)
181
          .admins_search(@search)
182
          .admins_order(@order_column, @order_direction)
183
    else
184
      list.admins_search(@search)
185
          .admins_order(@order_column, @order_direction)
186
    end
187
  end
188
189
  # Creates the invite if it doesn't exist, or updates the updated_at time if it does
190
  def create_or_update_invite(email)
191
    invite = Invitation.find_by(email: email, provider: @user_domain)
192
193
    # Invite already exists
194
    if invite.present?
195
      # Updates updated_at to now
196
      invite.touch
197
    else
198
      # Creates invite
199
      invite = Invitation.create(email: email, provider: @user_domain)
200
    end
201
202
    invite
203
  end
204
end
205