devise_invitable 0.4.rc → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@ It adds support to devise[http://github.com/plataformatec/devise] for send invit
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
- == Installation for Rails ~> 3.0 and Devise ~> 1.1
7
+ == Installation for Rails ~> 3.0 and Devise ~> 1.2
8
8
 
9
9
  Install DeviseInvitable gem, it will also install dependencies (such as devise and warden):
10
10
 
@@ -12,8 +12,8 @@ Install DeviseInvitable gem, it will also install dependencies (such as devise a
12
12
 
13
13
  Add DeviseInvitable to your Gemfile (and Devise if you weren't using them):
14
14
 
15
- gem 'devise', '~> 1.1.3'
16
- gem 'devise_invitable', '~> 0.3.4'
15
+ gem 'devise', '~> 1.2.0'
16
+ gem 'devise_invitable', '~> 0.4.0'
17
17
 
18
18
  === Automatic installation
19
19
 
@@ -51,6 +51,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
51
51
  change_table :users do |t|
52
52
  t.string :invitation_token, :limit => 60
53
53
  t.datetime :invitation_sent_at
54
+ t.datetime :invitation_accepted_at
54
55
  t.index :invitation_token
55
56
  end
56
57
 
@@ -61,7 +62,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
61
62
 
62
63
  == Model configuration
63
64
 
64
- DeviseInvitable adds a new configuration option:
65
+ DeviseInvitable adds some new configuration options:
65
66
 
66
67
  * invite_for: The period the generated invitation token is valid, after this period, the invited resource won't be able to accept the invitation. When invite_for is 0 (the default), the invitation won't expire.
67
68
 
@@ -77,6 +78,14 @@ or directly as parameters to the <tt>devise</tt> method:
77
78
 
78
79
  devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
79
80
 
81
+ * 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.
82
+
83
+ * 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.
84
+
85
+ * validate_on_invite: force a record to be valid before being actually invited.
86
+
87
+ * resend_invitation: resend invitation if user with invited status is invited again. Enabled by default.
88
+
80
89
  For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
81
90
 
82
91
  == Configuring views
@@ -91,6 +100,28 @@ You can also use the generator to generate scoped views:
91
100
 
92
101
  Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
93
102
 
103
+ == Configuring controllers
104
+
105
+ To change the controller's behavior, create a controller that inherits from <tt>Devise::InvitationsController</tt>. The available methods are: new, create, edit, and update. You should read the {original controllers source}[https://raw.github.com/scambra/devise_invitable/master/app/controllers/devise/invitations_controller.rb] before editing any of these actions. Your controller might now look something like this:
106
+
107
+ class Users::InvitationsController < Devise::InvitationsController
108
+ def update
109
+ if this
110
+ redirect_to root_path
111
+ else
112
+ super
113
+ end
114
+ end
115
+ end
116
+
117
+ Now just tell Devise that you want to use your controller, the controller above is 'users/invitations', so our routes.rb would have this line:
118
+
119
+ devise_for :users, :controllers => { :invitations => 'users/invitations' }
120
+
121
+ be sure that you generate the views and put them into the controller that you generated, so for this example it would be:
122
+
123
+ rails generate devise_invitable:views users/invitations
124
+
94
125
  == Usage
95
126
 
96
127
  === Send an invitation
@@ -100,20 +131,48 @@ To send an invitation to a user, use the <tt>invite!</tt> class method. <tt>:ema
100
131
  User.invite!(:email => "new_user@example.com", :name => "John Doe")
101
132
  # => an invitation email will be sent to new_user@example.com
102
133
 
134
+ If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to true.
135
+
136
+ User.invite!(:email => "new_user@example.com", :name => "John Doe") do |u|
137
+ u.skip_invitation = true
138
+ end
139
+ # => the record will be created, but the invitation email will not be sent
140
+
141
+ You can add :skip_invitation to attributes hash if skip_invitation is added to attr_accessible.
142
+
143
+ User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
144
+ # => the record will be created, but the invitation email will not be sent
145
+
103
146
  === Accept an invitation
104
147
 
105
148
  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.
106
149
 
107
150
  User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
108
151
 
152
+ === Callbacks
153
+
154
+ A callback event is fired before and after an invitation is accepted (User#accept_invitation!). For example, in your resource model you can add:
155
+
156
+ after_invitation_accepted :email_invited_by
157
+
158
+ def email_invited_by
159
+ # ...
160
+ end
161
+
162
+ The callbacks support all options and arguments available to the standard callbacks provided by AR.
163
+
109
164
  == Integration in a Rails application
110
165
 
111
166
  Since the invitations controller take care of all the creation/acceptation of an invitation, in most cases you wouldn't call the <tt>invite!</tt> and <tt>accept_invitation!</tt> methods directly.
112
167
  Instead, in your views, put a link to <tt>new_user_invitation_path</tt> or <tt>new_invitation_path(:user)</tt> or even <tt>/users/invitation/new</tt> to prepare and send an invitation (to a user in this example).
113
- After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).
168
+
169
+ After an invitation is created and sent, the inviter will be redirected to after_invite_path_for(resource_name), which is stored path or the same path as after_sign_in_path_for by default.
170
+
171
+ After an invitation is accepted, the invitee will be redirected to after_accept_path_for(resource), which is the same path as after_sign_in_path_for by default. If you want to override the path, override invitations controller and define after_accept_path_for method. This is useful in the common case that a user is invited to a specific location in your application. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
114
172
 
115
173
  The invitation email includes a link to accept the invitation that looks like this: <tt>/users/invitation/accept?invitation_token=abcd123</tt>. When clicked, the invited must set a password in order to accept its invitation. Note that if the invitation_token is not present or not valid, the invited is redirected to after_sign_out_path_for(resource_name).
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.
174
+
175
+ 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.
117
176
 
118
177
  == Controller filter
119
178
 
@@ -126,10 +185,29 @@ You would have a User model which is configured as invitable and an Admin model
126
185
  class ApplicationController < ActionController::Base
127
186
  protected
128
187
  def authenticate_inviter!
129
- authenticate_admin!
188
+ authenticate_admin!(:force => true)
130
189
  end
131
190
  end
132
191
 
192
+ And include DeviseInvitable::Inviter module into Admin model:
193
+
194
+ class Admin < ActiveRecord::Base
195
+ devise :database_authenticatable, :validatable
196
+ include DeviseInvitable::Inviter
197
+ end
198
+
199
+ == Has many invitations
200
+
201
+ If you want to get all records invited by a resource, you should define has_many association in the model allowed to send invitations.
202
+
203
+ For the default behavior, define it like this:
204
+
205
+ has_many :invitations, :class_name => self.class.to_s, :as => :invited_by
206
+
207
+ For the previous example, where admins send invitations to users, define it like this:
208
+
209
+ has_many :invitations, :class_name => 'User', :as => :invited_by
210
+
133
211
  == I18n
134
212
 
135
213
  DeviseInvitable uses flash messages with I18n with the flash keys <tt>:send_instructions</tt>, <tt>:invitation_token_invalid</tt> and <tt>:updated</tt>. To customize your app, you can modify the generated locale file:
@@ -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,19 +14,19 @@ class Devise::InvitationsController < ApplicationController
13
14
 
14
15
  # POST /resource/invitation
15
16
  def create
16
- self.resource = resource_class.invite!(params[resource_name])
17
+ self.resource = resource_class.invite!(params[resource_name], current_inviter)
17
18
 
18
19
  if resource.errors.empty?
19
20
  set_flash_message :notice, :send_instructions, :email => self.resource.email
20
- redirect_to after_sign_in_path_for(resource_name)
21
+ respond_with resource, :location => after_invite_path_for(resource)
21
22
  else
22
- render_with_scope :new
23
+ respond_with_navigational(resource) { render_with_scope :new }
23
24
  end
24
25
  end
25
26
 
26
27
  # GET /resource/invitation/accept?invitation_token=abcdef
27
28
  def edit
28
- if params[:invitation_token] && self.resource = resource_class.first(:conditions => { :invitation_token => params[:invitation_token] })
29
+ if params[:invitation_token] && self.resource = resource_class.to_adapter.find_first( :invitation_token => params[:invitation_token] )
29
30
  render_with_scope :edit
30
31
  else
31
32
  set_flash_message(:alert, :invitation_token_invalid)
@@ -39,9 +40,32 @@ class Devise::InvitationsController < ApplicationController
39
40
 
40
41
  if resource.errors.empty?
41
42
  set_flash_message :notice, :updated
42
- sign_in_and_redirect(resource_name, resource)
43
+ sign_in(resource_name, resource)
44
+ respond_with resource, :location => after_accept_path_for(resource)
43
45
  else
44
- render_with_scope :edit
46
+ respond_with_navigational(resource){ render_with_scope :edit }
47
+ end
48
+ end
49
+
50
+ protected
51
+ def current_inviter
52
+ @current_inviter ||= authenticate_inviter!
53
+ end
54
+
55
+ def has_invitations_left?
56
+ unless current_inviter.nil? || current_inviter.has_invitations_left?
57
+ build_resource
58
+ set_flash_message :alert, :no_invitations_remaining
59
+ respond_with_navigational(resource) { render_with_scope :new }
45
60
  end
46
61
  end
62
+
63
+ def after_invite_path_for(resource)
64
+ after_sign_in_path_for(resource)
65
+ end
66
+
67
+ def after_accept_path_for(resource)
68
+ after_sign_in_path_for(resource)
69
+ end
47
70
  end
71
+
@@ -1,6 +1,6 @@
1
1
  <h2>Send invitation</h2>
2
2
 
3
- <%= form_for resource, :as => resource_name, :url => invitation_path(resource_name) do |f| %>
3
+ <%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
4
4
  <%= devise_error_messages! %>
5
5
 
6
6
  <p><%= f.label :email %><br />
@@ -4,6 +4,7 @@ en:
4
4
  send_instructions: 'An invitation email has been sent to %{email}.'
5
5
  invitation_token_invalid: 'The invitation token provided is not valid!'
6
6
  updated: 'Your password was set successfully. You are now signed in.'
7
- mailer:
8
- invitation_instructions:
9
- subject: 'Invitation instructions'
7
+ no_invitations_remaining: "No invitations remaining"
8
+ mailer:
9
+ invitation_instructions:
10
+ subject: 'Invitation instructions'
@@ -1,7 +1,7 @@
1
1
  module DeviseInvitable::Controllers::Helpers
2
2
  protected
3
3
  def authenticate_inviter!
4
- send(:"authenticate_#{resource_name}!")
4
+ send(:"authenticate_#{resource_name}!", :force => true)
5
5
  end
6
6
  end
7
7
  ActionController::Base.send :include, DeviseInvitable::Controllers::Helpers
@@ -0,0 +1,39 @@
1
+ module DeviseInvitable
2
+ module Inviter
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ extend ClassMethods
7
+ attr_writer :invitation_limit unless respond_to? :invitation_limit
8
+ end
9
+
10
+ def invitation_limit
11
+ self[:invitation_limit] || self.class.invitation_limit
12
+ end
13
+
14
+ # Return true if this user has invitations left to send
15
+ def has_invitations_left?
16
+ if self.class.invitation_limit.present?
17
+ if invitation_limit
18
+ return invitation_limit > 0
19
+ else
20
+ return self.class.invitation_limit > 0
21
+ end
22
+ else
23
+ return true
24
+ end
25
+ end
26
+
27
+ protected
28
+ def decrement_invitation_limit!
29
+ if self.class.invitation_limit.present?
30
+ self.invitation_limit ||= self.class.invitation_limit
31
+ self.update_attribute(:invitation_limit, invitation_limit - 1)
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ Devise::Models.config(self, :invitation_limit)
37
+ end
38
+ end
39
+ end
@@ -3,12 +3,7 @@ module DeviseInvitable
3
3
 
4
4
  # Deliver an invitation email
5
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)
6
+ devise_mail(record, :invitation_instructions)
12
7
  end
13
8
  end
14
9
  end
@@ -21,12 +21,27 @@ module Devise
21
21
  module Invitable
22
22
  extend ActiveSupport::Concern
23
23
 
24
- # Accept an invitation by clearing invitation token and confirming it if model
25
- # is confirmable
24
+ attr_accessor :skip_invitation
25
+
26
+ included do
27
+ include ::DeviseInvitable::Inviter
28
+ belongs_to :invited_by, :polymorphic => true
29
+
30
+ include ActiveSupport::Callbacks
31
+ define_callbacks :invitation_accepted
32
+
33
+ attr_writer :skip_password
34
+ end
35
+
36
+ # Accept an invitation by clearing invitation token and and setting invitation_accepted_at
37
+ # Confirms it if model is confirmable
26
38
  def accept_invitation!
27
39
  if self.invited? && self.valid?
28
- self.invitation_token = nil
29
- self.save
40
+ run_callbacks :invitation_accepted do
41
+ self.invitation_token = nil
42
+ self.invitation_accepted_at = Time.now.utc if respond_to? :"invitation_accepted_at="
43
+ self.save(:validate => false)
44
+ end
30
45
  end
31
46
  end
32
47
 
@@ -37,12 +52,13 @@ module Devise
37
52
 
38
53
  # Reset invitation token and send invitation again
39
54
  def invite!
40
- if new_record? || invited?
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
55
+ was_invited = invited?
56
+ self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
57
+ generate_invitation_token if self.invitation_token.nil?
58
+ self.invitation_sent_at = Time.now.utc
59
+ if save(:validate => false)
60
+ self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
61
+ deliver_invitation unless @skip_invitation
46
62
  end
47
63
  end
48
64
 
@@ -53,7 +69,26 @@ module Devise
53
69
  invited? && invitation_period_valid?
54
70
  end
55
71
 
72
+ # Only verify password when is not invited
73
+ def valid_password?(password)
74
+ super unless invited?
75
+ end
76
+
77
+ def reset_password!(new_password, new_password_confirmation)
78
+ super
79
+ accept_invitation!
80
+ end
81
+
56
82
  protected
83
+ # Overriding the method in Devise's :validatable module so password is not required on inviting
84
+ def password_required?
85
+ !@skip_password && super
86
+ end
87
+
88
+ # Deliver the invitation email
89
+ def deliver_invitation
90
+ ::Devise.mailer.invitation_instructions(self).deliver
91
+ end
57
92
 
58
93
  # Checks if the invitation for the user is within the limit time.
59
94
  # We do this by calculating if the difference between today and the
@@ -88,21 +123,45 @@ module Devise
88
123
  # Attempt to find a user by it's email. If a record is not found, create a new
89
124
  # user and send invitation to it. If user is found, returns the user with an
90
125
  # email already exists error.
126
+ # If user is found and still have pending invitation, email is resend unless
127
+ # resend_invitation is set to false
91
128
  # Attributes must contain the user email, other attributes will be set in the record
92
- def invite!(attributes={})
93
- invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email))
94
- invitable.attributes = attributes
129
+ def _invite(attributes={}, invited_by=nil, &block)
130
+ invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
131
+ invitable.assign_attributes(attributes, :as => inviter_role(invited_by))
132
+ invitable.invited_by = invited_by
95
133
 
