entrance 0.4.3 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,12 +4,11 @@ require 'mongo_mapper'
|
|
|
4
4
|
require 'entrance'
|
|
5
5
|
|
|
6
6
|
MongoMapper.connection = Mongo::Connection.new('localhost')
|
|
7
|
-
MongoMapper.database = 'entrance-
|
|
7
|
+
MongoMapper.database = 'entrance-example'
|
|
8
8
|
|
|
9
9
|
Entrance.configure do |config|
|
|
10
10
|
config.remember_for = 1.month
|
|
11
11
|
config.cookie_secure = false # for testing
|
|
12
|
-
config.access_denied_redirect_to = '/login'
|
|
13
12
|
end
|
|
14
13
|
|
|
15
14
|
class User
|
|
@@ -4,7 +4,7 @@ require 'mongo_mapper'
|
|
|
4
4
|
require 'entrance'
|
|
5
5
|
|
|
6
6
|
MongoMapper.connection = Mongo::Connection.new('localhost')
|
|
7
|
-
MongoMapper.database = 'entrance-example'
|
|
7
|
+
MongoMapper.database = 'entrance-omniauth-example'
|
|
8
8
|
|
|
9
9
|
Entrance.configure do |config|
|
|
10
10
|
config.remember_for = 1.month
|
|
@@ -18,13 +18,16 @@ class User
|
|
|
18
18
|
key :state, :default => 'active'
|
|
19
19
|
|
|
20
20
|
key :name
|
|
21
|
+
key :email, :unique => true
|
|
22
|
+
|
|
23
|
+
key :password_hash, String
|
|
21
24
|
key :auth_provider, String
|
|
22
25
|
key :auth_uid, String
|
|
23
26
|
|
|
24
27
|
key :remember_token
|
|
25
28
|
key :remember_token_expires_at, Time
|
|
26
29
|
|
|
27
|
-
provides_entrance :local => false
|
|
30
|
+
provides_entrance :local => false, :remote => true
|
|
28
31
|
|
|
29
32
|
def active?
|
|
30
33
|
state.to_sym == :active
|
|
@@ -33,19 +33,6 @@ module Entrance
|
|
|
33
33
|
|
|
34
34
|
def registered(app)
|
|
35
35
|
|
|
36
|
-
::Entrance.model.class_eval do
|
|
37
|
-
|
|
38
|
-
def via_omniauth?
|
|
39
|
-
send(::Entrance.fields.auth_provider).present? \
|
|
40
|
-
&& send(::Entrance.fields.auth_uid).present?
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def password_required?
|
|
44
|
-
!via_omniauth? && (password.nil? || @password_changed)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
|
|
49
36
|
app.send(:include, Entrance::Controller) # provides redirects, etc
|
|
50
37
|
|
|
51
38
|
app.use ::OmniAuth::Builder do
|
data/lib/entrance/fields.rb
CHANGED
|
@@ -26,10 +26,12 @@ module Entrance
|
|
|
26
26
|
@auth_uid = 'auth_uid'
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
def validate(*attrs)
|
|
30
|
+
attrs.each do |attr|
|
|
31
|
+
field = send(attr)
|
|
32
|
+
unless fields.include?(field.to_sym)
|
|
33
|
+
raise "Couldn't find '#{field}' in the #{Entrance.model.name} model."
|
|
34
|
+
end
|
|
33
35
|
end
|
|
34
36
|
end
|
|
35
37
|
|
data/lib/entrance/model.rb
CHANGED
|
@@ -9,7 +9,8 @@ module Entrance
|
|
|
9
9
|
|
|
10
10
|
def provides_entrance(options = {}, &block)
|
|
11
11
|
Entrance.config.model = self.name
|
|
12
|
-
local
|
|
12
|
+
local = options.delete(:local) != false # true by default
|
|
13
|
+
remote = options.delete(:remote) == true # false by default
|
|
13
14
|
|
|
14
15
|
# if the target model class does not have a Model.where() method,
|
|
15
16
|
# then login_by_session wont work, nor the ClassMethods below.
|
|
@@ -22,11 +23,11 @@ module Entrance
|
|
|
22
23
|
yield fields if block_given?
|
|
23
24
|
|
|
24
25
|
# username and remember token are used both for local and remote (omniauth)
|
|
25
|
-
fields.
|
|
26
|
+
fields.validate(:username)
|
|
26
27
|
fields.validate_option(:remember)
|
|
27
28
|
|
|
28
29
|
if local # allows password & reset
|
|
29
|
-
fields.
|
|
30
|
+
fields.validate(:password)
|
|
30
31
|
fields.validate_option(:reset)
|
|
31
32
|
|
|
32
33
|
if self.respond_to?(:validates)
|
|
@@ -35,6 +36,11 @@ module Entrance
|
|
|
35
36
|
validates :password_confirmation, :presence => true, :if => :password_required?
|
|
36
37
|
end
|
|
37
38
|
end
|
|
39
|
+
|
|
40
|
+
if remote
|
|
41
|
+
fields.validate(:auth_provider, :auth_uid)
|
|
42
|
+
include RemoteAuthMethods if local # no need to if only remote
|
|
43
|
+
end
|
|
38
44
|
end
|
|
39
45
|
|
|
40
46
|
def authenticate(username, password)
|
|
@@ -86,14 +92,31 @@ module Entrance
|
|
|
86
92
|
token
|
|
87
93
|
end
|
|
88
94
|
|
|
95
|
+
def forget_me!
|
|
96
|
+
update_attribute(Entrance.fields.remember_token, nil)
|
|
97
|
+
update_attribute(Entrance.fields.remember_until, nil) if Entrance.fields.remember_until
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
89
102
|
def update_remember_token_expiration!(until_date = nil)
|
|
90
103
|
timestamp = Time.now + (until_date || Entrance.config.remember_for).to_i
|
|
91
104
|
update_attribute(Entrance.fields.remember_until, timestamp)
|
|
92
105
|
end
|
|
93
106
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
module RemoteAuthMethods
|
|
110
|
+
|
|
111
|
+
private
|
|
112
|
+
|
|
113
|
+
def from_remote_auth?
|
|
114
|
+
send(::Entrance.fields.auth_provider).present? \
|
|
115
|
+
&& send(::Entrance.fields.auth_uid).present?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def password_required?
|
|
119
|
+
!from_remote_auth? && super
|
|
97
120
|
end
|
|
98
121
|
|
|
99
122
|
end
|
data/lib/entrance/version.rb
CHANGED