| Total Complexity | 49 |
| Total Lines | 210 |
| Duplicated Lines | 0.48 % |
| Changes | 3 | ||
| 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 SessionsController 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 SessionsController < ApplicationController |
||
| 20 | include Authenticator |
||
| 21 | include Registrar |
||
| 22 | include Emailer |
||
| 23 | include LdapAuthenticator |
||
| 24 | |||
| 25 | skip_before_action :verify_authenticity_token, only: [:omniauth, :fail] |
||
| 26 | before_action :check_user_signup_allowed, only: [:new] |
||
| 27 | before_action :ensure_unauthenticated_except_twitter, only: [:new, :signin] |
||
| 28 | |||
| 29 | # GET /signin |
||
| 30 | def signin |
||
| 31 | check_if_twitter_account |
||
| 32 | |||
| 33 | if one_provider |
||
| 34 | provider_path = if Rails.configuration.omniauth_ldap |
||
| 35 | ldap_signin_path |
||
| 36 | else |
||
| 37 | "#{Rails.configuration.relative_url_root}/auth/#{providers.first}" |
||
| 38 | end |
||
| 39 | |||
| 40 | return redirect_to provider_path |
||
| 41 | end |
||
| 42 | end |
||
| 43 | |||
| 44 | # GET /ldap_signin |
||
| 45 | def ldap_signin |
||
| 46 | end |
||
| 47 | |||
| 48 | # GET /signup |
||
| 49 | def new |
||
| 50 | # Check if the user needs to be invited |
||
| 51 | if invite_registration |
||
| 52 | redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token] |
||
| 53 | |||
| 54 | session[:invite_token] = params[:invite_token] |
||
| 55 | end |
||
| 56 | |||
| 57 | check_if_twitter_account(true) |
||
| 58 | |||
| 59 | @user = User.new |
||
| 60 | end |
||
| 61 | |||
| 62 | # POST /users/login |
||
| 63 | def create |
||
| 64 | logger.info "Support: #{session_params[:email]} is attempting to login." |
||
| 65 | |||
| 66 | user = User.include_deleted.find_by(email: session_params[:email]) |
||
| 67 | |||
| 68 | is_super_admin = user&.has_role? :super_admin |
||
| 69 | |||
| 70 | # Scope user to domain if the user is not a super admin |
||
| 71 | user = User.include_deleted.find_by(email: session_params[:email], provider: @user_domain) unless is_super_admin |
||
| 72 | |||
| 73 | # Check user with that email exists |
||
| 74 | return redirect_to(signin_path, alert: I18n.t("invalid_credentials")) unless user |
||
| 75 | # Check correct password was entered |
||
| 76 | return redirect_to(signin_path, alert: I18n.t("invalid_credentials")) unless user.try(:authenticate, |
||
| 77 | session_params[:password]) |
||
| 78 | # Check that the user is not deleted |
||
| 79 | return redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") } if user.deleted? |
||
| 80 | |||
| 81 | unless is_super_admin |
||
| 82 | # Check that the user is a Greenlight account |
||
| 83 | return redirect_to(root_path, alert: I18n.t("invalid_login_method")) unless user.greenlight_account? |
||
| 84 | # Check that the user has verified their account |
||
| 85 | return redirect_to(account_activation_path(email: user.email)) unless user.activated? |
||
| 86 | end |
||
| 87 | |||
| 88 | login(user) |
||
| 89 | end |
||
| 90 | |||
| 91 | # GET /users/logout |
||
| 92 | def destroy |
||
| 93 | logout |
||
| 94 | redirect_to root_path |
||
| 95 | end |
||
| 96 | |||
| 97 | # GET/POST /auth/:provider/callback |
||
| 98 | def omniauth |
||
| 99 | @auth = request.env['omniauth.auth'] |
||
| 100 | |||
| 101 | begin |
||
| 102 | process_signin |
||
| 103 | rescue => e |
||
| 104 | logger.error "Error authenticating via omniauth: #{e}" |
||
| 105 | omniauth_fail |
||
| 106 | end |
||
| 107 | end |
||
| 108 | |||
| 109 | # POST /auth/failure |
||
| 110 | def omniauth_fail |
||
| 111 | if params[:message].nil? |
||
| 112 | redirect_to root_path, alert: I18n.t("omniauth_error") |
||
| 113 | else |
||
| 114 | redirect_to root_path, alert: I18n.t("omniauth_specific_error", error: params["message"]) |
||
| 115 | end |
||
| 116 | end |
||
| 117 | |||
| 118 | # GET /auth/ldap |
||
| 119 | def ldap |
||
| 120 | ldap_config = {} |
||
| 121 | ldap_config[:host] = ENV['LDAP_SERVER'] |
||
| 122 | ldap_config[:port] = ENV['LDAP_PORT'].to_i != 0 ? ENV['LDAP_PORT'].to_i : 389 |
||
| 123 | ldap_config[:bind_dn] = ENV['LDAP_BIND_DN'] |
||
| 124 | ldap_config[:password] = ENV['LDAP_PASSWORD'] |
||
| 125 | ldap_config[:encryption] = if ENV['LDAP_METHOD'] == 'ssl' |
||
| 126 | 'simple_tls' |
||
| 127 | elsif ENV['LDAP_METHOD'] == 'tls' |
||
| 128 | 'start_tls' |
||
| 129 | end |
||
| 130 | ldap_config[:base] = ENV['LDAP_BASE'] |
||
| 131 | ldap_config[:uid] = ENV['LDAP_UID'] |
||
| 132 | |||
| 133 | result = send_ldap_request(params[:session], ldap_config) |
||
| 134 | |||
| 135 | return redirect_to(ldap_signin_path, alert: I18n.t("invalid_credentials")) unless result |
||
| 136 | |||
| 137 | @auth = parse_auth(result.first, ENV['LDAP_ROLE_FIELD']) |
||
| 138 | |||
| 139 | begin |
||
| 140 | process_signin |
||
| 141 | rescue => e |
||
| 142 | logger.error "Support: Error authenticating via omniauth: #{e}" |
||
| 143 | omniauth_fail |
||
| 144 | end |
||
| 145 | end |
||
| 146 | |||
| 147 | private |
||
| 148 | |||
| 149 | # Verify that GreenLight is configured to allow user signup. |
||
| 150 | def check_user_signup_allowed |
||
| 151 | redirect_to root_path unless Rails.configuration.allow_user_signup |
||
| 152 | end |
||
| 153 | |||
| 154 | def session_params |
||
| 155 | params.require(:session).permit(:email, :password) |
||
| 156 | end |
||
| 157 | |||
| 158 | def one_provider |
||
| 159 | providers = configured_providers |
||
| 160 | |||
| 161 | (!allow_user_signup? || !allow_greenlight_accounts?) && providers.count == 1 && |
||
| 162 | !Rails.configuration.loadbalanced_configuration |
||
| 163 | end |
||
| 164 | |||
| 165 | def check_user_exists |
||
| 166 | User.exists?(social_uid: @auth['uid'], provider: current_provider) |
||
| 167 | end |
||
| 168 | |||
| 169 | def check_user_deleted(email) |
||
| 170 | User.deleted.exists?(email: email, provider: @user_domain) |
||
| 171 | end |
||
| 172 | |||
| 173 | def check_auth_deleted |
||
| 174 | User.deleted.exists?(social_uid: @auth['uid'], provider: current_provider) |
||
| 175 | end |
||
| 176 | |||
| 177 | def current_provider |
||
| 178 | @auth['provider'] == "bn_launcher" ? @auth['info']['customer'] : @auth['provider'] |
||
| 179 | end |
||
| 180 | |||
| 181 | # Check if the user already exists, if not then check for invitation |
||
| 182 | def passes_invite_reqs |
||
| 183 | return true if @user_exists |
||
| 184 | |||
| 185 | invitation = check_user_invited("", session[:invite_token], @user_domain) |
||
| 186 | invitation[:present] |
||
| 187 | end |
||
| 188 | |||
| 189 | def process_signin |
||
| 190 | @user_exists = check_user_exists |
||
| 191 | |||
| 192 | if !@user_exists && @auth['provider'] == "twitter" |
||
| 193 | return redirect_to root_path, flash: { alert: I18n.t("registration.deprecated.twitter_signup") } |
||
| 194 | end |
||
| 195 | |||
| 196 | # Check if user is deleted |
||
| 197 | return redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") } if check_auth_deleted |
||
| 198 | |||
| 199 | # If using invitation registration method, make sure user is invited |
||
| 200 | View Code Duplication | return redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless passes_invite_reqs |
|
|
|
|||
| 201 | |||
| 202 | user = User.from_omniauth(@auth) |
||
| 203 | |||
| 204 | logger.info "Support: Auth user #{user.email} is attempting to login." |
||
| 205 | |||
| 206 | # Add pending role if approval method and is a new user |
||
| 207 | if approval_registration && !@user_exists |
||
| 208 | user.add_role :pending |
||
| 209 | |||
| 210 | # Inform admins that a user signed up if emails are turned on |
||
| 211 | send_approval_user_signup_email(user) |
||
| 212 | |||
| 213 | return redirect_to root_path, flash: { success: I18n.t("registration.approval.signup") } |
||
| 214 | end |
||
| 215 | |||
| 216 | send_invite_user_signup_email(user) if invite_registration && !@user_exists |
||
| 217 | |||
| 218 | login(user) |
||
| 219 | |||
| 220 | if @auth['provider'] == "twitter" |
||
| 221 | flash[:alert] = if allow_user_signup? && allow_greenlight_accounts? |
||
| 222 | I18n.t("registration.deprecated.twitter_signin", link: signup_path(old_twitter_user_id: user.id)) |
||
| 223 | else |
||
| 224 | I18n.t("registration.deprecated.twitter_signin", link: signin_path(old_twitter_user_id: user.id)) |
||
| 225 | end |
||
| 226 | end |
||
| 227 | end |
||
| 228 | end |
||
| 229 |