| Total Complexity | 66 |
| Total Lines | 241 |
| Duplicated Lines | 4.15 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
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 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 |
||
| 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 |
|
|
|
|||
| 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? |
|
| 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? |
|
| 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 | end |
||
| 115 | |||
| 116 | # GET /u/:user_uid/change_password |
||
| 117 | def change_password |
||
| 118 | end |
||
| 119 | |||
| 120 | # GET /u/:user_uid/delete_account |
||
| 121 | def delete_account |
||
| 122 | end |
||
| 123 | |||
| 124 | # PATCH /u/:user_uid/edit |
||
| 125 | def update |
||
| 126 | redirect_path = current_user.admin_of?(@user) ? admins_path : edit_user_path(@user) |
||
| 127 | |||
| 128 | if params[:setting] == "password" |
||
| 129 | # Update the users password. |
||
| 130 | errors = {} |
||
| 131 | |||
| 132 | if @user.authenticate(user_params[:password]) |
||
| 133 | # Verify that the new passwords match. |
||
| 134 | if user_params[:new_password] == user_params[:password_confirmation] |
||
| 135 | @user.password = user_params[:new_password] |
||
| 136 | else |
||
| 137 | # New passwords don't match. |
||
| 138 | errors[:password_confirmation] = "doesn't match" |
||
| 139 | end |
||
| 140 | else |
||
| 141 | # Original password is incorrect, can't update. |
||
| 142 | errors[:password] = "is incorrect" |
||
| 143 | end |
||
| 144 | |||
| 145 | if errors.empty? && @user.save |
||
| 146 | # Notify the user that their account has been updated. |
||
| 147 | redirect_to redirect_path, flash: { success: I18n.t("info_update_success") } |
||
| 148 | else |
||
| 149 | # Append custom errors. |
||
| 150 | errors.each { |k, v| @user.errors.add(k, v) } |
||
| 151 | render :edit, params: { settings: params[:settings] } |
||
| 152 | end |
||
| 153 | else |
||
| 154 | if @user.update_attributes(user_params) |
||
| 155 | @user.update_attributes(email_verified: false) if user_params[:email] != @user.email |
||
| 156 | |||
| 157 | user_locale(@user) |
||
| 158 | |||
| 159 | if update_roles(params[:user][:role_ids]) |
||
| 160 | return redirect_to redirect_path, flash: { success: I18n.t("info_update_success") } |
||
| 161 | else |
||
| 162 | flash[:alert] = I18n.t("administrator.roles.invalid_assignment") |
||
| 163 | end |
||
| 164 | end |
||
| 165 | |||
| 166 | render :edit, params: { settings: params[:settings] } |
||
| 167 | end |
||
| 168 | end |
||
| 169 | |||
| 170 | # DELETE /u/:user_uid |
||
| 171 | def destroy |
||
| 172 | logger.info "Support: #{current_user.email} is deleting #{@user.email}." |
||
| 173 | |||
| 174 | if current_user && current_user == @user |
||
| 175 | @user.destroy |
||
| 176 | session.delete(:user_id) |
||
| 177 | elsif current_user.admin_of?(@user) |
||
| 178 | begin |
||
| 179 | @user.destroy |
||
| 180 | rescue => e |
||
| 181 | logger.error "Support: Error in user deletion: #{e}" |
||
| 182 | flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail")) |
||
| 183 | else |
||
| 184 | flash[:success] = I18n.t("administrator.flash.delete") |
||
| 185 | end |
||
| 186 | redirect_to(admins_path) && return |
||
| 187 | end |
||
| 188 | redirect_to root_path |
||
| 189 | end |
||
| 190 | |||
| 191 | # GET /u/:user_uid/recordings |
||
| 192 | def recordings |
||
| 193 | if current_user && current_user.uid == params[:user_uid] |
||
| 194 | @search, @order_column, @order_direction, recs = |
||
| 195 | all_recordings(current_user.rooms.pluck(:bbb_id), params.permit(:search, :column, :direction), true) |
||
| 196 | @pagy, @recordings = pagy_array(recs) |
||
| 197 | else |
||
| 198 | redirect_to root_path |
||
| 199 | end |
||
| 200 | end |
||
| 201 | |||
| 202 | # GET | POST /terms |
||
| 203 | def terms |
||
| 204 | redirect_to '/404' unless Rails.configuration.terms |
||
| 205 | |||
| 206 | if params[:accept] == "true" |
||
| 207 | current_user.update_attributes(accepted_terms: true) |
||
| 208 | login(current_user) |
||
| 209 | end |
||
| 210 | end |
||
| 211 | |||
| 212 | private |
||
| 213 | |||
| 214 | def find_user |
||
| 215 | @user = User.where(uid: params[:user_uid]).includes(:roles).first |
||
| 216 | end |
||
| 217 | |||
| 218 | def ensure_unauthenticated |
||
| 219 | redirect_to current_user.main_room if current_user && params[:old_twitter_user_id].nil? |
||
| 220 | end |
||
| 221 | |||
| 222 | def user_params |
||
| 223 | params.require(:user).permit(:name, :email, :image, :password, :password_confirmation, |
||
| 224 | :new_password, :provider, :accepted_terms, :language) |
||
| 225 | end |
||
| 226 | |||
| 227 | def send_registration_email |
||
| 228 | if invite_registration |
||
| 229 | send_invite_user_signup_email(@user) |
||
| 230 | elsif approval_registration |
||
| 231 | send_approval_user_signup_email(@user) |
||
| 232 | end |
||
| 233 | end |
||
| 234 | |||
| 235 | # Add validation errors to model if they exist |
||
| 236 | def valid_user_or_captcha |
||
| 237 | valid_user = @user.valid? |
||
| 238 | valid_captcha = Rails.configuration.recaptcha_enabled ? verify_recaptcha(model: @user) : true |
||
| 239 | |||
| 240 | logger.error("Support: #{@user.email} creation failed: User params are not valid.") unless valid_user |
||
| 241 | |||
| 242 | valid_user && valid_captcha |
||
| 243 | end |
||
| 244 | |||
| 245 | # Checks if the user passes the requirements to be invited |
||
| 246 | def passes_invite_reqs |
||
| 247 | # check if user needs to be invited and IS invited |
||
| 248 | invitation = check_user_invited(@user.email, session[:invite_token], @user_domain) |
||
| 249 | |||
| 250 | @user.email_verified = true if invitation[:verified] |
||
| 251 | |||
| 252 | invitation[:present] |
||
| 253 | end |
||
| 254 | |||
| 255 | # Checks that the user is allowed to edit this user |
||
| 256 | def check_admin_of |
||
| 257 | redirect_to current_user.main_room if @user != current_user && !current_user.admin_of?(@user) |
||
| 258 | end |
||
| 259 | end |
||
| 260 |