devise_invitable 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
@@ -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
|
-
|
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
|
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: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 7
|
10
|
+
version: 0.3.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sergio Cambra
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-04-01 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -106,27 +106,25 @@ extensions: []
|
|
106
106
|
extra_rdoc_files: []
|
107
107
|
|
108
108
|
files:
|
109
|
+
- app/controllers/devise/invitations_controller.rb
|
109
110
|
- app/views/devise/invitations/edit.html.erb
|
110
111
|
- app/views/devise/invitations/new.html.erb
|
111
112
|
- app/views/devise/mailer/invitation.html.erb
|
112
|
-
- app/controllers/devise/invitations_controller.rb~
|
113
|
-
- app/controllers/devise/invitations_controller.rb
|
114
113
|
- config/locales/en.yml
|
115
|
-
- lib/devise_invitable
|
116
|
-
- lib/devise_invitable/schema.rb
|
114
|
+
- lib/devise_invitable.rb
|
117
115
|
- lib/devise_invitable/mailer.rb
|
118
|
-
- lib/devise_invitable/routes.rb
|
119
|
-
- lib/devise_invitable/version.rb
|
120
116
|
- lib/devise_invitable/model.rb
|
121
|
-
- lib/devise_invitable/
|
117
|
+
- lib/devise_invitable/rails.rb
|
118
|
+
- lib/devise_invitable/routes.rb
|
119
|
+
- lib/devise_invitable/schema.rb
|
122
120
|
- lib/devise_invitable/controllers/helpers.rb
|
123
121
|
- lib/devise_invitable/controllers/url_helpers.rb
|
124
|
-
- lib/devise_invitable.rb
|
122
|
+
- lib/devise_invitable/version.rb
|
123
|
+
- lib/generators/active_record/devise_invitable_generator.rb
|
124
|
+
- lib/generators/active_record/templates/migration.rb
|
125
125
|
- lib/generators/devise_invitable/views_generator.rb
|
126
126
|
- lib/generators/devise_invitable/devise_invitable_generator.rb
|
127
127
|
- lib/generators/devise_invitable/install_generator.rb
|
128
|
-
- lib/generators/active_record/devise_invitable_generator.rb
|
129
|
-
- lib/generators/active_record/templates/migration.rb
|
130
128
|
- LICENSE
|
131
129
|
- README.rdoc
|
132
130
|
has_rdoc: true
|
@@ -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,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
|