From be662a65c6989dada094c3561b0e00d951993937 Mon Sep 17 00:00:00 2001
From: danielgrippi <danielgrippi@gmail.com>
Date: Wed, 18 May 2011 15:09:28 -0700
Subject: [PATCH] added token authenticatable to user model

---
 app/controllers/users_controller.rb              |  9 +++++++++
 app/models/user.rb                               |  2 +-
 config/initializers/devise.rb                    |  2 +-
 config/routes.rb                                 |  6 ++++++
 .../20110518184453_add_token_auth_to_user.rb     | 11 +++++++++++
 db/schema.rb                                     |  4 +++-
 spec/controllers/users_controller_spec.rb        | 16 +++++++++++++++-
 7 files changed, 46 insertions(+), 4 deletions(-)
 create mode 100644 db/migrate/20110518184453_add_token_auth_to_user.rb

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index cc8d482f38..fd37f6799a 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 5620d09dcb..b695d383be 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 1e1771d3e5..c55c139f07 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 799cf3d014..79c1ba85c0 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 0000000000..8e78312917
--- /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 b11a750c60..73327813c5 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 eb8a90758e..ade63a1a8b 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
-- 
GitLab