simple_auth 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +4 -0
  5. data/Gemfile +1 -1
  6. data/MIGRATE.md +40 -0
  7. data/README.md +34 -137
  8. data/Rakefile +8 -18
  9. data/bin/console +5 -0
  10. data/gemfiles/{rails_4_1.gemfile → rails_4_2.gemfile} +1 -1
  11. data/gemfiles/{rails_4_0.gemfile → rails_5_0.gemfile} +1 -1
  12. data/lib/simple_auth.rb +26 -11
  13. data/lib/simple_auth/action_controller.rb +53 -81
  14. data/lib/simple_auth/action_controller/require_login_action.rb +47 -0
  15. data/lib/simple_auth/config.rb +13 -36
  16. data/lib/simple_auth/generator.rb +2 -2
  17. data/lib/simple_auth/railtie.rb +0 -11
  18. data/lib/simple_auth/session.rb +19 -143
  19. data/lib/simple_auth/templates/install/initializer.rb +23 -0
  20. data/lib/simple_auth/version.rb +1 -6
  21. data/simple_auth.gemspec +6 -3
  22. data/test/controllers/admin/dashboard_controller_test.rb +31 -0
  23. data/test/controllers/dashboard_controller_test.rb +56 -0
  24. data/test/controllers/pages_controller_test.rb +16 -0
  25. data/test/generators/install_test.rb +13 -0
  26. data/test/support/dummy/app/controllers/admin/dashboard_controller.rb +35 -0
  27. data/{spec/support → test/support/dummy}/app/controllers/application_controller.rb +0 -0
  28. data/test/support/dummy/app/controllers/dashboard_controller.rb +23 -0
  29. data/test/support/dummy/app/controllers/pages_controller.rb +7 -0
  30. data/{spec/support → test/support/dummy}/app/models/user.rb +1 -1
  31. data/test/support/dummy/config/application.rb +17 -0
  32. data/test/support/dummy/config/initializers/simple_auth.rb +23 -0
  33. data/test/support/dummy/config/routes.rb +23 -0
  34. data/test/support/schema.rb +6 -0
  35. data/test/test_helper.rb +15 -0
  36. metadata +75 -65
  37. data/.rspec +0 -1
  38. data/gemfiles/rails_3_1.gemfile +0 -5
  39. data/gemfiles/rails_3_2.gemfile +0 -5
  40. data/lib/simple_auth/active_record.rb +0 -95
  41. data/lib/simple_auth/compat.rb +0 -2
  42. data/lib/simple_auth/compat/active_record.rb +0 -28
  43. data/lib/simple_auth/compat/config.rb +0 -17
  44. data/lib/simple_auth/exceptions.rb +0 -4
  45. data/lib/simple_auth/helper.rb +0 -12
  46. data/lib/simple_auth/rspec.rb +0 -29
  47. data/locales/en.yml +0 -5
  48. data/locales/pt-BR.yml +0 -5
  49. data/spec/controllers/redirect_logged_user_spec.rb +0 -87
  50. data/spec/controllers/require_logged_user_spec.rb +0 -146
  51. data/spec/schema.rb +0 -9
  52. data/spec/simple_auth/active_record_spec.rb +0 -146
  53. data/spec/simple_auth/compat_spec.rb +0 -45
  54. data/spec/simple_auth/config_spec.rb +0 -21
  55. data/spec/simple_auth/helper_spec.rb +0 -24
  56. data/spec/simple_auth/initializer_spec.rb +0 -9
  57. data/spec/simple_auth/session_spec.rb +0 -212
  58. data/spec/spec_helper.rb +0 -23
  59. data/spec/support/app/models/customer.rb +0 -3
  60. data/spec/support/app/models/person.rb +0 -4
  61. data/spec/support/app/views/dashboard/index.erb +0 -0
  62. data/spec/support/app/views/session/new.erb +0 -0
  63. data/spec/support/config/boot.rb +0 -16
  64. data/spec/support/config/database.yml +0 -3
  65. data/spec/support/config/routes.rb +0 -4
  66. data/templates/initializer.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a215455bb8f741f52ef5264369f23de3ba08c7d
4
- data.tar.gz: 1d8f0a5c88ff1984aafc1e15301bb45f35b756fa
3
+ metadata.gz: 1de8cb8aeaa7b6bfee0b0a72545d2a466b931837
4
+ data.tar.gz: f4319e57e9226858d1b90e9b8d2aa84c684b21ef
5
5
  SHA512:
