Commit 003bf61258ea20128315076936f09ea198e56bcb

Authored by Nihad Abbasov
1 parent 8d74123d

add auth token for users

app/models/user.rb
1 class User < ActiveRecord::Base 1 class User < ActiveRecord::Base
2 # Include default devise modules. Others available are: 2 # Include default devise modules. Others available are:
3 # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 3 # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
4 - devise :database_authenticatable, 4 + devise :database_authenticatable, :token_authenticatable,
5 :recoverable, :rememberable, :trackable, :validatable 5 :recoverable, :rememberable, :trackable, :validatable
6 6
7 # Setup accessible (or protected) attributes for your model 7 # Setup accessible (or protected) attributes for your model
@@ -25,6 +25,7 @@ class User &lt; ActiveRecord::Base @@ -25,6 +25,7 @@ class User &lt; ActiveRecord::Base
25 :foreign_key => :assignee_id, 25 :foreign_key => :assignee_id,
26 :dependent => :destroy 26 :dependent => :destroy
27 27
  28 + before_create :ensure_authentication_token
28 scope :not_in_project, lambda { |project| where("id not in (:ids)", :ids => project.users.map(&:id) ) } 29 scope :not_in_project, lambda { |project| where("id not in (:ids)", :ids => project.users.map(&:id) ) }
29 30
30 def identifier 31 def identifier
config/initializers/devise.rb
@@ -158,11 +158,11 @@ Devise.setup do |config| @@ -158,11 +158,11 @@ Devise.setup do |config|
158 158
159 # ==> Configuration for :token_authenticatable 159 # ==> Configuration for :token_authenticatable
160 # Defines name of the authentication token params key 160 # Defines name of the authentication token params key
161 - # config.token_authentication_key = :auth_token 161 + config.token_authentication_key = :private_token
162 162
163 # If true, authentication through token does not store user in session and needs 163 # If true, authentication through token does not store user in session and needs
164 # to be supplied on each request. Useful if you are using the token as API token. 164 # to be supplied on each request. Useful if you are using the token as API token.
165 - # config.stateless_token = false 165 + config.stateless_token = true
166 166
167 # ==> Scopes configuration 167 # ==> Scopes configuration
168 # Turn scoped views on. Before rendering "sessions/new", it will first check for 168 # Turn scoped views on. Before rendering "sessions/new", it will first check for
db/migrate/20111115063954_add_authentication_token_to_users.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +class AddAuthenticationTokenToUsers < ActiveRecord::Migration
  2 + def change
  3 + add_column :users, :authentication_token, :string
  4 + end
  5 +end
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 # 11 #
12 # It's strongly recommended to check this file into your version control system. 12 # It's strongly recommended to check this file into your version control system.
13 13
14 -ActiveRecord::Schema.define(:version => 20111111093150) do 14 +ActiveRecord::Schema.define(:version => 20111115063954) do
15 15
16 create_table "issues", :force => true do |t| 16 create_table "issues", :force => true do |t|
17 t.string "title" 17 t.string "title"
@@ -103,6 +103,7 @@ ActiveRecord::Schema.define(:version =&gt; 20111111093150) do @@ -103,6 +103,7 @@ ActiveRecord::Schema.define(:version =&gt; 20111111093150) do
103 t.string "skype", :default => "", :null => false 103 t.string "skype", :default => "", :null => false
104 t.string "linkedin", :default => "", :null => false 104 t.string "linkedin", :default => "", :null => false
105 t.string "twitter", :default => "", :null => false 105 t.string "twitter", :default => "", :null => false
  106 + t.string "authentication_token"
106 end 107 end
107 108
108 add_index "users", ["email"], :name => "index_users_on_email", :unique => true 109 add_index "users", ["email"], :name => "index_users_on_email", :unique => true
spec/models/user_spec.rb
@@ -19,15 +19,20 @@ describe User do @@ -19,15 +19,20 @@ describe User do
19 user.identifier.should == "test_mail.com" 19 user.identifier.should == "test_mail.com"
20 end 20 end
21 21
  22 + it "should have authentication token" do
  23 + user = Factory(:user)
  24 + user.authentication_token.should_not == ""
  25 + end
  26 +
22 describe "dependent" do 27 describe "dependent" do
23 - before do 28 + before do
24 @user = Factory :user 29 @user = Factory :user
25 - @note = Factory :note, 30 + @note = Factory :note,
26 :author => @user, 31 :author => @user,
27 :project => Factory(:project) 32 :project => Factory(:project)
28 end 33 end
29 34
30 - it "should destroy all notes with user" do 35 + it "should destroy all notes with user" do
31 Note.find_by_id(@note.id).should_not be_nil 36 Note.find_by_id(@note.id).should_not be_nil
32 @user.destroy 37 @user.destroy
33 Note.find_by_id(@note.id).should be_nil 38 Note.find_by_id(@note.id).should be_nil