134
+ invitable.skip_password = true
135
+ invitable.valid? if self.validate_on_invite
96
136
  if invitable.new_record?
97
- invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
137
+ invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
98
138
  else
99
- invitable.errors.add(:email, :taken) unless invitable.invited?
139
+ invitable.errors.add(invite_key, :taken) unless invitable.invited? && self.resend_invitation
140
+ end
141
+
142
+ if invitable.errors.empty?
143
+ yield invitable if block_given?
144
+ mail = invitable.invite!
100
145
  end
146
+ [invitable, mail]
147
+ end
148
+
149
+ # Override this method if the invitable is using Mass Assignment Security
150
+ # and the inviter has a non-default role.
151
+ def inviter_role(inviter)
152
+ :default
153
+ end
101
154
 
102
- invitable.invite! if invitable.errors.empty?
155
+ def invite!(attributes={}, invited_by=nil, &block)
156
+ invitable, mail = _invite(attributes, invited_by, &block)
103
157
  invitable
104
158
  end
105
159
 
160
+ def invite_mail!(attributes={}, invited_by=nil, &block)
161
+ invitable, mail = _invite(attributes, invited_by, &block)
162
+ mail
163
+ end
164
+
106
165
  # Attempt to find a user by it's invitation_token to set it's password.
