| Total Complexity | 71 |
| Total Lines | 287 |
| Duplicated Lines | 5.57 % |
| Changes | 5 | ||
| 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 | include Recorder |
||
| 25 | |||
| 26 | before_action :find_user, only: [:edit, :update, :destroy] |
||
| 27 | before_action :ensure_unauthenticated, only: [:new, :create] |
||
| 28 | |||
| 29 | # POST /u |
||
| 30 | def create |
||
| 31 | # Verify that GreenLight is configured to allow user signup. |
||
| 32 | return unless Rails.configuration.allow_user_signup |
||
| 33 | |||
| 34 | @user = User.new(user_params) |
||
| 35 | @user.provider = @user_domain |
||
| 36 | |||
| 37 | # User or recpatcha is not valid |
||
| 38 | render(:new) && return unless valid_user_or_captcha |
||
| 39 | |||
| 40 | # Redirect to root if user token is either invalid or expired |
||
| 41 | View Code Duplication | return redirect_to root_path, flash: { alert: I18n.t("registration.invite.fail") } unless passes_invite_reqs |
|
|
|
|||
| 42 | |||
| 43 | # User has passed all validations required |
||
| 44 | @user.save |
||
| 45 | |||
| 46 | # Set user to pending and redirect if Approval Registration is set |
||
| 47 | View Code Duplication | if approval_registration |
|
| 48 | @user.add_role :pending |
||
| 49 | |||
| 50 | return redirect_to root_path, |
||
| 51 | flash: { success: I18n.t("registration.approval.signup") } unless Rails.configuration.enable_email_verification |
||
| 52 | end |
||
| 53 | |||
| 54 | send_registration_email if Rails.configuration.enable_email_verification |
||
| 55 | |||
| 56 | # Sign in automatically if email verification is disabled or if user is already verified. |
||
| 57 | login(@user) && return if !Rails.configuration.enable_email_verification || @user.email_verified |
||
| 58 | |||
| 59 | send_verification |
||
| 60 | |||
| 61 | redirect_to root_path |
||
| 62 | end |
||
| 63 | |||
| 64 | # GET /signin |
||
| 65 | def signin |
||
| 66 | View Code Duplication | unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil? |
|
| 67 | flash[:alert] = I18n.t("registration.deprecated.new_signin") |
||
| 68 | session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil? |
||
| 69 | end |
||
| 70 | |||
| 71 | providers = configured_providers |
||
| 72 | if (!allow_user_signup? || !allow_greenlight_accounts?) && providers.count == 1 && |
||
| 73 | !Rails.configuration.loadbalanced_configuration |
||
| 74 | return redirect_to "#{Rails.configuration.relative_url_root}/auth/#{providers.first}" |
||
| 75 | end |
||
| 76 | end |
||
| 77 | |||
| 78 | # GET /ldap_signin |
||
| 79 | def ldap_signin |
||
| 80 | end |
||
| 81 | |||
| 82 | # GET /signup |
||
| 83 | def new |
||
| 84 | return redirect_to root_path unless Rails.configuration.allow_user_signup |
||
| 85 | |||
| 86 | # Check if the user needs to be invited |
||
| 87 | if invite_registration |
||
| 88 | redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token] |
||
| 89 | |||
| 90 | session[:invite_token] = params[:invite_token] |
||
| 91 | end |
||
| 92 | |||
| 93 | View Code Duplication | unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil? |
|
| 94 | logout |
||
| 95 | flash.now[:alert] = I18n.t("registration.deprecated.new_signin") |
||
| 96 | session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil? |
||
| 97 | end |
||
| 98 | |||
| 99 | @user = User.new |
||
| 100 | end |
||
| 101 | |||
| 102 | # GET /u/:user_uid/edit |
||
| 103 | def edit |
||
| 104 | if current_user |
||
| 105 | redirect_to current_user.main_room if @user != current_user && !current_user.admin_of?(@user) |
||
| 106 | else |
||
| 107 | redirect_to root_path |
||
| 108 | end |
||
| 109 | end |
||
| 110 | |||
| 111 | # PATCH /u/:user_uid/edit |
||
| 112 | def update |
||
| 113 | redirect_path = current_user.admin_of?(@user) ? admins_path : edit_user_path(@user) |
||
| 114 | |||
| 115 | if params[:setting] == "password" |
||
| 116 | # Update the users password. |
||
| 117 | errors = {} |
||
| 118 | |||
| 119 | if @user.authenticate(user_params[:password]) |
||
| 120 | # Verify that the new passwords match. |
||
| 121 | if user_params[:new_password] == user_params[:password_confirmation] |
||
| 122 | @user.password = user_params[:new_password] |
||
| 123 | else |
||
| 124 | # New passwords don't match. |
||
| 125 | errors[:password_confirmation] = "doesn't match" |
||
| 126 | end |
||
| 127 | else |
||
| 128 | # Original password is incorrect, can't update. |
||
| 129 | errors[:password] = "is incorrect" |
||
| 130 | end |
||
| 131 | |||
| 132 | if errors.empty? && @user.save |
||
| 133 | # Notify the user that their account has been updated. |
||
| 134 | flash[:success] = I18n.t("info_update_success") |
||
| 135 | redirect_to redirect_path |
||
| 136 | else |
||
| 137 | # Append custom errors. |
||
| 138 | errors.each { |k, v| @user.errors.add(k, v) } |
||
| 139 | render :edit, params: { settings: params[:settings] } |
||
| 140 | end |
||
| 141 | elsif user_params[:email] != @user.email && @user.update_attributes(user_params) && update_roles |
||
| 142 | @user.update_attributes(email_verified: false) |
||
| 143 | |||
| 144 | flash[:success] = I18n.t("info_update_success") |
||
| 145 | redirect_to redirect_path |
||
| 146 | elsif @user.update_attributes(user_params) && update_roles |
||
| 147 | update_locale(@user) |
||
| 148 | |||
| 149 | flash[:success] = I18n.t("info_update_success") |
||
| 150 | redirect_to redirect_path |
||
| 151 | else |
||
| 152 | render :edit, params: { settings: params[:settings] } |
||
| 153 | end |
||
| 154 | end |
||
| 155 | |||
| 156 | # DELETE /u/:user_uid |
||
| 157 | def destroy |
||
| 158 | if current_user && current_user == @user |
||
| 159 | @user.destroy |
||
| 160 | session.delete(:user_id) |
||
| 161 | elsif current_user.admin_of?(@user) |
||
| 162 | begin |
||
| 163 | @user.destroy |
||
| 164 | rescue => e |
||
| 165 | logger.error "Error in user deletion: #{e}" |
||
| 166 | flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail")) |
||
| 167 | else |
||
| 168 | flash[:success] = I18n.t("administrator.flash.delete") |
||
| 169 | end |
||
| 170 | redirect_to(admins_path) && return |
||
| 171 | end |
||
| 172 | redirect_to root_path |
||
| 173 | end |
||
| 174 | |||
| 175 | # GET /u/:user_uid/recordings |
||
| 176 | def recordings |
||
| 177 | if current_user && current_user.uid == params[:user_uid] |
||
| 178 | @search, @order_column, @order_direction, recs = |
||
| 179 | all_recordings(current_user.rooms.pluck(:bbb_id), current_user.provider, |
||
| 180 | params.permit(:search, :column, :direction), true) |
||
| 181 | @pagy, @recordings = pagy_array(recs) |
||
| 182 | else |
||
| 183 | redirect_to root_path |
||
| 184 | end |
||
| 185 | end |
||
| 186 | |||
| 187 | # GET | POST /terms |
||
| 188 | def terms |
||
| 189 | redirect_to '/404' unless Rails.configuration.terms |
||
| 190 | |||
| 191 | if params[:accept] == "true" |
||
| 192 | current_user.update_attributes(accepted_terms: true) |
||
| 193 | login(current_user) |
||
| 194 | end |
||
| 195 | end |
||
| 196 | |||
| 197 | private |
||
| 198 | |||
| 199 | def find_user |
||
| 200 | @user = User.where(uid: params[:user_uid]).includes(:roles).first |
||
| 201 | end |
||
| 202 | |||
| 203 | def ensure_unauthenticated |
||
| 204 | redirect_to current_user.main_room if current_user && params[:old_twitter_user_id].nil? |
||
| 205 | end |
||
| 206 | |||
| 207 | def user_params |
||
| 208 | params.require(:user).permit(:name, :email, :image, :password, :password_confirmation, |
||
| 209 | :new_password, :provider, :accepted_terms, :language) |
||
| 210 | end |
||
| 211 | |||
| 212 | def send_verification |
||
| 213 | # Start email verification and redirect to root. |
||
| 214 | begin |
||
| 215 | send_activation_email(@user) |
||
| 216 | rescue => e |
||
| 217 | logger.error "Error in email delivery: #{e}" |
||
| 218 | flash[:alert] = I18n.t(params[:message], default: I18n.t("delivery_error")) |
||
| 219 | else |
||
| 220 | flash[:success] = I18n.t("email_sent", email_type: t("verify.verification")) |
||
| 221 | end |
||
| 222 | end |
||
| 223 | |||
| 224 | def send_registration_email |
||
| 225 | begin |
||
| 226 | if invite_registration |
||
| 227 | send_invite_user_signup_email(@user) |
||
| 228 | elsif approval_registration |
||
| 229 | send_approval_user_signup_email(@user) |
||
| 230 | end |
||
| 231 | rescue => e |
||
| 232 | logger.error "Error in email delivery: #{e}" |
||
| 233 | flash[:alert] = I18n.t(params[:message], default: I18n.t("delivery_error")) |
||
| 234 | end |
||
| 235 | end |
||
| 236 | |||
| 237 | # Add validation errors to model if they exist |
||
| 238 | def valid_user_or_captcha |
||
| 239 | valid_user = @user.valid? |
||
| 240 | valid_captcha = Rails.configuration.recaptcha_enabled ? verify_recaptcha(model: @user) : true |
||
| 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 | def update_roles |
||
| 256 | if current_user.highest_priority_role.can_edit_roles |
||
| 257 | new_roles = params[:user][:role_ids].split(' ').map(&:to_i) |
||
| 258 | old_roles = @user.roles.pluck(:id) |
||
| 259 | |||
| 260 | added_role_ids = new_roles - old_roles |
||
| 261 | removed_role_ids = old_roles - new_roles |
||
| 262 | |||
| 263 | added_roles = [] |
||
| 264 | removed_roles = [] |
||
| 265 | current_user_role = current_user.highest_priority_role |
||
| 266 | |||
| 267 | added_role_ids.each do |id| |
||
| 268 | role = Role.find(id) |
||
| 269 | if (role.priority > current_user_role.priority || current_user_role.name == "admin") && |
||
| 270 | role.provider == @user_domain |
||
| 271 | added_roles << role |
||
| 272 | |||
| 273 | send_user_promoted_email(@user, role.name) if role.send_promoted_email |
||
| 274 | else |
||
| 275 | flash[:alert] = I18n.t("administrator.roles.invalid_assignment") |
||
| 276 | return false |
||
| 277 | end |
||
| 278 | end |
||
| 279 | |||
| 280 | removed_role_ids.each do |id| |
||
| 281 | role = Role.find(id) |
||
| 282 | |||
| 283 | if (role.priority > current_user_role.priority || current_user_role.name == "admin") && |
||
| 284 | role.provider == @user_domain |
||
| 285 | removed_roles << role |
||
| 286 | else |
||
| 287 | flash[:alert] = I18n.t("administrator.roles.invalid_removal") |
||
| 288 | return false |
||
| 289 | end |
||
| 290 | end |
||
| 291 | |||
| 292 | added_roles.each { |role| send_user_demoted_email(@user, role.name) if role.send_demoted_email } |
||
| 293 | removed_roles.each { |role| send_user_demoted_email(@user, role.name) if role.send_demoted_email } |
||
| 294 | |||
| 295 | @user.roles.delete(removed_roles) |
||
| 296 | @user.roles << added_roles |
||
| 297 | |||
| 298 | @user.roles = [Role.find_by(name: "user", provider: @user_domain)] if @user.roles.count.zero? |
||
| 299 | |||
| 300 | @user.save! |
||
| 301 | else |
||
| 302 | true |
||
| 303 | end |
||
| 304 | end |
||
| 305 | end |
||
| 306 |