| Total Complexity | 37 |
| Total Lines | 139 |
| Duplicated Lines | 0.72 % |
| Changes | 1 | ||
| 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:
| 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 | |||
| 27 | # POST /users/login |
||
| 28 | def create |
||
| 29 | logger.info "Support: #{session_params[:email]} is attempting to login." |
||
| 30 | |||
| 31 | admin = User.find_by(email: session_params[:email]) |
||
| 32 | if admin&.has_role? :super_admin |
||
| 33 | user = admin |
||
| 34 | else |
||
| 35 | user = User.find_by(email: session_params[:email], provider: @user_domain) |
||
| 36 | redirect_to(signin_path, alert: I18n.t("invalid_credentials")) && return unless user |
||
| 37 | redirect_to(root_path, alert: I18n.t("invalid_login_method")) && return unless user.greenlight_account? |
||
| 38 | redirect_to(account_activation_path(email: user.email)) && return unless user.activated? |
||
| 39 | end |
||
| 40 | redirect_to(signin_path, alert: I18n.t("invalid_credentials")) && return unless user.try(:authenticate, |
||
| 41 | session_params[:password]) |
||
| 42 | |||
| 43 | login(user) |
||
| 44 | end |
||
| 45 | |||
| 46 | # GET /users/logout |
||
| 47 | def destroy |
||
| 48 | logout |
||
| 49 | redirect_to root_path |
||
| 50 | end |
||
| 51 | |||
| 52 | # GET/POST /auth/:provider/callback |
||
| 53 | def omniauth |
||
| 54 | @auth = request.env['omniauth.auth'] |
||
| 55 | |||
| 56 | begin |
||
| 57 | process_signin |
||
| 58 | rescue => e |
||
| 59 | logger.error "Error authenticating via omniauth: #{e}" |
||
| 60 | omniauth_fail |
||
| 61 | end |
||
| 62 | end |
||
| 63 | |||
| 64 | # POST /auth/failure |
||
| 65 | def omniauth_fail |
||
| 66 | if params[:message].nil? |
||
| 67 | redirect_to root_path, alert: I18n.t("omniauth_error") |
||
| 68 | else |
||
| 69 | redirect_to root_path, alert: I18n.t("omniauth_specific_error", error: params["message"]) |
||
| 70 | end |
||
| 71 | end |
||
| 72 | |||
| 73 | # GET /auth/ldap |
||
| 74 | def ldap |
||
| 75 | ldap_config = {} |
||
| 76 | ldap_config[:host] = ENV['LDAP_SERVER'] |
||
| 77 | ldap_config[:port] = ENV['LDAP_PORT'].to_i != 0 ? ENV['LDAP_PORT'].to_i : 389 |
||
| 78 | ldap_config[:bind_dn] = ENV['LDAP_BIND_DN'] |
||
| 79 | ldap_config[:password] = ENV['LDAP_PASSWORD'] |
||
| 80 | ldap_config[:encryption] = if ENV['LDAP_METHOD'] == 'ssl' |
||
| 81 | 'simple_tls' |
||
| 82 | elsif ENV['LDAP_METHOD'] == 'tls' |
||
| 83 | 'start_tls' |
||
| 84 | end |
||
| 85 | ldap_config[:base] = ENV['LDAP_BASE'] |
||
| 86 | ldap_config[:uid] = ENV['LDAP_UID'] |
||
| 87 | |||
| 88 | result = send_ldap_request(params[:session], ldap_config) |
||
| 89 | |||
| 90 | return redirect_to(ldap_signin_path, alert: I18n.t("invalid_credentials")) unless result |
||
| 91 | |||
| 92 | @auth = parse_auth(result.first, ENV['LDAP_ROLE_FIELD']) |
||
| 93 | |||
| 94 | begin |
||
| 95 | process_signin |
||
| 96 | rescue => e |
||
| 97 | logger.error "Support: Error authenticating via omniauth: #{e}" |
||
| 98 | omniauth_fail |
||
| 99 | end |
||
| 100 | end |
||
| 101 | |||
| 102 | private |
||
| 103 | |||
| 104 | def session_params |
||
| 105 | params.require(:session).permit(:email, :password) |
||
| 106 | end |
||
| 107 | |||
| 108 | def check_user_exists |
||
| 109 | provider = @auth['provider'] == "bn_launcher" ? @auth['info']['customer'] : @auth['provider'] |
||
| 110 | User.exists?(social_uid: @auth['uid'], provider: provider) |
||
| 111 | end |
||
| 112 | |||
| 113 | # Check if the user already exists, if not then check for invitation |
||
| 114 | def passes_invite_reqs |
||
| 115 | return true if @user_exists |
||
| 116 | |||
| 117 | invitation = check_user_invited("", session[:invite_token], @user_domain) |
||
| 118 | invitation[:present] |
||
| 119 | end |
||
| 120 | |||
| 121 | def process_signin |
||
| 122 | @user_exists = check_user_exists |
||
| 123 | |||
| 124 | if !@user_exists && @auth['provider'] == "twitter" |
||
| 125 | return redirect_to root_path, flash: { alert: I18n.t("registration.deprecated.twitter_signup") } |
||
| 126 | end |
||
| 127 | |||
| 128 | # If using invitation registration method, make sure user is invited |
||
| 129 | View Code Duplication | return redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless passes_invite_reqs |
|
|
1 ignored issue
–
show
|
|||
| 130 | |||
| 131 | user = User.from_omniauth(@auth) |
||
| 132 | |||
| 133 | logger.info "Support: Auth user #{user.email} is attempting to login." |
||
| 134 | |||
| 135 | # Add pending role if approval method and is a new user |
||
| 136 | if approval_registration && !@user_exists |
||
| 137 | user.add_role :pending |
||
| 138 | |||
| 139 | # Inform admins that a user signed up if emails are turned on |
||
| 140 | send_approval_user_signup_email(user) |
||
| 141 | |||
| 142 | return redirect_to root_path, flash: { success: I18n.t("registration.approval.signup") } |
||
| 143 | end |
||
| 144 | |||
| 145 | send_invite_user_signup_email(user) if invite_registration && !@user_exists |
||
| 146 | |||
| 147 | login(user) |
||
| 148 | |||
| 149 | if @auth['provider'] == "twitter" |
||
| 150 | flash[:alert] = if allow_user_signup? && allow_greenlight_accounts? |
||
| 151 | I18n.t("registration.deprecated.twitter_signin", link: signup_path(old_twitter_user_id: user.id)) |
||
| 152 | else |
||
| 153 | I18n.t("registration.deprecated.twitter_signin", link: signin_path(old_twitter_user_id: user.id)) |
||
| 154 | end |
||
| 155 | end |
||
| 156 | end |
||
| 157 | end |
||
| 158 |