1 class UserController < ApplicationController
4 before_filter :authorize, :only => [:api_details, :api_gpx_files]
5 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
6 before_filter :set_locale, :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.languages = request.user_preferred_languages
29 flash[:notice] = I18n.t('user.new.flash create success message')
30 Notifier.deliver_signup_confirm(@user, @user.tokens.create)
31 redirect_to :action => 'login'
33 render :action => 'new'
39 @title = 'edit account'
40 if params[:user] and params[:user][:display_name] and params[:user][:description]
41 if params[:user][:email] != @user.email
42 @user.new_email = params[:user][:email]
45 @user.display_name = params[:user][:display_name]
47 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
48 @user.pass_crypt = params[:user][:pass_crypt]
49 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
52 @user.description = params[:user][:description]
53 @user.home_lat = params[:user][:home_lat]
54 @user.home_lon = params[:user][:home_lon]
57 if params[:user][:email] == @user.new_email
58 flash[:notice] = I18n.t('user.account.flash update success confirm needed')
59 Notifier.deliver_email_confirm(@user, @user.tokens.create)
61 flash[:notice] = I18n.t('user.account.flash update success')
68 if params[:user][:home_lat] and params[:user][:home_lon]
69 @user.home_lat = params[:user][:home_lat].to_f
70 @user.home_lon = params[:user][:home_lon].to_f
72 flash[:notice] = I18n.t('user.set_home.flash success')
73 redirect_to :controller => 'user', :action => 'account'
79 @user.data_public = true
81 flash[:notice] = I18n.t('user.go_public.flash success')
82 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
86 @title = I18n.t('user.lost_password.title')
87 if params[:user] and params[:user][:email]
88 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
91 token = user.tokens.create
92 Notifier.deliver_lost_password(user, token)
93 flash[:notice] = I18n.t('user.lost_password.notice.email on way')
95 flash[:notice] = I18n.t('user.lost_password.notice email cannot find')
101 @title = I18n.t('user.reset_password.title')
103 token = UserToken.find_by_token(params[:token])
105 pass = OSM::make_token(8)
107 user.pass_crypt = pass
108 user.pass_crypt_confirmation = pass
110 user.email_valid = true
113 Notifier.deliver_reset_password(user, pass)
114 flash[:notice] = I18n.t('user.reset_password.flash changed check mail')
116 flash[:notice] = I18n.t('user.reset_password.flash token bad')
120 redirect_to :action => 'login'
124 @title = 'create account'
125 # The user is logged in already, so don't show them the signup page, instead
126 # send them to the home page
127 redirect_to :controller => 'site', :action => 'index' if session[:user]
132 # The user is logged in already, if the referer param exists, redirect them to that
134 redirect_to params[:referer]
136 redirect_to :controller => 'site', :action => 'index'
142 email_or_display_name = params[:user][:email]
143 pass = params[:user][:password]
144 user = User.authenticate(:username => email_or_display_name, :password => pass)
146 session[:user] = user.id
148 redirect_to params[:referer]
150 redirect_to :controller => 'site', :action => 'index'
153 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
154 @notice = "Sorry, your account is not active yet.<br>Please click on the link in the account confirmation email to activate your account."
156 @notice = "Sorry, couldn't log in with those details."
163 token = UserToken.find_by_token(session[:token])
167 session[:token] = nil
171 redirect_to params[:referer]
173 redirect_to :controller => 'site', :action => 'index'
178 if params[:confirm_action]
179 token = UserToken.find_by_token(params[:confirm_string])
180 if token and !token.user.active?
183 @user.email_valid = true
186 flash[:notice] = 'Confirmed your account, thanks for signing up!'
187 session[:user] = @user.id
188 redirect_to :action => 'account', :display_name => @user.display_name
190 @notice = 'Something went wrong confirming that user.'
196 if params[:confirm_action]
197 token = UserToken.find_by_token(params[:confirm_string])
198 if token and token.user.new_email?
200 @user.email = @user.new_email
201 @user.new_email = nil
203 @user.email_valid = true
206 flash[:notice] = 'Confirmed your email address, thanks for signing up!'
207 session[:user] = @user.id
208 redirect_to :action => 'account', :display_name => @user.display_name
210 @notice = 'Something went wrong confirming that email address.'
216 @user.image = params[:user][:image]
218 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
224 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
228 render :text => @user.to_xml.to_s, :content_type => "text/xml"
232 doc = OSM::API.new.get_xml_doc
233 @user.traces.each do |trace|
234 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
236 render :text => doc.to_s, :content_type => "text/xml"
240 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
243 @title = @this_user.display_name
245 @not_found_user = params[:display_name]
246 render :action => 'no_such_user', :status => :not_found
251 if params[:display_name]
252 name = params[:display_name]
253 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
255 friend.user_id = @user.id
256 friend.friend_user_id = new_friend.id
257 unless @user.is_friends_with?(new_friend)
259 flash[:notice] = "#{name} is now your friend."
260 Notifier.deliver_friend_notification(friend)
262 friend.add_error("Sorry, failed to add #{name} as a friend.")
265 flash[:notice] = "You are already friends with #{name}."
268 redirect_to :controller => 'user', :action => 'view'
273 if params[:display_name]
274 name = params[:display_name]
275 friend = User.find_by_display_name(name, :conditions => {:visible => true})
276 if @user.is_friends_with?(friend)
277 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
278 flash[:notice] = "#{friend.display_name} was removed from your friends."
280 flash[:notice] = "#{friend.display_name} is not one of your friends."
283 redirect_to :controller => 'user', :action => 'view'