simple_auth 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source :rubygems
2
2
 
3
- gem "rspec-rails", "2.0.0.rc"
3
+ gem "rspec-rails", "2.0.0"
4
4
  gem "rails", "3.0.0"
5
5
  gem "sqlite3-ruby", :require => "sqlite3"
6
6
 
data/Gemfile.lock CHANGED
@@ -65,18 +65,18 @@ GEM
65
65
  rake (>= 0.8.4)
66
66
  thor (~> 0.14.0)
67
67
  rake (0.8.7)
68
- rspec (2.0.0.rc)
69
- rspec-core (= 2.0.0.rc)
70
- rspec-expectations (= 2.0.0.rc)
71
- rspec-mocks (= 2.0.0.rc)
72
- rspec-core (2.0.0.rc)
73
- rspec-expectations (2.0.0.rc)
68
+ rspec (2.0.0)
69
+ rspec-core (= 2.0.0)
70
+ rspec-expectations (= 2.0.0)
71
+ rspec-mocks (= 2.0.0)
72
+ rspec-core (2.0.0)
73
+ rspec-expectations (2.0.0)
74
74
  diff-lcs (>= 1.1.2)
75
- rspec-mocks (2.0.0.rc)
76
- rspec-core (= 2.0.0.rc)
77
- rspec-expectations (= 2.0.0.rc)
78
- rspec-rails (2.0.0.rc)
79
- rspec (= 2.0.0.rc)
75
+ rspec-mocks (2.0.0)
76
+ rspec-core (= 2.0.0)
77
+ rspec-expectations (= 2.0.0)
78
+ rspec-rails (2.0.0)
79
+ rspec (= 2.0.0)
80
80
  ruby-debug (0.10.3)
81
81
  columnize (>= 0.1)
82
82
  ruby-debug-base (~> 0.10.3.0)
@@ -103,7 +103,7 @@ PLATFORMS
103
103
 
104
104
  DEPENDENCIES
105
105
  rails (= 3.0.0)
106
- rspec-rails (= 2.0.0.rc)
106
+ rspec-rails (= 2.0.0)
107
107
  ruby-debug
108
108
  ruby-debug19
109
109
  sqlite3-ruby
data/README.markdown CHANGED
@@ -57,7 +57,7 @@ After you set up the model, you can go to the controller.
57
57
  @user_session = SimpleAuth::Session.new(params[:session])
58
58
 
59
59
  if @user_session.save
60
- redirect_to dashboard_path
60
+ redirect_to return_to(dashboard_path)
61
61
  else
62
62
  flash[:alert] = "Invalid username or password"
63
63
  render :new
@@ -70,6 +70,8 @@ After you set up the model, you can go to the controller.
70
70
  end
71
71
  end
72
72
 
73
+ The `return_to` helper will give you the requested url (before the user logged in) or the default url.
74
+
73
75
  You can restrict access by using 2 macros:
74
76
 
75
77
  class SignupController < ApplicationController
@@ -112,6 +114,8 @@ There are some helpers:
112
114
  current_user # controller & views
113
115
  current_session # controller & views
114
116
  when_logged(&block) # views
117
+ find_by_credential # model
118
+ find_by_credential! # model
115
119
 
116
120
  If you're having problems to use any helper, include the module <tt>SimpleAuth::Helper</tt> on your <tt>ApplicationHelper</tt>.
117
121
 
@@ -9,6 +9,12 @@ module SimpleAuth
9
9
 
10
10
  module InstanceMethods
11
11
  private
12
+ def return_to(url = nil, &block)
13
+ url = session.fetch("return_to", url)
14
+ url = instance_eval(&block) if block_given?
15
+ url
16
+ end
17
+
12
18
  def current_session
13
19
  @current_session ||= SimpleAuth::Session.find
14
20
  end
@@ -74,12 +74,12 @@ module SimpleAuth
74
74
  end
75
75
 
76
76
  module ClassMethods
77
- # Receive a credential and a password and try to authenticate the specified user.
78
- # If the credential is valid, then an user is returned; otherwise nil is returned.
77
+ # Find user by its credential.
79
78
  #
80
- # User.authenticate "johndoe", "test"
81
- # User.authenticate "john@doe.com", "test"
82
- def authenticate(credential, password)
79
+ # User.find_by_credential "john@doe.com" # using e-mail
80
+ # User.find_by_credential "john" # using username
81
+ #
82
+ def find_by_credential(credential)
83
83
  # Build a hash that will be passed to the finder
