Completed
Branch v2.4-alpha (b4736b)
by Ahmad
05:54
created

SessionsController.process_signin()   C

Complexity

Conditions 11

Size

Total Lines 36

Duplication

Lines 1
Ratio 2.78 %

Importance

Changes 0
Metric Value
cc 11
dl 1
loc 36
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like SessionsController.process_signin() 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
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
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
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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