devise-basecamper 0.2.2 → 0.4.1
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.
- data/devise-basecamper.gemspec +1 -1
- data/lib/devise-basecamper/model.rb +94 -13
- data/lib/devise-basecamper/version.rb +1 -1
- metadata +4 -4
data/devise-basecamper.gemspec
CHANGED
@@ -8,7 +8,8 @@ module Devise
|
|
8
8
|
defaults = {
|
9
9
|
:subdomain_class => :account,
|
10
10
|
:subdomain_field => :subdomain,
|
11
|
-
:scope_field => :account_id
|
11
|
+
:scope_field => :account_id,
|
12
|
+
:login_fields => [:username, :email]
|
12
13
|
}
|
13
14
|
@devise_basecamper_settings = defaults.merge(opts)
|
14
15
|
end
|
@@ -18,13 +19,21 @@ module Devise
|
|
18
19
|
return @devise_basecamper_settings
|
19
20
|
end
|
20
21
|
|
22
|
+
## Methods ------------------------------------------------------------
|
23
|
+
|
21
24
|
def find_for_authentication(conditions={})
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
authentication_keys = Devise.authentication_keys
|
26
|
+
|
27
|
+
## Process subdomain info by finding the parent resource id into the conditions
|
28
|
+
conditions = clean_conditions_for_subdomain(conditions)
|
29
|
+
|
30
|
+
## Process if "login" key used instead of default (:email)
|
31
|
+
if conditions[:login].present? && authentication_keys.include?(:login)
|
32
|
+
resource = find_with_login_instead_of_default( authentication_keys, conditions )
|
33
|
+
return resource
|
27
34
|
end
|
35
|
+
|
36
|
+
## Execute original find_for_authentication code
|
28
37
|
super
|
29
38
|
end
|
30
39
|
|
@@ -45,13 +54,85 @@ module Devise
|
|
45
54
|
private
|
46
55
|
|
47
56
|
def send_instructions_for(action_method, attributes={})
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
57
|
+
scope_field = self.basecamper[:scope_field].downcase.to_sym
|
58
|
+
subdomain_resource = find_subdomain_resource(attributes[:subdomain])
|
59
|
+
subdomain_resource_id = subdomain_resource.nil? ? nil : subdomain_resource.id
|
60
|
+
required_keys = Devise.send "#{action_method.to_s}_keys".to_sym
|
61
|
+
|
62
|
+
## Find our resource for sending the email
|
63
|
+
if attributes[:login].present? && reset_password_keys.include?(:login)
|
64
|
+
resource = find_with_login_instead_of_default(required_keys, attributes)
|
65
|
+
else
|
66
|
+
resource = find_or_initialize_with_errors(reset_password_keys,{
|
67
|
+
:email => attributes[:email], scope_field => subdomain_resource_id
|
68
|
+
})
|
69
|
+
end
|
70
|
+
|
71
|
+
resource.send("send_#{action_method.to_s}_instructions") if !resource.nil? && resource.persisted?
|
72
|
+
return resource
|
73
|
+
end
|
74
|
+
|
75
|
+
private # -------------------------------------------------------------
|
76
|
+
|
77
|
+
## If devise is configured to allow authentication using either a username
|
78
|
+
## or email, as described in the wiki we will need to process the find
|
79
|
+
## appropriately.
|
80
|
+
def find_with_login_instead_of_default(required_attributes={}, attributes={}, error=:invalid)
|
81
|
+
resource = nil
|
82
|
+
scope_field = self.basecamper[:scope_field]
|
83
|
+
login_fields = self.basecamper[:login_fields]
|
84
|
+
|
85
|
+
attributes = devise_parameter_filter.filter(attributes)
|
86
|
+
|
87
|
+
login_fields.each do |login_field|
|
88
|
+
login_field = login_field.downcase.to_sym
|
89
|
+
resource = to_adapter.find_first({
|
90
|
+
login_field => attributes[:login],
|
91
|
+
scope_field => attributes[scope_field]
|
92
|
+
})
|
93
|
+
|
94
|
+
break unless resource.nil?
|
95
|
+
end
|
96
|
+
|
97
|
+
unless resource
|
98
|
+
resource = new
|
99
|
+
|
100
|
+
required_attributes.each do |key|
|
101
|
+
unless key == self.basecamper[:subdomain_field]
|
102
|
+
value = attributes[key]
|
103
|
+
resource.send("#{key}=", value)
|
104
|
+
resource.errors.add(key, value.present? ? error : :blank)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
return resource
|
110
|
+
end
|
111
|
+
|
112
|
+
## Clean the conditions and set them appropriately for finding the resource
|
113
|
+
## with proper scoping.
|
114
|
+
def clean_conditions_for_subdomain(conditions={})
|
115
|
+
if conditions[:subdomain].present?
|
116
|
+
subdomain = conditions[:subdomain]
|
117
|
+
scope_field = self.basecamper[:scope_field]
|
118
|
+
subdomain_field = self.basecamper[:subdomain_field]
|
119
|
+
subdomain_resource = find_subdomain_resource(subdomain)
|
120
|
+
conditions[scope_field] = (subdomain_resource.nil?) ? nil : subdomain_resource.id
|
121
|
+
|
122
|
+
## Remove the subdomain_field from the conditions - it is not needed
|
123
|
+
conditions.delete(subdomain_field)
|
124
|
+
end
|
125
|
+
|
126
|
+
return conditions
|
127
|
+
end
|
128
|
+
|
129
|
+
## Search for the resource identified in the basecamper config and return it
|
130
|
+
## to the caller
|
131
|
+
def find_subdomain_resource(subdomain)
|
132
|
+
resource = self.basecamper[:subdomain_class].to_s.camelize.constantize
|
133
|
+
subdomain_field = self.basecamper[:subdomain_field]
|
134
|
+
|
135
|
+
return resource.to_adapter.find_first(subdomain_field => subdomain)
|
55
136
|
end
|
56
137
|
end
|
57
138
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise-basecamper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: orm_adapter
|
@@ -35,7 +35,7 @@ dependencies:
|
|
35
35
|
requirements:
|
36
36
|
- - ~>
|
37
37
|
- !ruby/object:Gem::Version
|
38
|
-
version:
|
38
|
+
version: 3.0.0
|
39
39
|
type: :runtime
|
40
40
|
prerelease: false
|
41
41
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 3.0.0
|
47
47
|
description: Implement basecamp style subdomain authentication with support for multiple
|
48
48
|
users under a single subdomain scoped account.
|
49
49
|
email:
|