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