6
- metadata.gz: 2720ed1e46481f2f76fec454b8a987f9e8e5398ab86c796ab4bd503313e1b1116136370b3865cc48fbbde7a1ac7462fa4bf4e89c05cf47dcf8202a14b1577ad7
7
- data.tar.gz: 2ef4c67cfb8cada8dba397db2a0b032ed474233a5cfa408897d269bac2898f5d80b3a6821a24f8fff0448c0b702d8341bf10565a6260ae9e5df01e19f72d8181
6
+ metadata.gz: 66b65c80f6776fc1fff063e454614639e8916c33a16bf8e381abb2bc1b400ca470d3c0a2f876945f3ce753d25d7e02b32b670cabc81ff21e0181c8d1d428d6f9
7
+ data.tar.gz: 80386bfdc372ce83bf8ef03b18081d2b6415fb0b92925a00bb26bfa8415bdf3d320c59c825a683e3e1e9cbdff72f981a50bb54e883ed8532695c46a614f94a3f
data/.gitignore CHANGED
@@ -5,3 +5,6 @@ log
5
5
  .bundle
6
6
  gemfiles/*.lock
7
7
  Gemfile.lock
8
+ *.log
9
+ /tmp
10
+ /log
@@ -1,11 +1,8 @@
1
+ sudo: false
2
+ cache: bundler
1
3
  rvm:
2
- - 1.9.3
3
- - 2.0.0
4
- - 2.1.0
5
- script: bundle exec rspec
4
+ - "2.2.4"
5
+ script: bundle exec rake
6
6
  gemfile:
7
- - Gemfile
8
- - gemfiles/rails_3_1.gemfile
9
- - gemfiles/rails_3_2.gemfile
10
- - gemfiles/rails_4_0.gemfile
11
- - gemfiles/rails_4_1.gemfile
7
+ - gemfiles/rails_4_2.gemfile
8
+ - gemfiles/rails_5_0.gemfile
@@ -1,3 +1,7 @@
1
+ #v3.0.0
2
+ - Reimplemented library.
3
+ - Add support for scoped authentication (e.g. user and admin).
4
+
1
5
  # v2.0.3
2
6
 
3
7
  - Assign the raw password/confirmation, so we can apply validations on the raw value.
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
  gemspec
@@ -0,0 +1,40 @@
1
+ # Migrate from previous versions to v3
2
+
3
+ Follow these steps:
4
+
5
+ 1. Rename your existing `config/initializers/simple_auth.rb` to `config/initializers/simple_auth.rb.old`.
6
+ 2. Generate a new initializer with `rails g simple_auth:install`. Update `config/initializers/simple_auth.rb` with your settings (check `simple_auth.rb.old`).
7
+ 3. Remove `config/initializers/simple_auth.rb.old`.
8
+ 4. Remove `authentication` from your model (e.g. `User`).
9
+ 5. Replace all calls from old version as the list below:
10
+ - Controllers: `require_logged_user` becomes `before_action :require_logged_user`.
11
+ - Controllers: `redirect_logged_user` becomes `before_action :redirect_logged_user`.
12
+ - Controllers & Views: `logged_in?` becomes `user_logged_in?`.
13
+ - Controllers: `authorized?` becomes `authorized_user?`.
14
+ - Controllers: `current_session.destroy` becomes `reset_session`.
15
+ 6. On your sessions controller, replace the call to `SimpleAuth::Session.new` to something like this:
16
+ ```ruby
17
+ class SessionsController < ApplicationController
18
+ def new
19
+ end
20
+
21
+ def create
22
+ @user = User.find_by_email(params[:email])
23
+
24
+ if @user.try(:authenticate, params[:password])
25
+ SimpleAuth::Session.create(scope: "user", session: session, record: @user)
26
+ redirect_to return_to(dashboard_path)
27
+ else
28
+ flash[:alert] = "Invalid username or password"
29
+ render :new
30
+ end
31
+ end
32
+
33
+ def destroy
34
+ reset_session
35
+ redirect_to root_path
36
+ end
37
+ end
38
+ ```
39
+
40
+ If you have any issue, just [open a ticket](https://github.com/fnando/simple_auth/issues/new).
data/README.md CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/fnando/simple_auth.svg)](https://travis-ci.org/fnando/simple_auth)
4
4
  [![Code Climate](https://codeclimate.com/github/fnando/simple_auth.png)](https://codeclimate.com/github/fnando/simple_auth)
5
+ [![Gem Version](https://badge.fury.io/rb/simple_auth.svg)](http://badge.fury.io/rb/simple_auth)
5
6
 
6
7
  SimpleAuth is an authentication library to be used when everything else is just too complicated.
7
8
 
8
- This library only supports in-site authentication and won't implement OpenID, Facebook Connect and like.
9
+ This library only handles session. You have to implement the authentication strategy as you want (e.g. in-site authentication, OAuth, etc).
9
10
 
10
- Rails 3.1.0+ is required.
11
+ Rails 4.2+ running over Ruby 2.1+ is required.
11
12
 
12
13
  ## Installation
13
14
 
@@ -19,67 +20,42 @@ Then run `rails generate simple_auth:install` to copy the initializer file.
19
20
 
20
21
  ## Usage
21
22
 
22
- Your user model should have the attribute `password_digest`. The credential field can be anything you want, but SimpleAuth uses `[:email, :login]` by default.
23
+ The initializer will install the required helper methods on your controller. So, let's say you want to support `user` and `admin` authentication. You'll need to specify the following scope.
23
24
 
24
25
  ```ruby
25
- class CreateUsers < ActiveRecord::Migration
26
- def change
27
- create_table :users do |t|
28
- t.string :email, null: false
29
- t.string :login, null: false
30
- t.string :password_digest, null: false
31
-
32
- t.timestamps
33
- end
26
+ # config/initializers/simple_auth.rb
27
+ SimpleAuth.setup do |config|
28
+ config.scopes = %i[user admin]
29
+ config.login_url = proc { login_path }
30
+ config.logged_url = proc { dashboard_path }
34
31
 
35
- add_index :users, :email, unique: true
36
- add_index :users, :login, unique: true
37
- add_index :users, [:email, :login]
38
- end
39
- end
40
- ```
41
-
42
- In your model, use the `authentication` macro.
43
-
44
- ```ruby
45
- class User < ActiveRecord::Base
46
- authentication
32
+ config.install_helpers!
47
33
  end
48
34
  ```
49
35
 
50
- This will add some callbacks and password validations. It will also inject helper methods like `Model.authenticate`.
51
-
52
- Session is valid only when both `Model#authorized?` and `Controller#authorized?` methods return `true`, which is the default behavior. You can override these methods with your own rules:
36
+ Session is valid only when `Controller#authorized_#{scope}?` method returns `true`, which is the default behavior. You can override these methods with your own rules; the following example shows how you can authorize all e-mails from `@example.com` to access the admin dashboard.
53
37
 
54
38
  ```ruby
55
- class User < ActiveRecord::Base
56
- authentication
57
-
58
- def authorized?
59
- deleted_at.nil?
60
- end
61
- end
62
-
63
39
  class Admin::DashboardController < ApplicationController
64
40
  private
65
- def authorized?
66
- current_user.admin?
41
+ def authorized_admin?
42
+ current_user.email.match(/@example.com\z/)
67
43
  end
68
44
  end
69
45
  ```
70
46
 
71
- After you set up the model, you can go to the controller.
47
+ So, how do you set up a new user session? That's really simple, actually.
72
48
 
73
49
  ```ruby
74
50
  class SessionsController < ApplicationController
75
51
  def new
76
- @user_session = SimpleAuth::Session.new
77
52
  end
78
53
 
79
54
  def create
80
- @user_session = SimpleAuth::Session.new(params[:session])
55
+ @user = User.find_by_email(params[:email])
81
56
 
82
- if @user_session.save
57
+ if @user.try(:authenticate, params[:password])
58
+ SimpleAuth::Session.create(scope: "user", session: session, record: @user)
83
59
  redirect_to return_to(dashboard_path)
84
60
  else
85
61
  flash[:alert] = "Invalid username or password"
@@ -88,79 +64,38 @@ class SessionsController < ApplicationController
88
64
  end
89
65
 
90
66
  def destroy
91
- current_session.destroy if logged_in?
67
+ reset_session
92
68
  redirect_to root_path
93
69
  end
94
70
  end
95
71
  ```
96
72
 
97
- The `return_to` helper will give you the requested url (before the user logged in) or the default url.
73
+ First thing to notice is that simple_auth doesn't care about how you authenticate. You could easily set up a different authentication strategy, e.g. API tokens. The important part is assign the `record:` and `scope:` options. The `return_to` helper will give you the requested url (before the user logged in) or the default url.
74
+
75
+ Same thing applies to destroying a session. You can just reset it, calling `reset_session`.
98
76
 
99
- You can restrict access by using 2 macros:
77
+ You can restrict access by using 2 macros. Use `redirect_logged_#{scope}` to avoid rendering a page for logged user.
100
78
 
101
79
  ```ruby
102
80
  class SignupController < ApplicationController
103
- redirect_logged_user :to => "/"
81
+ before_action :redirect_logged_user
104
82
  end
105
83
  ```
106
84
 
107
- Here's some usage examples:
108
-
109
- ```ruby
110
- redirect_logged_user :to => proc { login_path }
111
- redirect_logged_user :to => {:controller => "dashboard"}
112
- redirect_logged_user :only => [:index], :to => login_path
113
- redirect_logged_user :except => [:public], :to => login_path
114
- ```
115
-
116
- You can skip the `:to` option if you set it globally on your initializer:
117
-
118
- ```ruby
119
- SimpleAuth::Config.logged_url = {:controller => "session", :action => "new"}
120
- SimpleAuth::Config.logged_url = proc { login_path }
121
- ```
122
-
123
- To require a logged user, use the `require_logged_user` macro:
85
+ Use `require_logged_#{scope}` to enforce authenticated access.
124
86
 
125
87
  ```ruby
126
88
  class DashboardController < ApplicationController
127
- require_logged_user :to => proc { login_path }
89
+ before_action :require_logged_user
128
90
  end
129
91
  ```
130
92
 
131
- Here's some usage examples:
93
+ "So which helpers are defined?", you ask. Just three simple helpers.
132
94
 
133
95
  ```ruby
134
- require_logged_user :to => proc { login_path }
135
- require_logged_user :to => {:controller => "session", :action => "new"}
136
- require_logged_user :only => [:index], :to => login_path
137
- require_logged_user :except => [:public], :to => login_path
138
- ```
139
-
140
- You can skip the `:to` option if you set it globally on your initializer:
141
-
142
- ```ruby
143
- SimpleAuth::Config.login_url = {:controller => "session", :action => "new"}
144
- SimpleAuth::Config.login_url = proc { login_path }
145
- ```
146
-
147
- There are some helpers:
148
-
149
- ```ruby
150
- logged_in? # controller & views
151
- current_user # controller & views
152
- current_session # controller & views
153
- when_logged(&block) # views
154
- find_by_credential # model
155
- find_by_credential! # model
156
- ```
157
-
158
- If you're having problems to use any helper, include the module `SimpleAuth::Helper` on your `ApplicationHelper`.
159
-
160
- ```ruby
161
- module ApplicationHelper
162
- include SimpleAuth::Helper
163
- end
96
+ #{scope}_logged_in? # e.g. user_logged_in? (available in controller & views)
97
+ current_#{scope} # e.g. current_user (available in controller & views)
98
+ #{scope}_session # e.g. user_session (available in controller & views)
164
99
  ```
165
100
 
166
101
  ### Translations
@@ -170,54 +105,16 @@ These are the translations you'll need:
170
105
  ```yaml
171
106
  en:
172
107
  simple_auth:
173
- sessions:
174
- need_to_be_logged: "You need to be logged"
175
- invalid_credentials: "Invalid username or password"
176
- ```
177
-
178
- ### Compatibility Mode with v1
179
-
180
- The previous version was based on hashing with salt. If you want to migrate to the v2 release, you must do some things.
181
-
182
- First, add the following line to the configuration initializer (available at `config/initializers/simple_auth.rb`:
183
-
184
- ```ruby
185
- require "simple_auth/compat"
186
- ```
187
-
188
- Then create a field called `password_digest`. This field is required by the `ActiveRecord::Base.has_secure_password` method. You can create a migration with the following content:
189
-
190
- ```ruby
191
- class AddPasswordDigestToUsers < ActiveRecord::Migration
192
- def up
193
- add_column :users, :password_digest, :string, null: true
194
- SimpleAuth.migrate_passwords!
195
- change_column_null :users, :password_digest, false
196
- end
197
-
198
- def down
199
- remove_column :users, :password_digest
200
- end
201
- end
202
- ```
203
-
204
- Apply this migration with `rake db:migrate`. Go read a book; this is going to take a while.
205
-
206
- Check if your application is still working. If so, you can remove the `password_hash` column. Here's the migration to do it so.
207
-
208
- ```ruby
209
- class RemovePasswordHashFromUsers < ActiveRecord::Migration
210
- def change
211
- remove_column :users, :password_hash
212
- end
213
- end
108
+ user:
109
+ need_to_be_logged_in: "You need to be logged"
110
+ not_authorized: "You don't have permission to access this page"
214
111
  ```
215
112
 
216
- Again, apply this migration with `rake db:migrate`.
113
+ If you don't set these translations, a default message will be used.
217
114
 
218
115
  ## Maintainer
219
116
 
220
- * Nando Vieira (<http://simplesideias.com.br>)
117
+ * Nando Vieira (<http://nandovieira.com>)
221
118
 
222
119
  ## License:
223
120
 
data/Rakefile CHANGED
@@ -1,20 +1,10 @@
1
- require "bundler"
2
- Bundler::GemHelper.install_tasks
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
3
 
4
- require "rspec/core/rake_task"
5
- RSpec::Core::RakeTask.new
6
-
7
- desc "Run specs against all gemfiles"
8
- task "spec:all" do
9
- %w[
10
- Gemfile
11
- gemfiles/rails_3_1.gemfile
12
- gemfiles/rails_3_2.gemfile
13
- gemfiles/rails_4_0.gemfile
14
- gemfiles/rails_4_1.gemfile
15
- ].each do |gemfile|
16
- puts "\n=> Running with Gemfile: #{gemfile}"
17
- system "BUNDLE_GEMFILE=#{gemfile} bundle exec rspec"
18
- exit 1 unless $?.success?
19
- end
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
20
8
  end
9
+
10
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require "bundler/setup"
3
+ require "pry"
4
+ Pry.start
5
+ require "simple_auth"
@@ -1,4 +1,4 @@
1
1
  source "https://rubygems.org"
2
2
  gemspec path: ".."
3
3
 
4
- gem "rails", "~> 4.1.0"
4
+ gem "rails", "~> 4.2.4"
@@ -1,4 +1,4 @@
1
1
  source "https://rubygems.org"
2
2
  gemspec path: ".."
3
3
 
4
- gem "rails", "~> 4.0.0"
4
+ gem "rails", github: "rails/rails"
@@ -1,11 +1,26 @@
1
- require "rails/railtie"
2
- require "active_support/all"
3
-
4
- require "simple_auth/railtie"
5
- require "simple_auth/config"
6
- require "simple_auth/exceptions"
7
- require "simple_auth/action_controller"
8
- require "simple_auth/active_record"
9
- require "simple_auth/session"
10
- require "simple_auth/helper"
11
- require "simple_auth/version"
1
+ module SimpleAuth
2
+ require "rails/railtie"
3
+ require "active_support/concern"
4
+
5
+ require "simple_auth/version"
6
+ require "simple_auth/config"
7
+ require "simple_auth/railtie"
8
+ require "simple_auth/action_controller"
9
+ require "simple_auth/action_controller/require_login_action"
10
+ require "simple_auth/session"
11
+ require "simple_auth/generator"
12
+
13
+ def self.setup
14
+ yield config
15
+ end
16
+
17
+ def self.config
18
+ @config ||= Config.new
19
+ end
20
+
21
+ setup do |config|
22
+ config.scopes = %i[user]
23
+ config.login_url = -> { login_path }
24
+ config.logged_url = -> { dashboard_path }
25
+ end
26
+ end
@@ -1,102 +1,74 @@
1
+ # frozen_string_literal: true
1
2
  module SimpleAuth
2
3
  module ActionController
3
- def self.included(base)
4
- base.class_eval do
5
- include InstanceMethods
6
- extend ClassMethods
7
- end
8
- end
4
+ extend ActiveSupport::Concern
9
5
 
10
- module InstanceMethods
11
- private
12
- def return_to(url = nil, &block)
13
- url = session.delete(:return_to) || url
14
- url = instance_eval(&block) if block_given?
15
- url
16
- end
6
+ included do
7
+ install_simple_auth_scopes
8
+ end
17
9
 
18
- def current_session
19
- @current_session ||= SimpleAuth::Session.find
10
+ module ClassMethods
11
+ def install_simple_auth_scopes
12
+ SimpleAuth.config.scopes.each do |scope|
13
+ install_simple_auth_scope(scope)
14
+ helper_method "current_#{scope}", "#{scope}_logged_in?"
15
+ end
20
16
  end
21
17
 
22
- def current_user
23
- current_session && current_session.record
24
- end
18
+ def install_simple_auth_scope(scope)
19
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
20
+ def #{scope}_session
21
+ @#{scope}_session ||= Session.create(scope: :#{scope}, session: session)
22
+ end
25
23
 
26
- def authorized?
27
- true
28
- end
24
+ def current_#{scope}
25
+ #{scope}_session.record
26
+ end
29
27
 
30
- def logged_in?
31
- current_user != nil
32
- end
28
+ def #{scope}_logged_in?
29
+ current_#{scope}.present?
30
+ end
31
+ RUBY
33
32
 
34
- def activate_simple_auth
35
- SimpleAuth::Config.controller = self
36
- end
33
+ define_method "authorized_#{scope}?" do
34
+ true
35
+ end
37
36
 
38
- def simple_auth_url_for(method, controller, path)
39
- path ||= SimpleAuth::Config.send(method)
40
- path = controller.instance_eval(&path) if path.kind_of?(Proc)
41
- path
42
- end
37
+ define_method "require_logged_#{scope}" do
38
+ simple_auth_require_logged_scope(scope)
39
+ end
43
40
 
44
- def request_uri
45
- if request.respond_to?(:fullpath)
46
- request.fullpath
47
- else
48
- request.request_uri
41
+ define_method "redirect_logged_#{scope}" do
42
+ simple_auth_redirect_logged_scope(scope)
49
43
  end
50
44
  end
51
45
  end
52
46
 
53
- module ClassMethods
54
- # Redirect unlogged users to the specified <tt>:to</tt> path
55
- #
56
- # require_logged_user :to => proc { login_path }
57
- # require_logged_user :to => {:controller => "session", :action => "new"}
58
- # require_logged_user :only => [:index], :to => login_path
59
- # require_logged_user :except => [:public], :to => login_path
60
- #
61
- # You can set login url globally:
62
- #
63
- # SimpleAuth::Config.login_url = {:controller => "session", :action => "new"}
64
- # SimpleAuth::Config.login_url = proc { login_path }
65
- #
66
- def require_logged_user(options = {})
67
- before_filter options.except(:to) do |controller|
68
- controller.instance_eval do
69
- # Already logged in, so skip validation.
70
- next if current_session.try(:valid?) && authorized?
47
+ private
71
48
 
72
- session[:return_to] = request_uri if request.get?
49
+ def simple_auth
50
+ @simple_auth ||= SimpleAuth.config
51
+ end
73
52
 
74
- SimpleAuth::Session.destroy!
75
- flash.alert = t("simple_auth.sessions.need_to_be_logged")
76
- redirect_to simple_auth_url_for(:login_url, controller, options[:to])
77
- end
78
- end
79
- end
53
+ def return_to(url)
54
+ session[:return_to] || url
55
+ end
80
56
 
81
- # Redirect logged users to the specified <tt>:to</tt> path
82
- #
83
- # redirect_logged_user :to => proc { login_path }
84
- # redirect_logged_user :to => {:controller => "dashboard"}
85
- # redirect_logged_user :only => [:index], :to => login_path
86
- # redirect_logged_user :except => [:public], :to => login_path
87
- #
88
- # You can set the logged url globally:
89
- #
90
- # SimpleAuth::Config.logged_url = {:controller => "dashboard", :action => "index"}
91
- # SimpleAuth::Config.logged_url = proc { dashboard_path }
92
- #
93
- def redirect_logged_user(options = {})
94
- before_filter options.except(:to) do |controller|
95
- controller.instance_eval do
96
- redirect_to simple_auth_url_for(:logged_url, controller, options[:to]) if logged_in?
97
- end
98
- end
99
- end
57
+ def simple_auth_require_logged_scope(scope)
58
+ action = RequireLoginAction.new(self, scope)
59
+ return if action.valid?
60
+
61
+ reset_session
62
+ flash[:alert] = action.message
63
+ session[:return_to] = request.fullpath if request.get?
64
+ redirect_to instance_eval(&simple_auth.login_url)
65
+ end
66
+
67
+ def simple_auth_redirect_logged_scope(scope)
68
+ scope_session = send("#{scope}_session")
69
+ return unless scope_session.valid?
70
+
71
+ redirect_to instance_eval(&simple_auth.logged_url)
100
72
  end
101
73
  end
102
74
  end