Permalink
Browse files

Added logging_in_with..? methods

  • Loading branch information...
1 parent 9d8faa3 commit 14ca81421822109e9b63dd8da8a28539883be9bf @binarylogic committed Nov 8, 2008
View
@@ -1,6 +1,8 @@
== 1.0.1 released 2008-11-05
* Moved Rack standards into abstract_adapter for the controllers.
+* Added logging_in_with_credentials?, logging_in_with_unauthorized_record?
+* Fixed typo in abstract_adapter, black to block
== 1.0.0 released 2008-11-05
View
@@ -1,6 +1,6 @@
= Authlogic
-Authlogic is a framework agnostic object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database. Put simply, its the Chuck Norris of authentication solutions for rails, merb, etc.
+Authlogic is a simple framework agnostic object based authentication solution that handles all of the non sense for you. Put simply, its the Chuck Norris of authentication solutions for rails, merb, etc.
The last thing we need is another authentication solution, right? That's what I thought until I tried out some of the current solutions in both rails and merb. None of them felt right. They were either too complicated, bloated, littered my application with tons of code, or were just confusing. This is not the simple / elegant ruby we all fell in love with. We need a "ruby like" authentication solution. Authlogic is my attempt to satisfy that need...
@@ -269,7 +269,6 @@ Here is basically how this is done....
class User < ActiveRecord::Base
after_save :maintain_sessions!
- after_update :update_sessions!
private
def maintain_sessions!
@@ -286,7 +285,7 @@ When things come together like this I think its a sign that you are doing someth
I designed Authlogic to be framework agnostic, meaning it doesn't care what framework you use it in. Right out of the box it supports rails and merb. I have not had the opportunity to use other frameworks, but the only thing stopping Authlogic from being used in other frameworks is a simple adapter. Check out controller_adapters/rails_adapter, or controller_adapters/merb_adapter.
-Since pretty much all of the frameworks in ruby follow the Rack conventions, the code should be very similar across adapters. You're saying "but Ben, why not just hook into Rack and avoid the need for controller adapters all together?". It's not that simple, because rails doesn't inherit from the Rack::Request class, plus there are small differences between how rack is implemented in each framework. Authlogic has to hook into your controller with a before_filter anyways, so it can "activate" itself. Why not just use the controller object?
+Since pretty much all of the frameworks in ruby follow the Rack conventions, the code should be very similar across adapters. In fact that abstract adapter assumes you are using Rack. If you are using it properly there really isn't any code you should have to write. Check out the merb_adapter to see for yourself. You're saying "but Ben, why not just hook into Rack and avoid the need for controller adapters all together?". It's not that simple, because rails doesn't inherit from the Rack::Request class, plus there are small differences between how rack is implemented in each framework. Authlogic has to hook into your controller with a before_filter anyways, so it can "activate" itself. Why not just use the controller object?
The point in all of this rambling is that implementing Authlogic is as simple as creating an adapter. I created both the rails and merb adapters in under 10 minutes. If you have an adapter you created and would like to add please let me know and I will add it into the source.
View
@@ -8,7 +8,7 @@ Echoe.new 'authlogic' do |p|
p.author = "Ben Johnson of Binary Logic"
p.email = 'bjohnson@binarylogic.com'
p.project = 'authlogic'
- p.summary = "Framework agnostic object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database."
+ p.summary = "A simple framework agnostic object based authentication solution that handles all of the nonsense for you."
p.url = "http://github.com/binarylogic/authlogic"
p.dependencies = %w(activesupport activerecord)
p.include_rakefile = true
@@ -12,7 +12,7 @@ def initialize(controller)
def authenticate_with_http_basic(&block)
@auth = Rack::Auth::Basic::Request.new(controller.request.env)
if @auth.provided? and @auth.basic?
- black.call(*@auth.credentials)
+ block.call(*@auth.credentials)
else
false
end
@@ -205,6 +205,17 @@ def inspect # :nodoc:
"#<#{self.class.name} #{details.inspect}>"
end
+ # Returns true if logging in with credentials. Credentials mean username and password.
+ def logging_in_with_credentials?
+ login_with == :credentials
+ end
+
+ # Returns true if logging in with an unauthorized record
+ def logging_in_with_unauthorized_record?
+ login_with == :unauthorized_record
+ end
+ alias_method :logging_in_with_record?, :logging_in_with_unauthorized_record?
+
# Similar to ActiveRecord's new_record? Returns true if the session has not been saved yet.
def new_session?
new_session != false
@@ -109,14 +109,14 @@ def test_password
assert user.password_salt
assert user.remember_token
assert_equal true, user.tried_to_set_password
- assert_equal nil, user.password
+ assert_nil user.password
employee = Employee.new
employee.password = "awesome"
assert employee.crypted_password
assert employee.remember_token
assert_equal true, employee.tried_to_set_password
- assert_equal nil, employee.password
+ assert_nil employee.password
end
def test_valid_password
@@ -17,15 +17,15 @@ def test_controllers
end
thread1.join
assert_equal 1, Authlogic::Session::Base.send(:controllers).size
- assert_equal nil, Authlogic::Session::Base.controller
+ assert_nil Authlogic::Session::Base.controller
thread2 = Thread.new do
controller = MockController.new
Authlogic::Session::Base.controller = controller
assert_equal controller, Authlogic::Session::Base.controller
end
thread2.join
assert_equal 2, Authlogic::Session::Base.send(:controllers).size
- assert_equal nil, Authlogic::Session::Base.controller
+ assert_nil Authlogic::Session::Base.controller
end
def test_create
@@ -72,7 +72,7 @@ def test_scope_method # test_scope is reserved
assert_equal ben.remember_token, @controller.cookies["another_id_some_id_user_credentials"]
end
- assert_equal nil, UserSession.scope
+ assert_nil UserSession.scope
end
def test_with_scope_method # test_with_scope is reserved
@@ -98,23 +98,23 @@ def test_init
session = UserSession.new("login", "pass", true, :my_id)
assert_equal "login", session.login
- assert_equal nil, session.password
+ assert_nil session.password
assert_equal "pass", session.send(:protected_password)
assert_equal true, session.remember_me
assert_equal :my_id, session.id
session = UserSession.new({:login => "login", :password => "pass", :remember_me => true}, :my_id)
assert_equal "login", session.login
- assert_equal nil, session.password
+ assert_nil session.password
assert_equal "pass", session.send(:protected_password)
assert_equal true, session.remember_me
assert_equal :my_id, session.id
session = UserSession.new(users(:ben), :my_id)
- assert_equal nil, session.login
- assert_equal nil, session.password
- assert_equal nil, session.send(:protected_password)
- assert_equal nil, session.remember_me
+ assert_nil session.login
+ assert_nil session.password
+ assert_nil session.send(:protected_password)
+ assert_nil session.remember_me
assert_equal :my_id, session.id
assert_equal users(:ben), session.unauthorized_record
end
@@ -123,7 +123,7 @@ def test_credentials
session = UserSession.new
session.credentials = {:login => "login", :password => "pass", :remember_me => true}
assert_equal "login", session.login
- assert_equal nil, session.password
+ assert_nil session.password
assert_equal "pass", session.send(:protected_password)
assert_equal true, session.remember_me
assert_equal({:password => "<Protected>", :login => "login"}, session.credentials)
@@ -136,8 +136,8 @@ def test_destroy
assert_equal ben.remember_token, @controller.session["user_credentials"]
assert_equal ben.remember_token, @controller.cookies["user_credentials"]
session.destroy
- assert_equal nil, @controller.session["user_credentials"]
- assert_equal nil, @controller.cookies["user_credentials"]
+ assert_nil @controller.session["user_credentials"]
+ assert_nil @controller.cookies["user_credentials"]
end
def test_errors
@@ -177,7 +177,7 @@ def test_new_session
def test_remember_me
session = UserSession.new
- assert_equal nil, session.remember_me
+ assert_nil session.remember_me
assert !session.remember_me?
session.remember_me = false
@@ -189,7 +189,7 @@ def test_remember_me
assert session.remember_me?
session.remember_me = nil
- assert_equal nil, session.remember_me
+ assert_nil session.remember_me
assert !session.remember_me?
session.remember_me = "1"
@@ -203,7 +203,7 @@ def test_remember_me
def test_remember_me_until
session = UserSession.new
- assert_equal nil, session.remember_me_until
+ assert_nil session.remember_me_until
session.remember_me = true
assert 3.months.from_now <= session.remember_me_until
@@ -260,7 +260,7 @@ def test_unauthorized_record
def test_valid
session = UserSession.new
assert !session.valid?
- assert_equal nil, session.record
+ assert_nil session.record
assert session.errors.count > 0
ben = users(:ben)
@@ -5,7 +5,7 @@ def test_scope
UserSession.with_scope(:find_options => {:conditions => "awesome = 1"}, :id => "some_id") do
assert_equal({:find_options => {:conditions => "awesome = 1"}, :id => "some_id"}, UserSession.scope)
end
- assert_equal nil, UserSession.scope
+ assert_nil UserSession.scope
end
def test_with_scope

0 comments on commit 14ca814

Please sign in to comment.