1 class UserController < ApplicationController
4 before_filter :authorize, :only => [:api_details, :api_gpx_files]
5 before_filter :set_locale, :except => [:api_details, :api_gpx_files]
6 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
7 before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
8 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
9 before_filter :check_database_writable, :only => [:login, :new, :set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
10 before_filter :check_api_readable, :only => [:api_details, :api_gpx_files]
12 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
15 @title = 'create account'
17 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
18 render :action => 'new'
20 @user = User.new(params[:user])
23 @user.data_public = true
24 @user.description = "" if @user.description.nil?
25 @user.creation_ip = request.remote_ip
26 @user.locale = Language.find_by_code(I18n.locale.to_s)
27 @user.locale = Language.find_by_code("en") if @user.locale.nil?
30 flash[:notice] = I18n.t('user.new.flash create success message')
31 Notifier.deliver_signup_confirm(@user, @user.tokens.create)
32 redirect_to :action => 'login'
34 render :action => 'new'
40 @title = 'edit account'
41 if params[:user] and params[:user][:display_name] and params[:user][:description]
42 if params[:user][:email] != @user.email
43 @user.new_email = params[:user][:email]
46 @user.display_name = params[:user][:display_name]
48 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
49 @user.pass_crypt = params[:user][:pass_crypt]
50 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
53 @user.description = params[:user][:description]
54 @user.home_lat = params[:user][:home_lat]
55 @user.home_lon = params[:user][:home_lon]
58 if params[:user][:email] == @user.new_email
59 flash[:notice] = I18n.t('user.account.flash update success confirm needed')
60 Notifier.deliver_email_confirm(@user, @user.tokens.create)
62 flash[:notice] = I18n.t('user.account.flash update success')
69 if params[:user][:home_lat] and params[:user][:home_lon]
70 @user.home_lat = params[:user][:home_lat].to_f
71 @user.home_lon = params[:user][:home_lon].to_f
73 flash[:notice] = I18n.t('user.set_home.flash success')
74 redirect_to :controller => 'user', :action => 'account'
80 @user.data_public = true
82 flash[:notice] = I18n.t('user.go_public.flash success')
83 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
87 @title = I18n.t('user.lost_password.title')
88 if params[:user] and params[:user][:email]
89 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
92 token = user.tokens.create
93 Notifier.deliver_lost_password(user, token)
94 flash[:notice] = I18n.t('user.lost_password.notice.email on way')
96 flash[:notice] = I18n.t('user.lost_password.notice email cannot find')
102 @title = I18n.t('user.reset_password.title')
104 token = UserToken.find_by_token(params[:token])
106 pass = OSM::make_token(8)
108 user.pass_crypt = pass
109 user.pass_crypt_confirmation = pass
111 user.email_valid = true
114 Notifier.deliver_reset_password(user, pass)
115 flash[:notice] = I18n.t('user.reset_password.flash changed check mail')
117 flash[:notice] = I18n.t('user.reset_password.flash token bad')
121 redirect_to :action => 'login'
125 @title = 'create account'
126 # The user is logged in already, so don't show them the signup page, instead
127 # send them to the home page
128 redirect_to :controller => 'site', :action => 'index' if session[:user]
133 # The user is logged in already, if the referer param exists, redirect them to that
135 redirect_to params[:referer]
137 redirect_to :controller => 'site', :action => 'index'
143 email_or_display_name = params[:user][:email]
144 pass = params[:user][:password]
145 user = User.authenticate(:username => email_or_display_name, :password => pass)
147 session[:user] = user.id
149 redirect_to params[:referer]
151 redirect_to :controller => 'site', :action => 'index'
154 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
155 @notice = "Sorry, your account is not active yet.<br>Please click on the link in the account confirmation email to activate your account."
157 @notice = "Sorry, couldn't log in with those details."
164 token = UserToken.find_by_token(session[:token])
168 session[:token] = nil
172 redirect_to params[:referer]
174 redirect_to :controller => 'site', :action => 'index'
179 if params[:confirm_action]
180 token = UserToken.find_by_token(params[:confirm_string])
181 if token and !token.user.active?
184 @user.email_valid = true
187 flash[:notice] = 'Confirmed your account, thanks for signing up!'
188 session[:user] = @user.id
189 redirect_to :action => 'account', :display_name => @user.display_name
191 @notice = 'Something went wrong confirming that user.'
197 if params[:confirm_action]
198 token = UserToken.find_by_token(params[:confirm_string])
199 if token and token.user.new_email?
201 @user.email = @user.new_email
202 @user.new_email = nil
204 @user.email_valid = true
207 flash[:notice] = 'Confirmed your email address, thanks for signing up!'
208 session[:user] = @user.id
209 redirect_to :action => 'account', :display_name => @user.display_name
211 @notice = 'Something went wrong confirming that email address.'
217 @user.image = params[:user][:image]
219 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
225 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
229 render :text => @user.to_xml.to_s, :content_type => "text/xml"
233 doc = OSM::API.new.get_xml_doc
234 @user.traces.each do |trace|
235 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
237 render :text => doc.to_s, :content_type => "text/xml"
241 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
244 @title = @this_user.display_name
246 @not_found_user = params[:display_name]
247 render :action => 'no_such_user', :status => :not_found
252 if params[:display_name]
253 name = params[:display_name]
254 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
256 friend.user_id = @user.id
257 friend.friend_user_id = new_friend.id
258 unless @user.is_friends_with?(new_friend)
260 flash[:notice] = "#{name} is now your friend."
261 Notifier.deliver_friend_notification(friend)
263 friend.add_error("Sorry, failed to add #{name} as a friend.")
266 flash[:notice] = "You are already friends with #{name}."
269 redirect_to :controller => 'user', :action => 'view'
274 if params[:display_name]
275 name = params[:display_name]
276 friend = User.find_by_display_name(name, :conditions => {:visible => true})
277 if @user.is_friends_with?(friend)
278 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
279 flash[:notice] = "#{friend.display_name} was removed from your friends."
281 flash[:notice] = "#{friend.display_name} is not one of your friends."
284 redirect_to :controller => 'user', :action => 'view'