-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathsessions_controller.rb
More file actions
163 lines (131 loc) · 4.31 KB
/
Copy pathsessions_controller.rb
File metadata and controls
163 lines (131 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class SessionsController < ApplicationController
def login
redirect_to members_root_path if current_user.try(:general_member?)
end
def github
redirect_to "/auth/github"
end
def google
redirect_to "/auth/google_oauth2"
end
def create
omniauth = request.env["omniauth.auth"]
conditions = {provider: omniauth["provider"],
uid: omniauth["uid"].to_s}
authentication = Authentication.where(conditions).first
if current_user.present? && already_has_auth?(omniauth)
redirect_to members_root_path
elsif current_user.present?
add_auth_and_redirect(omniauth)
elsif authentication.try(:user)
set_session_and_redirect_returning_users(authentication.user)
else
set_auth_session_vars(omniauth)
redirect_to get_email_path
end
end
def destroy
reset_session
redirect_to root_url, notice: "You have logged out"
end
def failure
redirect_to root_url, alert: "Authentication error: #{params[:message].humanize}"
end
def get_email
end
def confirm_email
if new_user?
user = User.create(username: session[:username], email: params[:email])
# If a new user logged in with Google, set email_for_google to the
# email address they used to log in.
if session[:email_for_google].present?
user.email_for_google = session[:email_for_google]
end
if user.save
make_auth(user)
user.make_applicant!
set_user_session(user)
redirect_to edit_application_path(user.application)
else
flash[:message] = user.errors.full_messages.to_sentence
render :get_email
end
else
redirect_to root_path, alert: "It looks like you've previously logged in with a different authentication provider, so try logging in with a different one. Email admin@doubleunion.org for help if that isn't the case!"
end
end
# Development Only
def impersonate
raise unless Rails.env.development?
@all_users = User.all
end
def impersonate_login
raise unless Rails.env.development?
user = User.find(params[:user])
reset_session
set_session_and_redirect_returning_users(user)
end
private
def set_session_and_redirect_returning_users(user)
set_user_session(user)
if user.applicant?
redirect_to(edit_application_path(user.application)) && return
elsif user.former_member?
if Configurable[:accepting_applications]
redirect_to(edit_application_path(user.application)) && return
else
flash[:message] = "As a former member, you can no longer access the members sections."
redirect_to(root_path) && return
end
elsif user.general_member?
redirect_to(members_root_path) && return
else
user.make_applicant!
redirect_to(edit_application_path(user.application)) && return
end
end
def make_auth(user)
authentication = user.authentications.build
authentication.provider = session[:provider]
authentication.uid = session[:uid]
authentication.save!
end
def set_auth_session_vars(omniauth)
omniauth = set_provider(omniauth)
session[:provider] = omniauth.provider
session[:uid] = omniauth.uid
session[:username] = omniauth.try(:username) || omniauth.try(:email)
session[:email_for_google] = omniauth.try(:email) if omniauth.is_a?(GoogleAuth)
end
def add_auth_and_redirect(omniauth)
user = current_user
omniauth = set_provider(omniauth)
authentication = user.authentications.build
authentication.provider = omniauth.provider
authentication.uid = omniauth.uid
flash[:alert] = if authentication.save!
"#{omniauth.provider} authentication added!"
else
"Whoops, something went wrong! Sorry. Email admin@doubleunion.org if this keeps happening."
end
redirect_to edit_members_user_path(user.id)
end
def set_user_session(user)
set_current_user(user)
user.logged_in!
end
def set_provider(omniauth)
if omniauth["provider"] == "github"
GithubAuth.new(omniauth)
elsif omniauth["provider"] == "google_oauth2"
GoogleAuth.new(omniauth)
end
end
def new_user?
User.where(email: params[:email]).empty?
end
def already_has_auth?(omniauth)
current_auths = current_user.authentications.pluck(:provider)
current_auths.include?(omniauth["provider"])
end
end