devise_invitable 0.3.7 → 0.4.rc2
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 +49 -42
- data/app/controllers/devise/invitations_controller.rb +24 -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 +0 -5
- data/lib/devise_invitable/mailer.rb +8 -2
- data/lib/devise_invitable/model.rb +44 -39
- data/lib/devise_invitable/rails.rb +2 -5
- data/lib/devise_invitable/routes.rb +8 -5
- data/lib/devise_invitable/schema.rb +27 -2
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/devise_invitable.rb +16 -8
- data/lib/generators/active_record/templates/migration.rb +10 -3
- data/lib/generators/devise_invitable/devise_invitable_generator.rb +4 -0
- data/lib/generators/devise_invitable/install_generator.rb +7 -2
- data/lib/generators/mongoid/devise_invitable_generator.rb +8 -0
- metadata +28 -40
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.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
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,40 @@ 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 two 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
|
+
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
82
83
|
|
|
83
84
|
== Configuring views
|
|
84
85
|
|
|
@@ -96,31 +97,35 @@ Please refer to {Devise's README}[http://github.com/plataformatec/devise] for mo
|
|
|
96
97
|
|
|
97
98
|
=== Send an invitation
|
|
98
99
|
|
|
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.
|
|
100
|
+
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
101
|
|
|
102
|
-
User.invite(:email => "new_user@example.com", :name => "John Doe")
|
|
102
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe")
|
|
103
103
|
# => an invitation email will be sent to new_user@example.com
|
|
104
104
|
|
|
105
105
|
=== Accept an invitation
|
|
106
106
|
|
|
107
|
-
To accept an invitation with a token use the <tt>accept_invitation
|
|
107
|
+
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
108
|
|
|
109
|
-
User.accept_invitation(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
109
|
+
User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
|
|
110
110
|
|
|
111
111
|
== Integration in a Rails application
|
|
112
112
|
|
|
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
|
-
|
|
113
|
+
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.
|
|
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 (to a user in this example).
|
|
115
|
+
After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
|
|
116
|
+
|
|
117
|
+
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).
|
|
118
|
+
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.
|
|
119
|
+
|
|
120
|
+
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
121
|
|
|
117
122
|
== Controller filter
|
|
118
123
|
|
|
119
124
|
InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
|
|
120
125
|
|
|
121
|
-
Default behavior requires authentication of the same resource. For example, if your model User is
|
|
126
|
+
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
127
|
|
|
123
|
-
You would have a User model which is configured as invitable and an Admin model which is not. If you
|
|
128
|
+
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
129
|
|
|
125
130
|
class ApplicationController < ActionController::Base
|
|
126
131
|
protected
|
|
@@ -131,12 +136,13 @@ You would have a User model which is configured as invitable and an Admin model
|
|
|
131
136
|
|
|
132
137
|
== I18n
|
|
133
138
|
|
|
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:
|
|
139
|
+
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
140
|
|
|
136
141
|
en:
|
|
137
142
|
devise:
|
|
138
143
|
invitations:
|
|
139
|
-
send_instructions: 'An email
|
|
144
|
+
send_instructions: 'An invitation email has been sent to %{email}.'
|
|
145
|
+
invitation_token_invalid: 'The invitation token provided is not valid!'
|
|
140
146
|
updated: 'Your password was set successfully. You are now signed in.'
|
|
141
147
|
|
|
142
148
|
You can also create distinct messages based on the resource you've configured using the singular name given in routes:
|
|
@@ -145,17 +151,18 @@ You can also create distinct messages based on the resource you've configured us
|
|
|
145
151
|
devise:
|
|
146
152
|
invitations:
|
|
147
153
|
user:
|
|
148
|
-
send_instructions: 'A new user invitation has been sent.'
|
|
154
|
+
send_instructions: 'A new user invitation has been sent to %{email}.'
|
|
155
|
+
invitation_token_invalid: 'Your invitation token is not valid!'
|
|
149
156
|
updated: 'Welcome on board! You are now signed in.'
|
|
150
157
|
|
|
151
|
-
The DeviseInvitable mailer uses the
|
|
158
|
+
The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:
|
|
152
159
|
|
|
153
160
|
en:
|
|
154
161
|
devise:
|
|
155
162
|
mailer:
|
|
156
|
-
|
|
163
|
+
invitation_instructions:
|
|
157
164
|
subject: 'You got an invitation!'
|
|
158
|
-
user_subject: 'You got
|
|
165
|
+
user_subject: 'You got a user invitation!'
|
|
159
166
|
|
|
160
167
|
Take a look at the generated locale file (in <tt>config/locales/devise_invitable.en.yml</tt>) to check all available messages.
|
|
161
168
|
|
|
@@ -169,7 +176,7 @@ Check them all at:
|
|
|
169
176
|
|
|
170
177
|
http://github.com/scambra/devise_invitable/contributors
|
|
171
178
|
|
|
172
|
-
Special thanks to rymai[http://github.com/rymai] for
|
|
179
|
+
Special thanks to rymai[http://github.com/rymai] for the Rails 3 support, his fork was a great help.
|
|
173
180
|
|
|
174
181
|
== Note on Patches/Pull Requests
|
|
175
182
|
|
|
@@ -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,11 +14,15 @@ 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], authenticate_inviter!)
|
|
17
18
|
|
|
18
19
|
if resource.errors.empty?
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
if resource_class.invitation_limit.present?
|
|
21
|
+
current_user.invitation_limit ||= resource_class.invitation_limit
|
|
22
|
+
current_user.decrement!(:invitation_limit)
|
|
23
|
+
end
|
|
24
|
+
set_flash_message :notice, :send_instructions, :email => self.resource.email
|
|
25
|
+
redirect_to after_sign_in_path_for(resource_name)
|
|
21
26
|
else
|
|
22
27
|
render_with_scope :new
|
|
23
28
|
end
|
|
@@ -25,9 +30,12 @@ class Devise::InvitationsController < ApplicationController
|
|
|
25
30
|
|
|
26
31
|
# GET /resource/invitation/accept?invitation_token=abcdef
|
|
27
32
|
def edit
|
|
28
|
-
self.resource = resource_class.
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :invitation_token => params[:invitation_token] })
|
|
34
|
+
render_with_scope :edit
|
|
35
|
+
else
|
|
36
|
+
set_flash_message(:alert, :invitation_token_invalid)
|
|
37
|
+
redirect_to after_sign_out_path_for(resource_name)
|
|
38
|
+
end
|
|
31
39
|
end
|
|
32
40
|
|
|
33
41
|
# PUT /resource/invitation
|
|
@@ -41,4 +49,14 @@ class Devise::InvitationsController < ApplicationController
|
|
|
41
49
|
render_with_scope :edit
|
|
42
50
|
end
|
|
43
51
|
end
|
|
52
|
+
|
|
53
|
+
protected
|
|
54
|
+
|
|
55
|
+
def has_invitations_left?
|
|
56
|
+
if !current_user.has_invitations_left?
|
|
57
|
+
build_resource
|
|
58
|
+
set_flash_message :notice, :no_invitations_remaining
|
|
59
|
+
render_with_scope :new
|
|
60
|
+
end
|
|
61
|
+
end
|
|
44
62
|
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,10 +1,5 @@
|
|
|
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
4
|
send(:"authenticate_#{resource_name}!")
|
|
10
5
|
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,25 +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
|
|
|
24
|
+
included do
|
|
25
|
+
belongs_to :invited_by, :polymorphic => true
|
|
26
|
+
end
|
|
27
|
+
|
|
23
28
|
# Accept an invitation by clearing invitation token and confirming it if model
|
|
24
29
|
# is confirmable
|
|
25
30
|
def accept_invitation!
|
|
@@ -34,26 +39,30 @@ module Devise
|
|
|
34
39
|
persisted? && invitation_token.present?
|
|
35
40
|
end
|
|
36
41
|
|
|
37
|
-
#
|
|
38
|
-
def
|
|
39
|
-
|
|
42
|
+
# Return true if this user has invitations left to send
|
|
43
|
+
def has_invitations_left?
|
|
44
|
+
if self.class.invitation_limit.present?
|
|
45
|
+
if invitation_limit
|
|
46
|
+
return invitation_limit > 0
|
|
47
|
+
else
|
|
48
|
+
return self.class.invitation_limit > 0
|
|
49
|
+
end
|
|
50
|
+
else
|
|
51
|
+
return true
|
|
52
|
+
end
|
|
40
53
|
end
|
|
41
54
|
|
|
42
55
|
# Reset invitation token and send invitation again
|
|
43
56
|
def invite!
|
|
44
57
|
if new_record? || invited?
|
|
45
|
-
self.skip_confirmation! if self.new_record?
|
|
46
|
-
generate_invitation_token
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
59
|
+
generate_invitation_token if self.invitation_token.nil?
|
|
60
|
+
self.invitation_sent_at = Time.now.utc
|
|
61
|
+
save(:validate => false)
|
|
62
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
49
63
|
end
|
|
50
64
|
end
|
|
51
65
|
|
|
52
|
-
def resend_invitation!
|
|
53
|
-
ActiveSupport::Deprecation.warn('resend_invitation! has been renamed to invite!')
|
|
54
|
-
self.invite!
|
|
55
|
-
end
|
|
56
|
-
|
|
57
66
|
# Verify whether a invitation is active or not. If the user has been
|
|
58
67
|
# invited, we need to calculate if the invitation time has not expired
|
|
59
68
|
# for this user, in other words, if the invitation is still valid.
|
|
@@ -61,11 +70,6 @@ module Devise
|
|
|
61
70
|
invited? && invitation_period_valid?
|
|
62
71
|
end
|
|
63
72
|
|
|
64
|
-
# Only verify password when is not invited
|
|
65
|
-
def valid_password?(password)
|
|
66
|
-
super unless invited?
|
|
67
|
-
end
|
|
68
|
-
|
|
69
73
|
protected
|
|
70
74
|
|
|
71
75
|
# Checks if the invitation for the user is within the limit time.
|
|
@@ -94,8 +98,7 @@ module Devise
|
|
|
94
98
|
# Generates a new random token for invitation, and stores the time
|
|
95
99
|
# this token is being generated
|
|
96
100
|
def generate_invitation_token
|
|
97
|
-
self.invitation_token
|
|
98
|
-
self.invitation_sent_at = Time.now.utc
|
|
101
|
+
self.invitation_token = self.class.invitation_token
|
|
99
102
|
end
|
|
100
103
|
|
|
101
104
|
module ClassMethods
|
|
@@ -103,12 +106,13 @@ module Devise
|
|
|
103
106
|
# user and send invitation to it. If user is found, returns the user with an
|
|
104
107
|
# email already exists error.
|
|
105
108
|
# Attributes must contain the user email, other attributes will be set in the record
|
|
106
|
-
def invite!(attributes={})
|
|
109
|
+
def invite!(attributes={}, invited_by=nil)
|
|
107
110
|
invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email))
|
|
108
111
|
invitable.attributes = attributes
|
|
112
|
+
invitable.invited_by = invited_by
|
|
109
113
|
|
|
110
114
|
if invitable.new_record?
|
|
111
|
-
invitable.errors.clear if invitable.email.match Devise.email_regexp
|
|
115
|
+
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
112
116
|
else
|
|
113
117
|
invitable.errors.add(:email, :taken) unless invitable.invited?
|
|
114
118
|
end
|
|
@@ -117,19 +121,14 @@ module Devise
|
|
|
117
121
|
invitable
|
|
118
122
|
end
|
|
119
123
|
|
|
120
|
-
def send_invitation(attributes = {})
|
|
121
|
-
ActiveSupport::Deprecation.warn('send_invitation has been renamed to invite!')
|
|
122
|
-
self.invite!(attributes)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
124
|
# Attempt to find a user by it's invitation_token to set it's password.
|
|
126
125
|
# If a user is found, reset it's password and automatically try saving
|
|
127
126
|
# the record. If not user is found, returns a new user containing an
|
|
128
127
|
# error in invitation_token attribute.
|
|
129
128
|
# Attributes must contain invitation_token, password and confirmation
|
|
130
129
|
def accept_invitation!(attributes={})
|
|
131
|
-
invitable = find_or_initialize_with_error_by(:invitation_token, attributes
|
|
132
|
-
invitable.errors.add(:invitation_token, :invalid) if
|
|
130
|
+
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
|
131
|
+
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
|
133
132
|
if invitable.errors.empty?
|
|
134
133
|
invitable.attributes = attributes
|
|
135
134
|
invitable.accept_invitation!
|
|
@@ -137,7 +136,13 @@ module Devise
|
|
|
137
136
|
invitable
|
|
138
137
|
end
|
|
139
138
|
|
|
139
|
+
# Generate a token checking if one does not already exist in the database.
|
|
140
|
+
def invitation_token
|
|
141
|
+
generate_token(:invitation_token)
|
|
142
|
+
end
|
|
143
|
+
|
|
140
144
|
Devise::Models.config(self, :invite_for)
|
|
145
|
+
Devise::Models.config(self, :invitation_limit)
|
|
141
146
|
end
|
|
142
147
|
end
|
|
143
148
|
end
|
|
@@ -2,12 +2,9 @@ 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
|
-
|
|
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
|
|
7
|
+
config.after_initialize do
|
|
11
8
|
require 'devise/mailer'
|
|
12
9
|
Devise::Mailer.send :include, DeviseInvitable::Mailer
|
|
13
10
|
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
|
data/lib/devise_invitable.rb
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
require 'devise'
|
|
2
2
|
|
|
3
|
-
module Devise
|
|
4
|
-
# Time interval where the invitation token is valid.
|
|
5
|
-
mattr_accessor :invite_for
|
|
6
|
-
@@invite_for = 0
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
|
10
|
-
|
|
11
3
|
require 'devise_invitable/mailer'
|
|
12
4
|
require 'devise_invitable/routes'
|
|
13
5
|
require 'devise_invitable/schema'
|
|
14
6
|
require 'devise_invitable/controllers/url_helpers'
|
|
15
7
|
require 'devise_invitable/controllers/helpers'
|
|
16
8
|
require 'devise_invitable/rails'
|
|
9
|
+
|
|
10
|
+
module Devise
|
|
11
|
+
# The period the generated invitation token is valid.
|
|
12
|
+
mattr_accessor :invite_for
|
|
13
|
+
@@invite_for = 0
|
|
14
|
+
|
|
15
|
+
# Public: number of invitations the user is allowed to send
|
|
16
|
+
#
|
|
17
|
+
# Examples (in config/initializers/devise.rb)
|
|
18
|
+
#
|
|
19
|
+
# config.invitation_limit = nil
|
|
20
|
+
mattr_accessor :invitation_limit
|
|
21
|
+
@@invitation_limit = nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
|
|
2
2
|
def self.up
|
|
3
3
|
change_table :<%= table_name %> do |t|
|
|
4
|
-
t.string :invitation_token, :limit =>
|
|
4
|
+
t.string :invitation_token, :limit => 60
|
|
5
5
|
t.datetime :invitation_sent_at
|
|
6
|
+
t.integer :invitation_limit
|
|
7
|
+
t.integer :invited_by_id
|
|
6
8
|
t.index :invitation_token # for invitable
|
|
9
|
+
t.index :invited_by_id
|
|
7
10
|
end
|
|
8
11
|
|
|
9
12
|
# And allow null encrypted_password and password_salt:
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
change_column_null :<%= table_name %>, :encrypted_password, true
|
|
14
|
+
<% if class_name.constantize.columns_hash['password_salt'] -%>
|
|
15
|
+
change_column_null :<%= table_name %>, :password_salt, true
|
|
16
|
+
<% end -%>
|
|
12
17
|
end
|
|
13
18
|
|
|
14
19
|
def self.down
|
|
20
|
+
remove_column :<%= table_name %>, :invited_by_id
|
|
21
|
+
remove_column :<%= table_name %>, :invitation_limit
|
|
15
22
|
remove_column :<%= table_name %>, :invitation_sent_at
|
|
16
23
|
remove_column :<%= table_name %>, :invitation_token
|
|
17
24
|
end
|
|
@@ -5,6 +5,10 @@ module DeviseInvitable
|
|
|
5
5
|
|
|
6
6
|
desc "Add :invitable directive in the given model. Also generate migration for ActiveRecord"
|
|
7
7
|
|
|
8
|
+
# def devise_generate_model
|
|
9
|
+
# invoke "devise", [name]
|
|
10
|
+
# end
|
|
11
|
+
|
|
8
12
|
def inject_devise_invitable_content
|
|
9
13
|
path = File.join("app", "models", "#{file_path}.rb")
|
|
10
14
|
inject_into_file(path, "invitable, :", :after => "devise :") if File.exists?(path)
|
|
@@ -4,6 +4,10 @@ module DeviseInvitable
|
|
|
4
4
|
source_root File.expand_path("../../templates", __FILE__)
|
|
5
5
|
desc "Add DeviseInvitable config variables to the Devise initializer and copy DeviseInvitable locale files to your application."
|
|
6
6
|
|
|
7
|
+
# def devise_install
|
|
8
|
+
# invoke "devise:install"
|
|
9
|
+
# end
|
|
10
|
+
|
|
7
11
|
def add_config_options_to_initializer
|
|
8
12
|
devise_initializer_path = "config/initializers/devise.rb"
|
|
9
13
|
if File.exist?(devise_initializer_path)
|
|
@@ -15,8 +19,9 @@ module DeviseInvitable
|
|
|
15
19
|
inject_into_file(devise_initializer_path, :before => " # ==> Configuration for :confirmable\n") do
|
|
16
20
|
<<-CONTENT
|
|
17
21
|
# ==> Configuration for :invitable
|
|
18
|
-
#
|
|
19
|
-
#
|
|
22
|
+
# The period the generated invitation token is valid, after
|
|
23
|
+
# this period, the invited resource won't be able to accept the invitation.
|
|
24
|
+
# When invite_for is 0 (the default), the invitation won't expire.
|
|
20
25
|
# config.invite_for = 2.weeks
|
|
21
26
|
|
|
22
27
|
CONTENT
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: devise_invitable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
5
|
-
prerelease:
|
|
4
|
+
hash: 977940495
|
|
5
|
+
prerelease: true
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
version: 0.
|
|
8
|
+
- 4
|
|
9
|
+
- rc2
|
|
10
|
+
version: 0.4.rc2
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Sergio Cambra
|
|
@@ -15,39 +15,39 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-
|
|
18
|
+
date: 2011-02-14 00:00:00 +01:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies:
|
|
21
21
|
- !ruby/object:Gem::Dependency
|
|
22
|
-
name:
|
|
22
|
+
name: rspec-rails
|
|
23
23
|
prerelease: false
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
25
|
none: false
|
|
26
26
|
requirements:
|
|
27
|
-
- -
|
|
27
|
+
- - ~>
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
|
-
hash:
|
|
29
|
+
hash: 27
|
|
30
30
|
segments:
|
|
31
|
+
- 2
|
|
32
|
+
- 5
|
|
31
33
|
- 0
|
|
32
|
-
|
|
33
|
-
- 8
|
|
34
|
-
version: 0.9.8
|
|
34
|
+
version: 2.5.0
|
|
35
35
|
type: :development
|
|
36
36
|
version_requirements: *id001
|
|
37
37
|
- !ruby/object:Gem::Dependency
|
|
38
|
-
name:
|
|
38
|
+
name: bundler
|
|
39
39
|
prerelease: false
|
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
41
|
none: false
|
|
42
42
|
requirements:
|
|
43
|
-
- -
|
|
43
|
+
- - ~>
|
|
44
44
|
- !ruby/object:Gem::Version
|
|
45
|
-
hash:
|
|
45
|
+
hash: 25
|
|
46
46
|
segments:
|
|
47
|
+
- 1
|
|
47
48
|
- 0
|
|
48
|
-
-
|
|
49
|
-
|
|
50
|
-
version: 0.3.9
|
|
49
|
+
- 7
|
|
50
|
+
version: 1.0.7
|
|
51
51
|
type: :development
|
|
52
52
|
version_requirements: *id002
|
|
53
53
|
- !ruby/object:Gem::Dependency
|
|
@@ -64,38 +64,24 @@ dependencies:
|
|
|
64
64
|
- 0
|
|
65
65
|
- 0
|
|
66
66
|
version: 3.0.0
|
|
67
|
-
type: :
|
|
67
|
+
type: :runtime
|
|
68
68
|
version_requirements: *id003
|
|
69
|
-
- !ruby/object:Gem::Dependency
|
|
70
|
-
name: sqlite3-ruby
|
|
71
|
-
prerelease: false
|
|
72
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
|
73
|
-
none: false
|
|
74
|
-
requirements:
|
|
75
|
-
- - ">="
|
|
76
|
-
- !ruby/object:Gem::Version
|
|
77
|
-
hash: 3
|
|
78
|
-
segments:
|
|
79
|
-
- 0
|
|
80
|
-
version: "0"
|
|
81
|
-
type: :development
|
|
82
|
-
version_requirements: *id004
|
|
83
69
|
- !ruby/object:Gem::Dependency
|
|
84
70
|
name: devise
|
|
85
71
|
prerelease: false
|
|
86
|
-
requirement: &
|
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
|
87
73
|
none: false
|
|
88
74
|
requirements:
|
|
89
75
|
- - ~>
|
|
90
76
|
- !ruby/object:Gem::Version
|
|
91
|
-
hash:
|
|
77
|
+
hash: 7712074
|
|
92
78
|
segments:
|
|
93
79
|
- 1
|
|
94
|
-
-
|
|
95
|
-
-
|
|
96
|
-
version: 1.
|
|
80
|
+
- 2
|
|
81
|
+
- rc
|
|
82
|
+
version: 1.2.rc
|
|
97
83
|
type: :runtime
|
|
98
|
-
version_requirements: *
|
|
84
|
+
version_requirements: *id004
|
|
99
85
|
description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
|
|
100
86
|
email:
|
|
101
87
|
- sergio@entrecables.com
|
|
@@ -109,6 +95,7 @@ files:
|
|
|
109
95
|
- app/controllers/devise/invitations_controller.rb
|
|
110
96
|
- app/views/devise/invitations/edit.html.erb
|
|
111
97
|
- app/views/devise/invitations/new.html.erb
|
|
98
|
+
- app/views/devise/mailer/invitation_instructions.html.erb
|
|
112
99
|
- app/views/devise/mailer/invitation.html.erb
|
|
113
100
|
- config/locales/en.yml
|
|
114
101
|
- lib/devise_invitable.rb
|
|
@@ -125,6 +112,7 @@ files:
|
|
|
125
112
|
- lib/generators/devise_invitable/views_generator.rb
|
|
126
113
|
- lib/generators/devise_invitable/devise_invitable_generator.rb
|
|
127
114
|
- lib/generators/devise_invitable/install_generator.rb
|
|
115
|
+
- lib/generators/mongoid/devise_invitable_generator.rb
|
|
128
116
|
- LICENSE
|
|
129
117
|
- README.rdoc
|
|
130
118
|
has_rdoc: true
|
|
@@ -163,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
163
151
|
requirements: []
|
|
164
152
|
|
|
165
153
|
rubyforge_project:
|
|
166
|
-
rubygems_version: 1.
|
|
154
|
+
rubygems_version: 1.3.7
|
|
167
155
|
signing_key:
|
|
168
156
|
specification_version: 3
|
|
169
157
|
summary: An invitation strategy for Devise
|