devise_invitable 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of devise_invitable might be problematic. Click here for more details.
- data/README.rdoc +12 -1
- data/lib/devise_invitable/mailer.rb +9 -2
- data/lib/devise_invitable/model.rb +2 -2
- data/lib/devise_invitable/rails.rb +3 -0
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/generators/devise_invitable/install_generator.rb +6 -3
- metadata +33 -39
- data/app/controllers/devise_invitable/registrations_controller.rb~ +0 -15
- data/lib/devise_invitable.rb~ +0 -65
- data/lib/devise_invitable/controllers/helpers.rb~ +0 -21
- data/lib/devise_invitable/controllers/registrations.rb~ +0 -21
- data/lib/devise_invitable/model.rb~ +0 -224
- data/lib/devise_invitable/rails.rb~ +0 -21
data/README.rdoc
CHANGED
@@ -88,7 +88,7 @@ or directly as parameters to the <tt>devise</tt> method:
|
|
88
88
|
|
89
89
|
devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
|
90
90
|
|
91
|
-
* 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.
|
91
|
+
* invitation_limit: The number of invitations users can send. The default value of nil means users can send as many invites as they want, there is no limit for any user, invitation_limit column is not used. A setting of 0 means they can't send invitations. A setting n > 0 means they can send n invitations. You can change invitation_limit column for some users so they can send more or less invitations, even with global invitation_limit = 0.
|
92
92
|
|
93
93
|
* invite_key: The key to be used to check existing users when sending an invitation. You can use multiple keys. This value must be a hash with the invite key as hash keys, and regexp to validate format as values. If you don't want to validate the key you can set nil as validation format. The default value is looking for users by email and validating with Devise.email_regexp {:email => Devise.email_regexp}.
|
94
94
|
|
@@ -272,6 +272,17 @@ Take a look at the generated locale file (in <tt>config/locales/devise_invitable
|
|
272
272
|
|
273
273
|
DeviseInvitable supports ActiveRecord and Mongoid, like Devise.
|
274
274
|
|
275
|
+
== Testing
|
276
|
+
|
277
|
+
To test DeviseInvitable for the ActiveRecord ORM with RVM, Ruby 1.9.2, and Rubygems 1.8.17:
|
278
|
+
|
279
|
+
rvm use 1.9.2
|
280
|
+
rvm gemset create devise_invitable
|
281
|
+
rvm gemset use devise_invitable
|
282
|
+
gem install bundler
|
283
|
+
bundle
|
284
|
+
rake test DEVISE_ORM=active_record
|
285
|
+
|
275
286
|
== Contributors
|
276
287
|
|
277
288
|
Check them all at:
|
@@ -1,9 +1,16 @@
|
|
1
|
+
require 'devise/version'
|
2
|
+
|
1
3
|
module DeviseInvitable
|
2
4
|
module Mailer
|
3
5
|
|
4
6
|
# Deliver an invitation email
|
5
|
-
def invitation_instructions(record)
|
6
|
-
|
7
|
+
def invitation_instructions(record, opts={})
|
8
|
+
# optional arguments introduced in Devise 2.2.0, remove check once support for < 2.2.0 is dropped.
|
9
|
+
if Gem::Version.new(Devise::VERSION.dup) < Gem::Version.new('2.2.0')
|
10
|
+
devise_mail(record, :invitation_instructions)
|
11
|
+
else
|
12
|
+
devise_mail(record, :invitation_instructions, opts)
|
13
|
+
end
|
7
14
|
end
|
8
15
|
end
|
9
16
|
end
|
@@ -73,8 +73,8 @@ module Devise
|
|
73
73
|
# Accept an invitation by clearing invitation token and and setting invitation_accepted_at
|
74
74
|
# Confirms it if model is confirmable
|
75
75
|
def accept_invitation!
|
76
|
-
self.invitation_accepted_at = Time.now.utc
|
77
76
|
if self.invited_to_sign_up? && self.valid?
|
77
|
+
self.invitation_accepted_at = Time.now.utc
|
78
78
|
run_callbacks :invitation_accepted do
|
79
79
|
self.invitation_token = nil
|
80
80
|
self.confirmed_at = self.invitation_accepted_at if self.respond_to?(:confirmed_at)
|
@@ -218,7 +218,7 @@ module Devise
|
|
218
218
|
invite_key_array = invite_key_fields
|
219
219
|
attributes_hash = {}
|
220
220
|
invite_key_array.each do |k,v|
|
221
|
-
attributes_hash[k] = attributes.delete(k)
|
221
|
+
attributes_hash[k] = attributes.delete(k).try(:strip)
|
222
222
|
end
|
223
223
|
|
224
224
|
invitable = find_or_initialize_with_errors(invite_key_array, attributes_hash)
|
@@ -13,6 +13,9 @@ module DeviseInvitable
|
|
13
13
|
config.to_prepare do
|
14
14
|
require 'devise/mailer'
|
15
15
|
Devise::Mailer.send :include, DeviseInvitable::Mailer
|
16
|
+
end
|
17
|
+
# extend mapping with after_initialize becuase is not reloaded
|
18
|
+
config.after_initialize do
|
16
19
|
Devise::Mapping.send :include, DeviseInvitable::Mapping
|
17
20
|
end
|
18
21
|
end
|
@@ -25,9 +25,12 @@ module DeviseInvitable
|
|
25
25
|
# config.invite_for = 2.weeks
|
26
26
|
|
27
27
|
# Number of invitations users can send.
|
28
|
-
# If invitation_limit is nil,
|
29
|
-
#
|
30
|
-
# If invitation_limit
|
28
|
+
# - If invitation_limit is nil, there is no limit for invitations, users can
|
29
|
+
# send unlimited invitations, invitation_limit column is not used.
|
30
|
+
# - If invitation_limit is 0, users can't send invitations by default.
|
31
|
+
# - If invitation_limit n > 0, users can send n invitations.
|
32
|
+
# You can change invitation_limit column for some users so they can send more
|
33
|
+
# or less invitations, even with global invitation_limit = 0
|
31
34
|
# Default: nil
|
32
35
|
# config.invitation_limit = 5
|
33
36
|
|
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:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 5
|
10
|
+
version: 1.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sergio Cambra
|
@@ -15,11 +15,10 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2013-01-24 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
prerelease: false
|
21
|
+
type: :development
|
23
22
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
23
|
none: false
|
25
24
|
requirements:
|
@@ -31,27 +30,27 @@ dependencies:
|
|
31
30
|
- 1
|
32
31
|
- 0
|
33
32
|
version: 1.1.0
|
34
|
-
type: :development
|
35
33
|
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: devise
|
38
34
|
prerelease: false
|
35
|
+
name: bundler
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
type: :runtime
|
39
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
41
40
|
requirements:
|
42
41
|
- - ">="
|
43
42
|
- !ruby/object:Gem::Version
|
44
|
-
hash:
|
43
|
+
hash: 9
|
45
44
|
segments:
|
46
45
|
- 2
|
47
46
|
- 1
|
48
|
-
-
|
49
|
-
version: 2.1.
|
50
|
-
type: :runtime
|
47
|
+
- 1
|
48
|
+
version: 2.1.1
|
51
49
|
version_requirements: *id002
|
52
|
-
- !ruby/object:Gem::Dependency
|
53
|
-
name: railties
|
54
50
|
prerelease: false
|
51
|
+
name: devise
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
type: :runtime
|
55
54
|
requirement: &id003 !ruby/object:Gem::Requirement
|
56
55
|
none: false
|
57
56
|
requirements:
|
@@ -62,11 +61,11 @@ dependencies:
|
|
62
61
|
- 3
|
63
62
|
- 0
|
64
63
|
version: "3.0"
|
65
|
-
type: :runtime
|
66
64
|
version_requirements: *id003
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
|
-
name: actionmailer
|
69
65
|
prerelease: false
|
66
|
+
name: actionmailer
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
type: :runtime
|
70
69
|
requirement: &id004 !ruby/object:Gem::Requirement
|
71
70
|
none: false
|
72
71
|
requirements:
|
@@ -77,8 +76,9 @@ dependencies:
|
|
77
76
|
- 3
|
78
77
|
- 0
|
79
78
|
version: "3.0"
|
80
|
-
type: :runtime
|
81
79
|
version_requirements: *id004
|
80
|
+
prerelease: false
|
81
|
+
name: railties
|
82
82
|
description: It adds support for send invitations by email (it requires to be authenticated) and accept the invitation by setting a password.
|
83
83
|
email:
|
84
84
|
- sergio@entrecables.com
|
@@ -91,34 +91,28 @@ extra_rdoc_files: []
|
|
91
91
|
files:
|
92
92
|
- app/controllers/devise/invitations_controller.rb
|
93
93
|
- app/controllers/devise_invitable/registrations_controller.rb
|
94
|
-
- app/controllers/devise_invitable/registrations_controller.rb~
|
95
|
-
- app/views/devise/invitations/edit.html.erb
|
96
94
|
- app/views/devise/invitations/new.html.erb
|
95
|
+
- app/views/devise/invitations/edit.html.erb
|
97
96
|
- app/views/devise/mailer/invitation_instructions.html.erb
|
98
97
|
- config/locales/en.yml
|
98
|
+
- lib/generators/active_record/devise_invitable_generator.rb
|
99
|
+
- lib/generators/active_record/templates/migration.rb
|
100
|
+
- lib/generators/devise_invitable/install_generator.rb
|
101
|
+
- lib/generators/devise_invitable/devise_invitable_generator.rb
|
102
|
+
- lib/generators/devise_invitable/views_generator.rb
|
103
|
+
- lib/generators/devise_invitable/templates/simple_form_for/invitations/new.html.erb
|
104
|
+
- lib/generators/devise_invitable/templates/simple_form_for/invitations/edit.html.erb
|
105
|
+
- lib/generators/mongoid/devise_invitable_generator.rb
|
99
106
|
- lib/devise_invitable.rb
|
100
|
-
- lib/devise_invitable/mailer.rb
|
101
|
-
- lib/devise_invitable/model.rb
|
102
107
|
- lib/devise_invitable/rails.rb
|
108
|
+
- lib/devise_invitable/inviter.rb
|
109
|
+
- lib/devise_invitable/mapping.rb
|
103
110
|
- lib/devise_invitable/routes.rb
|
104
111
|
- lib/devise_invitable/version.rb
|
112
|
+
- lib/devise_invitable/mailer.rb
|
113
|
+
- lib/devise_invitable/model.rb
|
105
114
|
- lib/devise_invitable/controllers/helpers.rb
|
106
115
|
- lib/devise_invitable/controllers/url_helpers.rb
|
107
|
-
- lib/devise_invitable/controllers/registrations.rb~
|
108
|
-
- lib/devise_invitable/controllers/helpers.rb~
|
109
|
-
- lib/devise_invitable/rails.rb~
|
110
|
-
- lib/devise_invitable/inviter.rb
|
111
|
-
- lib/devise_invitable/mapping.rb
|
112
|
-
- lib/devise_invitable/model.rb~
|
113
|
-
- lib/generators/active_record/devise_invitable_generator.rb
|
114
|
-
- lib/generators/active_record/templates/migration.rb
|
115
|
-
- lib/generators/devise_invitable/views_generator.rb
|
116
|
-
- lib/generators/devise_invitable/devise_invitable_generator.rb
|
117
|
-
- lib/generators/devise_invitable/install_generator.rb
|
118
|
-
- lib/generators/devise_invitable/templates/simple_form_for/invitations/edit.html.erb
|
119
|
-
- lib/generators/devise_invitable/templates/simple_form_for/invitations/new.html.erb
|
120
|
-
- lib/generators/mongoid/devise_invitable_generator.rb
|
121
|
-
- lib/devise_invitable.rb~
|
122
116
|
- LICENSE
|
123
117
|
- README.rdoc
|
124
118
|
homepage: https://github.com/scambra/devise_invitable
|
@@ -156,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
150
|
requirements: []
|
157
151
|
|
158
152
|
rubyforge_project:
|
159
|
-
rubygems_version: 1.8.
|
153
|
+
rubygems_version: 1.8.24
|
160
154
|
signing_key:
|
161
155
|
specification_version: 3
|
162
156
|
summary: An invitation strategy for Devise
|
@@ -1,15 +0,0 @@
|
|
1
|
-
class DeviseInvitable::RegistrationsController < Devise::RegistrationsController
|
2
|
-
protected
|
3
|
-
|
4
|
-
def build_resource(*args)
|
5
|
-
hash = args.pop || resource_params || {}
|
6
|
-
if hash[:email]
|
7
|
-
self.resource = resource_class.where(:email => hash[:email], :encrypted_password => '').first
|
8
|
-
if self.resource
|
9
|
-
self.resource.attributes = hash
|
10
|
-
self.resource.accept_invitation!
|
11
|
-
end
|
12
|
-
end
|
13
|
-
self.resource ||= super(hash, *args)
|
14
|
-
end
|
15
|
-
end
|
data/lib/devise_invitable.rb~
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
module DeviseInvitable
|
2
|
-
autoload :Inviter, 'devise_invitable/inviter'
|
3
|
-
autoload :Mailer, 'devise_invitable/mailer'
|
4
|
-
module Controllers
|
5
|
-
autoload :UrlHelpers, 'devise_invitable/controllers/url_helpers'
|
6
|
-
autoload :Registrations, 'devise_invitable/controllers/registrations'
|
7
|
-
autoload :Helpers, 'devise_invitable/controllers/helpers'
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
require 'devise'
|
12
|
-
require 'devise_invitable/routes'
|
13
|
-
require 'devise_invitable/rails'
|
14
|
-
|
15
|
-
module Devise
|
16
|
-
# Public: Validity period of the invitation token (default: 0). If
|
17
|
-
# invite_for is 0 or nil, the invitation will never expire.
|
18
|
-
# Set invite_for in the Devise configuration file (in config/initializers/devise.rb).
|
19
|
-
#
|
20
|
-
# config.invite_for = 2.weeks # => The invitation token will be valid 2 weeks
|
21
|
-
mattr_accessor :invite_for
|
22
|
-
@@invite_for = 0
|
23
|
-
|
24
|
-
# Public: Flag that force a record to be valid before being actually invited
|
25
|
-
# (default: false).
|
26
|
-
#
|
27
|
-
# Examples (in config/initializers/devise.rb)
|
28
|
-
#
|
29
|
-
# config.validate_on_invite = true
|
30
|
-
mattr_accessor :validate_on_invite
|
31
|
-
@@validate_on_invite = false
|
32
|
-
|
33
|
-
# Public: number of invitations the user is allowed to send
|
34
|
-
#
|
35
|
-
# Examples (in config/initializers/devise.rb)
|
36
|
-
#
|
37
|
-
# config.invitation_limit = nil
|
38
|
-
mattr_accessor :invitation_limit
|
39
|
-
@@invitation_limit = nil
|
40
|
-
|
41
|
-
# Public: The key to be used to check existing users when sending an invitation,
|
42
|
-
# and the regexp used to test it when validate_on_invite is not set.
|
43
|
-
#
|
44
|
-
# Examples (in config/initializers/devise.rb)
|
45
|
-
#
|
46
|
-
# config.invite_key = {:email => /\A[^@]+@[^@]+\z/}
|
47
|
-
mattr_accessor :invite_key
|
48
|
-
@@invite_key = {:email => Devise.email_regexp}
|
49
|
-
|
50
|
-
# Public: Resend invitation if user with invited status is invited again
|
51
|
-
# (default: true)
|
52
|
-
#
|
53
|
-
# Example (in config/initializers/devise.rb)
|
54
|
-
#
|
55
|
-
# config.resend_invitation = false
|
56
|
-
mattr_accessor :resend_invitation
|
57
|
-
@@resend_invitation = true
|
58
|
-
|
59
|
-
# Public: The class name of the inviting model. If this is nil,
|
60
|
-
# the #invited_by association is declared to be polymorphic. (default: nil)
|
61
|
-
mattr_accessor :invited_by_class_name
|
62
|
-
@@invited_by_class_name = nil
|
63
|
-
end
|
64
|
-
|
65
|
-
Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/model', :route => :invitation
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module DeviseInvitable::Controllers::Helpers
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
included do
|
5
|
-
hide_action :after_invite_path_for, :after_accept_path_for
|
6
|
-
end
|
7
|
-
|
8
|
-
def after_invite_path_for(resource)
|
9
|
-
after_sign_in_path_for(resource)
|
10
|
-
end
|
11
|
-
|
12
|
-
def after_accept_path_for(resource)
|
13
|
-
after_sign_in_path_for(resource)
|
14
|
-
end
|
15
|
-
|
16
|
-
protected
|
17
|
-
def authenticate_inviter!
|
18
|
-
send(:"authenticate_#{resource_name}!", :force => true)
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module DeviseInvitable::Controllers::Registrations
|
2
|
-
def self.included(controller)
|
3
|
-
controller.alias_method_chain :build_resource, :invitation
|
4
|
-
end
|
5
|
-
|
6
|
-
protected
|
7
|
-
|
8
|
-
def build_resource_with_invitation(*args)
|
9
|
-
hash = args.pop || resource_params || {}
|
10
|
-
if hash[:email]
|
11
|
-
self.resource = resource_class.where(:email => hash[:email], :encrypted_password => '').first
|
12
|
-
if self.resource
|
13
|
-
puts self.resource.inspect
|
14
|
-
self.resource.attributes = hash
|
15
|
-
self.resource.accept_invitation!
|
16
|
-
puts self.resource.inspect
|
17
|
-
end
|
18
|
-
end
|
19
|
-
self.resource ||= build_resource_without_invitation(hash, *args)
|
20
|
-
end
|
21
|
-
end
|
@@ -1,224 +0,0 @@
|
|
1
|
-
require 'active_support/deprecation'
|
2
|
-
|
3
|
-
module Devise
|
4
|
-
module Models
|
5
|
-
# Invitable is responsible for sending invitation emails.
|
6
|
-
# When an invitation is sent to an email address, an account is created for it.
|
7
|
-
# Invitation email contains a link allowing the user to accept the invitation
|
8
|
-
# by setting a password (as reset password from Devise's recoverable module).
|
9
|
-
#
|
10
|
-
# Configuration:
|
11
|
-
#
|
12
|
-
# invite_for: The period the generated invitation token is valid, after
|
13
|
-
# this period, the invited resource won't be able to accept the invitation.
|
14
|
-
# When invite_for is 0 (the default), the invitation won't expire.
|
15
|
-
#
|
16
|
-
# Examples:
|
17
|
-
#
|
18
|
-
# User.find(1).invited_to_sign_up? # => true/false
|
19
|
-
# User.invite!(:email => 'someone@example.com') # => send invitation
|
20
|
-
# User.accept_invitation!(:invitation_token => '...') # => accept invitation with a token
|
21
|
-
# User.find(1).accept_invitation! # => accept invitation
|
22
|
-
# User.find(1).invite! # => reset invitation status and send invitation again
|
23
|
-
module Invitable
|
24
|
-
extend ActiveSupport::Concern
|
25
|
-
|
26
|
-
attr_accessor :skip_invitation
|
27
|
-
attr_accessor :completing_invite
|
28
|
-
|
29
|
-
included do
|
30
|
-
include ::DeviseInvitable::Inviter
|
31
|
-
belongs_to :invited_by, :polymorphic => true
|
32
|
-
|
33
|
-
include ActiveSupport::Callbacks
|
34
|
-
define_callbacks :invitation_accepted
|
35
|
-
|
36
|
-
attr_writer :skip_password
|
37
|
-
end
|
38
|
-
|
39
|
-
# Accept an invitation by clearing invitation token and and setting invitation_accepted_at
|
40
|
-
# Confirms it if model is confirmable
|
41
|
-
def accept_invitation!
|
42
|
-
self.completing_invite = true
|
43
|
-
if self.invited_to_sign_up? && self.valid?
|
44
|
-
run_callbacks :invitation_accepted do
|
45
|
-
self.invitation_token = nil
|
46
|
-
self.invitation_accepted_at = Time.now.utc if respond_to? :"invitation_accepted_at="
|
47
|
-
self.completing_invite = false
|
48
|
-
self.save(:validate => false)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# Verifies whether a user has accepted an invite, was never invited, or is in the process of accepting an invitation, or not
|
54
|
-
def accepting_or_not_invited?
|
55
|
-
!!completing_invite || invited_to_sign_up?
|
56
|
-
end
|
57
|
-
|
58
|
-
# Verifies whether a user has been invited or not
|
59
|
-
def invited_to_sign_up?
|
60
|
-
persisted? && invitation_token.present?
|
61
|
-
end
|
62
|
-
|
63
|
-
def invited?
|
64
|
-
invited_to_sign_up?
|
65
|
-
end
|
66
|
-
deprecate :invited?
|
67
|
-
|
68
|
-
# Reset invitation token and send invitation again
|
69
|
-
def invite!
|
70
|
-
was_invited = invited_to_sign_up?
|
71
|
-
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
|
72
|
-
generate_invitation_token if self.invitation_token.nil?
|
73
|
-
self.invitation_sent_at = Time.now.utc
|
74
|
-
|
75
|
-
# Call these before_validate methods since we aren't validating on save
|
76
|
-
self.downcase_keys if self.new_record? && self.respond_to?(:downcase_keys)
|
77
|
-
self.strip_whitespace if self.new_record? && self.respond_to?(:strip_whitespace)
|
78
|
-
|
79
|
-
if save(:validate => false)
|
80
|
-
self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
|
81
|
-
deliver_invitation unless @skip_invitation
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
# Verify whether a invitation is active or not. If the user has been
|
86
|
-
# invited, we need to calculate if the invitation time has not expired
|
87
|
-
# for this user, in other words, if the invitation is still valid.
|
88
|
-
def valid_invitation?
|
89
|
-
invited_to_sign_up? && invitation_period_valid?
|
90
|
-
end
|
91
|
-
|
92
|
-
# Only verify password when is not invited
|
93
|
-
def valid_password?(password)
|
94
|
-
super unless invited_to_sign_up?
|
95
|
-
end
|
96
|
-
|
97
|
-
def reset_password!(new_password, new_password_confirmation)
|
98
|
-
super
|
99
|
-
accept_invitation!
|
100
|
-
end
|
101
|
-
|
102
|
-
protected
|
103
|
-
# Overriding the method in Devise's :validatable module so password is not required on inviting
|
104
|
-
def password_required?
|
105
|
-
!@skip_password && super
|
106
|
-
end
|
107
|
-
|
108
|
-
# Deliver the invitation email
|
109
|
-
def deliver_invitation
|
110
|
-
::Devise.mailer.invitation_instructions(self).deliver
|
111
|
-
end
|
112
|
-
|
113
|
-
# Checks if the invitation for the user is within the limit time.
|
114
|
-
# We do this by calculating if the difference between today and the
|
115
|
-
# invitation sent date does not exceed the invite for time configured.
|
116
|
-
# Invite_for is a model configuration, must always be an integer value.
|
117
|
-
#
|
118
|
-
# Example:
|
119
|
-
#
|
120
|
-
# # invite_for = 1.day and invitation_sent_at = today
|
121
|
-
# invitation_period_valid? # returns true
|
122
|
-
#
|
123
|
-
# # invite_for = 5.days and invitation_sent_at = 4.days.ago
|
124
|
-
# invitation_period_valid? # returns true
|
125
|
-
#
|
126
|
-
# # invite_for = 5.days and invitation_sent_at = 5.days.ago
|
127
|
-
# invitation_period_valid? # returns false
|
128
|
-
#
|
129
|
-
# # invite_for = nil
|
130
|
-
# invitation_period_valid? # will always return true
|
131
|
-
#
|
132
|
-
def invitation_period_valid?
|
133
|
-
invitation_sent_at && (self.class.invite_for.to_i.zero? || invitation_sent_at.utc >= self.class.invite_for.ago)
|
134
|
-
end
|
135
|
-
|
136
|
-
# Generates a new random token for invitation, and stores the time
|
137
|
-
# this token is being generated
|
138
|
-
def generate_invitation_token
|
139
|
-
self.invitation_token = self.class.invitation_token
|
140
|
-
end
|
141
|
-
|
142
|
-
module ClassMethods
|
143
|
-
# Attempt to find a user by it's email. If a record is not found, create a new
|
144
|
-
# user and send invitation to it. If user is found, returns the user with an
|
145
|
-
# email already exists error.
|
146
|
-
# If user is found and still have pending invitation, email is resend unless
|
147
|
-
# resend_invitation is set to false
|
148
|
-
# Attributes must contain the user email, other attributes will be set in the record
|
149
|
-
def _invite(attributes={}, invited_by=nil, &block)
|
150
|
-
invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
|
151
|
-
invitable.assign_attributes(attributes, :as => inviter_role(invited_by))
|
152
|
-
invitable.invited_by = invited_by
|
153
|
-
|
154
|
-
invitable.skip_password = true
|
155
|
-
invitable.valid? if self.validate_on_invite
|
156
|
-
if invitable.new_record?
|
157
|
-
invitable.errors.clear if !self.validate_on_invite and invitable.email.try(:match, Devise.email_regexp)
|
158
|
-
else
|
159
|
-
invitable.errors.add(invite_key, :taken) unless invitable.invited_to_sign_up? && self.resend_invitation
|
160
|
-
end
|
161
|
-
|
162
|
-
if invitable.errors.empty?
|
163
|
-
yield invitable if block_given?
|
164
|
-
mail = invitable.invite!
|
165
|
-
end
|
166
|
-
[invitable, mail]
|
167
|
-
end
|
168
|
-
|
169
|
-
# Override this method if the invitable is using Mass Assignment Security
|
170
|
-
# and the inviter has a non-default role.
|
171
|
-
def inviter_role(inviter)
|
172
|
-
:default
|
173
|
-
end
|
174
|
-
|
175
|
-
def invite!(attributes={}, invited_by=nil, &block)
|
176
|
-
invitable, mail = _invite(attributes, invited_by, &block)
|
177
|
-
invitable
|
178
|
-
end
|
179
|
-
|
180
|
-
def invite_mail!(attributes={}, invited_by=nil, &block)
|
181
|
-
invitable, mail = _invite(attributes, invited_by, &block)
|
182
|
-
mail
|
183
|
-
end
|
184
|
-
|
185
|
-
# Attempt to find a user by it's invitation_token to set it's password.
|
186
|
-
# If a user is found, reset it's password and automatically try saving
|
187
|
-
# the record. If not user is found, returns a new user containing an
|
188
|
-
# error in invitation_token attribute.
|
189
|
-
# Attributes must contain invitation_token, password and confirmation
|
190
|
-
def accept_invitation!(attributes={})
|
191
|
-
invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
|
192
|
-
invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
|
193
|
-
if invitable.errors.empty?
|
194
|
-
invitable.attributes = attributes
|
195
|
-
invitable.accept_invitation!
|
196
|
-
end
|
197
|
-
invitable
|
198
|
-
end
|
199
|
-
|
200
|
-
# Generate a token checking if one does not already exist in the database.
|
201
|
-
def invitation_token
|
202
|
-
generate_token(:invitation_token)
|
203
|
-
end
|
204
|
-
|
205
|
-
# Callback convenience methods
|
206
|
-
def before_invitation_accepted(*args, &blk)
|
207
|
-
set_callback(:invitation_accepted, :before, *args, &blk)
|
208
|
-
end
|
209
|
-
|
210
|
-
def after_invitation_accepted(*args, &blk)
|
211
|
-
set_callback(:invitation_accepted, :after, *args, &blk)
|
212
|
-
end
|
213
|
-
|
214
|
-
|
215
|
-
Devise::Models.config(self, :invite_for)
|
216
|
-
Devise::Models.config(self, :validate_on_invite)
|
217
|
-
Devise::Models.config(self, :invitation_limit)
|
218
|
-
Devise::Models.config(self, :invite_key)
|
219
|
-
Devise::Models.config(self, :resend_invitation)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module DeviseInvitable
|
2
|
-
class Engine < ::Rails::Engine
|
3
|
-
|
4
|
-
ActiveSupport.on_load(:action_controller) do
|
5
|
-
include DeviseInvitable::Controllers::UrlHelpers
|
6
|
-
include DeviseInvitable::Controllers::Helpers
|
7
|
-
end
|
8
|
-
ActiveSupport.on_load(:action_view) { include DeviseInvitable::Controllers::UrlHelpers }
|
9
|
-
|
10
|
-
# We use to_prepare instead of after_initialize here because Devise is a Rails engine; its
|
11
|
-
# mailer is reloaded like the rest of the user's app. Got to make sure that our mailer methods
|
12
|
-
# are included each time Devise::Mailer is (re)loaded.
|
13
|
-
config.to_prepare do
|
14
|
-
require 'devise/mailer'
|
15
|
-
Devise::Mailer.send :include, DeviseInvitable::Mailer
|
16
|
-
Devise::Mapping.send :include, DeviseInvitable::Mapping
|
17
|
-
end
|
18
|
-
config.after_initialize do
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|