simple_user_auth 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/simple_user_auth.rb +15 -17
- data/lib/simple_user_auth/version.rb +1 -1
- data/test/model_test.rb +4 -1
- data/test/test_helper.rb +12 -1
- metadata +2 -6
- data/test/dummy/db/migrate/20110506183728_create_users.rb +0 -14
- data/test/dummy/db/schema.rb +0 -23
data/lib/simple_user_auth.rb
CHANGED
@@ -27,6 +27,7 @@ module SimpleUserAuth
|
|
27
27
|
klass.extend(ClassMethods)
|
28
28
|
klass.class_eval do
|
29
29
|
include ClassInstanceMethods
|
30
|
+
class_attribute :authenticator
|
30
31
|
attr_accessor :password
|
31
32
|
attr_accessor :current_password
|
32
33
|
validate :change_password_validator
|
@@ -44,14 +45,14 @@ module SimpleUserAuth
|
|
44
45
|
# Select the database field you want to find the user by with when you use the authenticate method
|
45
46
|
# e.g. authenticate_by(:email)
|
46
47
|
def authenticate_by(authenticator)
|
47
|
-
|
48
|
+
self.authenticator = authenticator
|
48
49
|
end
|
49
50
|
|
50
|
-
# Finds the user in the database by the authenticator and verifys them against the submitted password
|
51
|
+
# Finds the user in the database by the authenticator and verifys them against the submitted password, takes an optional :find_by to override authenticate_by
|
51
52
|
# e.g. User.authenticate(params[:session][:email], params[:session][:password])
|
52
|
-
|
53
|
-
|
54
|
-
user = find(:first, :conditions => ["#{authenticator} = ?", search])
|
53
|
+
# e.g. User.authenticate(params[:session][:phone_number], params[:session][:password], :find_by => :phone)
|
54
|
+
def authenticate(search, submitted_password, args = {})
|
55
|
+
user = find(:first, :conditions => ["#{args[:find_by] || authenticator} = ?", search])
|
55
56
|
return nil if user.nil?
|
56
57
|
return user if user.has_password?(submitted_password)
|
57
58
|
end
|
@@ -112,6 +113,8 @@ module SimpleUserAuth
|
|
112
113
|
klass.extend(ClassMethods)
|
113
114
|
klass.class_eval do
|
114
115
|
include ClassInstanceMethods
|
116
|
+
class_attribute :user_model_for_sign_in
|
117
|
+
class_attribute :deny_access_callback
|
115
118
|
helper_method :current_user, :signed_in?, :not_signed_in?, :current_user?
|
116
119
|
end
|
117
120
|
end
|
@@ -123,15 +126,15 @@ module SimpleUserAuth
|
|
123
126
|
# e.g. can_sign_in :user, :deny => :my_deny_callback
|
124
127
|
def can_sign_in(model, args = {})
|
125
128
|
klass = Kernel.const_get(model.to_s.camelize)
|
126
|
-
|
127
|
-
|
129
|
+
self.user_model_for_sign_in = klass
|
130
|
+
self.deny_access_callback = args[:deny]
|
128
131
|
end
|
129
132
|
|
130
133
|
end
|
131
134
|
|
132
135
|
module ClassInstanceMethods
|
133
136
|
|
134
|
-
# Signs in a user, if
|
137
|
+
# Signs in a user, if :remember_me is true, cookies will not expire.
|
135
138
|
def sign_in(user, args = {})
|
136
139
|
if args[:remember_me]
|
137
140
|
cookies.permanent.signed[remember_token_name] = [user.id, user.salt]
|
@@ -171,21 +174,16 @@ module SimpleUserAuth
|
|
171
174
|
private
|
172
175
|
|
173
176
|
def failed_authentication
|
174
|
-
|
175
|
-
|
176
|
-
send(callback)
|
177
|
+
if self.class.deny_access_callback && respond_to?(self.class.deny_access_callback)
|
178
|
+
send(self.class.deny_access_callback)
|
177
179
|
else
|
178
180
|
render :text => 'Access Denied', :status => 403
|
179
181
|
end
|
180
182
|
return false
|
181
183
|
end
|
182
184
|
|
183
|
-
def user_model
|
184
|
-
self.class.read_inheritable_attribute(:user_model_for_sign_in)
|
185
|
-
end
|
186
|
-
|
187
185
|
def remember_token_name
|
188
|
-
"#{
|
186
|
+
"#{self.class.user_model_for_sign_in.name.downcase}_remember_token".to_sym
|
189
187
|
end
|
190
188
|
|
191
189
|
def current_user=(user)
|
@@ -193,7 +191,7 @@ module SimpleUserAuth
|
|
193
191
|
end
|
194
192
|
|
195
193
|
def user_from_remember_token
|
196
|
-
|
194
|
+
self.class.user_model_for_sign_in.authenticate_with_salt(*remember_token)
|
197
195
|
end
|
198
196
|
|
199
197
|
def remember_token
|
data/test/model_test.rb
CHANGED
@@ -3,7 +3,7 @@ require "test_helper"
|
|
3
3
|
class ModelTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
def setup
|
6
|
-
@user = User.create(:email => "info@zenstack.com", :password => "password", :password_confirmation => "password")
|
6
|
+
@user = User.create(:email => "info@zenstack.com", :password => "password", :password_confirmation => "password", :other_search => "foo")
|
7
7
|
end
|
8
8
|
|
9
9
|
def teardown
|
@@ -31,6 +31,9 @@ class ModelTest < ActiveSupport::TestCase
|
|
31
31
|
test "authenticate" do
|
32
32
|
assert_equal(User.authenticate(@user.email, "password"), @user)
|
33
33
|
assert !User.authenticate("info@zenstack.com", "badpassword")
|
34
|
+
assert !User.authenticate("badsearch", "password", :find_by => :other_search)
|
35
|
+
assert_equal(User.authenticate("foo", "password", :find_by => :other_search), @user)
|
36
|
+
assert !User.authenticate("foo", "badpassword", :find_by => :other_search)
|
34
37
|
end
|
35
38
|
|
36
39
|
test "authenticate_with_salt" do
|
data/test/test_helper.rb
CHANGED
@@ -5,6 +5,17 @@ require 'test/unit'
|
|
5
5
|
require 'rails/test_help'
|
6
6
|
require 'dummy/config/environment.rb'
|
7
7
|
|
8
|
-
ActiveRecord::
|
8
|
+
ActiveRecord::Schema.define do
|
9
|
+
|
10
|
+
create_table "users", :force => true do |t|
|
11
|
+
t.string "email"
|
12
|
+
t.string "other_search"
|
13
|
+
t.string "encrypted_password"
|
14
|
+
t.string "salt"
|
15
|
+
t.datetime "created_at"
|
16
|
+
t.datetime "updated_at"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
9
20
|
|
10
21
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_user_auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.9
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Erich Menge
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-06-04 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: A simple no frills user authentication gem for my Rails projects.
|
@@ -43,8 +43,6 @@ files:
|
|
43
43
|
- test/dummy/config/initializers/secret_token.rb
|
44
44
|
- test/dummy/config/initializers/session_store.rb
|
45
45
|
- test/dummy/config/routes.rb
|
46
|
-
- test/dummy/db/migrate/20110506183728_create_users.rb
|
47
|
-
- test/dummy/db/schema.rb
|
48
46
|
- test/model_test.rb
|
49
47
|
- test/test_helper.rb
|
50
48
|
homepage: ""
|
@@ -88,7 +86,5 @@ test_files:
|
|
88
86
|
- test/dummy/config/initializers/secret_token.rb
|
89
87
|
- test/dummy/config/initializers/session_store.rb
|
90
88
|
- test/dummy/config/routes.rb
|
91
|
-
- test/dummy/db/migrate/20110506183728_create_users.rb
|
92
|
-
- test/dummy/db/schema.rb
|
93
89
|
- test/model_test.rb
|
94
90
|
- test/test_helper.rb
|
data/test/dummy/db/schema.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# This file is auto-generated from the current state of the database. Instead
|
2
|
-
# of editing this file, please use the migrations feature of Active Record to
|
3
|
-
# incrementally modify your database, and then regenerate this schema definition.
|
4
|
-
#
|
5
|
-
# Note that this schema.rb definition is the authoritative source for your
|
6
|
-
# database schema. If you need to create the application database on another
|
7
|
-
# system, you should be using db:schema:load, not running all the migrations
|
8
|
-
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
9
|
-
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
10
|
-
#
|
11
|
-
# It's strongly recommended to check this file into your version control system.
|
12
|
-
|
13
|
-
ActiveRecord::Schema.define(:version => 20110506183728) do
|
14
|
-
|
15
|
-
create_table "users", :force => true do |t|
|
16
|
-
t.string "email"
|
17
|
-
t.string "encrypted_password"
|
18
|
-
t.string "salt"
|
19
|
-
t.datetime "created_at"
|
20
|
-
t.datetime "updated_at"
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|