devise_invitable 0.3.4 → 0.6.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/README.rdoc +124 -43
- data/app/controllers/devise/invitations_controller.rb +36 -9
- data/app/views/devise/invitations/new.html.erb +1 -1
- data/config/locales/en.yml +5 -3
- data/lib/devise_invitable/controllers/helpers.rb +1 -6
- data/lib/devise_invitable/inviter.rb +39 -0
- data/lib/devise_invitable/mailer.rb +4 -3
- data/lib/devise_invitable/model.rb +109 -44
- data/lib/devise_invitable/model.rb~ +210 -0
- data/lib/devise_invitable/rails.rb +5 -2
- data/lib/devise_invitable/routes.rb +8 -5
- data/lib/devise_invitable/schema.rb +29 -2
- data/lib/devise_invitable/version.rb +3 -0
- data/lib/devise_invitable.rb +48 -6
- data/lib/generators/active_record/templates/migration.rb +15 -7
- data/lib/generators/devise_invitable/devise_invitable_generator.rb +4 -0
- data/lib/generators/devise_invitable/install_generator.rb +21 -2
- data/lib/generators/devise_invitable/views_generator.rb +11 -2
- data/lib/generators/mongoid/devise_invitable_generator.rb +8 -0
- metadata +62 -148
- data/.document +0 -5
- data/.gitignore +0 -22
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -108
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/devise_invitable.gemspec +0 -146
- data/test/generators_test.rb +0 -45
- data/test/integration/invitable_test.rb +0 -109
- data/test/integration_tests_helper.rb +0 -58
- data/test/mailers/invitation_test.rb +0 -62
- data/test/model_tests_helper.rb +0 -41
- data/test/models/invitable_test.rb +0 -188
- data/test/models_test.rb +0 -31
- data/test/rails_app/app/controllers/admins_controller.rb +0 -6
- data/test/rails_app/app/controllers/application_controller.rb +0 -3
- data/test/rails_app/app/controllers/home_controller.rb +0 -4
- data/test/rails_app/app/controllers/users_controller.rb +0 -12
- data/test/rails_app/app/helpers/application_helper.rb +0 -2
- data/test/rails_app/app/models/octopussy.rb +0 -11
- data/test/rails_app/app/models/user.rb +0 -4
- data/test/rails_app/app/views/home/index.html.erb +0 -0
- data/test/rails_app/app/views/layouts/application.html.erb +0 -15
- data/test/rails_app/app/views/users/invitations/new.html.erb +0 -15
- data/test/rails_app/config/application.rb +0 -46
- data/test/rails_app/config/boot.rb +0 -13
- data/test/rails_app/config/database.yml +0 -22
- data/test/rails_app/config/environment.rb +0 -5
- data/test/rails_app/config/environments/development.rb +0 -26
- data/test/rails_app/config/environments/production.rb +0 -49
- data/test/rails_app/config/environments/test.rb +0 -35
- data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
- data/test/rails_app/config/initializers/devise.rb +0 -144
- data/test/rails_app/config/initializers/inflections.rb +0 -10
- data/test/rails_app/config/initializers/mime_types.rb +0 -5
- data/test/rails_app/config/initializers/secret_token.rb +0 -7
- data/test/rails_app/config/initializers/session_store.rb +0 -8
- data/test/rails_app/config/locales/en.yml +0 -5
- data/test/rails_app/config/routes.rb +0 -4
- data/test/rails_app/config.ru +0 -4
- data/test/rails_app/script/rails +0 -6
- data/test/routes_test.rb +0 -20
- data/test/test_helper.rb +0 -30
- /data/app/views/devise/mailer/{invitation.html.erb → invitation_instructions.html.erb} +0 -0
|
@@ -1,31 +1,47 @@
|
|
|
1
1
|
module Devise
|
|
2
2
|
module Models
|
|
3
|
-
# Invitable is responsible
|
|
4
|
-
# When an invitation is sent to an email, an account is created for it.
|
|
5
|
-
#
|
|
3
|
+
# Invitable is responsible for sending invitation emails.
|
|
4
|
+
# When an invitation is sent to an email address, an account is created for it.
|
|
5
|
+
# Invitation email contains a link allowing the user to accept the invitation
|
|
6
|
+
# by setting a password (as reset password from Devise's recoverable module).
|
|
6
7
|
#
|
|
7
8
|
# Configuration:
|
|
8
9
|
#
|
|
9
|
-
# invite_for:
|
|
10
|
-
#
|
|
11
|
-
#
|
|
10
|
+
# invite_for: The period the generated invitation token is valid, after
|
|
11
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
12
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
12
13
|
#
|
|
13
14
|
# Examples:
|
|
14
15
|
#
|
|
15
|
-
# User.find(1).invited?
|
|
16
|
-
# User.invite!(:email => 'someone@example.com')
|
|
17
|
-
# User.accept_invitation!(:invitation_token => '...')
|
|
18
|
-
# User.find(1).accept_invitation!
|
|
19
|
-
# User.find(1).invite!
|
|
16
|
+
# User.find(1).invited? # => true/false
|
|
17
|
+
# User.invite!(:email => 'someone@example.com') # => send invitation
|
|
18
|
+
# User.accept_invitation!(:invitation_token => '...') # => accept invitation with a token
|
|
19
|
+
# User.find(1).accept_invitation! # => accept invitation
|
|
20
|
+
# User.find(1).invite! # => reset invitation status and send invitation again
|
|
20
21
|
module Invitable
|
|
21
22
|
extend ActiveSupport::Concern
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
attr_accessor :skip_invitation
|
|
25
|
+
|
|
26
|
+
included do
|
|
27
|
+
include ::DeviseInvitable::Inviter
|
|
28
|
+
belongs_to :invited_by, :polymorphic => true
|
|
29
|
+
|
|
30
|
+
include ActiveSupport::Callbacks
|
|
31
|
+
define_callbacks :invitation_accepted
|
|
32
|
+
|
|
33
|
+
attr_writer :skip_password
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Accept an invitation by clearing invitation token and and setting invitation_accepted_at
|
|
37
|
+
# Confirms it if model is confirmable
|
|
25
38
|
def accept_invitation!
|
|
26
|
-
if self.invited?
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
if self.invited? && self.valid?
|
|
40
|
+
run_callbacks :invitation_accepted do
|
|
41
|
+
self.invitation_token = nil
|
|
42
|
+
self.invitation_accepted_at = Time.now.utc if respond_to? :"invitation_accepted_at="
|
|
43
|
+
self.save(:validate => false)
|
|
44
|
+
end
|
|
29
45
|
end
|
|
30
46
|
end
|
|
31
47
|
|
|
@@ -34,26 +50,18 @@ module Devise
|
|
|
34
50
|
persisted? && invitation_token.present?
|
|
35
51
|
end
|
|
36
52
|
|
|
37
|
-
# Send invitation by email
|
|
38
|
-
def send_invitation
|
|
39
|
-
::Devise.mailer.invitation(self).deliver
|
|
40
|
-
end
|
|
41
|
-
|
|
42
53
|
# Reset invitation token and send invitation again
|
|
43
54
|
def invite!
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
55
|
+
was_invited = invited?
|
|
56
|
+
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
57
|
+
generate_invitation_token if self.invitation_token.nil?
|
|
58
|
+
self.invitation_sent_at = Time.now.utc
|
|
59
|
+
if save(:validate => false)
|
|
60
|
+
self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
|
|
61
|
+
deliver_invitation unless @skip_invitation
|
|
49
62
|
end
|
|
50
63
|
end
|
|
51
64
|
|
|
52
|
-
def resend_invitation!
|
|
53
|
-
ActiveSupport::Deprecation.warn('resend_invitation! has been renamed to invite!')
|
|
54
|
-
self.invite!
|
|
55
|
-
end
|
|
56
|
-
|
|
57
65
|
# Verify whether a invitation is active or not. If the user has been
|
|
58
66
|
# invited, we need to calculate if the invitation time has not expired
|
|
59
67
|
# for this user, in other words, if the invitation is still valid.
|
|
@@ -61,7 +69,26 @@ module Devise
|
|
|
61
69
|
invited? && invitation_period_valid?
|
|
62
70
|
end
|
|
63
71
|
|
|
72
|
+
# Only verify password when is not invited
|
|
73
|
+
def valid_password?(password)
|
|
74
|
+
super unless invited?
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def reset_password!(new_password, new_password_confirmation)
|
|
78
|
+
super
|
|
79
|
+
accept_invitation!
|
|
80
|
+
end
|
|
81
|
+
|
|
64
82
|
protected
|
|
83
|
+
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
84
|
+
def password_required?
|
|
85
|
+
!@skip_password && super
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Deliver the invitation email
|
|
89
|
+
def deliver_invitation
|
|
90
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
91
|
+
end
|
|
65
92
|
|
|
66
93
|
# Checks if the invitation for the user is within the limit time.
|
|
67
94
|
# We do this by calculating if the difference between today and the
|
|
@@ -89,32 +116,50 @@ module Devise
|
|
|
89
116
|
# Generates a new random token for invitation, and stores the time
|
|
90
117
|
# this token is being generated
|
|
91
118
|
def generate_invitation_token
|
|
92
|
-
self.invitation_token
|
|
93
|
-
self.invitation_sent_at = Time.now.utc
|
|
119
|
+
self.invitation_token = self.class.invitation_token
|
|
94
120
|
end
|
|
95
121
|
|
|
96
122
|
module ClassMethods
|
|
97
123
|
# Attempt to find a user by it's email. If a record is not found, create a new
|
|
98
124
|
# user and send invitation to it. If user is found, returns the user with an
|
|
99
125
|
# email already exists error.
|
|
126
|
+
# If user is found and still have pending invitation, email is resend unless
|
|
127
|
+
# resend_invitation is set to false
|
|
100
128
|
# Attributes must contain the user email, other attributes will be set in the record
|
|
101
|
-
def
|
|
102
|
-
invitable = find_or_initialize_with_error_by(
|
|
103
|
-
invitable.attributes
|
|
129
|
+
def _invite(attributes={}, invited_by=nil, &block)
|
|
130
|
+
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
|
131
|
+
invitable.assign_attributes(attributes, :as => inviter_role(invited_by))
|
|
132
|
+
invitable.invited_by = invited_by
|
|
104
133
|
|
|
134
|
+
invitable.skip_password = true
|
|
135
|
+
invitable.valid? if self.validate_on_invite
|
|
105
136
|
if invitable.new_record?
|
|
106
|
-
invitable.errors.clear if invitable.email.match Devise.email_regexp
|
|
137
|
+
invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
|
|
107
138
|
else
|
|
108
|
-
invitable.errors.add(
|
|
139
|
+
invitable.errors.add(invite_key, :taken) unless invitable.invited? && self.resend_invitation
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
if invitable.errors.empty?
|
|
143
|
+
yield invitable if block_given?
|
|
144
|
+
mail = invitable.invite!
|
|
109
145
|
end
|
|
146
|
+
[invitable, mail]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Override this method if the invitable is using Mass Assignment Security
|
|
150
|
+
# and the inviter has a non-default role.
|
|
151
|
+
def inviter_role(inviter)
|
|
152
|
+
:default
|
|
153
|
+
end
|
|
110
154
|
|
|
111
|
-
|
|
155
|
+
def invite!(attributes={}, invited_by=nil, &block)
|
|
156
|
+
invitable, mail = _invite(attributes, invited_by, &block)
|
|
112
157
|
invitable
|
|
113
158
|
end
|
|
114
159
|
|
|
115
|
-
def
|
|
116
|
-
|
|
117
|
-
|
|
160
|
+
def invite_mail!(attributes={}, invited_by=nil, &block)
|
|
161
|
+
invitable, mail = _invite(attributes, invited_by, &block)
|
|
162
|
+
mail
|
|
118
163
|
end
|
|
119
164
|
|
|
120
165
|
# Attempt to find a user by it's invitation_token to set it's password.
|
|
@@ -123,8 +168,8 @@ module Devise
|
|
|
123
168
|
# error in invitation_token attribute.
|
|
124
169
|
# Attributes must contain invitation_token, password and confirmation
|
|
125
170
|
def accept_invitation!(attributes={})
|
|
126
|
-
invitable = find_or_initialize_with_error_by(:invitation_token, attributes
|
|
127
|
-
invitable.errors.add(:invitation_token, :invalid) if
|
|
171
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
172
|
+
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
|
128
173
|
if invitable.errors.empty?
|
|
129
174
|
invitable.attributes = attributes
|
|
130
175
|
invitable.accept_invitation!
|
|
@@ -132,8 +177,28 @@ module Devise
|
|
|
132
177
|
invitable
|
|
133
178
|
end
|
|
134
179
|
|
|
180
|
+
# Generate a token checking if one does not already exist in the database.
|
|
181
|
+
def invitation_token
|
|
182
|
+
generate_token(:invitation_token)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Callback convenience methods
|
|
186
|
+
def before_invitation_accepted(*args, &blk)
|
|
187
|
+
set_callback(:invitation_accepted, :before, *args, &blk)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def after_invitation_accepted(*args, &blk)
|
|
191
|
+
set_callback(:invitation_accepted, :after, *args, &blk)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
|
|
135
195
|
Devise::Models.config(self, :invite_for)
|
|
196
|
+
Devise::Models.config(self, :validate_on_invite)
|
|
197
|
+
Devise::Models.config(self, :invitation_limit)
|
|
198
|
+
Devise::Models.config(self, :invite_key)
|
|
199
|
+
Devise::Models.config(self, :resend_invitation)
|
|
136
200
|
end
|
|
137
201
|
end
|
|
138
202
|
end
|
|
139
203
|
end
|
|
204
|
+
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Models
|
|
3
|
+
# Invitable is responsible for sending invitation emails.
|
|
4
|
+
# When an invitation is sent to an email address, an account is created for it.
|
|
5
|
+
# Invitation email contains a link allowing the user to accept the invitation
|
|
6
|
+
# by setting a password (as reset password from Devise's recoverable module).
|
|
7
|
+
#
|
|
8
|
+
# Configuration:
|
|
9
|
+
#
|
|
10
|
+
# invite_for: The period the generated invitation token is valid, after
|
|
11
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
12
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
13
|
+
#
|
|
14
|
+
# Examples:
|
|
15
|
+
#
|
|
16
|
+
# User.find(1).invited? # => true/false
|
|
17
|
+
# User.invite!(:email => 'someone@example.com') # => send invitation
|
|
18
|
+
# User.accept_invitation!(:invitation_token => '...') # => accept invitation with a token
|
|
19
|
+
# User.find(1).accept_invitation! # => accept invitation
|
|
20
|
+
# User.find(1).invite! # => reset invitation status and send invitation again
|
|
21
|
+
module Invitable
|
|
22
|
+
extend ActiveSupport::Concern
|
|
23
|
+
|
|
24
|
+
attr_accessor :skip_invitation
|
|
25
|
+
|
|
26
|
+
included do
|
|
27
|
+
include ::DeviseInvitable::Inviter
|
|
28
|
+
belongs_to :invited_by, :polymorphic => true
|
|
29
|
+
|
|
30
|
+
include ActiveSupport::Callbacks
|
|
31
|
+
define_callbacks :invitation_accepted
|
|
32
|
+
|
|
33
|
+
attr_writer :skip_password
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Accept an invitation by clearing invitation token and and setting invitation_accepted_at
|
|
37
|
+
# Confirms it if model is confirmable
|
|
38
|
+
def accept_invitation!
|
|
39
|
+
if self.invited? && self.valid?
|
|
40
|
+
run_callbacks :invitation_accepted do
|
|
41
|
+
self.invitation_token = nil
|
|
42
|
+
self.invitation_accepted_at = Time.now.utc if respond_to? :"invitation_accepted_at="
|
|
43
|
+
self.save(:validate => false)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Verifies whether a user has been invited or not
|
|
49
|
+
def invited?
|
|
50
|
+
persisted? && invitation_token.present?
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Reset invitation token and send invitation again
|
|
54
|
+
def invite!
|
|
55
|
+
was_invited = invited?
|
|
56
|
+
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
57
|
+
generate_invitation_token if self.invitation_token.nil?
|
|
58
|
+
self.invitation_sent_at = Time.now.utc
|
|
59
|
+
if save(:validate => false)
|
|
60
|
+
self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
|
|
61
|
+
deliver_invitation unless @skip_invitation
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Verify whether a invitation is active or not. If the user has been
|
|
66
|
+
# invited, we need to calculate if the invitation time has not expired
|
|
67
|
+
# for this user, in other words, if the invitation is still valid.
|
|
68
|
+
def valid_invitation?
|
|
69
|
+
invited? && invitation_period_valid?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Only verify password when is not invited
|
|
73
|
+
def valid_password?(password)
|
|
74
|
+
super unless invited?
|
|
75
|
+
end
|
|
76
|
+
=begin
|
|
77
|
+
def reset_password!(new_password, new_password_confirmation)
|
|
78
|
+
super
|
|
79
|
+
accept_invitation!
|
|
80
|
+
end
|
|
81
|
+
=end
|
|
82
|
+
protected
|
|
83
|
+
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
84
|
+
def password_required?
|
|
85
|
+
!@skip_password && super
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Deliver the invitation email
|
|
89
|
+
def deliver_invitation
|
|
90
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Clear invitation token when reset password token is cleared too
|
|
94
|
+
def clear_reset_password_token
|
|
95
|
+
self.invitation_token = nil if invited?
|
|
96
|
+
super
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Checks if the invitation for the user is within the limit time.
|
|
100
|
+
# We do this by calculating if the difference between today and the
|
|
101
|
+
# invitation sent date does not exceed the invite for time configured.
|
|
102
|
+
# Invite_for is a model configuration, must always be an integer value.
|
|
103
|
+
#
|
|
104
|
+
# Example:
|
|
105
|
+
#
|
|
106
|
+
# # invite_for = 1.day and invitation_sent_at = today
|
|
107
|
+
# invitation_period_valid? # returns true
|
|
108
|
+
#
|
|
109
|
+
# # invite_for = 5.days and invitation_sent_at = 4.days.ago
|
|
110
|
+
# invitation_period_valid? # returns true
|
|
111
|
+
#
|
|
112
|
+
# # invite_for = 5.days and invitation_sent_at = 5.days.ago
|
|
113
|
+
# invitation_period_valid? # returns false
|
|
114
|
+
#
|
|
115
|
+
# # invite_for = nil
|
|
116
|
+
# invitation_period_valid? # will always return true
|
|
117
|
+
#
|
|
118
|
+
def invitation_period_valid?
|
|
119
|
+
invitation_sent_at && (self.class.invite_for.to_i.zero? || invitation_sent_at.utc >= self.class.invite_for.ago)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Generates a new random token for invitation, and stores the time
|
|
123
|
+
# this token is being generated
|
|
124
|
+
def generate_invitation_token
|
|
125
|
+
self.invitation_token = self.class.invitation_token
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
module ClassMethods
|
|
129
|
+
# Attempt to find a user by it's email. If a record is not found, create a new
|
|
130
|
+
# user and send invitation to it. If user is found, returns the user with an
|
|
131
|
+
# email already exists error.
|
|
132
|
+
# If user is found and still have pending invitation, email is resend unless
|
|
133
|
+
# resend_invitation is set to false
|
|
134
|
+
# Attributes must contain the user email, other attributes will be set in the record
|
|
135
|
+
def _invite(attributes={}, invited_by=nil, &block)
|
|
136
|
+
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
|
137
|
+
invitable.assign_attributes(attributes, :as => inviter_role(invited_by))
|
|
138
|
+
invitable.invited_by = invited_by
|
|
139
|
+
|
|
140
|
+
invitable.skip_password = true
|
|
141
|
+
invitable.valid? if self.validate_on_invite
|
|
142
|
+
if invitable.new_record?
|
|
143
|
+
invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
|
|
144
|
+
else
|
|
145
|
+
invitable.errors.add(invite_key, :taken) unless invitable.invited? && self.resend_invitation
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
if invitable.errors.empty?
|
|
149
|
+
yield invitable if block_given?
|
|
150
|
+
mail = invitable.invite!
|
|
151
|
+
end
|
|
152
|
+
[invitable, mail]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Override this method if the invitable is using Mass Assignment Security
|
|
156
|
+
# and the inviter has a non-default role.
|
|
157
|
+
def inviter_role(inviter)
|
|
158
|
+
:default
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def invite!(attributes={}, invited_by=nil, &block)
|
|
162
|
+
invitable, mail = _invite(attributes, invited_by, &block)
|
|
163
|
+
invitable
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def invite_mail!(attributes={}, invited_by=nil, &block)
|
|
167
|
+
invitable, mail = _invite(attributes, invited_by, &block)
|
|
168
|
+
mail
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Attempt to find a user by it's invitation_token to set it's password.
|
|
172
|
+
# If a user is found, reset it's password and automatically try saving
|
|
173
|
+
# the record. If not user is found, returns a new user containing an
|
|
174
|
+
# error in invitation_token attribute.
|
|
175
|
+
# Attributes must contain invitation_token, password and confirmation
|
|
176
|
+
def accept_invitation!(attributes={})
|
|
177
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
178
|
+
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
|
179
|
+
if invitable.errors.empty?
|
|
180
|
+
invitable.attributes = attributes
|
|
181
|
+
invitable.accept_invitation!
|
|
182
|
+
end
|
|
183
|
+
invitable
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Generate a token checking if one does not already exist in the database.
|
|
187
|
+
def invitation_token
|
|
188
|
+
generate_token(:invitation_token)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Callback convenience methods
|
|
192
|
+
def before_invitation_accepted(*args, &blk)
|
|
193
|
+
set_callback(:invitation_accepted, :before, *args, &blk)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def after_invitation_accepted(*args, &blk)
|
|
197
|
+
set_callback(:invitation_accepted, :after, *args, &blk)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
Devise::Models.config(self, :invite_for)
|
|
202
|
+
Devise::Models.config(self, :validate_on_invite)
|
|
203
|
+
Devise::Models.config(self, :invitation_limit)
|
|
204
|
+
Devise::Models.config(self, :invite_key)
|
|
205
|
+
Devise::Models.config(self, :resend_invitation)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
@@ -2,9 +2,12 @@ module DeviseInvitable
|
|
|
2
2
|
class Engine < ::Rails::Engine
|
|
3
3
|
|
|
4
4
|
ActiveSupport.on_load(:action_controller) { include DeviseInvitable::Controllers::UrlHelpers }
|
|
5
|
-
ActiveSupport.on_load(:action_view)
|
|
5
|
+
ActiveSupport.on_load(:action_view) { include DeviseInvitable::Controllers::UrlHelpers }
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
# We use to_prepare instead of after_initialize here because Devise is a Rails engine; its
|
|
8
|
+
# mailer is reloaded like the rest of the user's app. Got to make sure that our mailer methods
|
|
9
|
+
# are included each time Devise::Mailer is (re)loaded.
|
|
10
|
+
config.to_prepare do
|
|
8
11
|
require 'devise/mailer'
|
|
9
12
|
Devise::Mailer.send :include, DeviseInvitable::Mailer
|
|
10
13
|
end
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
module ActionDispatch::Routing
|
|
2
2
|
class Mapper
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
|
|
4
|
+
protected
|
|
5
|
+
def devise_invitation(mapping, controllers)
|
|
6
|
+
resource :invitation, :only => [:new, :create, :update],
|
|
7
|
+
:path => mapping.path_names[:invitation], :controller => controllers[:invitations] do
|
|
8
|
+
get :edit, :path => mapping.path_names[:accept], :as => :accept
|
|
8
9
|
end
|
|
10
|
+
end
|
|
11
|
+
|
|
9
12
|
end
|
|
10
13
|
end
|
|
@@ -1,9 +1,36 @@
|
|
|
1
1
|
module DeviseInvitable
|
|
2
2
|
module Schema
|
|
3
|
-
#
|
|
3
|
+
# Add invitation_token and invitation_sent_at columns in the resource's database table.
|
|
4
|
+
#
|
|
5
|
+
# Examples
|
|
6
|
+
#
|
|
7
|
+
# # For a new resource migration:
|
|
8
|
+
# create_table :the_resources do
|
|
9
|
+
# t.database_authenticatable :null => false # you need at least this
|
|
10
|
+
# t.invitable
|
|
11
|
+
# ...
|
|
12
|
+
# end
|
|
13
|
+
# add_index :the_resources, :invitation_token # for invitable
|
|
14
|
+
#
|
|
15
|
+
# # or if the resource's table already exists, define a migration and put this in:
|
|
16
|
+
# change_table :the_resources do |t|
|
|
17
|
+
# t.string :invitation_token, :limit => 60
|
|
18
|
+
# t.datetime :invitation_sent_at
|
|
19
|
+
# t.datetime :invitation_accepted_at
|
|
20
|
+
# t.index :invitation_token # for invitable
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# # And allow encrypted_password to be null:
|
|
24
|
+
# change_column :the_resources, :encrypted_password, :string, :null => true
|
|
25
|
+
# # the following line is only if you use Devise's encryptable module!
|
|
26
|
+
# change_column :the_resources, :password_salt, :string, :null => true
|
|
4
27
|
def invitable
|
|
5
|
-
apply_devise_schema :invitation_token, String, :limit =>
|
|
28
|
+
apply_devise_schema :invitation_token, String, :limit => 60
|
|
6
29
|
apply_devise_schema :invitation_sent_at, DateTime
|
|
30
|
+
apply_devise_schema :invitation_accepted_at, DateTime
|
|
31
|
+
apply_devise_schema :invitation_limit, Integer
|
|
32
|
+
apply_devise_schema :invited_by_id, Integer
|
|
33
|
+
apply_devise_schema :invited_by_type, String
|
|
7
34
|
end
|
|
8
35
|
end
|
|
9
36
|
end
|
data/lib/devise_invitable.rb
CHANGED
|
@@ -1,16 +1,58 @@
|
|
|
1
1
|
require 'devise'
|
|
2
2
|
|
|
3
|
-
module
|
|
4
|
-
|
|
5
|
-
mattr_accessor :invite_for
|
|
6
|
-
@@invite_for = 0
|
|
3
|
+
module DeviseInvitable
|
|
4
|
+
autoload :Inviter, 'devise_invitable/inviter'
|
|
7
5
|
end
|
|
8
6
|
|
|
9
|
-
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
|
10
|
-
|
|
11
7
|
require 'devise_invitable/mailer'
|
|
12
8
|
require 'devise_invitable/routes'
|
|
13
9
|
require 'devise_invitable/schema'
|
|
14
10
|
require 'devise_invitable/controllers/url_helpers'
|
|
15
11
|
require 'devise_invitable/controllers/helpers'
|
|
16
12
|
require 'devise_invitable/rails'
|
|
13
|
+
|
|
14
|
+
module Devise
|
|
15
|
+
# Public: Validity period of the invitation token (default: 0). If
|
|
16
|
+
# invite_for is 0 or nil, the invitation will never expire.
|
|
17
|
+
# Set invite_for in the Devise configuration file (in config/initializers/devise.rb).
|
|
18
|
+
#
|
|
19
|
+
# config.invite_for = 2.weeks # => The invitation token will be valid 2 weeks
|
|
20
|
+
mattr_accessor :invite_for
|
|
21
|
+
@@invite_for = 0
|
|
22
|
+
|
|
23
|
+
# Public: Flag that force a record to be valid before being actually invited
|
|
24
|
+
# (default: false).
|
|
25
|
+
#
|
|
26
|
+
# Examples (in config/initializers/devise.rb)
|
|
27
|
+
#
|
|
28
|
+
# config.validate_on_invite = true
|
|
29
|
+
mattr_accessor :validate_on_invite
|
|
30
|
+
@@validate_on_invite = false
|
|
31
|
+
|
|
32
|
+
# Public: number of invitations the user is allowed to send
|
|
33
|
+
#
|
|
34
|
+
# Examples (in config/initializers/devise.rb)
|
|
35
|
+
#
|
|
36
|
+
# config.invitation_limit = nil
|
|
37
|
+
mattr_accessor :invitation_limit
|
|
38
|
+
@@invitation_limit = nil
|
|
39
|
+
|
|
40
|
+
# Public: The key to be used to check existing users when sending an invitation
|
|
41
|
+
#
|
|
42
|
+
# Examples (in config/initializers/devise.rb)
|
|
43
|
+
#
|
|
44
|
+
# config.invite_key = :email
|
|
45
|
+
mattr_accessor :invite_key
|
|
46
|
+
@@invite_key = :email
|
|
47
|
+
|
|
48
|
+
# Public: Resend invitation if user with invited status is invited again
|
|
49
|
+
# (default: true)
|
|
50
|
+
#
|
|
51
|
+
# Example (in config/initializers/devise.rb)
|
|
52
|
+
#
|
|
53
|
+
# config.resend_invitation = false
|
|
54
|
+
mattr_accessor :resend_invitation
|
|
55
|
+
@@resend_invitation = true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
|
|
2
2
|
def self.up
|
|
3
3
|
change_table :<%= table_name %> do |t|
|
|
4
|
-
t.string
|
|
5
|
-
t.datetime
|
|
6
|
-
t.
|
|
4
|
+
t.string :invitation_token, :limit => 60
|
|
5
|
+
t.datetime :invitation_sent_at
|
|
6
|
+
t.datetime :invitation_accepted_at
|
|
7
|
+
t.integer :invitation_limit
|
|
8
|
+
t.references :invited_by, :polymorphic => true
|
|
9
|
+
t.index :invitation_token # for invitable
|
|
10
|
+
t.index :invited_by_id
|
|
7
11
|
end
|
|
8
12
|
|
|
9
13
|
# And allow null encrypted_password and password_salt:
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
change_column_null :<%= table_name %>, :encrypted_password, true
|
|
15
|
+
<% if class_name.constantize.columns_hash['password_salt'] -%>
|
|
16
|
+
change_column_null :<%= table_name %>, :password_salt, true
|
|
17
|
+
<% end -%>
|
|
12
18
|
end
|
|
13
19
|
|
|
14
20
|
def self.down
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
change_table :<%= table_name %> do |t|
|
|
22
|
+
t.remove_references :invited_by, :polymorphic => true
|
|
23
|
+
t.remove :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token
|
|
24
|
+
end
|
|
17
25
|
end
|
|
18
26
|
end
|
|
@@ -5,6 +5,10 @@ module DeviseInvitable
|
|
|
5
5
|
|
|
6
6
|
desc "Add :invitable directive in the given model. Also generate migration for ActiveRecord"
|
|
7
7
|
|
|
8
|
+
# def devise_generate_model
|
|
9
|
+
# invoke "devise", [name]
|
|
10
|
+
# end
|
|
11
|
+
|
|
8
12
|
def inject_devise_invitable_content
|
|
9
13
|
path = File.join("app", "models", "#{file_path}.rb")
|
|
10
14
|
inject_into_file(path, "invitable, :", :after => "devise :") if File.exists?(path)
|
|
@@ -4,6 +4,10 @@ module DeviseInvitable
|
|
|
4
4
|
source_root File.expand_path("../../templates", __FILE__)
|
|
5
5
|
desc "Add DeviseInvitable config variables to the Devise initializer and copy DeviseInvitable locale files to your application."
|
|
6
6
|
|
|
7
|
+
# def devise_install
|
|
8
|
+
# invoke "devise:install"
|
|
9
|
+
# end
|
|
10
|
+
|
|
7
11
|
def add_config_options_to_initializer
|
|
8
12
|
devise_initializer_path = "config/initializers/devise.rb"
|
|
9
13
|
if File.exist?(devise_initializer_path)
|
|
@@ -15,10 +19,25 @@ module DeviseInvitable
|
|
|
15
19
|
inject_into_file(devise_initializer_path, :before => " # ==> Configuration for :confirmable\n") do
|
|
16
20
|
<<-CONTENT
|
|
17
21
|
# ==> Configuration for :invitable
|
|
18
|
-
#
|
|
19
|
-
#
|
|
22
|
+
# The period the generated invitation token is valid, after
|
|
23
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
24
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
20
25
|
# config.invite_for = 2.weeks
|
|
21
26
|
|
|
27
|
+
# Number of invitations users can send.
|
|
28
|
+
# If invitation_limit is nil, users can send unlimited invitations.
|
|
29
|
+
# If invitation_limit is 0, users can't send invitations.
|
|
30
|
+
# If invitation_limit n > 0, users can send n invitations.
|
|
31
|
+
# Default: nil
|
|
32
|
+
# config.invitation_limit = 5
|
|
33
|
+
|
|
34
|
+
# The key to be used to check existing users when sending an invitation
|
|
35
|
+
# config.invite_key = :email
|
|
36
|
+
|
|
37
|
+
# Flag that force a record to be valid before being actually invited
|
|
38
|
+
# Default: false
|
|
39
|
+
# config.validate_on_invite = true
|
|
40
|
+
|
|
22
41
|
CONTENT
|
|
23
42
|
end
|
|
24
43
|
end
|