devise_invitable 0.5.0 → 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 +54 -3
- data/app/controllers/devise/invitations_controller.rb +8 -3
- data/app/views/devise/invitations/new.html.erb +1 -1
- data/lib/devise_invitable/controllers/helpers.rb +1 -1
- data/lib/devise_invitable/inviter.rb +5 -1
- data/lib/devise_invitable/mailer.rb +1 -6
- data/lib/devise_invitable/model.rb +62 -25
- data/lib/devise_invitable/model.rb~ +210 -0
- data/lib/devise_invitable/schema.rb +2 -0
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/devise_invitable.rb +9 -0
- data/lib/generators/active_record/templates/migration.rb +2 -1
- data/lib/generators/devise_invitable/views_generator.rb +11 -2
- metadata +22 -23
data/README.rdoc
CHANGED
|
@@ -51,6 +51,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
|
|
|
51
51
|
change_table :users do |t|
|
|
52
52
|
t.string :invitation_token, :limit => 60
|
|
53
53
|
t.datetime :invitation_sent_at
|
|
54
|
+
t.datetime :invitation_accepted_at
|
|
54
55
|
t.index :invitation_token
|
|
55
56
|
end
|
|
56
57
|
|
|
@@ -61,7 +62,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
|
|
|
61
62
|
|
|
62
63
|
== Model configuration
|
|
63
64
|
|
|
64
|
-
DeviseInvitable adds
|
|
65
|
+
DeviseInvitable adds some new configuration options:
|
|
65
66
|
|
|
66
67
|
* invite_for: The period the generated invitation token is valid, after this period, the invited resource won't be able to accept the invitation. When invite_for is 0 (the default), the invitation won't expire.
|
|
67
68
|
|
|
@@ -83,6 +84,8 @@ or directly as parameters to the <tt>devise</tt> method:
|
|
|
83
84
|
|
|
84
85
|
* validate_on_invite: force a record to be valid before being actually invited.
|
|
85
86
|
|
|
87
|
+
* resend_invitation: resend invitation if user with invited status is invited again. Enabled by default.
|
|
88
|
+
|
|
86
89
|
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
87
90
|
|
|
88
91
|
== Configuring views
|
|
@@ -97,6 +100,28 @@ You can also use the generator to generate scoped views:
|
|
|
97
100
|
|
|
98
101
|
Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
|
|
99
102
|
|
|
103
|
+
== Configuring controllers
|
|
104
|
+
|
|
105
|
+
To change the controller's behavior, create a controller that inherits from <tt>Devise::InvitationsController</tt>. The available methods are: new, create, edit, and update. You should read the {original controllers source}[https://raw.github.com/scambra/devise_invitable/master/app/controllers/devise/invitations_controller.rb] before editing any of these actions. Your controller might now look something like this:
|
|
106
|
+
|
|
107
|
+
class Users::InvitationsController < Devise::InvitationsController
|
|
108
|
+
def update
|
|
109
|
+
if this
|
|
110
|
+
redirect_to root_path
|
|
111
|
+
else
|
|
112
|
+
super
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
Now just tell Devise that you want to use your controller, the controller above is 'users/invitations', so our routes.rb would have this line:
|
|
118
|
+
|
|
119
|
+
devise_for :users, :controllers => { :invitations => 'users/invitations' }
|
|
120
|
+
|
|
121
|
+
be sure that you generate the views and put them into the controller that you generated, so for this example it would be:
|
|
122
|
+
|
|
123
|
+
rails generate devise_invitable:views users/invitations
|
|
124
|
+
|
|
100
125
|
== Usage
|
|
101
126
|
|
|
102
127
|
=== Send an invitation
|
|
@@ -124,12 +149,26 @@ To accept an invitation with a token use the <tt>accept_invitation!</tt> class m
|
|
|
124
149
|
|
|
125
150
|
User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
126
151
|
|
|
152
|
+
=== Callbacks
|
|
153
|
+
|
|
154
|
+
A callback event is fired before and after an invitation is accepted (User#accept_invitation!). For example, in your resource model you can add:
|
|
155
|
+
|
|
156
|
+
after_invitation_accepted :email_invited_by
|
|
157
|
+
|
|
158
|
+
def email_invited_by
|
|
159
|
+
# ...
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
The callbacks support all options and arguments available to the standard callbacks provided by AR.
|
|
163
|
+
|
|
127
164
|
== Integration in a Rails application
|
|
128
165
|
|
|
129
166
|
Since the invitations controller take care of all the creation/acceptation of an invitation, in most cases you wouldn't call the <tt>invite!</tt> and <tt>accept_invitation!</tt> methods directly.
|
|
130
167
|
Instead, in your views, put a link to <tt>new_user_invitation_path</tt> or <tt>new_invitation_path(:user)</tt> or even <tt>/users/invitation/new</tt> to prepare and send an invitation (to a user in this example).
|
|
131
168
|
|
|
132
|
-
After an invitation is created and sent, the inviter will be redirected to
|
|
169
|
+
After an invitation is created and sent, the inviter will be redirected to after_invite_path_for(resource_name), which is stored path or the same path as after_sign_in_path_for by default.
|
|
170
|
+
|
|
171
|
+
After an invitation is accepted, the invitee will be redirected to after_accept_path_for(resource), which is the same path as after_sign_in_path_for by default. If you want to override the path, override invitations controller and define after_accept_path_for method. This is useful in the common case that a user is invited to a specific location in your application. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
|
|
133
172
|
|
|
134
173
|
The invitation email includes a link to accept the invitation that looks like this: <tt>/users/invitation/accept?invitation_token=abcd123</tt>. When clicked, the invited must set a password in order to accept its invitation. Note that if the invitation_token is not present or not valid, the invited is redirected to after_sign_out_path_for(resource_name).
|
|
135
174
|
|
|
@@ -146,7 +185,7 @@ You would have a User model which is configured as invitable and an Admin model
|
|
|
146
185
|
class ApplicationController < ActionController::Base
|
|
147
186
|
protected
|
|
148
187
|
def authenticate_inviter!
|
|
149
|
-
authenticate_admin!
|
|
188
|
+
authenticate_admin!(:force => true)
|
|
150
189
|
end
|
|
151
190
|
end
|
|
152
191
|
|
|
@@ -157,6 +196,18 @@ And include DeviseInvitable::Inviter module into Admin model:
|
|
|
157
196
|
include DeviseInvitable::Inviter
|
|
158
197
|
end
|
|
159
198
|
|
|
199
|
+
== Has many invitations
|
|
200
|
+
|
|
201
|
+
If you want to get all records invited by a resource, you should define has_many association in the model allowed to send invitations.
|
|
202
|
+
|
|
203
|
+
For the default behavior, define it like this:
|
|
204
|
+
|
|
205
|
+
has_many :invitations, :class_name => self.class.to_s, :as => :invited_by
|
|
206
|
+
|
|
207
|
+
For the previous example, where admins send invitations to users, define it like this:
|
|
208
|
+
|
|
209
|
+
has_many :invitations, :class_name => 'User', :as => :invited_by
|
|
210
|
+
|
|
160
211
|
== I18n
|
|
161
212
|
|
|
162
213
|
DeviseInvitable uses flash messages with I18n with the flash keys <tt>:send_instructions</tt>, <tt>:invitation_token_invalid</tt> and <tt>:updated</tt>. To customize your app, you can modify the generated locale file:
|
|
@@ -18,7 +18,7 @@ class Devise::InvitationsController < ApplicationController
|
|
|
18
18
|
|
|
19
19
|
if resource.errors.empty?
|
|
20
20
|
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
21
|
-
respond_with resource, :location =>
|
|
21
|
+
respond_with resource, :location => after_invite_path_for(resource)
|
|
22
22
|
else
|
|
23
23
|
respond_with_navigational(resource) { render_with_scope :new }
|
|
24
24
|
end
|
|
@@ -26,7 +26,7 @@ class Devise::InvitationsController < ApplicationController
|
|
|
26
26
|
|
|
27
27
|
# GET /resource/invitation/accept?invitation_token=abcdef
|
|
28
28
|
def edit
|
|
29
|
-
if params[:invitation_token] && self.resource = resource_class.
|
|
29
|
+
if params[:invitation_token] && self.resource = resource_class.to_adapter.find_first( :invitation_token => params[:invitation_token] )
|
|
30
30
|
render_with_scope :edit
|
|
31
31
|
else
|
|
32
32
|
set_flash_message(:alert, :invitation_token_invalid)
|
|
@@ -55,12 +55,17 @@ class Devise::InvitationsController < ApplicationController
|
|
|
55
55
|
def has_invitations_left?
|
|
56
56
|
unless current_inviter.nil? || current_inviter.has_invitations_left?
|
|
57
57
|
build_resource
|
|
58
|
-
set_flash_message :alert, :no_invitations_remaining
|
|
58
|
+
set_flash_message :alert, :no_invitations_remaining
|
|
59
59
|
respond_with_navigational(resource) { render_with_scope :new }
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
def after_invite_path_for(resource)
|
|
64
|
+
after_sign_in_path_for(resource)
|
|
65
|
+
end
|
|
66
|
+
|
|
63
67
|
def after_accept_path_for(resource)
|
|
64
68
|
after_sign_in_path_for(resource)
|
|
65
69
|
end
|
|
66
70
|
end
|
|
71
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<h2>Send invitation</h2>
|
|
2
2
|
|
|
3
|
-
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name) do |f| %>
|
|
3
|
+
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
|
|
4
4
|
<%= devise_error_messages! %>
|
|
5
5
|
|
|
6
6
|
<p><%= f.label :email %><br />
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module DeviseInvitable::Controllers::Helpers
|
|
2
2
|
protected
|
|
3
3
|
def authenticate_inviter!
|
|
4
|
-
send(:"authenticate_#{resource_name}!", true)
|
|
4
|
+
send(:"authenticate_#{resource_name}!", :force => true)
|
|
5
5
|
end
|
|
6
6
|
end
|
|
7
7
|
ActionController::Base.send :include, DeviseInvitable::Controllers::Helpers
|
|
@@ -4,7 +4,11 @@ module DeviseInvitable
|
|
|
4
4
|
|
|
5
5
|
included do
|
|
6
6
|
extend ClassMethods
|
|
7
|
-
|
|
7
|
+
attr_writer :invitation_limit unless respond_to? :invitation_limit
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def invitation_limit
|
|
11
|
+
self[:invitation_limit] || self.class.invitation_limit
|
|
8
12
|
end
|
|
9
13
|
|
|
10
14
|
# Return true if this user has invitations left to send
|
|
@@ -3,12 +3,7 @@ module DeviseInvitable
|
|
|
3
3
|
|
|
4
4
|
# Deliver an invitation email
|
|
5
5
|
def invitation_instructions(record)
|
|
6
|
-
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def invitation(record)
|
|
10
|
-
ActiveSupport::Deprecation.warn('invitation has been renamed to invitation_instructions')
|
|
11
|
-
invitation_instructions(record)
|
|
6
|
+
devise_mail(record, :invitation_instructions)
|
|
12
7
|
end
|
|
13
8
|
end
|
|
14
9
|
end
|
|
@@ -24,16 +24,24 @@ module Devise
|
|
|
24
24
|
attr_accessor :skip_invitation
|
|
25
25
|
|
|
26
26
|
included do
|
|
27
|
-
include ::DeviseInvitable::Inviter
|
|
27
|
+
include ::DeviseInvitable::Inviter
|
|
28
28
|
belongs_to :invited_by, :polymorphic => true
|
|
29
|
+
|
|
30
|
+
include ActiveSupport::Callbacks
|
|
31
|
+
define_callbacks :invitation_accepted
|
|
32
|
+
|
|
33
|
+
attr_writer :skip_password
|
|
29
34
|
end
|
|
30
35
|
|
|
31
|
-
# Accept an invitation by clearing invitation token and
|
|
32
|
-
# is confirmable
|
|
36
|
+
# Accept an invitation by clearing invitation token and and setting invitation_accepted_at
|
|
37
|
+
# Confirms it if model is confirmable
|
|
33
38
|
def accept_invitation!
|
|
34
39
|
if self.invited? && self.valid?
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
37
45
|
end
|
|
38
46
|
end
|
|
39
47
|
|
|
@@ -44,15 +52,13 @@ module Devise
|
|
|
44
52
|
|
|
45
53
|
# Reset invitation token and send invitation again
|
|
46
54
|
def invite!
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if
|
|
53
|
-
|
|
54
|
-
!!deliver_invitation unless @skip_invitation
|
|
55
|
-
end
|
|
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
|
|
56
62
|
end
|
|
57
63
|
end
|
|
58
64
|
|
|
@@ -67,6 +73,11 @@ module Devise
|
|
|
67
73
|
def valid_password?(password)
|
|
68
74
|
super unless invited?
|
|
69
75
|
end
|
|
76
|
+
|
|
77
|
+
def reset_password!(new_password, new_password_confirmation)
|
|
78
|
+
super
|
|
79
|
+
accept_invitation!
|
|
80
|
+
end
|
|
70
81
|
|
|
71
82
|
protected
|
|
72
83
|
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
@@ -79,12 +90,6 @@ module Devise
|
|
|
79
90
|
::Devise.mailer.invitation_instructions(self).deliver
|
|
80
91
|
end
|
|
81
92
|
|
|
82
|
-
# Clear invitation token when reset password token is cleared too
|
|
83
|
-
def clear_reset_password_token
|
|
84
|
-
self.invitation_token = nil if invited?
|
|
85
|
-
super
|
|
86
|
-
end
|
|
87
|
-
|
|
88
93
|
# Checks if the invitation for the user is within the limit time.
|
|
89
94
|
# We do this by calculating if the difference between today and the
|
|
90
95
|
# invitation sent date does not exceed the invite for time configured.
|
|
@@ -118,25 +123,45 @@ module Devise
|
|
|
118
123
|
# Attempt to find a user by it's email. If a record is not found, create a new
|
|
119
124
|
# user and send invitation to it. If user is found, returns the user with an
|
|
120
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
|
|
121
128
|
# Attributes must contain the user email, other attributes will be set in the record
|
|
122
|
-
def
|
|
129
|
+
def _invite(attributes={}, invited_by=nil, &block)
|
|
123
130
|
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
|
124
|
-
invitable.attributes
|
|
131
|
+
invitable.assign_attributes(attributes, :as => inviter_role(invited_by))
|
|
125
132
|
invitable.invited_by = invited_by
|
|
126
133
|
|
|
134
|
+
invitable.skip_password = true
|
|
135
|
+
invitable.valid? if self.validate_on_invite
|
|
127
136
|
if invitable.new_record?
|
|
128
|
-
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
137
|
+
invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
|
|
129
138
|
else
|
|
130
|
-
invitable.errors.add(invite_key, :taken) unless invitable.invited?
|
|
139
|
+
invitable.errors.add(invite_key, :taken) unless invitable.invited? && self.resend_invitation
|
|
131
140
|
end
|
|
132
141
|
|
|
133
142
|
if invitable.errors.empty?
|
|
134
143
|
yield invitable if block_given?
|
|
135
|
-
invitable.invite!
|
|
144
|
+
mail = invitable.invite!
|
|
136
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
|
|
154
|
+
|
|
155
|
+
def invite!(attributes={}, invited_by=nil, &block)
|
|
156
|
+
invitable, mail = _invite(attributes, invited_by, &block)
|
|
137
157
|
invitable
|
|
138
158
|
end
|
|
139
159
|
|
|
160
|
+
def invite_mail!(attributes={}, invited_by=nil, &block)
|
|
161
|
+
invitable, mail = _invite(attributes, invited_by, &block)
|
|
162
|
+
mail
|
|
163
|
+
end
|
|
164
|
+
|
|
140
165
|
# Attempt to find a user by it's invitation_token to set it's password.
|
|
141
166
|
# If a user is found, reset it's password and automatically try saving
|
|
142
167
|
# the record. If not user is found, returns a new user containing an
|
|
@@ -156,12 +181,24 @@ module Devise
|
|
|
156
181
|
def invitation_token
|
|
157
182
|
generate_token(:invitation_token)
|
|
158
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
|
+
|
|
159
194
|
|
|
160
195
|
Devise::Models.config(self, :invite_for)
|
|
161
196
|
Devise::Models.config(self, :validate_on_invite)
|
|
162
197
|
Devise::Models.config(self, :invitation_limit)
|
|
163
198
|
Devise::Models.config(self, :invite_key)
|
|
199
|
+
Devise::Models.config(self, :resend_invitation)
|
|
164
200
|
end
|
|
165
201
|
end
|
|
166
202
|
end
|
|
167
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
|
+
|
|
@@ -16,6 +16,7 @@ module DeviseInvitable
|
|
|
16
16
|
# change_table :the_resources do |t|
|
|
17
17
|
# t.string :invitation_token, :limit => 60
|
|
18
18
|
# t.datetime :invitation_sent_at
|
|
19
|
+
# t.datetime :invitation_accepted_at
|
|
19
20
|
# t.index :invitation_token # for invitable
|
|
20
21
|
# end
|
|
21
22
|
#
|
|
@@ -26,6 +27,7 @@ module DeviseInvitable
|
|
|
26
27
|
def invitable
|
|
27
28
|
apply_devise_schema :invitation_token, String, :limit => 60
|
|
28
29
|
apply_devise_schema :invitation_sent_at, DateTime
|
|
30
|
+
apply_devise_schema :invitation_accepted_at, DateTime
|
|
29
31
|
apply_devise_schema :invitation_limit, Integer
|
|
30
32
|
apply_devise_schema :invited_by_id, Integer
|
|
31
33
|
apply_devise_schema :invited_by_type, String
|
data/lib/devise_invitable.rb
CHANGED
|
@@ -44,6 +44,15 @@ module Devise
|
|
|
44
44
|
# config.invite_key = :email
|
|
45
45
|
mattr_accessor :invite_key
|
|
46
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
|
|
47
56
|
end
|
|
48
57
|
|
|
49
58
|
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
|
@@ -3,6 +3,7 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
|
|
|
3
3
|
change_table :<%= table_name %> do |t|
|
|
4
4
|
t.string :invitation_token, :limit => 60
|
|
5
5
|
t.datetime :invitation_sent_at
|
|
6
|
+
t.datetime :invitation_accepted_at
|
|
6
7
|
t.integer :invitation_limit
|
|
7
8
|
t.references :invited_by, :polymorphic => true
|
|
8
9
|
t.index :invitation_token # for invitable
|
|
@@ -19,7 +20,7 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
|
|
|
19
20
|
def self.down
|
|
20
21
|
change_table :<%= table_name %> do |t|
|
|
21
22
|
t.remove_references :invited_by, :polymorphic => true
|
|
22
|
-
t.remove :invitation_limit, :invitation_sent_at, :invitation_token
|
|
23
|
+
t.remove :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
end
|
|
@@ -2,9 +2,18 @@ require 'generators/devise/views_generator'
|
|
|
2
2
|
|
|
3
3
|
module DeviseInvitable
|
|
4
4
|
module Generators
|
|
5
|
-
class ViewsGenerator <
|
|
6
|
-
source_root File.expand_path("../../../../app/views", __FILE__)
|
|
5
|
+
class ViewsGenerator < Rails::Generators::Base
|
|
7
6
|
desc 'Copies all DeviseInvitable views to your application.'
|
|
7
|
+
|
|
8
|
+
argument :scope, :required => false, :default => nil,
|
|
9
|
+
:desc => "The scope to copy views to"
|
|
10
|
+
|
|
11
|
+
include ::Devise::Generators::ViewPathTemplates
|
|
12
|
+
source_root File.expand_path("../../../../app/views/devise", __FILE__)
|
|
13
|
+
def copy_views
|
|
14
|
+
view_directory :invitations
|
|
15
|
+
view_directory :mailer
|
|
16
|
+
end
|
|
8
17
|
end
|
|
9
18
|
end
|
|
10
19
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: devise_invitable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 5
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
version: 0.
|
|
8
|
+
- 6
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.6.1
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Sergio Cambra
|
|
@@ -15,8 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-
|
|
19
|
-
default_executable:
|
|
18
|
+
date: 2011-12-27 00:00:00 Z
|
|
20
19
|
dependencies:
|
|
21
20
|
- !ruby/object:Gem::Dependency
|
|
22
21
|
name: bundler
|
|
@@ -40,21 +39,14 @@ dependencies:
|
|
|
40
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
40
|
none: false
|
|
42
41
|
requirements:
|
|
43
|
-
- -
|
|
44
|
-
- !ruby/object:Gem::Version
|
|
45
|
-
hash: 7
|
|
46
|
-
segments:
|
|
47
|
-
- 3
|
|
48
|
-
- 0
|
|
49
|
-
- 0
|
|
50
|
-
version: 3.0.0
|
|
51
|
-
- - <=
|
|
42
|
+
- - ~>
|
|
52
43
|
- !ruby/object:Gem::Version
|
|
53
44
|
hash: 3
|
|
54
45
|
segments:
|
|
55
46
|
- 3
|
|
56
|
-
-
|
|
57
|
-
|
|
47
|
+
- 1
|
|
48
|
+
- 0
|
|
49
|
+
version: 3.1.0
|
|
58
50
|
type: :runtime
|
|
59
51
|
version_requirements: *id002
|
|
60
52
|
- !ruby/object:Gem::Dependency
|
|
@@ -63,14 +55,21 @@ dependencies:
|
|
|
63
55
|
requirement: &id003 !ruby/object:Gem::Requirement
|
|
64
56
|
none: false
|
|
65
57
|
requirements:
|
|
66
|
-
- -
|
|
58
|
+
- - ">="
|
|
67
59
|
- !ruby/object:Gem::Version
|
|
68
|
-
hash:
|
|
60
|
+
hash: 11
|
|
69
61
|
segments:
|
|
70
62
|
- 1
|
|
71
|
-
-
|
|
63
|
+
- 4
|
|
64
|
+
- 6
|
|
65
|
+
version: 1.4.6
|
|
66
|
+
- - <
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
hash: 3
|
|
69
|
+
segments:
|
|
72
70
|
- 1
|
|
73
|
-
|
|
71
|
+
- 6
|
|
72
|
+
version: "1.6"
|
|
74
73
|
type: :runtime
|
|
75
74
|
version_requirements: *id003
|
|
76
75
|
description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
|
|
@@ -98,6 +97,7 @@ files:
|
|
|
98
97
|
- lib/devise_invitable/controllers/url_helpers.rb
|
|
99
98
|
- lib/devise_invitable/version.rb
|
|
100
99
|
- lib/devise_invitable/inviter.rb
|
|
100
|
+
- lib/devise_invitable/model.rb~
|
|
101
101
|
- lib/generators/active_record/devise_invitable_generator.rb
|
|
102
102
|
- lib/generators/active_record/templates/migration.rb
|
|
103
103
|
- lib/generators/devise_invitable/views_generator.rb
|
|
@@ -106,7 +106,6 @@ files:
|
|
|
106
106
|
- lib/generators/mongoid/devise_invitable_generator.rb
|
|
107
107
|
- LICENSE
|
|
108
108
|
- README.rdoc
|
|
109
|
-
has_rdoc: true
|
|
110
109
|
homepage: https://github.com/scambra/devise_invitable
|
|
111
110
|
licenses: []
|
|
112
111
|
|
|
@@ -142,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
142
141
|
requirements: []
|
|
143
142
|
|
|
144
143
|
rubyforge_project:
|
|
145
|
-
rubygems_version: 1.
|
|
144
|
+
rubygems_version: 1.8.10
|
|
146
145
|
signing_key:
|
|
147
146
|
specification_version: 3
|
|
148
147
|
summary: An invitation strategy for Devise
|