diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index cc8d482f389308ee1150ad232a23563ad81100d5..fd37f6799a3f91bd6fbb80ec386800cb176b0ff5 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -141,4 +141,13 @@ class UsersController < ApplicationController tar_path = PhotoMover::move_photos(current_user) send_data( File.open(tar_path).read, :filename => "#{current_user.id}.tar" ) end + + def generate_new_token + if current_user.reset_authentication_token! + @token = current_user.authentication_token + else + @token = "No token created" + end + render :text => @token + end end diff --git a/app/models/user.rb b/app/models/user.rb index 5620d09dcbcae8a32809e95f76350f623fd6e302..b695d383be431975f69e570802b0f5c8fcc5ef30 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,7 +13,7 @@ class User < ActiveRecord::Base devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, - :timeoutable + :timeoutable, :token_authenticatable before_validation :strip_and_downcase_username before_validation :set_current_language, :on => :create diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 1e1771d3e5716d8616bcf387b5cfe5941c06b6c5..c55c139f07e133086ef64cbdd759fb2bbf891ac7 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -113,7 +113,7 @@ Devise.setup do |config| # ==> Configuration for :token_authenticatable # Defines name of the authentication token params key - # config.token_authentication_key = :auth_token + config.token_authentication_key = :auth_token # ==> Scopes configuration # Turn scoped views on. Before rendering "sessions/new", it will first check for diff --git a/config/routes.rb b/config/routes.rb index 799cf3d0145b557b5ebc4f73948132a2c5f95bb8..79c1ba85c0579e83fa2baaa0b1b32cc8f8ee829b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -59,6 +59,12 @@ Diaspora::Application.routes.draw do :invitations => "invitations"} do get 'invitations/resend/:id' => 'invitations#resend', :as => 'invitation_resend' end + + # generating a new user token (for devise) + match 'users/generate_new_token' => 'users#generate_new_token' + + + get 'login' => redirect('/users/sign_in') scope 'admins', :controller => :admins do diff --git a/db/migrate/20110518184453_add_token_auth_to_user.rb b/db/migrate/20110518184453_add_token_auth_to_user.rb new file mode 100644 index 0000000000000000000000000000000000000000..8e783129179f00b1bd693391f10cb0337f6c40cd --- /dev/null +++ b/db/migrate/20110518184453_add_token_auth_to_user.rb @@ -0,0 +1,11 @@ +class AddTokenAuthToUser < ActiveRecord::Migration + def self.up + add_column(:users, :authentication_token, :string, :limit => 30) + add_index(:users, :authentication_token, :unique => true) + end + + def self.down + remove_index(:users, :column => :authentication_token) + remove_column(:users, :authentication_token) + end +end diff --git a/db/schema.rb b/db/schema.rb index b11a750c607570c05fe1e9e0d7a7d8a4a94e87b6..73327813c51d239b8d70abbac070e1bb18a8becc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110518010050) do +ActiveRecord::Schema.define(:version => 20110518184453) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -361,8 +361,10 @@ ActiveRecord::Schema.define(:version => 20110518010050) do t.integer "invitation_limit" t.integer "invited_by_id" t.string "invited_by_type" + t.string "authentication_token", :limit => 30 end + add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true add_index "users", ["email"], :name => "index_users_on_email" add_index "users", ["invitation_service", "invitation_identifier"], :name => "index_users_on_invitation_service_and_invitation_identifier", :unique => true add_index "users", ["invitation_token"], :name => "index_users_on_invitation_token" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index eb8a90758eaebdc32667320bca3fd2c6856d2d09..ade63a1a8b63b6b6af3ad334a77a72741a7ba4ce 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -138,4 +138,18 @@ describe UsersController do assigns[:email_prefs]['mentioned'].should be_false end end -end \ No newline at end of file + + describe '#generate_new_token' do + it 'generates a new token for the current user' do + lambda { + get 'generate_new_token' + }.should change{ @user.reload.authentication_token } + end + + it 'displays a token' do + get 'generate_new_token' + response.body.should include(@user.reload.authentication_token) + end + end + +end