Completed
Branch v2.4-alpha (b4736b)
by Ahmad
05:54
created

UsersController   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 242
Duplicated Lines 4.13 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 10
loc 242
rs 2.96
c 3
b 0
f 0
wmc 68

9 Methods

Rating   Name   Duplication   Size   Complexity  
A valid_user_or_captcha() 0 8 3
A send_registration_email() 0 7 3
A check_admin_of() 0 3 4
A ldap_signin() 0 2 1
A delete_account() 0 2 1
A change_password() 0 2 1
B signin() 4 18 8
A find_user() 0 3 1
A passes_invite_reqs() 0 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like UsersController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 UsersController < ApplicationController
20
  include Pagy::Backend
21
  include Authenticator
22
  include Emailer
23
  include Registrar
24
  include Recorder
25
  include Rolify
26
27
  before_action :find_user, only: [:edit, :change_password, :delete_account, :update, :destroy]
28
  before_action :ensure_unauthenticated, only: [:new, :create, :signin]
29
  before_action :check_admin_of, only: [:edit, :change_password, :delete_account]
30
31
  # POST /u
32
  def create
33
    # Verify that GreenLight is configured to allow user signup.
34
    return unless Rails.configuration.allow_user_signup
35
36
    @user = User.new(user_params)
37
    @user.provider = @user_domain
38
39
    # User or recpatcha is not valid
40
    render(:new) && return unless valid_user_or_captcha
41
42
    # Redirect to root if user token is either invalid or expired
43 View Code Duplication
    return redirect_to root_path, flash: { alert: I18n.t("registration.invite.fail") } unless passes_invite_reqs
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
44
45
    # User has passed all validations required
46
    @user.save
47
48
    logger.info "Support: #{@user.email} user has been created."
49
50
    # Set user to pending and redirect if Approval Registration is set
51
    if approval_registration
52
      @user.add_role :pending
53
54
      return redirect_to root_path,
55
        flash: { success: I18n.t("registration.approval.signup") } unless Rails.configuration.enable_email_verification
56
    end
57
58
    send_registration_email
59
60
    # Sign in automatically if email verification is disabled or if user is already verified.
61
    login(@user) && return if !Rails.configuration.enable_email_verification || @user.email_verified
62
63
    send_activation_email(@user)
64
65
    redirect_to root_path
66
  end
67
68
  # GET /signin
69
  def signin
70 View Code Duplication
    unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil?
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
71
      flash[:alert] = I18n.t("registration.deprecated.new_signin")
72
      session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil?
73
    end
74
75
    providers = configured_providers
76
    if (!allow_user_signup? || !allow_greenlight_accounts?) && providers.count == 1 &&
77
       !Rails.configuration.loadbalanced_configuration
78
      provider_path = if Rails.configuration.omniauth_ldap
79
        ldap_signin_path
80
      else
81
        "#{Rails.configuration.relative_url_root}/auth/#{providers.first}"
82
      end
83
84
      return redirect_to provider_path
85
    end
86
  end
87
88
  # GET /ldap_signin
89
  def ldap_signin
90
  end
91
92
  # GET /signup
93
  def new
94
    return redirect_to root_path unless Rails.configuration.allow_user_signup
95
96
    # Check if the user needs to be invited
97
    if invite_registration
98
      redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token]
99
100
      session[:invite_token] = params[:invite_token]
101
    end
102
103 View Code Duplication
    unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil?
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
104
      logout
105
      flash.now[:alert] = I18n.t("registration.deprecated.new_signin")
106
      session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil?
107
    end
108
109
    @user = User.new
110
  end
111
112
  # GET /u/:user_uid/edit
113
  def edit
114
    redirect_to root_path unless current_user
115
  end
116
117
  # GET /u/:user_uid/change_password
118
  def change_password
119
  end
120
121
  # GET /u/:user_uid/delete_account
122
  def delete_account
123
  end
124
125
  # PATCH /u/:user_uid/edit
126
  def update
127
    redirect_path = current_user.admin_of?(@user) ? admins_path : edit_user_path(@user)
128
129
    if params[:setting] == "password"
130
      # Update the users password.
131
      errors = {}
132
133
      if @user.authenticate(user_params[:password])
