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