107
166
  # If a user is found, reset it's password and automatically try saving
108
167
  # the record. If not user is found, returns a new user containing an
@@ -122,9 +181,24 @@ module Devise
122
181
  def invitation_token
123
182
  generate_token(:invitation_token)
124
183
  end
184
+
185
+ # Callback convenience methods
186
+ def before_invitation_accepted(*args, &blk)
187
+ set_callback(:invitation_accepted, :before, *args, &blk)
188
+ end
189
+
190
+ def after_invitation_accepted(*args, &blk)
191
+ set_callback(:invitation_accepted, :after, *args, &blk)
192
+ end
193
+
125
194
 
126
195
  Devise::Models.config(self, :invite_for)
196
+ Devise::Models.config(self, :validate_on_invite)
197
+ Devise::Models.config(self, :invitation_limit)
198
+ Devise::Models.config(self, :invite_key)
199
+ Devise::Models.config(self, :resend_invitation)
127
200
  end
128
201
  end
129
202
  end
130
203
  end
204
+
@@ -1,31 +1,47 @@
1
1
  module Devise
2
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.
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: 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.
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? # 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
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
- # Accept an invitation by clearing invitation token and confirming it if model
24
- # is confirmable
24
+ attr_accessor :skip_invitation
25
+
26
+ included do
27
+ include ::DeviseInvitable::Inviter
28
+ belongs_to :invited_by, :polymorphic => true
29
+
30
+ include ActiveSupport::Callbacks
31
+ define_callbacks :invitation_accepted
32
+
33
+ attr_writer :skip_password
34
+ end
35
+
36
+ # Accept an invitation by clearing invitation token and and setting invitation_accepted_at
37
+ # Confirms it if model is confirmable
25
38
  def accept_invitation!