134
        # Verify that the new passwords match.
135
        if user_params[:new_password] == user_params[:password_confirmation]
136
          @user.password = user_params[:new_password]
137
        else
138
          # New passwords don't match.
139
          errors[:password_confirmation] = "doesn't match"
140
        end
141
      else
142
        # Original password is incorrect, can't update.
143
        errors[:password] = "is incorrect"
144
      end
145
146
      if errors.empty? && @user.save
147
        # Notify the user that their account has been updated.
148
        redirect_to redirect_path, flash: { success: I18n.t("info_update_success") }
149
      else
150
        # Append custom errors.
151
        errors.each { |k, v| @user.errors.add(k, v) }
152
        render :edit, params: { settings: params[:settings] }
153
      end
154
    else
155
      if @user.update_attributes(user_params)
156
        @user.update_attributes(email_verified: false) if user_params[:email] != @user.email
157
158
        user_locale(@user)
159
160
        if update_roles(params[:user][:role_ids])
161
          return redirect_to redirect_path, flash: { success: I18n.t("info_update_success") }
162
        else
163
          flash[:alert] = I18n.t("administrator.roles.invalid_assignment")
164
        end
165
      end
166
167
      render :edit, params: { settings: params[:settings] }
168
    end
169
  end
170
171
  # DELETE /u/:user_uid
172
  def destroy
173
    logger.info "Support: #{current_user.email} is deleting #{@user.email}."
174
175
    if current_user && current_user == @user
176
      @user.destroy
177
      session.delete(:user_id)
178
    elsif current_user.admin_of?(@user)
179
      begin
180
        @user.destroy
181
      rescue => e
182
        logger.error "Support: Error in user deletion: #{e}"
183
        flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail"))
184
      else
185
        flash[:success] = I18n.t("administrator.flash.delete")
186
      end
187
      redirect_to(admins_path) && return
188
    end
189
    redirect_to root_path
190
  end
191
192
  # GET /u/:user_uid/recordings
193
  def recordings
194
    if current_user && current_user.uid == params[:user_uid]
195
      @search, @order_column, @order_direction, recs =
196
        all_recordings(current_user.rooms.pluck(:bbb_id), params.permit(:search, :column, :direction), true)
197
      @pagy, @recordings = pagy_array(recs)
198
    else
199
      redirect_to root_path
200
    end
201
  end
202
203
  # GET | POST /terms
204
  def terms
205
    redirect_to '/404' unless Rails.configuration.terms
206
207
    if params[:accept] == "true"
208
      current_user.update_attributes(accepted_terms: true)
209
      login(current_user)
210
    end
211
  end
212
213
  private
214
215
  def find_user
216
    @user = User.where(uid: params[:user_uid]).includes(:roles).first
217
  end
218
219
  def ensure_unauthenticated
220
    redirect_to current_user.main_room if current_user && params[:old_twitter_user_id].nil?
221
  end
222
223
  def user_params
224
    params.require(:user).permit(:name, :email, :image, :password, :password_confirmation,
225
      :new_password, :provider, :accepted_terms, :language)
226
  end
227
228
  def send_registration_email
229
    if invite_registration
230
      send_invite_user_signup_email(@user)
231
    elsif approval_registration
232
      send_approval_user_signup_email(@user)
233
    end
234
  end
235
236
  # Add validation errors to model if they exist
237
  def valid_user_or_captcha
238
    valid_user = @user.valid?
239
    valid_captcha = Rails.configuration.recaptcha_enabled ? verify_recaptcha(model: @user) : true
240
241
    logger.error("Support: #{@user.email} creation failed: User params are not valid.") unless valid_user
242
243
    valid_user && valid_captcha
244
  end
245
246
  # Checks if the user passes the requirements to be invited
247
  def passes_invite_reqs
248
    # check if user needs to be invited and IS invited
249
    invitation = check_user_invited(@user.email, session[:invite_token], @user_domain)
250
251
    @user.email_verified = true if invitation[:verified]
252
253
    invitation[:present]
254
  end
255
256
  # Checks that the user is allowed to edit this user
257
  def check_admin_of
258
    redirect_to current_user.main_room if current_user && @user != current_user && !current_user.admin_of?(@user)
259
  end
260
end
261