devise_invitable 0.3.2 → 0.4.rc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +124 -52
- data/app/controllers/devise/invitations_controller.rb +9 -6
- data/app/controllers/devise/invitations_controller.rb~ +48 -0
- data/app/views/devise/mailer/invitation_instructions.html.erb +8 -0
- data/config/locales/en.yml +5 -4
- data/lib/devise_invitable/controllers/helpers.rb +1 -1
- data/lib/devise_invitable/mailer.rb +12 -5
- data/lib/devise_invitable/model.rb +27 -36
- data/lib/devise_invitable/model.rb~ +129 -0
- data/lib/devise_invitable/rails.rb +2 -1
- data/lib/devise_invitable/routes.rb +8 -5
- data/lib/devise_invitable/schema.rb +24 -2
- data/lib/devise_invitable/version.rb +3 -0
- data/lib/devise_invitable.rb +9 -13
- data/lib/generators/active_record/devise_invitable_generator.rb +13 -0
- data/lib/generators/active_record/templates/migration.rb +20 -0
- data/lib/generators/devise_invitable/devise_invitable_generator.rb +20 -0
- data/lib/generators/devise_invitable/install_generator.rb +39 -0
- data/lib/generators/devise_invitable/views_generator.rb +1 -1
- metadata +74 -121
- 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 -136
- 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/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,95 +1,170 @@
|
|
|
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
|
-
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
|
|
7
|
+
== Installation for Rails ~> 3.0 and Devise ~> 1.1
|
|
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.1.3'
|
|
16
|
+
gem 'devise_invitable', '~> 0.3.4'
|
|
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
|
+
|
|
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:
|
|
23
41
|
|
|
24
42
|
create_table :users do
|
|
25
43
|
...
|
|
26
44
|
t.invitable
|
|
27
45
|
...
|
|
28
46
|
end
|
|
29
|
-
add_index :users, :invitation_token
|
|
47
|
+
add_index :users, :invitation_token
|
|
30
48
|
|
|
31
|
-
|
|
49
|
+
or for a model that already exists, define a migration to add DeviseInvitable to your model:
|
|
32
50
|
|
|
33
|
-
|
|
34
|
-
|
|
51
|
+
change_table :users do |t|
|
|
52
|
+
t.string :invitation_token, :limit => 60
|
|
53
|
+
t.datetime :invitation_sent_at
|
|
54
|
+
t.index :invitation_token
|
|
35
55
|
end
|
|
36
56
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
...
|
|
42
|
-
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
|
|
43
61
|
|
|
44
62
|
== Model configuration
|
|
45
63
|
|
|
46
|
-
DeviseInvitable adds a new configuration option
|
|
64
|
+
DeviseInvitable adds a new configuration option:
|
|
65
|
+
|
|
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.
|
|
67
|
+
|
|
68
|
+
You can set this configuration option in the Devise initializer as follow:
|
|
69
|
+
|
|
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
|
|
75
|
+
|
|
76
|
+
or directly as parameters to the <tt>devise</tt> method:
|
|
77
|
+
|
|
78
|
+
devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
|
|
79
|
+
|
|
80
|
+
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
47
81
|
|
|
48
82
|
== Configuring views
|
|
49
83
|
|
|
50
|
-
All
|
|
84
|
+
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:
|
|
51
85
|
|
|
52
|
-
|
|
86
|
+
rails generate devise_invitable:views
|
|
53
87
|
|
|
54
|
-
|
|
88
|
+
You can also use the generator to generate scoped views:
|
|
55
89
|
|
|
56
|
-
|
|
90
|
+
rails generate devise_invitable:views users
|
|
57
91
|
|
|
58
|
-
|
|
92
|
+
Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
|
|
59
93
|
|
|
60
|
-
==
|
|
94
|
+
== Usage
|
|
61
95
|
|
|
62
|
-
|
|
96
|
+
=== Send an invitation
|
|
63
97
|
|
|
64
|
-
|
|
98
|
+
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.
|
|
65
99
|
|
|
66
|
-
|
|
100
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe")
|
|
101
|
+
# => an invitation email will be sent to new_user@example.com
|
|
67
102
|
|
|
68
|
-
|
|
103
|
+
=== Accept an invitation
|
|
69
104
|
|
|
70
|
-
To accept an invitation with a token use accept_invitation
|
|
105
|
+
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.
|
|
71
106
|
|
|
72
|
-
User.accept_invitation!(:invitation_token => params[:invitation_token])
|
|
107
|
+
User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
73
108
|
|
|
74
|
-
|
|
109
|
+
== Integration in a Rails application
|
|
75
110
|
|
|
76
|
-
|
|
111
|
+
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.
|
|
112
|
+
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).
|
|
113
|
+
After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
|
|
77
114
|
|
|
78
|
-
|
|
115
|
+
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).
|
|
116
|
+
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.
|
|
79
117
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
118
|
+
== Controller filter
|
|
119
|
+
|
|
120
|
+
InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
|
|
121
|
+
|
|
122
|
+
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.
|
|
123
|
+
|
|
124
|
+
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:
|
|
125
|
+
|
|
126
|
+
class ApplicationController < ActionController::Base
|
|
127
|
+
protected
|
|
128
|
+
def authenticate_inviter!
|
|
129
|
+
authenticate_admin!
|
|
130
|
+
end
|
|
84
131
|
end
|
|
85
132
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
133
|
+
== I18n
|
|
134
|
+
|
|
135
|
+
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:
|
|
136
|
+
|
|
137
|
+
en:
|
|
138
|
+
devise:
|
|
139
|
+
invitations:
|
|
140
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
141
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
142
|
+
updated: 'Your password was set successfully. You are now signed in.'
|
|
143
|
+
|
|
144
|
+
You can also create distinct messages based on the resource you've configured using the singular name given in routes:
|
|
145
|
+
|
|
146
|
+
en:
|
|
147
|
+
devise:
|
|
148
|
+
invitations:
|
|
149
|
+
user:
|
|
150
|
+
send_instructions: 'A new user invitation has been sent to %{email}.'
|
|
151
|
+
invitation_token_invalid: 'Your invitation token is not valid!'
|
|
152
|
+
updated: 'Welcome on board! You are now signed in.'
|
|
153
|
+
|
|
154
|
+
The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
|
|
155
|
+
|
|
156
|
+
en:
|
|
157
|
+
devise:
|
|
158
|
+
mailer:
|
|
159
|
+
invitation_instructions:
|
|
160
|
+
subject: 'You got an invitation!'
|
|
161
|
+
user_subject: 'You got a user invitation!'
|
|
162
|
+
|
|
163
|
+
Take a look at the generated locale file (in <tt>config/locales/devise_invitable.en.yml</tt>) to check all available messages.
|
|
89
164
|
|
|
90
|
-
|
|
165
|
+
== Other ORMs
|
|
91
166
|
|
|
92
|
-
|
|
167
|
+
DeviseInvitable supports ActiveRecord and Mongoid, like Devise.
|
|
93
168
|
|
|
94
169
|
== Contributors
|
|
95
170
|
|
|
@@ -97,17 +172,14 @@ Check them all at:
|
|
|
97
172
|
|
|
98
173
|
http://github.com/scambra/devise_invitable/contributors
|
|
99
174
|
|
|
100
|
-
Special thanks to http://github.com/rymai
|
|
175
|
+
Special thanks to rymai[http://github.com/rymai] for the Rails 3 support, his fork was a great help.
|
|
101
176
|
|
|
102
177
|
== Note on Patches/Pull Requests
|
|
103
178
|
|
|
104
179
|
* Fork the project.
|
|
105
180
|
* Make your feature addition or bug fix.
|
|
106
|
-
* Add tests for it. This is important so I don't break it in a
|
|
107
|
-
|
|
108
|
-
* Commit, do not mess with rakefile, version, or history.
|
|
109
|
-
(if you want to have your own version, that is fine but
|
|
110
|
-
bump version in a commit by itself I can ignore when I pull)
|
|
181
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
|
182
|
+
* 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)
|
|
111
183
|
* Send me a pull request. Bonus points for topic branches.
|
|
112
184
|
|
|
113
185
|
== Copyright
|
|
@@ -1,7 +1,7 @@
|
|
|
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
5
|
before_filter :require_no_authentication, :only => [:edit, :update]
|
|
6
6
|
helper_method :after_sign_in_path_for
|
|
7
7
|
|
|
@@ -16,8 +16,8 @@ class Devise::InvitationsController < ApplicationController
|
|
|
16
16
|
self.resource = resource_class.invite!(params[resource_name])
|
|
17
17
|
|
|
18
18
|
if resource.errors.empty?
|
|
19
|
-
set_flash_message :notice, :send_instructions
|
|
20
|
-
redirect_to
|
|
19
|
+
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
20
|
+
redirect_to after_sign_in_path_for(resource_name)
|
|
21
21
|
else
|
|
22
22
|
render_with_scope :new
|
|
23
23
|
end
|
|
@@ -25,9 +25,12 @@ class Devise::InvitationsController < ApplicationController
|
|
|
25
25
|
|
|
26
26
|
# GET /resource/invitation/accept?invitation_token=abcdef
|
|
27
27
|
def edit
|
|
28
|
-
self.resource = resource_class.
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :invitation_token => params[:invitation_token] })
|
|
29
|
+
render_with_scope :edit
|
|
30
|
+
else
|
|
31
|
+
set_flash_message(:alert, :invitation_token_invalid)
|
|
32
|
+
redirect_to after_sign_out_path_for(resource_name)
|
|
33
|
+
end
|
|
31
34
|
end
|
|
32
35
|
|
|
33
36
|
# PUT /resource/invitation
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class Devise::InvitationsController < ApplicationController
|
|
2
|
+
include Devise::Controllers::InternalHelpers
|
|
3
|
+
|
|
4
|
+
before_filter :authenticate_inviter!, :only => [:new, :create]
|
|
5
|
+
before_filter :require_no_authentication, :only => [:edit, :update]
|
|
6
|
+
helper_method :after_sign_in_path_for
|
|
7
|
+
|
|
8
|
+
# GET /resource/invitation/new
|
|
9
|
+
def new
|
|
10
|
+
build_resource
|
|
11
|
+
render_with_scope :new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# POST /resource/invitation
|
|
15
|
+
def create
|
|
16
|
+
self.resource = resource_class.invite!(params[resource_name])
|
|
17
|
+
|
|
18
|
+
if resource.errors.empty?
|
|
19
|
+
puts params.inspect
|
|
20
|
+
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
21
|
+
redirect_to after_sign_in_path_for(resource_name)
|
|
22
|
+
else
|
|
23
|
+
render_with_scope :new
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# GET /resource/invitation/accept?invitation_token=abcdef
|
|
28
|
+
def edit
|
|
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
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# PUT /resource/invitation
|
|
38
|
+
def update
|
|
39
|
+
self.resource = resource_class.accept_invitation!(params[resource_name])
|
|
40
|
+
|
|
41
|
+
if resource.errors.empty?
|
|
42
|
+
set_flash_message :notice, :updated
|
|
43
|
+
sign_in_and_redirect(resource_name, resource)
|
|
44
|
+
else
|
|
45
|
+
render_with_scope :edit
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
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,9 @@
|
|
|
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.'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
mailer:
|
|
8
|
+
invitation_instructions:
|
|
9
|
+
subject: 'Invitation instructions'
|
|
@@ -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,29 +1,30 @@
|
|
|
1
1
|
module Devise
|
|
2
2
|
module Models
|
|
3
|
-
# Invitable is responsible
|
|
4
|
-
# When an invitation is sent to an email, an account is created for it.
|
|
5
|
-
#
|
|
3
|
+
# Invitable is responsible for sending invitation emails.
|
|
4
|
+
# When an invitation is sent to an email address, an account is created for it.
|
|
5
|
+
# Invitation email contains a link allowing the user to accept the invitation
|
|
6
|
+
# by setting a password (as reset password from Devise's recoverable module).
|
|
6
7
|
#
|
|
7
8
|
# Configuration:
|
|
8
9
|
#
|
|
9
|
-
# invite_for:
|
|
10
|
-
#
|
|
11
|
-
#
|
|
10
|
+
# invite_for: The period the generated invitation token is valid, after
|
|
11
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
12
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
12
13
|
#
|
|
13
14
|
# Examples:
|
|
14
15
|
#
|
|
15
|
-
# User.find(1).invited?
|
|
16
|
-
# User.invite!(:email => 'someone@example.com')
|
|
17
|
-
# User.accept_invitation!(:invitation_token => '...')
|
|
18
|
-
# User.find(1).accept_invitation!
|
|
19
|
-
# User.find(1).invite!
|
|
16
|
+
# User.find(1).invited? # => true/false
|
|
17
|
+
# User.invite!(:email => 'someone@example.com') # => send invitation
|
|
18
|
+
# User.accept_invitation!(:invitation_token => '...') # => accept invitation with a token
|
|
19
|
+
# User.find(1).accept_invitation! # => accept invitation
|
|
20
|
+
# User.find(1).invite! # => reset invitation status and send invitation again
|
|
20
21
|
module Invitable
|
|
21
22
|
extend ActiveSupport::Concern
|
|
22
23
|
|
|
23
24
|
# Accept an invitation by clearing invitation token and confirming it if model
|
|
24
25
|
# is confirmable
|
|
25
26
|
def accept_invitation!
|
|
26
|
-
if self.invited?
|
|
27
|
+
if self.invited? && self.valid?
|
|
27
28
|
self.invitation_token = nil
|
|
28
29
|
self.save
|
|
29
30
|
end
|
|
@@ -34,26 +35,17 @@ module Devise
|
|
|
34
35
|
persisted? && invitation_token.present?
|
|
35
36
|
end
|
|
36
37
|
|
|
37
|
-
# Send invitation by email
|
|
38
|
-
def send_invitation
|
|
39
|
-
::Devise.mailer.invitation(self).deliver
|
|
40
|
-
end
|
|
41
|
-
|
|
42
38
|
# Reset invitation token and send invitation again
|
|
43
39
|
def invite!
|
|
44
40
|
if new_record? || invited?
|
|
45
|
-
self.skip_confirmation! if self.new_record?
|
|
46
|
-
generate_invitation_token
|
|
47
|
-
|
|
48
|
-
|
|
41
|
+
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
42
|
+
generate_invitation_token if self.invitation_token.nil?
|
|
43
|
+
self.invitation_sent_at = Time.now.utc
|
|
44
|
+
save(:validate => false)
|
|
45
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
49
46
|
end
|
|
50
47
|
end
|
|
51
48
|
|
|
52
|
-
def resend_invitation!
|
|
53
|
-
ActiveSupport::Deprecation.warn('resend_invitation! has been renamed to invite!')
|
|
54
|
-
self.invite!
|
|
55
|
-
end
|
|
56
|
-
|
|
57
49
|
# Verify whether a invitation is active or not. If the user has been
|
|
58
50
|
# invited, we need to calculate if the invitation time has not expired
|
|
59
51
|
# for this user, in other words, if the invitation is still valid.
|
|
@@ -89,8 +81,7 @@ module Devise
|
|
|
89
81
|
# Generates a new random token for invitation, and stores the time
|
|
90
82
|
# this token is being generated
|
|
91
83
|
def generate_invitation_token
|
|
92
|
-
self.invitation_token
|
|
93
|
-
self.invitation_sent_at = Time.now.utc
|
|
84
|
+
self.invitation_token = self.class.invitation_token
|
|
94
85
|
end
|
|
95
86
|
|
|
96
87
|
module ClassMethods
|
|
@@ -103,7 +94,7 @@ module Devise
|
|
|
103
94
|
invitable.attributes = attributes
|
|
104
95
|
|
|
105
96
|
if invitable.new_record?
|
|
106
|
-
invitable.errors.clear if invitable.email.match Devise.email_regexp
|
|
97
|
+
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
107
98
|
else
|
|
108
99
|
invitable.errors.add(:email, :taken) unless invitable.invited?
|
|
109
100
|
end
|
|
@@ -112,19 +103,14 @@ module Devise
|
|
|
112
103
|
invitable
|
|
113
104
|
end
|
|
114
105
|
|
|
115
|
-
def send_invitation(attributes = {})
|
|
116
|
-
ActiveSupport::Deprecation.warn('send_invitation has been renamed to invite!')
|
|
117
|
-
self.invite!(attributes)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
106
|
# Attempt to find a user by it's invitation_token to set it's password.
|
|
121
107
|
# If a user is found, reset it's password and automatically try saving
|
|
122
108
|
# the record. If not user is found, returns a new user containing an
|
|
123
109
|
# error in invitation_token attribute.
|
|
124
110
|
# Attributes must contain invitation_token, password and confirmation
|
|
125
111
|
def accept_invitation!(attributes={})
|
|
126
|
-
invitable = find_or_initialize_with_error_by(:invitation_token, attributes
|
|
127
|
-
invitable.errors.add(:invitation_token, :invalid) if
|
|
112
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
113
|
+
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
|
128
114
|
if invitable.errors.empty?
|
|
129
115
|
invitable.attributes = attributes
|
|
130
116
|
invitable.accept_invitation!
|
|
@@ -132,6 +118,11 @@ module Devise
|
|
|
132
118
|
invitable
|
|
133
119
|
end
|
|
134
120
|
|
|
121
|
+
# Generate a token checking if one does not already exist in the database.
|
|
122
|
+
def invitation_token
|
|
123
|
+
generate_token(:invitation_token)
|
|
124
|
+
end
|
|
125
|
+
|
|
135
126
|
Devise::Models.config(self, :invite_for)
|
|
136
127
|
end
|
|
137
128
|
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Models
|
|
3
|
+
# Invitable is responsible to send emails with invitations.
|
|
4
|
+
# When an invitation is sent to an email, an account is created for it.
|
|
5
|
+
# An invitation has a link to set the password, as reset password from recoverable.
|
|
6
|
+
#
|
|
7
|
+
# Configuration:
|
|
8
|
+
#
|
|
9
|
+
# invite_for: the time you want the user will have to confirm the account after
|
|
10
|
+
# is invited. When invite_for is zero, the invitation won't expire.
|
|
11
|
+
# By default invite_for is 0.
|
|
12
|
+
#
|
|
13
|
+
# Examples:
|
|
14
|
+
#
|
|
15
|
+
# User.find(1).invited? # true/false
|
|
16
|
+
# User.invite!(:email => 'someone@example.com') # send invitation
|
|
17
|
+
# User.accept_invitation!(:invitation_token => '...') # accept invitation with a token
|
|
18
|
+
# User.find(1).accept_invitation! # accept invitation
|
|
19
|
+
# User.find(1).invite! # reset invitation status and send invitation again
|
|
20
|
+
module Invitable
|
|
21
|
+
extend ActiveSupport::Concern
|
|
22
|
+
|
|
23
|
+
# Accept an invitation by clearing invitation token and confirming it if model
|
|
24
|
+
# is confirmable
|
|
25
|
+
def accept_invitation!
|
|
26
|
+
if self.invited?
|
|
27
|
+
self.invitation_token = nil
|
|
28
|
+
self.save
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Verifies whether a user has been invited or not
|
|
33
|
+
def invited?
|
|
34
|
+
persisted? && invitation_token.present?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Send invitation by email
|
|
38
|
+
def send_invitation
|
|
39
|
+
::Devise.mailer.invitation(self).deliver
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Reset invitation token and send invitation again
|
|
43
|
+
def invite!
|
|
44
|
+
if new_record? || invited?
|
|
45
|
+
self.skip_confirmation! if self.new_record? and self.respond_to? :skip_confirmation!
|
|
46
|
+
generate_invitation_token
|
|
47
|
+
save(:validate=>false)
|
|
48
|
+
send_invitation
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Verify whether a invitation is active or not. If the user has been
|
|
53
|
+
# invited, we need to calculate if the invitation time has not expired
|
|
54
|
+
# for this user, in other words, if the invitation is still valid.
|
|
55
|
+
def valid_invitation?
|
|
56
|
+
invited? && invitation_period_valid?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
protected
|
|
60
|
+
|
|
61
|
+
# Checks if the invitation for the user is within the limit time.
|
|
62
|
+
# We do this by calculating if the difference between today and the
|
|
63
|
+
# invitation sent date does not exceed the invite for time configured.
|
|
64
|
+
# Invite_for is a model configuration, must always be an integer value.
|
|
65
|
+
#
|
|
66
|
+
# Example:
|
|
67
|
+
#
|
|
68
|
+
# # invite_for = 1.day and invitation_sent_at = today
|
|
69
|
+
# invitation_period_valid? # returns true
|
|
70
|
+
#
|
|
71
|
+
# # invite_for = 5.days and invitation_sent_at = 4.days.ago
|
|
72
|
+
# invitation_period_valid? # returns true
|
|
73
|
+
#
|
|
74
|
+
# # invite_for = 5.days and invitation_sent_at = 5.days.ago
|
|
75
|
+
# invitation_period_valid? # returns false
|
|
76
|
+
#
|
|
77
|
+
# # invite_for = nil
|
|
78
|
+
# invitation_period_valid? # will always return true
|
|
79
|
+
#
|
|
80
|
+
def invitation_period_valid?
|
|
81
|
+
invitation_sent_at && (self.class.invite_for.to_i.zero? || invitation_sent_at.utc >= self.class.invite_for.ago)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Generates a new random token for invitation, and stores the time
|
|
85
|
+
# this token is being generated
|
|
86
|
+
def generate_invitation_token
|
|
87
|
+
self.invitation_token = Devise.friendly_token
|
|
88
|
+
self.invitation_sent_at = Time.now.utc
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
module ClassMethods
|
|
92
|
+
# Attempt to find a user by it's email. If a record is not found, create a new
|
|
93
|
+
# user and send invitation to it. If user is found, returns the user with an
|
|
94
|
+
# email already exists error.
|
|
95
|
+
# Attributes must contain the user email, other attributes will be set in the record
|
|
96
|
+
def invite!(attributes={})
|
|
97
|
+
invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email))
|
|
98
|
+
invitable.attributes = attributes
|
|
99
|
+
|
|
100
|
+
if invitable.new_record?
|
|
101
|
+
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
102
|
+
else
|
|
103
|
+
invitable.errors.add(:email, :taken) unless invitable.invited?
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
invitable.invite! if invitable.errors.empty?
|
|
107
|
+
invitable
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Attempt to find a user by it's invitation_token to set it's password.
|
|
111
|
+
# If a user is found, reset it's password and automatically try saving
|
|
112
|
+
# the record. If not user is found, returns a new user containing an
|
|
113
|
+
# error in invitation_token attribute.
|
|
114
|
+
# Attributes must contain invitation_token, password and confirmation
|
|
115
|
+
def accept_invitation!(attributes={})
|
|
116
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
117
|
+
invitable.errors.add(:invitation_token, :invalid) if attributes[:invitation_token] && !invitable.new_record? && !invitable.valid_invitation?
|
|
118
|
+
if invitable.errors.empty?
|
|
119
|
+
invitable.attributes = attributes
|
|
120
|
+
invitable.accept_invitation!
|
|
121
|
+
end
|
|
122
|
+
invitable
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
Devise::Models.config(self, :invite_for)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -2,9 +2,10 @@ 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
|
config.after_initialize do
|
|
8
|
+
require 'devise/mailer'
|
|
8
9
|
Devise::Mailer.send :include, DeviseInvitable::Mailer
|
|
9
10
|
end
|
|
10
11
|
|