From: Steve Coast Date: Sat, 26 Aug 2006 17:53:43 +0000 (+0000) Subject: more rails front end bits X-Git-Tag: live~8629 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/97978ceeb9047ec6894b822ca947a466c2462988?hp=a42dc9f4826da27b21fa1f66ea6318110afaa0b2 more rails front end bits --- diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 82ef80920..366646ae3 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -2,10 +2,10 @@ # Likewise, all the methods added will be available for all controllers. class ApplicationController < ActionController::Base + def authorize_web + @user = User.find_by_token(session[:token]) + end - - # HTTP AUTH stuff for the API - def authorize(realm='Web Password', errormessage="Could't authenticate you") username, passwd = get_auth_data # check if authorized diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index d01b86bba..380c41436 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -1,4 +1,6 @@ class SiteController < ApplicationController + before_filter :authorize_web + def index end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 3012765d2..d61455bf1 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -14,7 +14,38 @@ class UserController < ApplicationController end def new + render :layout => 'site' + end + + def login + if params[:user] + email = params[:user][:email] + pass = params[:user][:password] + u = User.authenticate(email, pass) + if u + u.token = User.make_token + u.timeout = 1.day.from_now + u.save + session[:token] = u.token + redirect_to :controller => 'site', :action => 'index' + return + end + end + + render :layout => 'site' + end + def logout + if session[:token] + u = User.find_by_token(session[:token]) + if u + u.token = User.make_token + u.timeout = Time.now + u.save + end + end + session[:token] = nil + redirect_to :controller => 'site', :action => 'index' end def confirm @@ -23,6 +54,9 @@ class UserController < ApplicationController @user.active = true @user.save flash[:notice] = 'Confirmed your account' + + #FIXME: login the person magically + redirect_to :action => 'login' else flash[:notice] = 'Something went wrong confirming that user' diff --git a/app/models/user.rb b/app/models/user.rb index 589546e2d..350ea2c3a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,7 +13,7 @@ class User < ActiveRecord::Base def set_defaults self.creation_time = Time.now self.timeout = Time.now - self.token = make_token() + self.token = User.make_token() end def pass_crypt=(str) @@ -27,9 +27,12 @@ class User < ActiveRecord::Base def self.authenticate(email, passwd) find_first([ "email = ? AND pass_crypt =?", email, Digest::MD5.hexdigest(passwd) ]) end + + def self.authenticate_token(token) + find_first([ "token = ? ", token]) + end - private - def make_token + def self.make_token chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' confirmstring = '' diff --git a/app/views/layouts/site.rhtml b/app/views/layouts/site.rhtml index 0ff110ced..aaf4e0324 100644 --- a/app/views/layouts/site.rhtml +++ b/app/views/layouts/site.rhtml @@ -20,15 +20,27 @@ - <%= link_to 'Login', {:controller => 'user', :action => 'login'}, {:id => 'loginanchor'}%> / - <%= link_to 'Sign up', {:controller => 'user', :action => 'new'}, {:id => 'registeranchor'} %> + <% if @user %> + Welcome, <%= @user.email %> / + <%= link_to 'Logout', {:controller => 'user', :action => 'logout'}, {:id => 'loginanchor'}%> + <% else %> + <%= link_to 'Login', {:controller => 'user', :action => 'login'}, {:id => 'loginanchor'}%> / + <%= link_to 'Sign up', {:controller => 'user', :action => 'new'}, {:id => 'registeranchor'} %> + <% end %>
@@ -70,13 +82,14 @@ - - - + + diff --git a/app/views/site/index.rhtml b/app/views/site/index.rhtml index e08565a21..25cebdc23 100644 --- a/app/views/site/index.rhtml +++ b/app/views/site/index.rhtml @@ -14,6 +14,27 @@
+<% unless @user %> +
+ We're trialing adverts to support the project. Login and they go away. +
+<% end %> + +