-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.rb
164 lines (146 loc) · 7.15 KB
/
helpers.rb
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
164
module Sinatra
module ViewHelpers
def linkify_entry(element)
haml("%a{:href=>'/entry/#{element.name}'} #{element.name}")
end
def download_element(element,kind)
haml("%a{:href=>'/entry/#{element.name}/raw/#{kind}.xml'} #{kind}")
end
def edit_entry(element)
haml("%a{:href=>'/entry/#{element.name}/edit'} Edit")
end
end
module AuthHelpers
def current_user
session.delete :authentication_id # Clean up old auth values
begin
if session[:user_id] && session[:authentication_provider]
@current_auth ||= Authentication.first(:user_id => session[:user_id],
:provider => session[:authentication_provider]
)
@current_user ||= @current_auth.user
end
return @current_user if @current_user
rescue # => Invalid cookie value formats?
@current_user = nil
@current_auth = nil
end
# Clean up any old/bad cookie values:
session.delete :user_id
session.delete :authentication_provider
end
def current_auth
current_user
@current_auth
end
def authenticate
authentication_route = params[:name] ? params[:name] : 'No authentication recognized (invalid callback)'
# get the full hash from omniauth
omniauth = request.env['omniauth.auth']
# continue only if hash and parameter exist
unless omniauth and params[:name]
flash[:error] = "Error while authenticating via #{authentication_route.capitalize}. The authentication did not return valid data."
redirect to '/'
end
# create a new regularised authentication hash
@authhash = Hash.new
oaeuh = omniauth['extra'] && (omniauth['extra']['user_hash'] ||
omniauth['extra']['raw_info'])
oaui = omniauth['user_info'] || omniauth['info']
case authentication_route
when "facebook"
@authhash[:email] = oaeuh['email'] || ''
@authhash[:name] = oaeuh['name'] || ''
@authhash[:uid] = oaeuh['name'] || ''
@authhash[:provider] = omniauth['provider'] || ''
when "twitter"
@authhash[:email] = oaui['email'] || ''
@authhash[:name] = omniauth['info']['nickname'] || ''
# @authhash[:nick] = oaui['screen_name'] || ''
@authhash[:uid] = (oaeuh['id'] || '').to_s
@authhash[:provider] = omniauth['provider'] || ''
when 'github'
@authhash[:email] = oaui['email'] || ''
@authhash[:name] = oaui['name'] || ''
@authhash[:uid] = (oaeuh['id'] || '').to_s
@authhash[:provider] = omniauth['provider'] || ''
when 'google', 'yahoo', 'linked_in', 'myopenid', 'openid', 'open_id'
@authhash[:email] = oaui['email'] || ''
@authhash[:name] = oaui['name'] || ''
@authhash[:uid] = (omniauth['uid'] || '').to_s
@authhash[:provider] = omniauth['provider'] || ''
when 'aol'
@authhash[:email] = oaui['email'] || ''
@authhash[:name] = oaui['name'] || ''
@authhash[:uid] = (omniauth['uid'] || '').to_s
@authhash[:provider] = omniauth['provider'] || ''
else
# REVISIT: debug to output the hash that has been returned when adding new authentications
return '<pre>'+omniauth.to_yaml+'</pre>'
end
if @authhash[:uid].empty? or @authhash[:provider].empty?
flash[:error] = 'Error while authenticating via #{authentication_route}/#{@authhash[:provider].capitalize} The authentication returned invalid data for the user id.'
redirect to(session[:return_to])
end
auth = Authentication.first(:provider => @authhash[:provider],
:uid => @authhash[:uid])
# if the user is currently signed in, he/she might want to add
# another account to signin
unless current_user.nil?
if auth
flash[:notice] = "You are now signed in using your #{@authhash[:provider].capitalize} account"
session[:user_id] ||= auth.user.id
session[:authentication_provider] = auth.provider # They're now signed in using the new account
redirect to(session[:return_to]) # Already signed in, and we already had this authentication
else
auth = current_user.authentications.create!(:provider => @authhash[:provider], :uid => @authhash[:uid], :user_name => @authhash[:name], :user_email => @authhash[:email])
flash[:notice] = 'Your ' + @authhash[:provider].capitalize + ' account has been added for signing in at this site.'
session[:authentication_provider] = auth.provider # They're now signed in using the new account
session[:user_name] = @authhash[:name] unless @authhash[:name].empty?
redirect to(session[:return_to])
end
else
if auth
# Signin existing user
# in the session his user id and the authentication id used for signing in is stored
session[:user_id] = auth.user.id
session[:authentication_provider] = auth.provider # They're now signed in using the new account
session[:user_name] = @authhash[:name] if @authhash[:name] != ''
flash[:notice] = 'Signed in successfully via ' + @authhash[:provider].capitalize + '.'
redirect to(session[:return_to])
end
if email = @authhash[:email] and email != '' and
auth = Authentication::first(:user_email => email)
# Would have been seen as a new user, but instead we found that we know their email address already
provider = @authhash[:provider]
auth = auth.user.authentications.create!(
:provider => provider,
:uid => @authhash[:uid],
:user_name => @authhash[:name],
:user_email => @authhash[:email]
)
flash[:notice] = 'Your ' + provider.capitalize + ' account has been added for signing in at this site.'
session[:user_id] = auth.user.id
session[:authentication_provider] = auth.provider # They're now signed in using the new account
session[:user_name] = @authhash[:name] if @authhash[:name] != ''
redirect to(session[:return_to])
end
# this is a new user; add them
@current_user = User.create
session[:user_id] = @current_user.id
session[:user_name] = @authhash[:name] if @authhash[:name] != ''
auth = current_user.authentications.create!(
:provider => @authhash[:provider],
:uid => @authhash[:uid],
:user_name => @authhash[:name],
:user_email => @authhash[:email]
)
session[:authentication_provider] = auth.provider
# puts env['omniauth.auth'].to_yaml
end
end
def authenticated?
! session[:user_id].nil?
end
end
end