Completed
Branch more-refac (5b2fc3)
by Ahmad
03:46
created

Authenticator.check_email_verified()   B

Complexity

Conditions 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 21
rs 8.4426
c 0
b 0
f 0
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
module Authenticator
20
  extend ActiveSupport::Concern
21
22
  # Logs a user into GreenLight.
23
  def login(user)
24
    migrate_twitter_user(user)
25
26
    session[:user_id] = user.id
27
28
    # If there are not terms, or the user has accepted them, check for email verification
29
    if !Rails.configuration.terms || user.accepted_terms
30
      check_email_verified(user)
31
    else
32
      redirect_to terms_path
33
    end
34
  end
35
36
  # If email verification is disabled, or the user has verified, go to their room
37
  def check_email_verified(user)
38
    # Admin users should be redirected to the admin page
39
    if user.has_role? :super_admin
40
      redirect_to admins_path
41
    elsif user.activated?
42
      # Dont redirect to any of these urls
43
      dont_redirect_to = [root_url, signin_url, signup_url, unauthorized_url, internal_error_url, not_found_url]
44
      url = if cookies[:return_to] && !dont_redirect_to.include?(cookies[:return_to])
45
        cookies[:return_to]
46
      else
47
        user.main_room
48
      end
49
50
      # Delete the cookie if it exists
51
      cookies.delete :return_to if cookies[:return_to]
52
53
      redirect_to url
54
    else
55
      redirect_to resend_path
56
    end
57
  end
58
59
  def ensure_unauthenticated_except_twitter
60
    redirect_to current_user.main_room if current_user && params[:old_twitter_user_id].nil?
61
  end
62
63
  # Logs current user out of GreenLight.
64
  def logout
65
    session.delete(:user_id) if current_user
66
  end
67
68
  private
69
70
  # Migrates all of the twitter users rooms to the new account
71
  def migrate_twitter_user(user)
72
    if !session["old_twitter_user_id"].nil? && user.provider != "twitter"
73
      old_user = User.find(session["old_twitter_user_id"])
74
75
      old_user.rooms.each do |room|
76
        room.owner = user
77
78
        room.name = "Old " + room.name if room.id == old_user.main_room.id
79
80
        room.save!
81
      end
82
83
      # Query for the old user again so the migrated rooms don't get deleted
84
      old_user.reload
85
      old_user.destroy!
86
87
      session["old_twitter_user_id"] = nil
88
89
      flash[:success] = I18n.t("registration.deprecated.merge_success")
90
    end
91
  end
92
end
93