84
84
  options = {:conditions => [[], {}]}
85
85
 
@@ -94,7 +94,28 @@ module SimpleAuth
94
94
  options[:conditions][0] = options[:conditions][0].join(" OR ")
95
95
 
96
96
  # Find the record using the conditions we built
97
- record = SimpleAuth::Config.model_class.first(options)
97
+ SimpleAuth::Config.model_class.first(options)
98
+ end
99
+
100
+ # Find user by its credential. If no user is found, raise
101
+ # ActiveRecord::RecordNotFound exception.
102
+ #
103
+ # User.find_by_credential! "john@doe.com"
104
+ #
105
+ def find_by_credential!(credential)
106
+ record = find_by_credential(credential)
107
+ raise ::ActiveRecord::RecordNotFound, "couldn't find #{SimpleAuth::Config.model} using #{credential.inspect} as credential" unless record
108
+ record
109
+ end
110
+
111
+ # Receive a credential and a password and try to authenticate the specified user.
112
+ # If the credential is valid, then an user is returned; otherwise nil is returned.
113
+ #
114
+ # User.authenticate "johndoe", "test"
115
+ # User.authenticate "john@doe.com", "test"
116
+ #
117
+ def authenticate(credential, password)
118
+ record = find_by_credential(credential)
98
119
 
99
120
  # If no record has been found
100
121
  return nil unless record
@@ -4,20 +4,18 @@ module SimpleAuth
4
4
  require "simple_auth/generator"
5
5
  end
6
6
 
7
- config.after_initialize do
8
- ActiveSupport.on_load(:action_controller) do
7
+ initializer "simple_auth.initialize" do |app|
8
+ ::ActionController::Base.instance_eval do
9
9
  include SimpleAuth::ActionController
10
10
  helper SimpleAuth::Helper
11
11
  prepend_before_filter :activate_simple_auth
12
12
  helper_method :current_user, :current_session, :logged_in?
13
13
  end
14
14
 
15
- ActiveSupport.on_load(:active_record) do
15
+ ::ActiveRecord::Base.instance_eval do
16
16
  include SimpleAuth::ActiveRecord
17
17
  end
18
- end
19
18
 
20
- initializer "simple_auth.initialize" do |app|
21
19
  ::I18n.load_path += Dir[File.dirname(__FILE__) + "/../../config/locales/*.yml"]
22
20
  end
23
21
  end
@@ -2,7 +2,7 @@ module SimpleAuth
2
2
  module Version
3
3
  MAJOR = "1"
4
4
  MINOR = "0"
5
- PATCH = "1"
5
+ PATCH = "2"
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
data/simple_auth.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_auth}
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nando Vieira"]
12
- s.date = %q{2010-10-12}
12
+ s.date = %q{2010-10-14}
13
13
  s.description = %q{When Authlogic & Devise are just too much.}
14
14
  s.email = %q{fnando.vieira@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -19,9 +19,18 @@ describe ApplicationController do
19
19
  end
20
20
  end
21
21
 
22
+ it "should return the request url" do
23
+ get :index, :some => "param"
24
+ controller.send(:return_to, "/dashboard").should == "/stub_resources?some=param"
25
+ end
26
+
27
+ it "should return the default url" do
28
+ controller.send(:return_to, "/dashboard").should == "/dashboard"
29
+ end
30
+
22
31
  it "should set return to" do
23
- get :index
24
- session[:return_to].should == "/stub_resources"
32
+ get :index, :some => "param"
33
+ session[:return_to].should == "/stub_resources?some=param"
25
34
  end
26
35
 
27
36
  it "should set warning message" do
@@ -121,5 +121,20 @@ describe SimpleAuth::ActiveRecord do
121
121
  it "should not authenticate using wrong password" do
122
122
  User.authenticate("johndoe", "invalid").should be_nil
123
123
  end
124
+
125
+ it "should return nil when no user has been found" do
126
+ User.find_by_credential("invalid").should be_nil
127
+ end
128
+
129
+ it "should raise error when no user has been found" do
130
+ expect {
131
+ User.find_by_credential!("invalid")
132
+ }.to raise_error(ActiveRecord::RecordNotFound)
133
+ end
134
+
135
+ it "should return user" do
136
+ User.find_by_credential(subject.email).should == subject
137
+ User.find_by_credential!(subject.email).should == subject
138
+ end
124
139
  end
125
140
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 1
9
- version: 1.0.1
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-12 00:00:00 -03:00
17
+ date: 2010-10-14 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency