| Total Complexity | 54 |
| Total Lines | 236 |
| Duplicated Lines | 2.97 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
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, :update_password] |
||
| 28 | before_action :ensure_unauthenticated_except_twitter, only: [:create] |
||
| 29 | before_action :check_user_signup_allowed, only: [:create] |
||
| 30 | before_action :check_admin_of, only: [:edit, :change_password, :delete_account] |
||
| 31 | |||
| 32 | # POST /u |
||
| 33 | def create |
||
| 34 | @user = User.new(user_params) |
||
| 35 | @user.provider = @user_domain |
||
| 36 | |||
| 37 | # User or recpatcha is not valid |
||
| 38 | render("sessions/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 | logger.info "Support: #{@user.email} user has been created." |
||
| 47 | |||
| 48 | # Set user to pending and redirect if Approval Registration is set |
||
| 49 | if approval_registration |
||
| 50 | @user.set_role :pending |
||
| 51 | |||
| 52 | return redirect_to root_path, |
||
| 53 | flash: { success: I18n.t("registration.approval.signup") } unless Rails.configuration.enable_email_verification |
||
| 54 | end |
||
| 55 | |||
| 56 | send_registration_email |
||
| 57 | |||
| 58 | # Sign in automatically if email verification is disabled or if user is already verified. |
||
| 59 | if !Rails.configuration.enable_email_verification || @user.email_verified |
||
| 60 | @user.set_role(initial_user_role(@user.email)) |
||
| 61 | |||
| 62 | login(@user) && return |
||
| 63 | end |
||
| 64 | |||
| 65 | send_activation_email(@user, @user.create_activation_token) |
||
| 66 | |||
| 67 | redirect_to root_path |
||
| 68 | end |
||
| 69 | |||
| 70 | # GET /u/:user_uid/edit |
||
| 71 | def edit |
||
| 72 | redirect_to root_path unless current_user |
||
| 73 | end |
||
| 74 | |||
| 75 | # GET /u/:user_uid/change_password |
||
| 76 | def change_password |
||
| 77 | redirect_to edit_user_path unless current_user.greenlight_account? |
||
| 78 | end |
||
| 79 | |||
| 80 | # GET /u/:user_uid/delete_account |
||
| 81 | def delete_account |
||
| 82 | end |
||
| 83 | |||
| 84 | # POST /u/:user_uid/edit |
||
| 85 | def update |
||
| 86 | View Code Duplication | if session[:prev_url].present? |
|
| 87 | path = session[:prev_url] |
||
| 88 | session.delete(:prev_url) |
||
| 89 | else |
||
| 90 | path = admins_path |
||
| 91 | end |
||
| 92 | |||
| 93 | redirect_path = current_user.admin_of?(@user, "can_manage_users") ? path : edit_user_path(@user) |
||
| 94 | |||
| 95 | unless can_edit_user?(@user, current_user) |
||
| 96 | params[:user][:name] = @user.name |
||
| 97 | params[:user][:email] = @user.email |
||
| 98 | end |
||
| 99 | |||
| 100 | if @user.update_attributes(user_params) |
||
| 101 | @user.update_attributes(email_verified: false) if user_params[:email] != @user.email |
||
| 102 | |||
| 103 | user_locale(@user) |
||
| 104 | |||
| 105 | if update_roles(params[:user][:role_id]) |
||
| 106 | return redirect_to redirect_path, flash: { success: I18n.t("info_update_success") } |
||
| 107 | else |
||
| 108 | flash[:alert] = I18n.t("administrator.roles.invalid_assignment") |
||
| 109 | end |
||
| 110 | end |
||
| 111 | |||
| 112 | render :edit |
||
| 113 | end |
||
| 114 | |||
| 115 | # POST /u/:user_uid/change_password |
||
| 116 | def update_password |
||
| 117 | # Update the users password. |
||
| 118 | if @user.authenticate(user_params[:password]) |
||
| 119 | # Verify that the new passwords match. |
||
| 120 | if user_params[:new_password] == user_params[:password_confirmation] |
||
| 121 | @user.password = user_params[:new_password] |
||
| 122 | else |
||
| 123 | # New passwords don't match. |
||
| 124 | @user.errors.add(:password_confirmation, "doesn't match") |
||
| 125 | end |
||
| 126 | else |
||
| 127 | # Original password is incorrect, can't update. |
||
| 128 | @user.errors.add(:password, "is incorrect") |
||
| 129 | end |
||
| 130 | |||
| 131 | # Notify the user that their account has been updated. |
||
| 132 | return redirect_to change_password_path, |
||
| 133 | flash: { success: I18n.t("info_update_success") } if @user.errors.empty? && @user.save |
||
| 134 | |||
| 135 | # redirect_to change_password_path |
||
| 136 | render :change_password |
||
| 137 | end |
||
| 138 | |||
| 139 | # DELETE /u/:user_uid |
||
| 140 | def destroy |
||
| 141 | # Include deleted users in the check |
||
| 142 | admin_path = request.referer.present? ? request.referer : admins_path |
||
| 143 | @user = User.include_deleted.find_by(uid: params[:user_uid]) |
||
| 144 | |||
| 145 | logger.info "Support: #{current_user.email} is deleting #{@user.email}." |
||
| 146 | |||
| 147 | self_delete = current_user == @user |
||
| 148 | redirect_url = self_delete ? root_path : admin_path |
||
| 149 | |||
| 150 | begin |
||
| 151 | if current_user && (self_delete || current_user.admin_of?(@user, "can_manage_users")) |
||
| 152 | # Permanently delete if the user is deleting themself |
||
| 153 | perm_delete = self_delete || (params[:permanent].present? && params[:permanent] == "true") |
||
| 154 | |||
| 155 | # Permanently delete the rooms under the user if they have not been reassigned |
||
| 156 | if perm_delete |
||
| 157 | @user.rooms.include_deleted.each do |room| |
||
| 158 | # Destroy all recordings then permanently delete the room |
||
| 159 | delete_all_recordings(room.bbb_id) |
||
| 160 | room.destroy(true) |
||
| 161 | end |
||
| 162 | end |
||
| 163 | |||
| 164 | @user.destroy(perm_delete) |
||
| 165 | |||
| 166 | # Log the user out if they are deleting themself |
||
| 167 | session.delete(:user_id) if self_delete |
||
| 168 | |||
| 169 | return redirect_to redirect_url, flash: { success: I18n.t("administrator.flash.delete") } unless self_delete |
||
| 170 | else |
||
| 171 | flash[:alert] = I18n.t("administrator.flash.delete_fail") |
||
| 172 | end |
||
| 173 | rescue => e |
||
| 174 | logger.error "Support: Error in user deletion: #{e}" |
||
| 175 | flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail")) |
||
| 176 | end |
||
| 177 | |||
| 178 | redirect_to redirect_url |
||
| 179 | end |
||
| 180 | |||
| 181 | # GET /u/:user_uid/recordings |
||
| 182 | def recordings |
||
| 183 | if current_user && current_user.uid == params[:user_uid] |
||
| 184 | @search, @order_column, @order_direction, recs = |
||
| 185 | all_recordings(current_user.rooms.pluck(:bbb_id), params.permit(:search, :column, :direction), true) |
||
| 186 | @pagy, @recordings = pagy_array(recs) |
||
| 187 | else |
||
| 188 | redirect_to root_path |
||
| 189 | end |
||
| 190 | end |
||
| 191 | |||
| 192 | # GET | POST /terms |
||
| 193 | def terms |
||
| 194 | redirect_to '/404' unless Rails.configuration.terms |
||
| 195 | |||
| 196 | if params[:accept] == "true" |
||
| 197 | current_user.update_attributes(accepted_terms: true) |
||
| 198 | login(current_user) |
||
| 199 | end |
||
| 200 | end |
||
| 201 | |||
| 202 | # GET /shared_access_list |
||
| 203 | def shared_access_list |
||
| 204 | # Don't allow searchs unless atleast 3 characters are passed |
||
| 205 | return redirect_to '/404' if params[:search].length < 3 |
||
| 206 | |||
| 207 | roles_can_appear = [] |
||
| 208 | Role.where(provider: @user_domain).each do |role| |
||
| 209 | roles_can_appear << role.name if role.get_permission("can_appear_in_share_list") && role.priority >= 0 |
||
| 210 | end |
||
| 211 | |||
| 212 | initial_list = User.where.not(uid: params[:owner_uid]) |
||
| 213 | .with_role(roles_can_appear) |
||
| 214 | .shared_list_search(params[:search]) |
||
| 215 | |||
| 216 | initial_list = initial_list.where(provider: @user_domain) if Rails.configuration.loadbalanced_configuration |
||
| 217 | |||
| 218 | # Respond with JSON object of users |
||
| 219 | respond_to do |format| |
||
| 220 | format.json { render body: initial_list.pluck_to_hash(:uid, :name).to_json } |
||
| 221 | end |
||
| 222 | end |
||
| 223 | |||
| 224 | private |
||
| 225 | |||
| 226 | def find_user |
||
| 227 | @user = User.find_by(uid: params[:user_uid]) |
||
| 228 | end |
||
| 229 | |||
| 230 | # Verify that GreenLight is configured to allow user signup. |
||
| 231 | def check_user_signup_allowed |
||
| 232 | redirect_to root_path unless Rails.configuration.allow_user_signup |
||
| 233 | end |
||
| 234 | |||
| 235 | def user_params |
||
| 236 | params.require(:user).permit(:name, :email, :image, :password, :password_confirmation, |
||
| 237 | :new_password, :provider, :accepted_terms, :language) |
||
| 238 | end |
||
| 239 | |||
| 240 | def send_registration_email |
||
| 241 | if invite_registration |
||
| 242 | send_invite_user_signup_email(@user) |
||
| 243 | elsif approval_registration |
||
| 244 | send_approval_user_signup_email(@user) |
||
| 245 | end |
||
| 246 | end |
||
| 247 | |||
| 248 | # Checks that the user is allowed to edit this user |
||
| 249 | def check_admin_of |
||
| 250 | redirect_to root_path if current_user && |
||
| 251 | @user != current_user && |
||
| 252 | !current_user.admin_of?(@user, "can_manage_users") |
||
| 253 | end |
||
| 254 | end |
||
| 255 |