authlogic 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of authlogic might be problematic. Click here for more details.
data/VERSION.yml
CHANGED
data/authlogic.gemspec
CHANGED
@@ -11,7 +11,7 @@ module Authlogic
|
|
11
11
|
add_acts_as_authentic_module(Methods)
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
# All configuration for the logged in status feature set.
|
16
16
|
module Config
|
17
17
|
# The timeout to determine when a user is logged in or not.
|
@@ -23,30 +23,31 @@ module Authlogic
|
|
23
23
|
end
|
24
24
|
alias_method :logged_in_timeout=, :logged_in_timeout
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
# All methods for the logged in status feature seat.
|
28
28
|
module Methods
|
29
29
|
def self.included(klass)
|
30
30
|
return if !klass.column_names.include?("last_request_at")
|
31
|
-
|
31
|
+
|
32
32
|
klass.class_eval do
|
33
33
|
include InstanceMethods
|
34
34
|
scope :logged_in, where("last_request_at > ?", logged_in_timeout.seconds.ago)
|
35
35
|
scope :logged_out, where("last_request_at is NULL or last_request_at <= ?", logged_in_timeout.seconds.ago)
|
36
36
|
end
|
37
|
-
|
37
|
+
end
|
38
|
+
|
38
39
|
module InstanceMethods
|
39
40
|
# Returns true if the last_request_at > logged_in_timeout.
|
40
41
|
def logged_in?
|
41
42
|
raise "Can not determine the records login state because there is no last_request_at column" if !respond_to?(:last_request_at)
|
42
43
|
!last_request_at.nil? && last_request_at > logged_in_timeout.seconds.ago
|
43
44
|
end
|
44
|
-
|
45
|
+
|
45
46
|
# Opposite of logged_in?
|
46
47
|
def logged_out?
|
47
48
|
!logged_in?
|
48
49
|
end
|
49
|
-
|
50
|
+
|
50
51
|
private
|
51
52
|
def logged_in_timeout
|
52
53
|
self.class.logged_in_timeout
|
@@ -11,7 +11,7 @@ module Authlogic
|
|
11
11
|
add_acts_as_authentic_module(Methods)
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
# Change how the perishable token works.
|
16
16
|
module Config
|
17
17
|
# When using the find_using_perishable_token method the token can expire. If the token is expired, no
|
@@ -23,7 +23,7 @@ module Authlogic
|
|
23
23
|
rw_config(:perishable_token_valid_for, (!value.nil? && value.to_i) || value, 10.minutes.to_i)
|
24
24
|
end
|
25
25
|
alias_method :perishable_token_valid_for=, :perishable_token_valid_for
|
26
|
-
|
26
|
+
|
27
27
|
# Authlogic tries to expire and change the perishable token as much as possible, without comprising
|
28
28
|
# it's purpose. This is for security reasons. If you want to manage it yourself, you can stop
|
29
29
|
# Authlogic from getting your in way by setting this to true.
|
@@ -35,21 +35,21 @@ module Authlogic
|
|
35
35
|
end
|
36
36
|
alias_method :disable_perishable_token_maintenance=, :disable_perishable_token_maintenance
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
# All methods relating to the perishable token.
|
40
40
|
module Methods
|
41
41
|
def self.included(klass)
|
42
42
|
return if !klass.column_names.include?("perishable_token")
|
43
|
-
|
43
|
+
|
44
44
|
klass.class_eval do
|
45
45
|
extend ClassMethods
|
46
46
|
include InstanceMethods
|
47
|
-
|
47
|
+
|
48
48
|
validates_uniqueness_of :perishable_token, :if => :perishable_token_changed?
|
49
49
|
before_save :reset_perishable_token, :unless => :disable_perishable_token_maintenance?
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
# Class level methods for the perishable token
|
54
54
|
module ClassMethods
|
55
55
|
# Use this methdo to find a record with a perishable token. This method does 2 things for you:
|
@@ -63,37 +63,37 @@ module Authlogic
|
|
63
63
|
def find_using_perishable_token(token, age = self.perishable_token_valid_for)
|
64
64
|
return if token.blank?
|
65
65
|
age = age.to_i
|
66
|
-
|
66
|
+
|
67
67
|
conditions_sql = "perishable_token = ?"
|
68
68
|
conditions_subs = [token]
|
69
|
-
|
69
|
+
|
70
70
|
if column_names.include?("updated_at") && age > 0
|
71
71
|
conditions_sql += " and updated_at > ?"
|
72
72
|
conditions_subs << age.seconds.ago
|
73
73
|
end
|
74
|
-
|
75
|
-
|
74
|
+
|
75
|
+
where(conditions_sql, *conditions_subs).first
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
# This method will raise ActiveRecord::NotFound if no record is found.
|
79
79
|
def find_using_perishable_token!(token, age = perishable_token_valid_for)
|
80
80
|
find_using_perishable_token(token, age) || raise(ActiveRecord::RecordNotFound)
|
81
81
|
end
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
# Instance level methods for the perishable token.
|
85
85
|
module InstanceMethods
|
86
86
|
# Resets the perishable token to a random friendly token.
|
87
87
|
def reset_perishable_token
|
88
88
|
self.perishable_token = Random.friendly_token
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
# Same as reset_perishable_token, but then saves the record afterwards.
|
92
92
|
def reset_perishable_token!
|
93
93
|
reset_perishable_token
|
94
94
|
save_without_session_maintenance(:validate => false)
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
# A convenience method based on the disable_perishable_token_maintenance configuration option.
|
98
98
|
def disable_perishable_token_maintenance?
|
99
99
|
self.class.disable_perishable_token_maintenance == true
|
@@ -12,14 +12,14 @@ module Authlogic
|
|
12
12
|
attr_writer :scope
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
# = Scopes
|
17
17
|
module ClassMethods
|
18
18
|
# The current scope set, should be used in the block passed to with_scope.
|
19
19
|
def scope
|
20
20
|
Thread.current[:authlogic_scope]
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
# What with_scopes focuses on is scoping the query when finding the object and the name of the cookie / session. It works very similar to
|
24
24
|
# ActiveRecord::Base#with_scopes. It accepts a hash with any of the following options:
|
25
25
|
#
|
@@ -34,11 +34,11 @@ module Authlogic
|
|
34
34
|
#
|
35
35
|
# Eseentially what the above does is scope the searching of the object with the sql you provided. So instead of:
|
36
36
|
#
|
37
|
-
# User.
|
37
|
+
# User.where("login = 'ben'").first
|
38
38
|
#
|
39
39
|
# it would be:
|
40
40
|
#
|
41
|
-
# User.
|
41
|
+
# User.where("login = 'ben' and account_id = 2").first
|
42
42
|
#
|
43
43
|
# You will also notice the :id option. This works just like the id method. It scopes your cookies. So the name of your cookie will be:
|
44
44
|
#
|
@@ -65,31 +65,31 @@ module Authlogic
|
|
65
65
|
self.scope = nil
|
66
66
|
result
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
private
|
70
70
|
def scope=(value)
|
71
71
|
Thread.current[:authlogic_scope] = value
|
72
72
|
end
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
module InstanceMethods
|
76
76
|
# Setting the scope if it exists upon instantiation.
|
77
77
|
def initialize(*args)
|
78
78
|
self.scope = self.class.scope
|
79
79
|
super
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
# The scope of the current object
|
83
83
|
def scope
|
84
84
|
@scope ||= {}
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
private
|
88
88
|
# Used for things like cookie_key, session_key, etc.
|
89
89
|
def build_key(last_part)
|
90
90
|
[scope[:id], super].compact.join("_")
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
def search_for_record(*args)
|
94
94
|
klass.send(:with_scope, :find => (scope[:find_options] || {})) do
|
95
95
|
klass.send(*args)
|
@@ -4,16 +4,16 @@ module SessionTest
|
|
4
4
|
class ScopesTest < ActiveSupport::TestCase
|
5
5
|
def test_scope_method
|
6
6
|
assert_nil Authlogic::Session::Base.scope
|
7
|
-
|
7
|
+
|
8
8
|
thread1 = Thread.new do
|
9
9
|
scope = {:id => :scope1}
|
10
10
|
Authlogic::Session::Base.send(:scope=, scope)
|
11
11
|
assert_equal scope, Authlogic::Session::Base.scope
|
12
12
|
end
|
13
13
|
thread1.join
|
14
|
-
|
14
|
+
|
15
15
|
assert_nil Authlogic::Session::Base.scope
|
16
|
-
|
16
|
+
|
17
17
|
thread2 = Thread.new do
|
18
18
|
scope = {:id => :scope2}
|
19
19
|
Authlogic::Session::Base.send(:scope=, scope)
|
@@ -23,17 +23,17 @@ module SessionTest
|
|
23
23
|
|
24
24
|
assert_nil Authlogic::Session::Base.scope
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def test_with_scope_method
|
28
28
|
assert_raise(ArgumentError) { UserSession.with_scope }
|
29
|
-
|
29
|
+
|
30
30
|
UserSession.with_scope(:find_options => {:conditions => "awesome = 1"}, :id => "some_id") do
|
31
31
|
assert_equal({:find_options => {:conditions => "awesome = 1"}, :id => "some_id"}, UserSession.scope)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
assert_nil UserSession.scope
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def test_initialize
|
38
38
|
UserSession.with_scope(:find_options => {:conditions => "awesome = 1"}, :id => "some_id") do
|
39
39
|
session = UserSession.new
|
@@ -42,18 +42,18 @@ module SessionTest
|
|
42
42
|
assert_equal "another_id_some_id_test", session.send(:build_key, "test")
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def test_search_for_record_with_scopes
|
47
47
|
binary_logic = companies(:binary_logic)
|
48
48
|
ben = users(:ben)
|
49
49
|
zack = users(:zack)
|
50
|
-
|
50
|
+
|
51
51
|
session = UserSession.new
|
52
52
|
assert_equal zack, session.send(:search_for_record, "find_by_login", zack.login)
|
53
|
-
|
53
|
+
|
54
54
|
session.scope = {:find_options => {:conditions => ["company_id = ?", binary_logic.id]}}
|
55
55
|
assert_nil session.send(:search_for_record, "find_by_login", zack.login)
|
56
|
-
|
56
|
+
|
57
57
|
assert_equal ben, session.send(:search_for_record, "find_by_login", ben.login)
|
58
58
|
end
|
59
59
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: authlogic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 3.0.
|
5
|
+
version: 3.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ben Johnson of Binary Logic
|
@@ -187,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
187
187
|
requirements:
|
188
188
|
- - ">="
|
189
189
|
- !ruby/object:Gem::Version
|
190
|
-
hash:
|
190
|
+
hash: 4144864465011850466
|
191
191
|
segments:
|
192
192
|
- 0
|
193
193
|
version: "0"
|