devise_invitable 0.3.1 → 0.4.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 +152 -51
- data/app/controllers/devise/invitations_controller.rb +23 -6
- data/app/views/devise/mailer/invitation_instructions.html.erb +8 -0
- data/config/locales/en.yml +5 -3
- data/lib/devise_invitable/controllers/helpers.rb +2 -2
- data/lib/devise_invitable/mailer.rb +12 -5
- data/lib/devise_invitable/model.rb +91 -38
- data/lib/devise_invitable/rails.rb +3 -4
- 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 +38 -13
- data/lib/generators/active_record/devise_invitable_generator.rb +13 -0
- data/lib/generators/active_record/templates/migration.rb +25 -0
- data/lib/generators/devise_invitable/devise_invitable_generator.rb +20 -0
- data/lib/generators/devise_invitable/install_generator.rb +49 -0
- data/lib/generators/devise_invitable/views_generator.rb +10 -0
- data/lib/generators/mongoid/devise_invitable_generator.rb +8 -0
- metadata +54 -137
- 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/app/views/devise/mailer/invitation.html.erb +0 -8
- data/devise_invitable.gemspec +0 -136
- data/init.rb +0 -1
- 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 -172
- 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/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/config/application.rb +0 -45
- 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 -142
- 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/routes_test.rb +0 -20
- data/test/test_helper.rb +0 -29
data/README.rdoc
CHANGED
|
@@ -1,102 +1,203 @@
|
|
|
1
|
-
=
|
|
1
|
+
= DeviseInvitable
|
|
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
|
-
|
|
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
|
-
|
|
7
|
+
== Installation for Rails ~> 3.0 and Devise ~> 1.1
|
|
8
8
|
|
|
9
|
-
|
|
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
|
-
|
|
15
|
+
gem 'devise', '~> 1.1.3'
|
|
16
|
+
gem 'devise_invitable', '~> 0.3.4'
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
config.gem 'devise'
|
|
19
|
-
config.gem 'devise_invitable'
|
|
18
|
+
=== Automatic installation
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
Run the following generator to add DeviseInvitable’s configuration option in the Devise configuration file (config/initializers/devise.rb):
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
rails generate devise_invitable:install
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
When you are done, you are ready to add DeviseInvitable to any of your Devise models using the following generator:
|
|
25
|
+
|
|
26
|
+
rails generate devise_invitable MODEL
|
|
27
|
+
|
|
28
|
+
Replace MODEL by the class name you want to add DeviseInvitable, like User, Admin, etc. This will add the :invitable flag to your model's Devise modules. The generator will also create a migration file (if your ORM support them). Continue reading this file to understand exactly what the generator produces and how to use it.
|
|
29
|
+
|
|
30
|
+
=== Manual installation
|
|
31
|
+
|
|
32
|
+
Follow the walkthrough for Devise and after it's done, follow this walkthrough.
|
|
33
|
+
|
|
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
|
+
|
|
36
|
+
class User < ActiveRecord::Base
|
|
37
|
+
devise :database_authenticatable, :confirmable, :invitable
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
Add t.invitable to your Devise model migration:
|
|
26
41
|
|
|
27
42
|
create_table :users do
|
|
28
43
|
...
|
|
29
44
|
t.invitable
|
|
30
45
|
...
|
|
31
46
|
end
|
|
32
|
-
add_index :users, :invitation_token
|
|
47
|
+
add_index :users, :invitation_token
|
|
33
48
|
|
|
34
|
-
|
|
49
|
+
or for a model that already exists, define a migration to add DeviseInvitable to your model:
|
|
35
50
|
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
change_table :users do |t|
|
|
52
|
+
t.string :invitation_token, :limit => 60
|
|
53
|
+
t.datetime :invitation_sent_at
|
|
54
|
+
t.index :invitation_token
|
|
38
55
|
end
|
|
39
56
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
...
|
|
45
|
-
end
|
|
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
|
|
46
61
|
|
|
47
62
|
== Model configuration
|
|
48
63
|
|
|
49
|
-
DeviseInvitable adds
|
|
64
|
+
DeviseInvitable adds three new configuration options:
|
|
50
65
|
|
|
51
|
-
|
|
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.
|
|
52
67
|
|
|
53
|
-
|
|
68
|
+
You can set this configuration option in the Devise initializer as follow:
|
|
54
69
|
|
|
55
|
-
|
|
70
|
+
# ==> Configuration for :invitable
|
|
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.
|
|
74
|
+
# config.invite_for = 2.weeks
|
|
56
75
|
|
|
57
|
-
|
|
76
|
+
or directly as parameters to the <tt>devise</tt> method:
|
|
58
77
|
|
|
59
|
-
|
|
78
|
+
devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
|
|
60
79
|
|
|
61
|
-
|
|
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.
|
|
62
81
|
|
|
63
|
-
|
|
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.
|
|
64
83
|
|
|
65
|
-
|
|
84
|
+
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
66
85
|
|
|
67
|
-
|
|
86
|
+
== Configuring views
|
|
68
87
|
|
|
69
|
-
|
|
88
|
+
All the views are packaged inside the gem. If you'd like to customize the views, invoke the following generator and it will copy all the views to your application:
|
|
70
89
|
|
|
71
|
-
|
|
90
|
+
rails generate devise_invitable:views
|
|
72
91
|
|
|
73
|
-
|
|
92
|
+
You can also use the generator to generate scoped views:
|
|
74
93
|
|
|
75
|
-
|
|
94
|
+
rails generate devise_invitable:views users
|
|
76
95
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
|
|
97
|
+
|
|
98
|
+
== Usage
|
|
99
|
+
|
|
100
|
+
=== Send an invitation
|
|
101
|
+
|
|
102
|
+
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.
|
|
103
|
+
|
|
104
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe")
|
|
105
|
+
# => an invitation email will be sent to new_user@example.com
|
|
106
|
+
|
|
107
|
+
If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to true.
|
|
108
|
+
|
|
109
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe") do |u|
|
|
110
|
+
u.skip_invitation = true
|
|
81
111
|
end
|
|
112
|
+
# => the record will be created, but the invitation email will not be sent
|
|
113
|
+
|
|
114
|
+
You can add :skip_invitation to attributes hash if skip_invitation is added to attr_accessible.
|
|
115
|
+
|
|
116
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
|
|
117
|
+
# => the record will be created, but the invitation email will not be sent
|
|
118
|
+
|
|
119
|
+
=== Accept an invitation
|
|
120
|
+
|
|
121
|
+
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.
|
|
122
|
+
|
|
123
|
+
User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
124
|
+
|
|
125
|
+
== Integration in a Rails application
|
|
126
|
+
|
|
127
|
+
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.
|
|
128
|
+
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).
|
|
129
|
+
After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
|
|
130
|
+
|
|
131
|
+
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).
|
|
132
|
+
You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
|
|
133
|
+
|
|
134
|
+
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.
|
|
135
|
+
|
|
136
|
+
== Controller filter
|
|
137
|
+
|
|
138
|
+
InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
|
|
139
|
+
|
|
140
|
+
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.
|
|
141
|
+
|
|
142
|
+
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:
|
|
143
|
+
|
|
144
|
+
class ApplicationController < ActionController::Base
|
|
145
|
+
protected
|
|
146
|
+
def authenticate_inviter!
|
|
147
|
+
authenticate_admin!
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
== I18n
|
|
152
|
+
|
|
153
|
+
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:
|
|
154
|
+
|
|
155
|
+
en:
|
|
156
|
+
devise:
|
|
157
|
+
invitations:
|
|
158
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
159
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
160
|
+
updated: 'Your password was set successfully. You are now signed in.'
|
|
161
|
+
|
|
162
|
+
You can also create distinct messages based on the resource you've configured using the singular name given in routes:
|
|
163
|
+
|
|
164
|
+
en:
|
|
165
|
+
devise:
|
|
166
|
+
invitations:
|
|
167
|
+
user:
|
|
168
|
+
send_instructions: 'A new user invitation has been sent to %{email}.'
|
|
169
|
+
invitation_token_invalid: 'Your invitation token is not valid!'
|
|
170
|
+
updated: 'Welcome on board! You are now signed in.'
|
|
171
|
+
|
|
172
|
+
The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
|
|
173
|
+
|
|
174
|
+
en:
|
|
175
|
+
devise:
|
|
176
|
+
mailer:
|
|
177
|
+
invitation_instructions:
|
|
178
|
+
subject: 'You got an invitation!'
|
|
179
|
+
user_subject: 'You got a user invitation!'
|
|
180
|
+
|
|
181
|
+
Take a look at the generated locale file (in <tt>config/locales/devise_invitable.en.yml</tt>) to check all available messages.
|
|
182
|
+
|
|
183
|
+
== Other ORMs
|
|
184
|
+
|
|
185
|
+
DeviseInvitable supports ActiveRecord and Mongoid, like Devise.
|
|
186
|
+
|
|
187
|
+
== Contributors
|
|
82
188
|
|
|
83
|
-
|
|
84
|
-
change_column :your_table, :encrypted_password, :string, :null => true
|
|
85
|
-
change_column :your_table, :password_salt, :string, :null => true
|
|
189
|
+
Check them all at:
|
|
86
190
|
|
|
87
|
-
|
|
191
|
+
http://github.com/scambra/devise_invitable/contributors
|
|
88
192
|
|
|
89
|
-
|
|
193
|
+
Special thanks to rymai[http://github.com/rymai] for the Rails 3 support, his fork was a great help.
|
|
90
194
|
|
|
91
195
|
== Note on Patches/Pull Requests
|
|
92
196
|
|
|
93
197
|
* Fork the project.
|
|
94
198
|
* Make your feature addition or bug fix.
|
|
95
|
-
* Add tests for it. This is important so I don't break it in a
|
|
96
|
-
|
|
97
|
-
* Commit, do not mess with rakefile, version, or history.
|
|
98
|
-
(if you want to have your own version, that is fine but
|
|
99
|
-
bump version in a commit by itself I can ignore when I pull)
|
|
199
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
|
200
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
|
100
201
|
* Send me a pull request. Bonus points for topic branches.
|
|
101
202
|
|
|
102
203
|
== Copyright
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
class Devise::InvitationsController < ApplicationController
|
|
2
2
|
include Devise::Controllers::InternalHelpers
|
|
3
3
|
|
|
4
|
-
before_filter :
|
|
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,10 +14,10 @@ class Devise::InvitationsController < ApplicationController
|
|
|
13
14
|
|
|
14
15
|
# POST /resource/invitation
|
|
15
16
|
def create
|
|
16
|
-
self.resource = resource_class.
|
|
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
|
+
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
20
21
|
redirect_to after_sign_in_path_for(resource_name)
|
|
21
22
|
else
|
|
22
23
|
render_with_scope :new
|
|
@@ -25,9 +26,12 @@ class Devise::InvitationsController < ApplicationController
|
|
|
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
|
|
@@ -41,4 +45,17 @@ class Devise::InvitationsController < ApplicationController
|
|
|
41
45
|
render_with_scope :edit
|
|
42
46
|
end
|
|
43
47
|
end
|
|
48
|
+
|
|
49
|
+
protected
|
|
50
|
+
def current_inviter
|
|
51
|
+
@current_inviter ||= authenticate_inviter!
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def has_invitations_left?
|
|
55
|
+
unless current_inviter.nil? || current_inviter.has_invitations_left?
|
|
56
|
+
build_resource
|
|
57
|
+
set_flash_message :alert, :no_invitations_remaining
|
|
58
|
+
render_with_scope :new
|
|
59
|
+
end
|
|
60
|
+
end
|
|
44
61
|
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<p>Hello <%= @resource.email %>!</p>
|
|
2
|
+
|
|
3
|
+
<p>Someone has invited you to <%= root_url %>, you can accept it through the link below.</p>
|
|
4
|
+
|
|
5
|
+
<p><%= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @resource.invitation_token) %></p>
|
|
6
|
+
|
|
7
|
+
<p>If you don't want to accept the invitation, please ignore this email.<br />
|
|
8
|
+
Your account won't be created until you access the link above and set your password.</p>
|
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,7 +1,7 @@
|
|
|
1
1
|
module DeviseInvitable::Controllers::Helpers
|
|
2
2
|
protected
|
|
3
|
-
def
|
|
4
|
-
send(:"authenticate_#{resource_name}!")
|
|
3
|
+
def authenticate_inviter!
|
|
4
|
+
send(:"authenticate_#{resource_name}!", true)
|
|
5
5
|
end
|
|
6
6
|
end
|
|
7
7
|
ActionController::Base.send :include, DeviseInvitable::Controllers::Helpers
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
module DeviseInvitable
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
module DeviseInvitable
|
|
2
|
+
module Mailer
|
|
3
|
+
|
|
4
|
+
# Deliver an invitation email
|
|
5
|
+
def invitation_instructions(record)
|
|
6
|
+
setup_mail(record, :invitation_instructions)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def invitation(record)
|
|
10
|
+
ActiveSupport::Deprecation.warn('invitation has been renamed to invitation_instructions')
|
|
11
|
+
invitation_instructions(record)
|
|
12
|
+
end
|
|
6
13
|
end
|
|
7
14
|
end
|
|
@@ -1,55 +1,70 @@
|
|
|
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.
|
|
17
|
-
# User.accept_invitation!(:invitation_token => '...')
|
|
18
|
-
# User.find(1).accept_invitation!
|
|
19
|
-
# User.find(1).
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
extend ActiveSupport::Concern
|
|
23
|
+
|
|
24
|
+
attr_accessor :skip_invitation
|
|
25
|
+
|
|
26
|
+
included do
|
|
27
|
+
belongs_to :invited_by, :polymorphic => true
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
# Accept an invitation by clearing invitation token and confirming it if model
|
|
28
31
|
# is confirmable
|
|
29
32
|
def accept_invitation!
|
|
30
|
-
if self.invited?
|
|
33
|
+
if self.invited? && self.valid?
|
|
31
34
|
self.invitation_token = nil
|
|
32
|
-
self.save
|
|
35
|
+
self.save
|
|
33
36
|
end
|
|
34
37
|
end
|
|
35
38
|
|
|
36
39
|
# Verifies whether a user has been invited or not
|
|
37
40
|
def invited?
|
|
38
|
-
|
|
41
|
+
persisted? && invitation_token.present?
|
|
39
42
|
end
|
|
40
43
|
|
|
41
|
-
#
|
|
42
|
-
def
|
|
43
|
-
|
|
44
|
+
# Return true if this user has invitations left to send
|
|
45
|
+
def has_invitations_left?
|
|
46
|
+
if self.class.invitation_limit.present?
|
|
47
|
+
if invitation_limit
|
|
48
|
+
return invitation_limit > 0
|
|
49
|
+
else
|
|
50
|
+
return self.class.invitation_limit > 0
|
|
51
|
+
end
|
|
52
|
+
else
|
|
53
|
+
return true
|
|
54
|
+
end
|
|
44
55
|
end
|
|
45
56
|
|
|
46
57
|
# Reset invitation token and send invitation again
|
|
47
|
-
def
|
|
58
|
+
def invite!
|
|
48
59
|
if new_record? || invited?
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
@skip_password = true
|
|
61
|
+
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
62
|
+
generate_invitation_token if self.invitation_token.nil?
|
|
63
|
+
self.invitation_sent_at = Time.now.utc
|
|
64
|
+
if save(:validate => self.class.validate_on_invite)
|
|
65
|
+
self.invited_by.decrement_invitation_limit! if self.invited_by
|
|
66
|
+
!!deliver_invitation unless @skip_invitation
|
|
67
|
+
end
|
|
53
68
|
end
|
|
54
69
|
end
|
|
55
70
|
|
|
@@ -60,7 +75,34 @@ module Devise
|
|
|
60
75
|
invited? && invitation_period_valid?
|
|
61
76
|
end
|
|
62
77
|
|
|
78
|
+
# Only verify password when is not invited
|
|
79
|
+
def valid_password?(password)
|
|
80
|
+
super unless invited?
|
|
81
|
+
end
|
|
82
|
+
|
|
63
83
|
protected
|
|
84
|
+
def decrement_invitation_limit!
|
|
85
|
+
if self.class.invitation_limit.present?
|
|
86
|
+
self.invitation_limit ||= self.class.invitation_limit
|
|
87
|
+
self.decrement!(:invitation_limit)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
92
|
+
def password_required?
|
|
93
|
+
!@skip_password && super
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Deliver the invitation email
|
|
97
|
+
def deliver_invitation
|
|
98
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Clear invitation token when reset password token is cleared too
|
|
102
|
+
def clear_reset_password_token
|
|
103
|
+
self.invitation_token = nil if invited?
|
|
104
|
+
super
|
|
105
|
+
end
|
|
64
106
|
|
|
65
107
|
# Checks if the invitation for the user is within the limit time.
|
|
66
108
|
# We do this by calculating if the difference between today and the
|
|
@@ -88,26 +130,29 @@ module Devise
|
|
|
88
130
|
# Generates a new random token for invitation, and stores the time
|
|
89
131
|
# this token is being generated
|
|
90
132
|
def generate_invitation_token
|
|
91
|
-
self.invitation_token
|
|
92
|
-
self.invitation_sent_at = Time.now.utc
|
|
133
|
+
self.invitation_token = self.class.invitation_token
|
|
93
134
|
end
|
|
94
135
|
|
|
95
136
|
module ClassMethods
|
|
96
137
|
# Attempt to find a user by it's email. If a record is not found, create a new
|
|
97
138
|
# user and send invitation to it. If user is found, returns the user with an
|
|
98
139
|
# email already exists error.
|
|
99
|
-
#
|
|
100
|
-
def
|
|
101
|
-
invitable =
|
|
140
|
+
# Attributes must contain the user email, other attributes will be set in the record
|
|
141
|
+
def invite!(attributes={}, invited_by=nil, &block)
|
|
142
|
+
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
|
143
|
+
invitable.attributes = attributes
|
|
144
|
+
invitable.invited_by = invited_by
|
|
102
145
|
|
|
103
146
|
if invitable.new_record?
|
|
104
|
-
invitable.errors.
|
|
105
|
-
invitable.errors.add(:email, :invalid) unless invitable.email.match Devise.email_regexp
|
|
147
|
+
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
106
148
|
else
|
|
107
|
-
invitable.errors.add(
|
|
149
|
+
invitable.errors.add(invite_key, :taken) unless invitable.invited?
|
|
108
150
|
end
|
|
109
151
|
|
|
110
|
-
|
|
152
|
+
if invitable.errors.empty?
|
|
153
|
+
yield invitable if block_given?
|
|
154
|
+
invitable.invite!
|
|
155
|
+
end
|
|
111
156
|
invitable
|
|
112
157
|
end
|
|
113
158
|
|
|
@@ -117,16 +162,24 @@ module Devise
|
|
|
117
162
|
# error in invitation_token attribute.
|
|
118
163
|
# Attributes must contain invitation_token, password and confirmation
|
|
119
164
|
def accept_invitation!(attributes={})
|
|
120
|
-
invitable = find_or_initialize_with_error_by(:invitation_token, attributes
|
|
121
|
-
invitable.errors.add(:invitation_token, :invalid) if
|
|
165
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
166
|
+
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
|
122
167
|
if invitable.errors.empty?
|
|
123
168
|
invitable.attributes = attributes
|
|
124
|
-
invitable.accept_invitation!
|
|
169
|
+
invitable.accept_invitation!
|
|
125
170
|
end
|
|
126
171
|
invitable
|
|
127
172
|
end
|
|
128
173
|
|
|
174
|
+
# Generate a token checking if one does not already exist in the database.
|
|
175
|
+
def invitation_token
|
|
176
|
+
generate_token(:invitation_token)
|
|
177
|
+
end
|
|
178
|
+
|
|
129
179
|
Devise::Models.config(self, :invite_for)
|
|
180
|
+
Devise::Models.config(self, :validate_on_invite)
|
|
181
|
+
Devise::Models.config(self, :invitation_limit)
|
|
182
|
+
Devise::Models.config(self, :invite_key)
|
|
130
183
|
end
|
|
131
184
|
end
|
|
132
185
|
end
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
module DeviseInvitable
|
|
2
2
|
class Engine < ::Rails::Engine
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
ActionView::Base.send :include, DeviseInvitable::Controllers::UrlHelpers
|
|
7
|
-
end
|
|
4
|
+
ActiveSupport.on_load(:action_controller) { include DeviseInvitable::Controllers::UrlHelpers }
|
|
5
|
+
ActiveSupport.on_load(:action_view) { include DeviseInvitable::Controllers::UrlHelpers }
|
|
8
6
|
|
|
9
7
|
config.after_initialize do
|
|
8
|
+
require 'devise/mailer'
|
|
10
9
|
Devise::Mailer.send :include, DeviseInvitable::Mailer
|
|
11
10
|
end
|
|
12
11
|
|
|
@@ -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
|