1 class UserController < ApplicationController
2 layout 'site', :except => :api_details
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 => [:account, :go_public, :make_friend, :remove_friend]
8 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
9 before_filter :check_database_writable, :only => [:login, :new, :account, :go_public, :make_friend, :remove_friend]
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]
13 before_filter :require_cookies, :only => [:login, :confirm]
14 before_filter :require_administrator, :only => [:set_status, :delete, :list]
15 before_filter :lookup_this_user, :only => [:set_status, :delete]
17 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
19 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
22 @title = t 'user.new.title'
24 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
25 render :action => 'new'
27 @user = User.new(params[:user])
29 @user.status = "pending"
30 @user.data_public = true
31 @user.description = "" if @user.description.nil?
32 @user.creation_ip = request.remote_ip
33 @user.languages = request.user_preferred_languages
36 flash[:notice] = t 'user.new.flash create success message'
37 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
38 redirect_to :action => 'login'
40 render :action => 'new'
46 @title = t 'user.account.title'
47 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
49 if params[:user] and params[:user][:display_name] and params[:user][:description]
50 @user.display_name = params[:user][:display_name]
51 @user.new_email = params[:user][:new_email]
53 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
54 @user.pass_crypt = params[:user][:pass_crypt]
55 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
58 @user.description = params[:user][:description]
59 @user.languages = params[:user][:languages].split(",")
61 case params[:image_action]
62 when "new" then @user.image = params[:user][:image]
63 when "delete" then @user.image = nil
66 @user.home_lat = params[:user][:home_lat]
67 @user.home_lon = params[:user][:home_lon]
72 if @user.new_email.nil? or @user.new_email.empty?
73 flash[:notice] = t 'user.account.flash update success'
75 flash[:notice] = t 'user.account.flash update success confirm needed'
78 Notifier.deliver_email_confirm(@user, @user.tokens.create)
80 # Ignore errors sending email
84 redirect_to :action => "account", :display_name => @user.display_name
88 flash[:errors].each do |attr,msg|
89 attr = "new_email" if attr == "email"
90 @user.errors.add(attr,msg)
97 @user.data_public = true
99 flash[:notice] = t 'user.go_public.flash success'
100 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
104 @title = t 'user.lost_password.title'
106 if params[:user] and params[:user][:email]
107 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
110 token = user.tokens.create
111 Notifier.deliver_lost_password(user, token)
112 flash[:notice] = t 'user.lost_password.notice email on way'
113 redirect_to :action => 'login'
115 flash.now[:error] = t 'user.lost_password.notice email cannot find'
121 @title = t 'user.reset_password.title'
124 token = UserToken.find_by_token(params[:token])
130 @user.pass_crypt = params[:user][:pass_crypt]
131 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
132 @user.status = "active" if @user.status == "pending"
133 @user.email_valid = true
137 flash[:notice] = t 'user.reset_password.flash changed'
138 redirect_to :action => 'login'
142 flash[:error] = t 'user.reset_password.flash token bad'
143 redirect_to :action => 'lost_password'
149 @title = t 'user.new.title'
151 # The user is logged in already, so don't show them the signup
152 # page, instead send them to the home page
153 redirect_to :controller => 'site', :action => 'index' if session[:user]
157 @title = t 'user.login.title'
160 email_or_display_name = params[:user][:email]
161 pass = params[:user][:password]
162 user = User.authenticate(:username => email_or_display_name, :password => pass)
165 session[:user] = user.id
166 session_expires_after 1.month if params[:remember_me]
168 # The user is logged in, if the referer param exists, redirect
169 # them to that unless they've also got a block on them, in
170 # which case redirect them to the block so they can clear it.
171 if user.blocked_on_view
172 redirect_to user.blocked_on_view, :referrer => params[:referrer]
173 elsif params[:referer]
174 redirect_to params[:referer]
176 redirect_to :controller => 'site', :action => 'index'
178 elsif User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
179 flash.now[:error] = t 'user.login.account not active'
180 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
181 flash.now[:error] = t 'user.login.account suspended'
183 flash.now[:error] = t 'user.login.auth failure'
189 @title = t 'user.logout.title'
191 if params[:session] == request.session_options[:id]
193 token = UserToken.find_by_token(session[:token])
197 session[:token] = nil
200 session_expires_automatically
202 redirect_to params[:referer]
204 redirect_to :controller => 'site', :action => 'index'
210 if params[:confirm_action]
211 token = UserToken.find_by_token(params[:confirm_string])
212 if token and !token.user.active?
214 @user.status = "active"
215 @user.email_valid = true
217 referer = token.referer
219 flash[:notice] = t 'user.confirm.success'
220 session[:user] = @user.id
224 redirect_to :action => 'account', :display_name => @user.display_name
227 flash.now[:error] = t 'user.confirm.failure'
233 if params[:confirm_action]
234 token = UserToken.find_by_token(params[:confirm_string])
235 if token and token.user.new_email?
237 @user.email = @user.new_email
238 @user.new_email = nil
239 @user.email_valid = true
241 flash[:notice] = t 'user.confirm_email.success'
243 flash[:errors] = @user.errors
246 session[:user] = @user.id
247 redirect_to :action => 'account', :display_name => @user.display_name
249 flash.now[:error] = t 'user.confirm_email.failure'
255 doc = OSM::API.new.get_xml_doc
256 @user.traces.each do |trace|
257 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
259 render :text => doc.to_s, :content_type => "text/xml"
263 @this_user = User.find_by_display_name(params[:display_name])
266 (@this_user.visible? or (@user and @user.administrator?))
267 @title = @this_user.display_name
269 @title = t 'user.no_such_user.title'
270 @not_found_user = params[:display_name]
271 render :action => 'no_such_user', :status => :not_found
276 if params[:display_name]
277 name = params[:display_name]
278 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
280 friend.user_id = @user.id
281 friend.friend_user_id = new_friend.id
282 unless @user.is_friends_with?(new_friend)
284 flash[:notice] = t 'user.make_friend.success', :name => name
285 Notifier.deliver_friend_notification(friend)
287 friend.add_error(t('user.make_friend.failed', :name => name))
290 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
294 redirect_to params[:referer]
296 redirect_to :controller => 'user', :action => 'view'
302 if params[:display_name]
303 name = params[:display_name]
304 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
305 if @user.is_friends_with?(friend)
306 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
307 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
309 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
313 redirect_to params[:referer]
315 redirect_to :controller => 'user', :action => 'view'
321 # sets a user's status
323 @this_user.update_attributes(:status => params[:status])
324 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
328 # delete a user, marking them as deleted and removing personal data
331 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
335 # display a list of users matching specified criteria
338 ids = params[:user].keys.collect { |id| id.to_i }
340 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
341 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
343 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
345 conditions = Hash.new
346 conditions[:status] = params[:status] if params[:status]
347 conditions[:creation_ip] = params[:ip] if params[:ip]
349 @user_pages, @users = paginate(:users,
350 :conditions => conditions,
359 # require that the user is a administrator, or fill out a helpful error message
360 # and return them to the user page.
361 def require_administrator
362 if @user and not @user.administrator?
363 flash[:error] = t('user.filter.not_an_administrator')
365 if params[:display_name]
366 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
368 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
371 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
376 # ensure that there is a "this_user" instance variable
378 @this_user = User.find_by_display_name(params[:display_name])
379 rescue ActiveRecord::RecordNotFound
380 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user