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
|
# File 'lib/sinatra-authentication.rb', line 7
def self.registered(app)
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
get '/users' do
login_required
redirect "/" unless current_user.admin?
@users = User.all
if @users != []
haml get_view_as_string("index.haml"), :layout => use_layout?
else
redirect '/signup'
end
end
get '/users/:id' do
login_required
@user = User.get(:id => params[:id])
haml get_view_as_string("show.haml"), :layout => use_layout?
end
get '/logged_in' do
if session[:user]
"true"
else
"false"
end
end
get '/login' do
haml get_view_as_string("login.haml"), :layout => use_layout?
end
post '/login' do
if user = User.authenticate(params[:email], params[:password])
session[:user] = user.id
if Rack.const_defined?('Flash')
flash[:notice] = "Login successful."
end
if session[:return_to]
redirect_url = session[:return_to]
session[:return_to] = false
redirect redirect_url
else
redirect '/'
end
else
if Rack.const_defined?('Flash')
flash[:notice] = "The email or password you entered is incorrect."
end
redirect '/login'
end
end
get '/logout' do
session[:user] = nil
if Rack.const_defined?('Flash')
flash[:notice] = "Logout successful."
end
redirect '/'
end
get '/signup' do
haml get_view_as_string("signup.haml"), :layout => use_layout?
end
post '/signup' do
@user = User.set(params[:user])
if @user && @user.id
session[:user] = @user.id
if Rack.const_defined?('Flash')
flash[:notice] = "Account created."
end
redirect '/'
else
if Rack.const_defined?('Flash')
flash[:notice] = 'There were some problems creating your account. Please be sure you\'ve entered all your information correctly.'
end
redirect '/signup'
end
end
get '/users/:id/edit' do
login_required
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
@user = User.get(:id => params[:id])
haml get_view_as_string("edit.haml"), :layout => use_layout?
end
post '/users/:id/edit' do
login_required
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
user = User.get(:id => params[:id])
user_attributes = params[:user]
if params[:user][:password] == ""
user_attributes.delete("password")
user_attributes.delete("password_confirmation")
end
if user.update(user_attributes)
if Rack.const_defined?('Flash')
flash[:notice] = 'Account updated.'
end
redirect '/'
else
if Rack.const_defined?('Flash')
flash[:notice] = 'Whoops, looks like there were some problems with your updates.'
end
redirect "/users/#{user.id}/edit"
end
end
get '/users/:id/delete' do
login_required
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
if User.delete(params[:id])
if Rack.const_defined?('Flash')
flash[:notice] = "User deleted."
end
else
if Rack.const_defined?('Flash')
flash[:notice] = "Deletion failed."
end
end
redirect '/'
end
end
|