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]
11 before_filter :require_allow_read_prefs, :only => [:api_details]
12 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
14 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
17 @title = t 'user.new.title'
19 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
20 render :action => 'new'
22 @user = User.new(params[:user])
25 @user.data_public = true
26 @user.description = "" if @user.description.nil?
27 @user.creation_ip = request.remote_ip
28 @user.languages = request.user_preferred_languages
31 flash[:notice] = t 'user.new.flash create success message'
32 Notifier.deliver_signup_confirm(@user, @user.tokens.create)
33 redirect_to :action => 'login'
35 render :action => 'new'
41 @title = t 'user.account.title'
42 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
44 if params[:user] and params[:user][:display_name] and params[:user][:description]
45 if params[:user][:email] != @user.email
46 @user.new_email = params[:user][:email]
49 @user.display_name = params[:user][:display_name]
51 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
52 @user.pass_crypt = params[:user][:pass_crypt]
53 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
56 @user.description = params[:user][:description]
57 @user.languages = params[:user][:languages].split(",")
58 @user.home_lat = params[:user][:home_lat]
59 @user.home_lon = params[:user][:home_lon]
64 if params[:user][:email] == @user.new_email
65 flash[:notice] = t 'user.account.flash update success confirm needed'
66 Notifier.deliver_email_confirm(@user, @user.tokens.create)
68 flash[:notice] = t 'user.account.flash update success'
75 if params[:user][:home_lat] and params[:user][:home_lon]
76 @user.home_lat = params[:user][:home_lat].to_f
77 @user.home_lon = params[:user][:home_lon].to_f
79 flash[:notice] = t 'user.set_home.flash success'
80 redirect_to :controller => 'user', :action => 'account'
86 @user.data_public = true
88 flash[:notice] = t 'user.go_public.flash success'
89 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
93 @title = t 'user.lost_password.title'
95 if params[:user] and params[:user][:email]
96 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
99 token = user.tokens.create
100 Notifier.deliver_lost_password(user, token)
101 @notice = t 'user.lost_password.notice email on way'
103 @notice = t 'user.lost_password.notice email cannot find'
109 @title = t 'user.reset_password.title'
112 token = UserToken.find_by_token(params[:token])
117 @user.pass_crypt = params[:user][:pass_crypt]
118 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
120 @user.email_valid = true
124 flash[:notice] = t 'user.reset_password.flash changed'
125 redirect_to :action => 'login'
129 flash[:notice] = t 'user.reset_password.flash token bad'
130 redirect_to :action => 'lost_password'
136 @title = t 'user.new.title'
138 # The user is logged in already, so don't show them the signup page, instead
139 # send them to the home page
140 redirect_to :controller => 'site', :action => 'index' if session[:user]
145 # The user is logged in already, if the referer param exists, redirect them to that
147 redirect_to params[:referer]
149 redirect_to :controller => 'site', :action => 'index'
154 @title = t 'user.login.title'
157 email_or_display_name = params[:user][:email]
158 pass = params[:user][:password]
159 user = User.authenticate(:username => email_or_display_name, :password => pass)
161 session[:user] = user.id
163 redirect_to params[:referer]
165 redirect_to :controller => 'site', :action => 'index'
168 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
169 @notice = t 'user.login.account not active'
171 @notice = t 'user.login.auth failure'
178 token = UserToken.find_by_token(session[:token])
182 session[:token] = nil
186 redirect_to params[:referer]
188 redirect_to :controller => 'site', :action => 'index'
193 if params[:confirm_action]
194 token = UserToken.find_by_token(params[:confirm_string])
195 if token and !token.user.active?
198 @user.email_valid = true
201 flash[:notice] = t 'user.confirm.success'
202 session[:user] = @user.id
203 redirect_to :action => 'account', :display_name => @user.display_name
205 @notice = t 'user.confirm.failure'
211 if params[:confirm_action]
212 token = UserToken.find_by_token(params[:confirm_string])
213 if token and token.user.new_email?
215 @user.email = @user.new_email
216 @user.new_email = nil
218 @user.email_valid = true
221 flash[:notice] = t 'user.confirm_email.success'
222 session[:user] = @user.id
223 redirect_to :action => 'account', :display_name => @user.display_name
225 @notice = t 'user.confirm_email.failure'
231 @user.image = params[:user][:image]
233 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
239 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
243 render :text => @user.to_xml.to_s, :content_type => "text/xml"
247 doc = OSM::API.new.get_xml_doc
248 @user.traces.each do |trace|
249 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
251 render :text => doc.to_s, :content_type => "text/xml"
255 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
258 @title = @this_user.display_name
260 @title = t 'user.no_such_user.title'
261 @not_found_user = params[:display_name]
262 render :action => 'no_such_user', :status => :not_found
267 if params[:display_name]
268 name = params[:display_name]
269 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
271 friend.user_id = @user.id
272 friend.friend_user_id = new_friend.id
273 unless @user.is_friends_with?(new_friend)
275 flash[:notice] = t 'user.make_friend.success', :name => name
276 Notifier.deliver_friend_notification(friend)
278 friend.add_error(t('user.make_friend.failed', :name => name))
281 flash[:notice] = t 'user.make_friend.already_a_friend', :name => name
284 redirect_to :controller => 'user', :action => 'view'
289 if params[:display_name]
290 name = params[:display_name]
291 friend = User.find_by_display_name(name, :conditions => {:visible => true})
292 if @user.is_friends_with?(friend)
293 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
294 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
296 flash[:notice] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
299 redirect_to :controller => 'user', :action => 'view'