devise_invitable 0.3.4 → 0.5.0
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 +72 -42
- data/app/controllers/devise/invitations_controller.rb +31 -9
- data/config/locales/en.yml +5 -3
- data/lib/devise_invitable/controllers/helpers.rb +1 -6
- data/lib/devise_invitable/inviter.rb +35 -0
- data/lib/devise_invitable/mailer.rb +8 -2
- data/lib/devise_invitable/model.rb +68 -40
- data/lib/devise_invitable/rails.rb +5 -2
- data/lib/devise_invitable/routes.rb +8 -5
- data/lib/devise_invitable/schema.rb +27 -2
- data/lib/devise_invitable/version.rb +3 -0
- data/lib/devise_invitable.rb +39 -6
- data/lib/generators/active_record/templates/migration.rb +14 -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/mongoid/devise_invitable_generator.rb +8 -0
- metadata +55 -140
- 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
data/README.rdoc
CHANGED
|
@@ -2,26 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
It adds support to devise[http://github.com/plataformatec/devise] for send invitations by email (it requires to be authenticated) and accept the invitation setting the password.
|
|
4
4
|
|
|
5
|
-
DeviseInvitable currently only support
|
|
5
|
+
DeviseInvitable currently only support Rails 3, if you want to use it with Rails 2.3 you must install version {0.2.3}[http://rubygems.org/gems/devise_invitable/versions/0.2.3]
|
|
6
6
|
|
|
7
|
-
== Installation for Rails ~> 3.0
|
|
7
|
+
== Installation for Rails ~> 3.0 and Devise ~> 1.2
|
|
8
8
|
|
|
9
|
-
Install
|
|
9
|
+
Install DeviseInvitable gem, it will also install dependencies (such as devise and warden):
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
gem install devise_invitable
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Add DeviseInvitable to your Gemfile (and Devise if you weren't using them):
|
|
14
14
|
|
|
15
|
-
gem 'devise'
|
|
16
|
-
gem 'devise_invitable'
|
|
15
|
+
gem 'devise', '~> 1.2.0'
|
|
16
|
+
gem 'devise_invitable', '~> 0.4.0'
|
|
17
17
|
|
|
18
18
|
=== Automatic installation
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
Run the following generator to add DeviseInvitable’s configuration option in the Devise configuration file (config/initializers/devise.rb):
|
|
21
21
|
|
|
22
22
|
rails generate devise_invitable:install
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
When you are done, you are ready to add DeviseInvitable to any of your Devise models using the following generator:
|
|
25
25
|
|
|
26
26
|
rails generate devise_invitable MODEL
|
|
27
27
|
|
|
@@ -31,7 +31,7 @@ Replace MODEL by the class name you want to add DeviseInvitable, like User, Admi
|
|
|
31
31
|
|
|
32
32
|
Follow the walkthrough for Devise and after it's done, follow this walkthrough.
|
|
33
33
|
|
|
34
|
-
Add :invitable to the
|
|
34
|
+
Add :invitable to the <tt>devise</tt> call in your model (we’re assuming here you already have a User model with some Devise modules):
|
|
35
35
|
|
|
36
36
|
class User < ActiveRecord::Base
|
|
37
37
|
devise :database_authenticatable, :confirmable, :invitable
|
|
@@ -46,39 +46,44 @@ Add t.invitable to your Devise model migration:
|
|
|
46
46
|
end
|
|
47
47
|
add_index :users, :invitation_token
|
|
48
48
|
|
|
49
|
-
or for a model that
|
|
49
|
+
or for a model that already exists, define a migration to add DeviseInvitable to your model:
|
|
50
50
|
|
|
51
51
|
change_table :users do |t|
|
|
52
|
-
t.string :invitation_token, :limit =>
|
|
52
|
+
t.string :invitation_token, :limit => 60
|
|
53
53
|
t.datetime :invitation_sent_at
|
|
54
54
|
t.index :invitation_token
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
# Allow null encrypted_password
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
DeviseInvitable doesn't use _attr_accessible_ or _attr_protected_, so be sure to define attributes as accessible or protected in your model.
|
|
57
|
+
# Allow null encrypted_password
|
|
58
|
+
change_column_null :users, :encrypted_password, true
|
|
59
|
+
# Allow null password_salt (add it if you are using Devise's encryptable module)
|
|
60
|
+
change_column_null :users, :password_salt, true
|
|
62
61
|
|
|
63
62
|
== Model configuration
|
|
64
63
|
|
|
65
|
-
DeviseInvitable adds
|
|
64
|
+
DeviseInvitable adds four new configuration options:
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
* 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.
|
|
68
67
|
|
|
69
|
-
You can set
|
|
68
|
+
You can set this configuration option in the Devise initializer as follow:
|
|
70
69
|
|
|
71
70
|
# ==> Configuration for :invitable
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
71
|
+
# The period the generated invitation token is valid, after
|
|
72
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
73
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
75
74
|
# config.invite_for = 2.weeks
|
|
76
75
|
|
|
77
|
-
or directly as parameters to the <tt>devise</tt> method
|
|
76
|
+
or directly as parameters to the <tt>devise</tt> method:
|
|
78
77
|
|
|
79
78
|
devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
|
|
80
79
|
|
|
81
|
-
|
|
80
|
+
* invitation_limit: The number of invitations users can send. The default value of nil means users can send as many invites as they want. A setting of 0 means they can't send invitations. A setting n > 0 means they can send n invitations.
|
|
81
|
+
|
|
82
|
+
* invite_key: The key to be used to check existing users when sending an invitation. The key must be an unique field. The default value is looking for users by email.
|
|
83
|
+
|
|
84
|
+
* validate_on_invite: force a record to be valid before being actually invited.
|
|
85
|
+
|
|
86
|
+
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
82
87
|
|
|
83
88
|
== Configuring views
|
|
84
89
|
|
|
@@ -96,31 +101,47 @@ Please refer to {Devise's README}[http://github.com/plataformatec/devise] for mo
|
|
|
96
101
|
|
|
97
102
|
=== Send an invitation
|
|
98
103
|
|
|
99
|
-
To send an invitation to a user, use the <tt>invite!</tt> class method.
|
|
100
|
-
You can also include other attributes in the hash. The record will not be validated.
|
|
104
|
+
To send an invitation to a user, use the <tt>invite!</tt> class method. <tt>:email</tt> must be present in the parameters hash. You can also include other attributes in the hash. The record will not be validated.
|
|
101
105
|
|
|
102
|
-
User.invite(:email => "new_user@example.com", :name => "John Doe")
|
|
106
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe")
|
|
103
107
|
# => an invitation email will be sent to new_user@example.com
|
|
104
108
|
|
|
109
|
+
If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to true.
|
|
110
|
+
|
|
111
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe") do |u|
|
|
112
|
+
u.skip_invitation = true
|
|
113
|
+
end
|
|
114
|
+
# => the record will be created, but the invitation email will not be sent
|
|
115
|
+
|
|
116
|
+
You can add :skip_invitation to attributes hash if skip_invitation is added to attr_accessible.
|
|
117
|
+
|
|
118
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
|
|
119
|
+
# => the record will be created, but the invitation email will not be sent
|
|
120
|
+
|
|
105
121
|
=== Accept an invitation
|
|
106
122
|
|
|
107
|
-
To accept an invitation with a token use the <tt>accept_invitation
|
|
123
|
+
To accept an invitation with a token use the <tt>accept_invitation!</tt> class method. <tt>:invitation_token</tt> must be present in the parameters hash. You can also include other attributes in the hash.
|
|
108
124
|
|
|
109
|
-
User.accept_invitation(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
125
|
+
User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
110
126
|
|
|
111
127
|
== Integration in a Rails application
|
|
112
128
|
|
|
113
|
-
Since the invitations controller take care of all the
|
|
114
|
-
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.
|
|
115
|
-
|
|
129
|
+
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
|
+
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
|
+
|
|
132
|
+
After an invitation is created and sent, the inviter 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. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
|
|
133
|
+
|
|
134
|
+
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
|
+
|
|
136
|
+
The controller sets the invited_by_id attribute for the new user to the current user. This will let you easily keep track of who invited who.
|
|
116
137
|
|
|
117
138
|
== Controller filter
|
|
118
139
|
|
|
119
140
|
InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
|
|
120
141
|
|
|
121
|
-
Default behavior requires authentication of the same resource. For example, if your model User is
|
|
142
|
+
Default behavior requires authentication of the same resource as the invited one. For example, if your model User is invitable, it will allow all authenticated users to send invitations to other users.
|
|
122
143
|
|
|
123
|
-
You would have a User model which is configured as invitable and an Admin model which is not. If you
|
|
144
|
+
You would have a User model which is configured as invitable and an Admin model which is not. If you want to allow only admins to send invitations, simply overwrite the authenticate_inviter! method as follow:
|
|
124
145
|
|
|
125
146
|
class ApplicationController < ActionController::Base
|
|
126
147
|
protected
|
|
@@ -129,14 +150,22 @@ You would have a User model which is configured as invitable and an Admin model
|
|
|
129
150
|
end
|
|
130
151
|
end
|
|
131
152
|
|
|
153
|
+
And include DeviseInvitable::Inviter module into Admin model:
|
|
154
|
+
|
|
155
|
+
class Admin < ActiveRecord::Base
|
|
156
|
+
devise :database_authenticatable, :validatable
|
|
157
|
+
include DeviseInvitable::Inviter
|
|
158
|
+
end
|
|
159
|
+
|
|
132
160
|
== I18n
|
|
133
161
|
|
|
134
|
-
DeviseInvitable uses flash messages with I18n with the flash keys <tt>:send_instructions</tt> and <tt>:updated</tt>. To customize your app, you can modify the generated locale file:
|
|
162
|
+
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:
|
|
135
163
|
|
|
136
164
|
en:
|
|
137
165
|
devise:
|
|
138
166
|
invitations:
|
|
139
|
-
send_instructions: 'An email
|
|
167
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
168
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
140
169
|
updated: 'Your password was set successfully. You are now signed in.'
|
|
141
170
|
|
|
142
171
|
You can also create distinct messages based on the resource you've configured using the singular name given in routes:
|
|
@@ -145,17 +174,18 @@ You can also create distinct messages based on the resource you've configured us
|
|
|
145
174
|
devise:
|
|
146
175
|
invitations:
|
|
147
176
|
user:
|
|
148
|
-
send_instructions: 'A new user invitation has been sent.'
|
|
177
|
+
send_instructions: 'A new user invitation has been sent to %{email}.'
|
|
178
|
+
invitation_token_invalid: 'Your invitation token is not valid!'
|
|
149
179
|
updated: 'Welcome on board! You are now signed in.'
|
|
150
180
|
|
|
151
|
-
The DeviseInvitable mailer uses the
|
|
181
|
+
The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
|
|
152
182
|
|
|
153
183
|
en:
|
|
154
184
|
devise:
|
|
155
185
|
mailer:
|
|
156
|
-
|
|
186
|
+
invitation_instructions:
|
|
157
187
|
subject: 'You got an invitation!'
|
|
158
|
-
user_subject: 'You got
|
|
188
|
+
user_subject: 'You got a user invitation!'
|
|
159
189
|
|
|
160
190
|
Take a look at the generated locale file (in <tt>config/locales/devise_invitable.en.yml</tt>) to check all available messages.
|
|
161
191
|
|
|
@@ -169,7 +199,7 @@ Check them all at:
|
|
|
169
199
|
|
|
170
200
|
http://github.com/scambra/devise_invitable/contributors
|
|
171
201
|
|
|
172
|
-
Special thanks to rymai[http://github.com/rymai] for
|
|
202
|
+
Special thanks to rymai[http://github.com/rymai] for the Rails 3 support, his fork was a great help.
|
|
173
203
|
|
|
174
204
|
== Note on Patches/Pull Requests
|
|
175
205
|
|
|
@@ -2,6 +2,7 @@ class Devise::InvitationsController < ApplicationController
|
|
|
2
2
|
include Devise::Controllers::InternalHelpers
|
|
3
3
|
|
|
4
4
|
before_filter :authenticate_inviter!, :only => [:new, :create]
|
|
5
|
+
before_filter :has_invitations_left?, :only => [:create]
|
|
5
6
|
before_filter :require_no_authentication, :only => [:edit, :update]
|
|
6
7
|
helper_method :after_sign_in_path_for
|
|
7
8
|
|
|
@@ -13,21 +14,24 @@ class Devise::InvitationsController < ApplicationController
|
|
|
13
14
|
|
|
14
15
|
# POST /resource/invitation
|
|
15
16
|
def create
|
|
16
|
-
self.resource = resource_class.invite!(params[resource_name])
|
|
17
|
+
self.resource = resource_class.invite!(params[resource_name], current_inviter)
|
|
17
18
|
|
|
18
19
|
if resource.errors.empty?
|
|
19
|
-
set_flash_message :notice, :send_instructions
|
|
20
|
-
|
|
20
|
+
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
21
|
+
respond_with resource, :location => redirect_location(resource_name, resource)
|
|
21
22
|
else
|
|
22
|
-
render_with_scope :new
|
|
23
|
+
respond_with_navigational(resource) { render_with_scope :new }
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# GET /resource/invitation/accept?invitation_token=abcdef
|
|
27
28
|
def edit
|
|
28
|
-
self.resource = resource_class.
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :invitation_token => params[:invitation_token] })
|
|
30
|
+
render_with_scope :edit
|
|
31
|
+
else
|
|
32
|
+
set_flash_message(:alert, :invitation_token_invalid)
|
|
33
|
+
redirect_to after_sign_out_path_for(resource_name)
|
|
34
|
+
end
|
|
31
35
|
end
|
|
32
36
|
|
|
33
37
|
# PUT /resource/invitation
|
|
@@ -36,9 +40,27 @@ class Devise::InvitationsController < ApplicationController
|
|
|
36
40
|
|
|
37
41
|
if resource.errors.empty?
|
|
38
42
|
set_flash_message :notice, :updated
|
|
39
|
-
|
|
43
|
+
sign_in(resource_name, resource)
|
|
44
|
+
respond_with resource, :location => after_accept_path_for(resource)
|
|
40
45
|
else
|
|
41
|
-
render_with_scope :edit
|
|
46
|
+
respond_with_navigational(resource){ render_with_scope :edit }
|
|
42
47
|
end
|
|
43
48
|
end
|
|
49
|
+
|
|
50
|
+
protected
|
|
51
|
+
def current_inviter
|
|
52
|
+
@current_inviter ||= authenticate_inviter!
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def has_invitations_left?
|
|
56
|
+
unless current_inviter.nil? || current_inviter.has_invitations_left?
|
|
57
|
+
build_resource
|
|
58
|
+
set_flash_message :alert, :no_invitations_remaining
|
|
59
|
+
respond_with_navigational(resource) { render_with_scope :new }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def after_accept_path_for(resource)
|
|
64
|
+
after_sign_in_path_for(resource)
|
|
65
|
+
end
|
|
44
66
|
end
|
data/config/locales/en.yml
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
en:
|
|
2
2
|
devise:
|
|
3
3
|
invitations:
|
|
4
|
-
send_instructions: 'An email
|
|
4
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
5
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
5
6
|
updated: 'Your password was set successfully. You are now signed in.'
|
|
7
|
+
no_invitations_remaining: "No invitations remaining"
|
|
6
8
|
mailer:
|
|
7
|
-
|
|
8
|
-
subject: 'Invitation'
|
|
9
|
+
invitation_instructions:
|
|
10
|
+
subject: 'Invitation instructions'
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
module DeviseInvitable::Controllers::Helpers
|
|
2
2
|
protected
|
|
3
|
-
def authenticate_resource!
|
|
4
|
-
ActiveSupport::Deprecation.warn('authenticate_resource! has been renamed to authenticate_inviter!')
|
|
5
|
-
authenticate_inviter!
|
|
6
|
-
end
|
|
7
|
-
|
|
8
3
|
def authenticate_inviter!
|
|
9
|
-
send(:"authenticate_#{resource_name}!")
|
|
4
|
+
send(:"authenticate_#{resource_name}!", true)
|
|
10
5
|
end
|
|
11
6
|
end
|
|
12
7
|
ActionController::Base.send :include, DeviseInvitable::Controllers::Helpers
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module DeviseInvitable
|
|
2
|
+
module Inviter
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
extend ClassMethods
|
|
7
|
+
attr_accessor :invitation_limit unless respond_to? :invitation_limit
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Return true if this user has invitations left to send
|
|
11
|
+
def has_invitations_left?
|
|
12
|
+
if self.class.invitation_limit.present?
|
|
13
|
+
if invitation_limit
|
|
14
|
+
return invitation_limit > 0
|
|
15
|
+
else
|
|
16
|
+
return self.class.invitation_limit > 0
|
|
17
|
+
end
|
|
18
|
+
else
|
|
19
|
+
return true
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
protected
|
|
24
|
+
def decrement_invitation_limit!
|
|
25
|
+
if self.class.invitation_limit.present?
|
|
26
|
+
self.invitation_limit ||= self.class.invitation_limit
|
|
27
|
+
self.update_attribute(:invitation_limit, invitation_limit - 1)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
module ClassMethods
|
|
32
|
+
Devise::Models.config(self, :invitation_limit)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
module DeviseInvitable
|
|
2
2
|
module Mailer
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
# Deliver an invitation email
|
|
5
|
+
def invitation_instructions(record)
|
|
6
|
+
setup_mail(record, :invitation_instructions)
|
|
7
|
+
end
|
|
8
|
+
|
|
4
9
|
def invitation(record)
|
|
5
|
-
|
|
10
|
+
ActiveSupport::Deprecation.warn('invitation has been renamed to invitation_instructions')
|
|
11
|
+
invitation_instructions(record)
|
|
6
12
|
end
|
|
7
13
|
end
|
|
8
14
|
end
|
|
@@ -1,29 +1,37 @@
|
|
|
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
|
|
|
24
|
+
attr_accessor :skip_invitation
|
|
25
|
+
|
|
26
|
+
included do
|
|
27
|
+
include ::DeviseInvitable::Inviter
|
|
28
|
+
belongs_to :invited_by, :polymorphic => true
|
|
29
|
+
end
|
|
30
|
+
|
|
23
31
|
# Accept an invitation by clearing invitation token and confirming it if model
|
|
24
32
|
# is confirmable
|
|
25
33
|
def accept_invitation!
|
|
26
|
-
if self.invited?
|
|
34
|
+
if self.invited? && self.valid?
|
|
27
35
|
self.invitation_token = nil
|
|
28
36
|
self.save
|
|
29
37
|
end
|
|
@@ -34,26 +42,20 @@ module Devise
|
|
|
34
42
|
persisted? && invitation_token.present?
|
|
35
43
|
end
|
|
36
44
|
|
|
37
|
-
# Send invitation by email
|
|
38
|
-
def send_invitation
|
|
39
|
-
::Devise.mailer.invitation(self).deliver
|
|
40
|
-
end
|
|
41
|
-
|
|
42
45
|
# Reset invitation token and send invitation again
|
|
43
46
|
def invite!
|
|
44
47
|
if new_record? || invited?
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
@skip_password = true
|
|
49
|
+
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
50
|
+
generate_invitation_token if self.invitation_token.nil?
|
|
51
|
+
self.invitation_sent_at = Time.now.utc
|
|
52
|
+
if save(:validate => self.class.validate_on_invite)
|
|
53
|
+
self.invited_by.decrement_invitation_limit! if self.invited_by
|
|
54
|
+
!!deliver_invitation unless @skip_invitation
|
|
55
|
+
end
|
|
49
56
|
end
|
|
50
57
|
end
|
|
51
58
|
|
|
52
|
-
def resend_invitation!
|
|
53
|
-
ActiveSupport::Deprecation.warn('resend_invitation! has been renamed to invite!')
|
|
54
|
-
self.invite!
|
|
55
|
-
end
|
|
56
|
-
|
|
57
59
|
# Verify whether a invitation is active or not. If the user has been
|
|
58
60
|
# invited, we need to calculate if the invitation time has not expired
|
|
59
61
|
# for this user, in other words, if the invitation is still valid.
|
|
@@ -61,7 +63,27 @@ module Devise
|
|
|
61
63
|
invited? && invitation_period_valid?
|
|
62
64
|
end
|
|
63
65
|
|
|
66
|
+
# Only verify password when is not invited
|
|
67
|
+
def valid_password?(password)
|
|
68
|
+
super unless invited?
|
|
69
|
+
end
|
|
70
|
+
|
|
64
71
|
protected
|
|
72
|
+
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
73
|
+
def password_required?
|
|
74
|
+
!@skip_password && super
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Deliver the invitation email
|
|
78
|
+
def deliver_invitation
|
|
79
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
80
|
+
end
|
|
81
|
+
|
|
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
|
|
65
87
|
|
|
66
88
|
# Checks if the invitation for the user is within the limit time.
|
|
67
89
|
# We do this by calculating if the difference between today and the
|
|
@@ -89,8 +111,7 @@ module Devise
|
|
|
89
111
|
# Generates a new random token for invitation, and stores the time
|
|
90
112
|
# this token is being generated
|
|
91
113
|
def generate_invitation_token
|
|
92
|
-
self.invitation_token
|
|
93
|
-
self.invitation_sent_at = Time.now.utc
|
|
114
|
+
self.invitation_token = self.class.invitation_token
|
|
94
115
|
end
|
|
95
116
|
|
|
96
117
|
module ClassMethods
|
|
@@ -98,33 +119,32 @@ module Devise
|
|
|
98
119
|
# user and send invitation to it. If user is found, returns the user with an
|
|
99
120
|
# email already exists error.
|
|
100
121
|
# Attributes must contain the user email, other attributes will be set in the record
|
|
101
|
-
def invite!(attributes={})
|
|
102
|
-
invitable = find_or_initialize_with_error_by(
|
|
122
|
+
def invite!(attributes={}, invited_by=nil, &block)
|
|
123
|
+
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
|
103
124
|
invitable.attributes = attributes
|
|
125
|
+
invitable.invited_by = invited_by
|
|
104
126
|
|
|
105
127
|
if invitable.new_record?
|
|
106
|
-
invitable.errors.clear if invitable.email.match Devise.email_regexp
|
|
128
|
+
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
107
129
|
else
|
|
108
|
-
invitable.errors.add(
|
|
130
|
+
invitable.errors.add(invite_key, :taken) unless invitable.invited?
|
|
109
131
|
end
|
|
110
132
|
|
|
111
|
-
|
|
133
|
+
if invitable.errors.empty?
|
|
134
|
+
yield invitable if block_given?
|
|
135
|
+
invitable.invite!
|
|
136
|
+
end
|
|
112
137
|
invitable
|
|
113
138
|
end
|
|
114
139
|
|
|
115
|
-
def send_invitation(attributes = {})
|
|
116
|
-
ActiveSupport::Deprecation.warn('send_invitation has been renamed to invite!')
|
|
117
|
-
self.invite!(attributes)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
140
|
# Attempt to find a user by it's invitation_token to set it's password.
|
|
121
141
|
# If a user is found, reset it's password and automatically try saving
|
|
122
142
|
# the record. If not user is found, returns a new user containing an
|
|
123
143
|
# error in invitation_token attribute.
|
|
124
144
|
# Attributes must contain invitation_token, password and confirmation
|
|
125
145
|
def accept_invitation!(attributes={})
|
|
126
|
-
invitable = find_or_initialize_with_error_by(:invitation_token, attributes
|
|
127
|
-
invitable.errors.add(:invitation_token, :invalid) if
|
|
146
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
147
|
+
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
|
128
148
|
if invitable.errors.empty?
|
|
129
149
|
invitable.attributes = attributes
|
|
130
150
|
invitable.accept_invitation!
|
|
@@ -132,7 +152,15 @@ module Devise
|
|
|
132
152
|
invitable
|
|
133
153
|
end
|
|
134
154
|
|
|
155
|
+
# Generate a token checking if one does not already exist in the database.
|
|
156
|
+
def invitation_token
|
|
157
|
+
generate_token(:invitation_token)
|
|
158
|
+
end
|
|
159
|
+
|
|
135
160
|
Devise::Models.config(self, :invite_for)
|
|
161
|
+
Devise::Models.config(self, :validate_on_invite)
|
|
162
|
+
Devise::Models.config(self, :invitation_limit)
|
|
163
|
+
Devise::Models.config(self, :invite_key)
|
|
136
164
|
end
|
|
137
165
|
end
|
|
138
166
|
end
|
|
@@ -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,34 @@
|
|
|
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.index :invitation_token # for invitable
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# # And allow encrypted_password to be null:
|
|
23
|
+
# change_column :the_resources, :encrypted_password, :string, :null => true
|
|
24
|
+
# # the following line is only if you use Devise's encryptable module!
|
|
25
|
+
# change_column :the_resources, :password_salt, :string, :null => true
|
|
4
26
|
def invitable
|
|
5
|
-
apply_devise_schema :invitation_token, String, :limit =>
|
|
27
|
+
apply_devise_schema :invitation_token, String, :limit => 60
|
|
6
28
|
apply_devise_schema :invitation_sent_at, DateTime
|
|
29
|
+
apply_devise_schema :invitation_limit, Integer
|
|
30
|
+
apply_devise_schema :invited_by_id, Integer
|
|
31
|
+
apply_devise_schema :invited_by_type, String
|
|
7
32
|
end
|
|
8
33
|
end
|
|
9
34
|
end
|