Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds the possibility to configure the record's primary_key. #338

Open
wants to merge 2 commits into from

4 participants

@julianalucena

No description provided.

@binarylogic
Owner

Why would this deviate from the klass primary_key?

@julianalucena

I have two different Rails app and when the user logs in one app, he will be logged in both apps. To do so, both apps should interpret the cookie equally, but the same user has different primary_keys (databse auto increment column) for each app. So I needed to configure another column to be saved on the cookie, that is the user's id on the main app.

@tiegz
Collaborator

I might be misunderstanding, but you might be able to use the id feature for this. For example:

us1 = UserSession.new(params)
us1.id = :session1
us1.save

us2 = UserSession.new(params)
us2.id = :session2
us2.save

and then later:

us1 = UserSession.find(:session1)
us2 = UserSession.find(:session2)
@julianalucena

@tiegz, thanks for the help.

Yes, maybe this could solve. But this seems conceptually wrong to me because this change should be a configuration, do you know? And solve it locally when creating and finding sessions keeps things in the wrong place.

@tiegz
Collaborator

Ah, I see.

For your instance, tho, won't each app be using separate classes each with different logic? So if they're using different column names for their primary key, can't you set the primary_key specifically in each app's class?

@julianalucena

Actually, @tiegz, I want to maintain the primary_key as it is in the apps, I just need the session to consider another column as key to identify the user.

Got it?

@tiegz
Collaborator

Do you mean the session cookie key that identifiers the user, like user_credentials_id?

@julianalucena

Yes, because the session cookey key uses the user's id.

@tiegz
Collaborator

Ok, sorry, I understand now!

I'd say +1 on this. I think this could also be handy for obfuscating the user's id, for apps that use some sort of guid to identify users.

@fltiago

+1

@binarylogic
Owner

Cool, I'll have to merge in the new changes, unless someone else wants to tackle it. But I'll pull once we can merge without conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  .gitignore
@@ -1,5 +1,5 @@
.DS_Store
-.swp
+*.swp
*.log
*.sqlite3
pkg/*
View
12 lib/authlogic/acts_as_authentic/session_maintenance.rb
@@ -57,6 +57,17 @@ def session_class(value = nil)
rw_config(:session_class, value, const)
end
alias_method :session_class=, :session_class
+
+ # This method allows you to change the record's primary_key in the
+ # context of Authlogic.
+ #
+ # * <tt>Default:</tt> :id
+ # * <tt>Accepts:</tt> Symbol
+ def authlogic_record_primary_key(value = nil)
+ rw_config(:authlogic_record_primary_key, value,
+ base_class.primary_key.to_sym)
+ end
+ alias_method :authlogic_record_primary_key=, :authlogic_record_primary_key
end
module Methods
@@ -64,6 +75,7 @@ def self.included(klass)
klass.class_eval do
before_save :get_session_information, :if => :update_sessions?
before_save :maintain_sessions, :if => :update_sessions?
+ authlogic_record_primary_key
end
end
View
4 lib/authlogic/session/cookies.rb
@@ -155,7 +155,7 @@ def cookie_credentials
def persist_by_cookie
persistence_token, record_id = cookie_credentials
if !persistence_token.nil?
- record = record_id.nil? ? search_for_record("find_by_persistence_token", persistence_token) : search_for_record("find_by_#{klass.primary_key}", record_id)
+ record = record_id.nil? ? search_for_record("find_by_persistence_token", persistence_token) : search_for_record("find_by_#{klass.authlogic_record_primary_key}", record_id)
self.unauthorized_record = record if record && record.persistence_token == persistence_token
valid?
else
@@ -165,7 +165,7 @@ def persist_by_cookie
def save_cookie
controller.cookies[cookie_key] = {
- :value => "#{record.persistence_token}::#{record.send(record.class.primary_key)}",
+ :value => "#{record.persistence_token}::#{record.send(record.class.authlogic_record_primary_key)}",
:expires => remember_me_until,
:secure => secure,
:httponly => httponly,
View
4 lib/authlogic/session/session.rb
@@ -36,7 +36,7 @@ def persist_by_session
# This is done for performance reasons and to save on queries.
record = record_id.nil? ?
search_for_record("find_by_persistence_token", persistence_token) :
- search_for_record("find_by_#{klass.primary_key}", record_id)
+ search_for_record("find_by_#{klass.authlogic_record_primary_key}", record_id)
self.unauthorized_record = record if record && record.persistence_token == persistence_token
valid?
else
@@ -45,7 +45,7 @@ def persist_by_session
end
def session_credentials
- [controller.session[session_key], controller.session["#{session_key}_#{klass.primary_key}"]].compact
+ [controller.session[session_key], controller.session["#{session_key}_#{klass.authlogic_record_primary_key}"]].compact
end
def session_key
View
2  lib/authlogic/test_case/rails_request_adapter.rb
@@ -9,7 +9,7 @@ def authenticate_with_http_basic(&block)
def cookies
new_cookies = MockCookieJar.new
super.each do |key, value|
- new_cookies[key] = value[:value]
+ new_cookies[key] = { :value => value[:value] }
end
new_cookies
end
View
8 test/acts_as_authentic_test/session_maintenance_test.rb
@@ -10,6 +10,14 @@ def test_maintain_sessions_config
assert User.maintain_sessions
end
+ def test_authlogic_record_primary_key_config
+ assert_equal User.authlogic_record_primary_key, :id
+ User.authlogic_record_primary_key = :login
+ assert_equal User.authlogic_record_primary_key, :login
+ User.authlogic_record_primary_key = :id
+ assert_equal User.authlogic_record_primary_key, :id
+ end
+
def test_login_after_create
assert User.create(:login => "awesome", :password => "saweet", :password_confirmation => "saweet", :email => "awesome@awesome.com")
assert UserSession.find
Something went wrong with that request. Please try again.