added token authenticatable to user model

This commit is contained in:
danielgrippi 2011-05-18 15:09:28 -07:00
parent 01796fb7be
commit be662a65c6
7 changed files with 46 additions and 4 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -138,4 +138,18 @@ describe UsersController do
assigns[:email_prefs]['mentioned'].should be_false
end
end
end
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