26
- if self.invited?
27
- self.invitation_token = nil
28
- self.save
39
+ if self.invited? && self.valid?
40
+ run_callbacks :invitation_accepted do
41
+ self.invitation_token = nil
42
+ self.invitation_accepted_at = Time.now.utc if respond_to? :"invitation_accepted_at="
43
+ self.save(:validate => false)
44
+ end
29
45
  end
30
46
  end
31
47
 
@@ -34,18 +50,15 @@ module Devise
34
50
  persisted? && invitation_token.present?
35
51
  end
36
52
 
37
- # Send invitation by email
38
- def send_invitation
39
- ::Devise.mailer.invitation(self).deliver
40
- end
41
-
42
53
  # Reset invitation token and send invitation again
43
54
  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
55
+ was_invited = invited?
56
+ self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
57
+ generate_invitation_token if self.invitation_token.nil?
58
+ self.invitation_sent_at = Time.now.utc
59
+ if save(:validate => false)
60
+ self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
61
+ deliver_invitation unless @skip_invitation
49
62
  end
50
63
  end
51
64
 
@@ -56,7 +69,32 @@ module Devise
56
69
  invited? && invitation_period_valid?
57
70
  end
58
71
 
72
+ # Only verify password when is not invited
73
+ def valid_password?(password)
74
+ super unless invited?
75
+ end
76
+ =begin
77
+ def reset_password!(new_password, new_password_confirmation)
78
+ super
79
+ accept_invitation!
80
+ end
81
+ =end
59
82
  protected
83
+ # Overriding the method in Devise's :validatable module so password is not required on inviting
84
+ def password_required?
85
+ !@skip_password && super
86
+ end
87
+
88
+ # Deliver the invitation email
89
+ def deliver_invitation
90
+ ::Devise.mailer.invitation_instructions(self).deliver
91
+ end
92
+
93
+ # Clear invitation token when reset password token is cleared too
94
+ def clear_reset_password_token
95
+ self.invitation_token = nil if invited?
96
+ super
97
+ end
60
98
 
61
99
  # Checks if the invitation for the user is within the limit time.
62
100
  # We do this by calculating if the difference between today and the
@@ -84,29 +122,52 @@ module Devise
84
122
  # Generates a new random token for invitation, and stores the time
85
123
  # this token is being generated
86
124
  def generate_invitation_token
87
- self.invitation_token = Devise.friendly_token
88
- self.invitation_sent_at = Time.now.utc
125
+ self.invitation_token = self.class.invitation_token
89
126
  end
90
127
 
91
128
  module ClassMethods
92
129
  # Attempt to find a user by it's email. If a record is not found, create a new
93
130
  # user and send invitation to it. If user is found, returns the user with an
94
131
  # email already exists error.
132
+ # If user is found and still have pending invitation, email is resend unless
133
+ # resend_invitation is set to false
95
134
  # 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
135
+ def _invite(attributes={}, invited_by=nil, &block)
136
+ invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
137
+ invitable.assign_attributes(attributes, :as => inviter_role(invited_by))
138
+ invitable.invited_by = invited_by
99
139
 
140
+ invitable.skip_password = true
141
+ invitable.valid? if self.validate_on_invite
100
142
  if invitable.new_record?
101
- invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
143
+ invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
102
144
  else
103
- invitable.errors.add(:email, :taken) unless invitable.invited?
145
+ invitable.errors.add(invite_key, :taken) unless invitable.invited? && self.resend_invitation
104
146
  end
105
147
 
106
- invitable.invite! if invitable.errors.empty?
148
+ if invitable.errors.empty?
149
+ yield invitable if block_given?
150
+ mail = invitable.invite!
151
+ end
152
+ [invitable, mail]
153
+ end
154
+
155
+ # Override this method if the invitable is using Mass Assignment Security
156
+ # and the inviter has a non-default role.
157
+ def inviter_role(inviter)
158
+ :default
159
+ end
160
+
161
+ def invite!(attributes={}, invited_by=nil, &block)
162
+ invitable, mail = _invite(attributes, invited_by, &block)
107
163
  invitable
108
164
  end
109
165
 
166
+ def invite_mail!(attributes={}, invited_by=nil, &block)
167
+ invitable, mail = _invite(attributes, invited_by, &block)
168
+ mail
169
+ end
170
+
110
171
  # Attempt to find a user by it's invitation_token to set it's password.
111
172
  # If a user is found, reset it's password and automatically try saving
112
173
  # the record. If not user is found, returns a new user containing an
@@ -114,7 +175,7 @@ module Devise
114
175
  # Attributes must contain invitation_token, password and confirmation
115
176
  def accept_invitation!(attributes={})
116
177
  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?
178
+ invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
118
179
  if invitable.errors.empty?
119
180
  invitable.attributes = attributes
120
181
  invitable.accept_invitation!
@@ -122,8 +183,28 @@ module Devise
122
183
  invitable
123
184
  end
124
185
 
186
+ # Generate a token checking if one does not already exist in the database.
187
+ def invitation_token
188
+ generate_token(:invitation_token)
189
+ end
190
+
191
+ # Callback convenience methods
192
+ def before_invitation_accepted(*args, &blk)
193
+ set_callback(:invitation_accepted, :before, *args, &blk)
194
+ end
195
+
196
+ def after_invitation_accepted(*args, &blk)
197
+ set_callback(:invitation_accepted, :after, *args, &blk)
198
+ end
199
+
200
+
125
201
  Devise::Models.config(self, :invite_for)
202
+ Devise::Models.config(self, :validate_on_invite)
203
+ Devise::Models.config(self, :invitation_limit)
204
+ Devise::Models.config(self, :invite_key)
205
+ Devise::Models.config(self, :resend_invitation)
126
206
  end
127
207
  end
128
208
  end
129
209
  end
210
+
@@ -4,7 +4,10 @@ module DeviseInvitable
4
4
  ActiveSupport.on_load(:action_controller) { include DeviseInvitable::Controllers::UrlHelpers }
5
5
  ActiveSupport.on_load(:action_view) { include DeviseInvitable::Controllers::UrlHelpers }
6
6
 
7
- config.after_initialize do
7
+ # We use to_prepare instead of after_initialize here because Devise is a Rails engine; its
8
+ # mailer is reloaded like the rest of the user's app. Got to make sure that our mailer methods
9
+ # are included each time Devise::Mailer is (re)loaded.
10
+ config.to_prepare do
8
11
  require 'devise/mailer'
9
12
  Devise::Mailer.send :include, DeviseInvitable::Mailer
10
13
  end
@@ -16,6 +16,7 @@ module DeviseInvitable
16
16
  # change_table :the_resources do |t|
17
17
  # t.string :invitation_token, :limit => 60
18
18
  # t.datetime :invitation_sent_at
19
+ # t.datetime :invitation_accepted_at
19
20
  # t.index :invitation_token # for invitable
20
21
  # end
21
22
  #
@@ -26,6 +27,10 @@ module DeviseInvitable
26
27
  def invitable
27
28
  apply_devise_schema :invitation_token, String, :limit => 60
28
29
  apply_devise_schema :invitation_sent_at, DateTime
30
+ apply_devise_schema :invitation_accepted_at, DateTime
31
+ apply_devise_schema :invitation_limit, Integer
32
+ apply_devise_schema :invited_by_id, Integer
33
+ apply_devise_schema :invited_by_type, String
29
34
  end
30
35
  end
31
36
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseInvitable
2
- VERSION = '0.4.rc'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -1,5 +1,9 @@
1
1
  require 'devise'
2
2
 
3
+ module DeviseInvitable
4
+ autoload :Inviter, 'devise_invitable/inviter'
5
+ end
6
+
3
7
  require 'devise_invitable/mailer'
4
8
  require 'devise_invitable/routes'
5
9
  require 'devise_invitable/schema'
@@ -8,9 +12,47 @@ require 'devise_invitable/controllers/helpers'
8
12
  require 'devise_invitable/rails'
9
13
 
10
14
  module Devise
11
- # The period the generated invitation token is valid.
15
+ # Public: Validity period of the invitation token (default: 0). If
16
+ # invite_for is 0 or nil, the invitation will never expire.
17
+ # Set invite_for in the Devise configuration file (in config/initializers/devise.rb).
18
+ #
19
+ # config.invite_for = 2.weeks # => The invitation token will be valid 2 weeks
12
20
  mattr_accessor :invite_for
13
21
  @@invite_for = 0
22
+
23
+ # Public: Flag that force a record to be valid before being actually invited
24
+ # (default: false).
25
+ #
26
+ # Examples (in config/initializers/devise.rb)
27
+ #
28
+ # config.validate_on_invite = true
29
+ mattr_accessor :validate_on_invite
30
+ @@validate_on_invite = false
31
+
32
+ # Public: number of invitations the user is allowed to send
33
+ #
34
+ # Examples (in config/initializers/devise.rb)
35
+ #
36
+ # config.invitation_limit = nil
37
+ mattr_accessor :invitation_limit
38
+ @@invitation_limit = nil
39
+
40
+ # Public: The key to be used to check existing users when sending an invitation
41
+ #
42
+ # Examples (in config/initializers/devise.rb)
43
+ #
44
+ # config.invite_key = :email
45
+ mattr_accessor :invite_key
46
+ @@invite_key = :email
47
+
48
+ # Public: Resend invitation if user with invited status is invited again
49
+ # (default: true)
50
+ #
51
+ # Example (in config/initializers/devise.rb)
52
+ #
53
+ # config.resend_invitation = false
54
+ mattr_accessor :resend_invitation
55
+ @@resend_invitation = true
14
56
  end
15
57
 
16
58
  Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
@@ -1,9 +1,13 @@
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 => 60
5
- t.datetime :invitation_sent_at
6
- t.index :invitation_token # for invitable
4
+ t.string :invitation_token, :limit => 60
5
+ t.datetime :invitation_sent_at
6
+ t.datetime :invitation_accepted_at
7
+ t.integer :invitation_limit
8
+ t.references :invited_by, :polymorphic => true
9
+ t.index :invitation_token # for invitable
10
+ t.index :invited_by_id
7
11
  end
8
12
 
9
13
  # And allow null encrypted_password and password_salt:
@@ -14,7 +18,9 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
14
18
  end
15
19
 
16
20
  def self.down
17
- remove_column :<%= table_name %>, :invitation_sent_at
18
- remove_column :<%= table_name %>, :invitation_token
21
+ change_table :<%= table_name %> do |t|
22
+ t.remove_references :invited_by, :polymorphic => true
23
+ t.remove :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token
24
+ end
19
25
  end
20
26
  end
@@ -24,6 +24,20 @@ module DeviseInvitable
24
24
  # When invite_for is 0 (the default), the invitation won't expire.
25
25
  # config.invite_for = 2.weeks
26
26
 
27
+ # Number of invitations users can send.
28
+ # If invitation_limit is nil, users can send unlimited invitations.
29
+ # If invitation_limit is 0, users can't send invitations.
30
+ # If invitation_limit n > 0, users can send n invitations.
31
+ # Default: nil
32
+ # config.invitation_limit = 5
33
+
34
+ # The key to be used to check existing users when sending an invitation
35
+ # config.invite_key = :email
36
+
37
+ # Flag that force a record to be valid before being actually invited
38
+ # Default: false
39
+ # config.validate_on_invite = true
40
+
27
41
  CONTENT
28
42
  end
29
43
  end
@@ -2,9 +2,18 @@ require 'generators/devise/views_generator'
2
2
 
3
3
  module DeviseInvitable
4
4
  module Generators
5
- class ViewsGenerator < Devise::Generators::ViewsGenerator
6
- source_root File.expand_path("../../../../app/views", __FILE__)
5
+ class ViewsGenerator < Rails::Generators::Base
7
6
  desc 'Copies all DeviseInvitable views to your application.'
7
+
8
+ argument :scope, :required => false, :default => nil,
9
+ :desc => "The scope to copy views to"
10
+
11
+ include ::Devise::Generators::ViewPathTemplates
12
+ source_root File.expand_path("../../../../app/views/devise", __FILE__)
13
+ def copy_views
14
+ view_directory :invitations
15
+ view_directory :mailer
16
+ end
8
17
  end
9
18
  end
10
19
  end
@@ -0,0 +1,8 @@
1
+ require 'generators/devise/orm_helpers'
2
+
3
+ module Mongoid
4
+ module Generators
5
+ class DeviseInvitableGenerator < Rails::Generators::NamedBase
6
+ end
7
+ end
8
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_invitable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7712090
5
- prerelease: true
4
+ hash: 5
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - rc
10
- version: 0.4.rc
8
+ - 6
9
+ - 1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergio Cambra
@@ -15,91 +15,63 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-06 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-12-27 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: rspec-rails
21
+ name: bundler
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 11
28
+ hash: 25
30
29
  segments:
31
- - 2
32
30
  - 1
33
31
  - 0
34
- version: 2.1.0
32
+ - 7
33
+ version: 1.0.7
35
34
  type: :development
36
35
  version_requirements: *id001
37
36
  - !ruby/object:Gem::Dependency
38
- name: steak
37
+ name: rails
39
38
  prerelease: false
40
39
  requirement: &id002 !ruby/object:Gem::Requirement
41
40
  none: false
42
41
  requirements:
43
42
  - - ~>
44
43
  - !ruby/object:Gem::Version
45
- hash: 15424051
44
+ hash: 3
46
45
  segments:
46
+ - 3
47
47
  - 1
48
48
  - 0
49
- - 0
50
- - rc
51
- - 3
52
- version: 1.0.0.rc.3
53
- type: :development
49
+ version: 3.1.0
50
+ type: :runtime
54
51
  version_requirements: *id002
55
52
  - !ruby/object:Gem::Dependency
56
- name: bundler
53
+ name: devise
57
54
  prerelease: false
58
55
  requirement: &id003 !ruby/object:Gem::Requirement
59
56
  none: false
60
57
  requirements:
61
- - - ~>
58
+ - - ">="
62
59
  - !ruby/object:Gem::Version
63
- hash: 25
60
+ hash: 11
64
61
  segments:
65
62
  - 1
66
- - 0
67
- - 7
68
- version: 1.0.7
69
- type: :development
70
- version_requirements: *id003
71
- - !ruby/object:Gem::Dependency
72
- name: rails
73
- prerelease: false
74
- requirement: &id004 !ruby/object:Gem::Requirement
75
- none: false
76
- requirements:
77
- - - ~>
63
+ - 4
64
+ - 6
65
+ version: 1.4.6
66
+ - - <
78
67
  - !ruby/object:Gem::Version
79
- hash: 7
80
- segments:
81
- - 3
82
- - 0
83
- - 0
84
- version: 3.0.0
85
- type: :runtime
86
- version_requirements: *id004
87
- - !ruby/object:Gem::Dependency
88
- name: devise
89
- prerelease: false
90
- requirement: &id005 !ruby/object:Gem::Requirement
91
- none: false
92
- requirements:
93
- - - ~>
94
- - !ruby/object:Gem::Version
95
- hash: 7712074
68
+ hash: 3
96
69
  segments:
97
70
  - 1
98
- - 2
99
- - rc
100
- version: 1.2.rc
71
+ - 6
72
+ version: "1.6"
101
73
  type: :runtime
102
- version_requirements: *id005
74
+ version_requirements: *id003
103
75
  description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
104
76
  email:
105
77
  - sergio@entrecables.com
@@ -110,32 +82,31 @@ extensions: []
110
82
  extra_rdoc_files: []
111
83
 
112
84
  files:
85
+ - app/controllers/devise/invitations_controller.rb
113
86
  - app/views/devise/invitations/edit.html.erb
114
87
  - app/views/devise/invitations/new.html.erb
115
88
  - app/views/devise/mailer/invitation_instructions.html.erb
116
- - app/views/devise/mailer/invitation.html.erb
117
- - app/controllers/devise/invitations_controller.rb~
118
- - app/controllers/devise/invitations_controller.rb
119
89
  - config/locales/en.yml
120
- - lib/devise_invitable/rails.rb
121
- - lib/devise_invitable/schema.rb
90
+ - lib/devise_invitable.rb
122
91
  - lib/devise_invitable/mailer.rb
123
- - lib/devise_invitable/routes.rb
124
- - lib/devise_invitable/version.rb
125
92
  - lib/devise_invitable/model.rb
126
- - lib/devise_invitable/model.rb~
93
+ - lib/devise_invitable/rails.rb
94
+ - lib/devise_invitable/routes.rb
95
+ - lib/devise_invitable/schema.rb
127
96
  - lib/devise_invitable/controllers/helpers.rb
128
97
  - lib/devise_invitable/controllers/url_helpers.rb
129
- - lib/devise_invitable.rb
98
+ - lib/devise_invitable/version.rb
99
+ - lib/devise_invitable/inviter.rb
100
+ - lib/devise_invitable/model.rb~
101
+ - lib/generators/active_record/devise_invitable_generator.rb
102
+ - lib/generators/active_record/templates/migration.rb
130
103
  - lib/generators/devise_invitable/views_generator.rb
131
104
  - lib/generators/devise_invitable/devise_invitable_generator.rb
132
105
  - lib/generators/devise_invitable/install_generator.rb
133
- - lib/generators/active_record/devise_invitable_generator.rb
134
- - lib/generators/active_record/templates/migration.rb
106
+ - lib/generators/mongoid/devise_invitable_generator.rb
135
107
  - LICENSE
136
108
  - README.rdoc
137
- has_rdoc: true
138
- homepage: http://github.com/rymai/devise_invitable
109
+ homepage: https://github.com/scambra/devise_invitable
139
110
  licenses: []
140
111
 
141
112
  post_install_message:
@@ -159,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
130
  required_rubygems_version: !ruby/object:Gem::Requirement
160
131
  none: false
161
132
  requirements:
162
- - - ~>
133
+ - - ">="
163
134
  - !ruby/object:Gem::Version
164
135
  hash: 23
165
136
  segments:
@@ -170,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
141
  requirements: []
171
142
 
172
143
  rubyforge_project:
173
- rubygems_version: 1.3.7
144
+ rubygems_version: 1.8.10
174
145
  signing_key:
175
146
  specification_version: 3
176
147
  summary: An invitation strategy for Devise
@@ -1,48 +0,0 @@
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
@@ -1,8 +0,0 @@
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>