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.
@@ -16,5 +16,5 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Devise::Basecamper::VERSION
17
17
 
18
18
  gem.add_dependency('orm_adapter', '~> 0.1')
19
- gem.add_dependency('devise', '~> 2.2.0')
19
+ gem.add_dependency('devise', '~> 3.0.0')
20
20
  end
@@ -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
- if conditions[:subdomain].present?
23
- resource = self.basecamper[:subdomain_class].to_s.camelize.constantize
24
- subdomain_source = resource.to_adapter.find_first(self.basecamper[:subdomain_field] => conditions[:subdomain])
25
- conditions[self.basecamper[:scope_field]] = (subdomain_source.nil?) ? nil : subdomain_source.id
26
- conditions.delete(self.basecamper[:subdomain_field])
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
- resource = self.basecamper[:subdomain_class].to_s.camelize.constantize
49
- subdomain_source = resource.to_adapter.find_first(self.basecamper[:subdomain_field] => attributes[:subdomain])
50
- action_object = find_or_initialize_with_errors([self.basecamper[:scope_field], :email], {
51
- :email => attributes[:email], self.basecamper[:scope_field] => (subdomain_source.nil? ? nil : subdomain_source.id.to_s)
52
- })
53
- action_object.send("send_#{action_method.to_s}_instructions") if action_object.persisted?
54
- action_object
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
@@ -1,5 +1,5 @@
1
1
  module Devise
2
2
  module Basecamper
3
- VERSION = "0.2.2"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  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.2.2
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-04-01 00:00:00.000000000 Z
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: 2.2.0
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: 2.2.0
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: