devise_invitable 0.4.rc → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +19 -1
- data/app/controllers/devise/invitations_controller.rb +15 -1
- data/config/locales/en.yml +4 -3
- data/lib/devise_invitable/controllers/helpers.rb +1 -1
- data/lib/devise_invitable/model.rb +62 -6
- data/lib/devise_invitable/schema.rb +3 -0
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/devise_invitable.rb +30 -1
- data/lib/generators/active_record/templates/migration.rb +10 -5
- data/lib/generators/devise_invitable/install_generator.rb +10 -0
- data/lib/generators/mongoid/devise_invitable_generator.rb +8 -0
- metadata +26 -62
- data/app/controllers/devise/invitations_controller.rb~ +0 -48
- data/app/views/devise/mailer/invitation.html.erb +0 -8
- data/lib/devise_invitable/model.rb~ +0 -129
data/README.rdoc
CHANGED
|
@@ -61,7 +61,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
|
|
|
61
61
|
|
|
62
62
|
== Model configuration
|
|
63
63
|
|
|
64
|
-
DeviseInvitable adds
|
|
64
|
+
DeviseInvitable adds three new configuration options:
|
|
65
65
|
|
|
66
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
67
|
|
|
@@ -77,6 +77,10 @@ or directly as parameters to the <tt>devise</tt> method:
|
|
|
77
77
|
|
|
78
78
|
devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
|
|
79
79
|
|
|
80
|
+
* invitation_limit: The number of invitations users can send. The default value of nil means users can send as many invites as they want. A setting of 0 means they can't send invitations. A setting n > 0 means they can send n invitations.
|
|
81
|
+
|
|
82
|
+
* invite_key: The key to be used to check existing users when sending an invitation. The key must be an unique field. The default value is looking for users by email.
|
|
83
|
+
|
|
80
84
|
For more details, see <tt>config/initializers/devise.rb</tt> (after you invoked the "devise_invitable:install" generator described above).
|
|
81
85
|
|
|
82
86
|
== Configuring views
|
|
@@ -100,6 +104,18 @@ To send an invitation to a user, use the <tt>invite!</tt> class method. <tt>:ema
|
|
|
100
104
|
User.invite!(:email => "new_user@example.com", :name => "John Doe")
|
|
101
105
|
# => an invitation email will be sent to new_user@example.com
|
|
102
106
|
|
|
107
|
+
If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to true.
|
|
108
|
+
|
|
109
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe") do |u|
|
|
110
|
+
u.skip_invitation = true
|
|
111
|
+
end
|
|
112
|
+
# => the record will be created, but the invitation email will not be sent
|
|
113
|
+
|
|
114
|
+
You can add :skip_invitation to attributes hash if skip_invitation is added to attr_accessible.
|
|
115
|
+
|
|
116
|
+
User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
|
|
117
|
+
# => the record will be created, but the invitation email will not be sent
|
|
118
|
+
|
|
103
119
|
=== Accept an invitation
|
|
104
120
|
|
|
105
121
|
To accept an invitation with a token use the <tt>accept_invitation!</tt> class method. <tt>:invitation_token</tt> must be present in the parameters hash. You can also include other attributes in the hash.
|
|
@@ -115,6 +131,8 @@ After an invitation is created and sent, the inviter will be redirected to after
|
|
|
115
131
|
The invitation email includes a link to accept the invitation that looks like this: <tt>/users/invitation/accept?invitation_token=abcd123</tt>. When clicked, the invited must set a password in order to accept its invitation. Note that if the invitation_token is not present or not valid, the invited is redirected to after_sign_out_path_for(resource_name).
|
|
116
132
|
You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks. More on {Devise's README}[http://github.com/plataformatec/devise], "Controller filters and helpers" section.
|
|
117
133
|
|
|
134
|
+
The controller sets the invited_by_id attribute for the new user to the current user. This will let you easily keep track of who invited who.
|
|
135
|
+
|
|
118
136
|
== Controller filter
|
|
119
137
|
|
|
120
138
|
InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.
|
|
@@ -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,7 +14,7 @@ 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
|
|
@@ -44,4 +45,17 @@ class Devise::InvitationsController < ApplicationController
|
|
|
44
45
|
render_with_scope :edit
|
|
45
46
|
end
|
|
46
47
|
end
|
|
48
|
+
|
|
49
|
+
protected
|
|
50
|
+
def current_inviter
|
|
51
|
+
@current_inviter ||= authenticate_inviter!
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def has_invitations_left?
|
|
55
|
+
unless current_inviter.nil? || current_inviter.has_invitations_left?
|
|
56
|
+
build_resource
|
|
57
|
+
set_flash_message :alert, :no_invitations_remaining
|
|
58
|
+
render_with_scope :new
|
|
59
|
+
end
|
|
60
|
+
end
|
|
47
61
|
end
|
data/config/locales/en.yml
CHANGED
|
@@ -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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
no_invitations_remaining: "No invitations remaining"
|
|
8
|
+
mailer:
|
|
9
|
+
invitation_instructions:
|
|
10
|
+
subject: 'Invitation instructions'
|
|
@@ -21,6 +21,12 @@ module Devise
|
|
|
21
21
|
module Invitable
|
|
22
22
|
extend ActiveSupport::Concern
|
|
23
23
|
|
|
24
|
+
attr_accessor :skip_invitation
|
|
25
|
+
|
|
26
|
+
included do
|
|
27
|
+
belongs_to :invited_by, :polymorphic => true
|
|
28
|
+
end
|
|
29
|
+
|
|
24
30
|
# Accept an invitation by clearing invitation token and confirming it if model
|
|
25
31
|
# is confirmable
|
|
26
32
|
def accept_invitation!
|
|
@@ -35,14 +41,30 @@ module Devise
|
|
|
35
41
|
persisted? && invitation_token.present?
|
|
36
42
|
end
|
|
37
43
|
|
|
44
|
+
# Return true if this user has invitations left to send
|
|
45
|
+
def has_invitations_left?
|
|
46
|
+
if self.class.invitation_limit.present?
|
|
47
|
+
if invitation_limit
|
|
48
|
+
return invitation_limit > 0
|
|
49
|
+
else
|
|
50
|
+
return self.class.invitation_limit > 0
|
|
51
|
+
end
|
|
52
|
+
else
|
|
53
|
+
return true
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
38
57
|
# Reset invitation token and send invitation again
|
|
39
58
|
def invite!
|
|
40
59
|
if new_record? || invited?
|
|
60
|
+
@skip_password = true
|
|
41
61
|
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
|
42
62
|
generate_invitation_token if self.invitation_token.nil?
|
|
43
63
|
self.invitation_sent_at = Time.now.utc
|
|
44
|
-
save(:validate =>
|
|
45
|
-
|
|
64
|
+
if save(:validate => self.class.validate_on_invite)
|
|
65
|
+
self.invited_by.decrement_invitation_limit! if self.invited_by
|
|
66
|
+
!!deliver_invitation unless @skip_invitation
|
|
67
|
+
end
|
|
46
68
|
end
|
|
47
69
|
end
|
|
48
70
|
|
|
@@ -53,7 +75,34 @@ module Devise
|
|
|
53
75
|
invited? && invitation_period_valid?
|
|
54
76
|
end
|
|
55
77
|
|
|
78
|
+
# Only verify password when is not invited
|
|
79
|
+
def valid_password?(password)
|
|
80
|
+
super unless invited?
|
|
81
|
+
end
|
|
82
|
+
|
|
56
83
|
protected
|
|
84
|
+
def decrement_invitation_limit!
|
|
85
|
+
if self.class.invitation_limit.present?
|
|
86
|
+
self.invitation_limit ||= self.class.invitation_limit
|
|
87
|
+
self.decrement!(:invitation_limit)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
|
92
|
+
def password_required?
|
|
93
|
+
!@skip_password && super
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Deliver the invitation email
|
|
97
|
+
def deliver_invitation
|
|
98
|
+
::Devise.mailer.invitation_instructions(self).deliver
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Clear invitation token when reset password token is cleared too
|
|
102
|
+
def clear_reset_password_token
|
|
103
|
+
self.invitation_token = nil if invited?
|
|
104
|
+
super
|
|
105
|
+
end
|
|
57
106
|
|
|
58
107
|
# Checks if the invitation for the user is within the limit time.
|
|
59
108
|
# We do this by calculating if the difference between today and the
|
|
@@ -89,17 +138,21 @@ module Devise
|
|
|
89
138
|
# user and send invitation to it. If user is found, returns the user with an
|
|
90
139
|
# email already exists error.
|
|
91
140
|
# 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(
|
|
141
|
+
def invite!(attributes={}, invited_by=nil, &block)
|
|
142
|
+
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
|
94
143
|
invitable.attributes = attributes
|
|
144
|
+
invitable.invited_by = invited_by
|
|
95
145
|
|
|
96
146
|
if invitable.new_record?
|
|
97
147
|
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
|
|
98
148
|
else
|
|
99
|
-
invitable.errors.add(
|
|
149
|
+
invitable.errors.add(invite_key, :taken) unless invitable.invited?
|
|
100
150
|
end
|
|
101
151
|
|
|
102
|
-
|
|
152
|
+
if invitable.errors.empty?
|
|
153
|
+
yield invitable if block_given?
|
|
154
|
+
invitable.invite!
|
|
155
|
+
end
|
|
103
156
|
invitable
|
|
104
157
|
end
|
|
105
158
|
|
|
@@ -124,6 +177,9 @@ module Devise
|
|
|
124
177
|
end
|
|
125
178
|
|
|
126
179
|
Devise::Models.config(self, :invite_for)
|
|
180
|
+
Devise::Models.config(self, :validate_on_invite)
|
|
181
|
+
Devise::Models.config(self, :invitation_limit)
|
|
182
|
+
Devise::Models.config(self, :invite_key)
|
|
127
183
|
end
|
|
128
184
|
end
|
|
129
185
|
end
|
|
@@ -26,6 +26,9 @@ module DeviseInvitable
|
|
|
26
26
|
def invitable
|
|
27
27
|
apply_devise_schema :invitation_token, String, :limit => 60
|
|
28
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
|
|
29
32
|
end
|
|
30
33
|
end
|
|
31
34
|
end
|
data/lib/devise_invitable.rb
CHANGED
|
@@ -8,9 +8,38 @@ require 'devise_invitable/controllers/helpers'
|
|
|
8
8
|
require 'devise_invitable/rails'
|
|
9
9
|
|
|
10
10
|
module Devise
|
|
11
|
-
#
|
|
11
|
+
# Public: Validity period of the invitation token (default: 0). If
|
|
12
|
+
# invite_for is 0 or nil, the invitation will never expire.
|
|
13
|
+
# Set invite_for in the Devise configuration file (in config/initializers/devise.rb).
|
|
14
|
+
#
|
|
15
|
+
# config.invite_for = 2.weeks # => The invitation token will be valid 2 weeks
|
|
12
16
|
mattr_accessor :invite_for
|
|
13
17
|
@@invite_for = 0
|
|
18
|
+
|
|
19
|
+
# Public: Flag that force a record to be valid before being actually invited
|
|
20
|
+
# (default: false).
|
|
21
|
+
#
|
|
22
|
+
# Examples (in config/initializers/devise.rb)
|
|
23
|
+
#
|
|
24
|
+
# config.validate_on_invite = true
|
|
25
|
+
mattr_accessor :validate_on_invite
|
|
26
|
+
@@validate_on_invite = false
|
|
27
|
+
|
|
28
|
+
# Public: number of invitations the user is allowed to send
|
|
29
|
+
#
|
|
30
|
+
# Examples (in config/initializers/devise.rb)
|
|
31
|
+
#
|
|
32
|
+
# config.invitation_limit = nil
|
|
33
|
+
mattr_accessor :invitation_limit
|
|
34
|
+
@@invitation_limit = nil
|
|
35
|
+
|
|
36
|
+
# Public: The key to be used to check existing users when sending an invitation
|
|
37
|
+
#
|
|
38
|
+
# Examples (in config/initializers/devise.rb)
|
|
39
|
+
#
|
|
40
|
+
# config.invite_key = :email
|
|
41
|
+
mattr_accessor :invite_key
|
|
42
|
+
@@invite_key = :email
|
|
14
43
|
end
|
|
15
44
|
|
|
16
45
|
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
|
@@ -1,9 +1,12 @@
|
|
|
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
|
|
5
|
-
t.datetime
|
|
6
|
-
t.
|
|
4
|
+
t.string :invitation_token, :limit => 60
|
|
5
|
+
t.datetime :invitation_sent_at
|
|
6
|
+
t.integer :invitation_limit
|
|
7
|
+
t.references :invited_by, :polymorphic => true
|
|
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:
|
|
@@ -14,7 +17,9 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
|
|
|
14
17
|
end
|
|
15
18
|
|
|
16
19
|
def self.down
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
change_table :<%= table_name %> do |t|
|
|
21
|
+
remove_references :invited_by, :polymorphic => true
|
|
22
|
+
remove :invitation_limit, :invitation_sent_at, :invitation_token
|
|
23
|
+
end
|
|
19
24
|
end
|
|
20
25
|
end
|
|
@@ -24,6 +24,16 @@ 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
|
+
|
|
27
37
|
CONTENT
|
|
28
38
|
end
|
|
29
39
|
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:
|
|
5
|
-
prerelease:
|
|
4
|
+
hash: 15
|
|
5
|
+
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 4
|
|
9
|
-
-
|
|
10
|
-
version: 0.4.
|
|
9
|
+
- 0
|
|
10
|
+
version: 0.4.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Sergio Cambra
|
|
@@ -15,47 +15,13 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-
|
|
18
|
+
date: 2011-03-31 00:00:00 +02:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies:
|
|
21
|
-
- !ruby/object:Gem::Dependency
|
|
22
|
-
name: rspec-rails
|
|
23
|
-
prerelease: false
|
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
|
-
none: false
|
|
26
|
-
requirements:
|
|
27
|
-
- - ~>
|
|
28
|
-
- !ruby/object:Gem::Version
|
|
29
|
-
hash: 11
|
|
30
|
-
segments:
|
|
31
|
-
- 2
|
|
32
|
-
- 1
|
|
33
|
-
- 0
|
|
34
|
-
version: 2.1.0
|
|
35
|
-
type: :development
|
|
36
|
-
version_requirements: *id001
|
|
37
|
-
- !ruby/object:Gem::Dependency
|
|
38
|
-
name: steak
|
|
39
|
-
prerelease: false
|
|
40
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
|
-
none: false
|
|
42
|
-
requirements:
|
|
43
|
-
- - ~>
|
|
44
|
-
- !ruby/object:Gem::Version
|
|
45
|
-
hash: 15424051
|
|
46
|
-
segments:
|
|
47
|
-
- 1
|
|
48
|
-
- 0
|
|
49
|
-
- 0
|
|
50
|
-
- rc
|
|
51
|
-
- 3
|
|
52
|
-
version: 1.0.0.rc.3
|
|
53
|
-
type: :development
|
|
54
|
-
version_requirements: *id002
|
|
55
21
|
- !ruby/object:Gem::Dependency
|
|
56
22
|
name: bundler
|
|
57
23
|
prerelease: false
|
|
58
|
-
requirement: &
|
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
59
25
|
none: false
|
|
60
26
|
requirements:
|
|
61
27
|
- - ~>
|
|
@@ -67,11 +33,11 @@ dependencies:
|
|
|
67
33
|
- 7
|
|
68
34
|
version: 1.0.7
|
|
69
35
|
type: :development
|
|
70
|
-
version_requirements: *
|
|
36
|
+
version_requirements: *id001
|
|
71
37
|
- !ruby/object:Gem::Dependency
|
|
72
38
|
name: rails
|
|
73
39
|
prerelease: false
|
|
74
|
-
requirement: &
|
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
75
41
|
none: false
|
|
76
42
|
requirements:
|
|
77
43
|
- - ~>
|
|
@@ -83,23 +49,23 @@ dependencies:
|
|
|
83
49
|
- 0
|
|
84
50
|
version: 3.0.0
|
|
85
51
|
type: :runtime
|
|
86
|
-
version_requirements: *
|
|
52
|
+
version_requirements: *id002
|
|
87
53
|
- !ruby/object:Gem::Dependency
|
|
88
54
|
name: devise
|
|
89
55
|
prerelease: false
|
|
90
|
-
requirement: &
|
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
91
57
|
none: false
|
|
92
58
|
requirements:
|
|
93
59
|
- - ~>
|
|
94
60
|
- !ruby/object:Gem::Version
|
|
95
|
-
hash:
|
|
61
|
+
hash: 31
|
|
96
62
|
segments:
|
|
97
63
|
- 1
|
|
98
64
|
- 2
|
|
99
|
-
-
|
|
100
|
-
version: 1.2.
|
|
65
|
+
- 0
|
|
66
|
+
version: 1.2.0
|
|
101
67
|
type: :runtime
|
|
102
|
-
version_requirements: *
|
|
68
|
+
version_requirements: *id003
|
|
103
69
|
description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
|
|
104
70
|
email:
|
|
105
71
|
- sergio@entrecables.com
|
|
@@ -110,32 +76,30 @@ extensions: []
|
|
|
110
76
|
extra_rdoc_files: []
|
|
111
77
|
|
|
112
78
|
files:
|
|
79
|
+
- app/controllers/devise/invitations_controller.rb
|
|
113
80
|
- app/views/devise/invitations/edit.html.erb
|
|
114
81
|
- app/views/devise/invitations/new.html.erb
|
|
115
82
|
- 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
83
|
- config/locales/en.yml
|
|
120
|
-
- lib/devise_invitable
|
|
121
|
-
- lib/devise_invitable/schema.rb
|
|
84
|
+
- lib/devise_invitable.rb
|
|
122
85
|
- lib/devise_invitable/mailer.rb
|
|
123
|
-
- lib/devise_invitable/routes.rb
|
|
124
|
-
- lib/devise_invitable/version.rb
|
|
125
86
|
- lib/devise_invitable/model.rb
|
|
126
|
-
- lib/devise_invitable/
|
|
87
|
+
- lib/devise_invitable/rails.rb
|
|
88
|
+
- lib/devise_invitable/routes.rb
|
|
89
|
+
- lib/devise_invitable/schema.rb
|
|
127
90
|
- lib/devise_invitable/controllers/helpers.rb
|
|
128
91
|
- lib/devise_invitable/controllers/url_helpers.rb
|
|
129
|
-
- lib/devise_invitable.rb
|
|
92
|
+
- lib/devise_invitable/version.rb
|
|
93
|
+
- lib/generators/active_record/devise_invitable_generator.rb
|
|
94
|
+
- lib/generators/active_record/templates/migration.rb
|
|
130
95
|
- lib/generators/devise_invitable/views_generator.rb
|
|
131
96
|
- lib/generators/devise_invitable/devise_invitable_generator.rb
|
|
132
97
|
- lib/generators/devise_invitable/install_generator.rb
|
|
133
|
-
- lib/generators/
|
|
134
|
-
- lib/generators/active_record/templates/migration.rb
|
|
98
|
+
- lib/generators/mongoid/devise_invitable_generator.rb
|
|
135
99
|
- LICENSE
|
|
136
100
|
- README.rdoc
|
|
137
101
|
has_rdoc: true
|
|
138
|
-
homepage:
|
|
102
|
+
homepage: https://github.com/scambra/devise_invitable
|
|
139
103
|
licenses: []
|
|
140
104
|
|
|
141
105
|
post_install_message:
|
|
@@ -159,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
159
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
124
|
none: false
|
|
161
125
|
requirements:
|
|
162
|
-
- -
|
|
126
|
+
- - ">="
|
|
163
127
|
- !ruby/object:Gem::Version
|
|
164
128
|
hash: 23
|
|
165
129
|
segments:
|
|
@@ -170,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
170
134
|
requirements: []
|
|
171
135
|
|
|
172
136
|
rubyforge_project:
|
|
173
|
-
rubygems_version: 1.
|
|
137
|
+
rubygems_version: 1.5.2
|
|
174
138
|
signing_key:
|
|
175
139
|
specification_version: 3
|
|
176
140
|
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>
|
|
@@ -1,129 +0,0 @@
|
|
|
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
|