devise_sms_activable 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +73 -0
- data/app/controllers/devise/sms_activations_controller.rb +39 -0
- data/app/views/devise/sms_activations/insert.html.erb +11 -0
- data/app/views/devise/sms_activations/new.html.erb +12 -0
- data/config/locales/en.yml +15 -0
- data/lib/devise_sms_activable.rb +30 -0
- data/lib/devise_sms_activable/controllers/helpers.rb +11 -0
- data/lib/devise_sms_activable/controllers/url_helpers.rb +24 -0
- data/lib/devise_sms_activable/hooks.rb +11 -0
- data/lib/devise_sms_activable/rails.rb +12 -0
- data/lib/devise_sms_activable/routes.rb +13 -0
- data/lib/devise_sms_activable/schema.rb +13 -0
- data/lib/devise_sms_activable/version.rb +3 -0
- data/lib/generators/active_record/devise_sms_activable_generator.rb +13 -0
- data/lib/generators/active_record/templates/migration.rb +18 -0
- data/lib/generators/devise_sms_activable/devise_sms_activable_generator.rb +20 -0
- data/lib/generators/devise_sms_activable/install_generator.rb +52 -0
- data/lib/generators/devise_sms_activable/views_generator.rb +10 -0
- data/lib/generators/mongoid/devise_sms_activable_generator.rb +8 -0
- data/lib/generators/templates/lib/sms_sender.rb +6 -0
- data/lib/models/sms_activable.rb +180 -0
- metadata +123 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Stefano Valicchia
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
= devise_sms_activable
|
2
|
+
|
3
|
+
Devise Sms Activable is a snap-in for Devise that will make any resource activable via SMS.
|
4
|
+
The user will receive an SMS with a token that can be entered on the site to activate the account.
|
5
|
+
Ask the user his phone (and phone confirmation to double check) on registration and the token will be sended automagically.
|
6
|
+
|
7
|
+
== Installation for Rails ~> 3.0 and Devise ~> 1.1
|
8
|
+
|
9
|
+
Install DeviseSmsActivable gem, it will also install dependencies (such as devise and warden):
|
10
|
+
|
11
|
+
gem install devise_sms_activable
|
12
|
+
|
13
|
+
Add DeviseSmsActivable to your Gemfile (and Devise if you weren't using them):
|
14
|
+
|
15
|
+
gem 'devise', '>= 1.1.0'
|
16
|
+
gem 'devise_sms_activable', '~> 0.0.9'
|
17
|
+
|
18
|
+
=== Automatic installation
|
19
|
+
|
20
|
+
Run the following generator to add DeviseSmsActivable’s configuration option in the Devise configuration file (config/initializers/devise.rb) and the sms sender class in your lib folder:
|
21
|
+
|
22
|
+
rails generate devise_sms_activable:install
|
23
|
+
|
24
|
+
When you are done, you are ready to add DeviseSmsActivable to any of your Devise models using the following generator:
|
25
|
+
|
26
|
+
rails generate devise_sms_activable MODEL
|
27
|
+
|
28
|
+
Replace MODEL by the class name you want to add DeviseSmsActivable, like User, Admin, etc. This will add the :sms_activable flag to your model's Devise modules. The generator will also create a migration file (if your ORM support them). Continue reading this file to understand exactly what the generator produces and how to use it.
|
29
|
+
|
30
|
+
== Configuring views
|
31
|
+
|
32
|
+
All the views are packaged inside the gem. If you'd like to customize the views, invoke the following generator and it will copy all the views to your application:
|
33
|
+
|
34
|
+
rails generate devise_sms_activable:views
|
35
|
+
|
36
|
+
You can also use the generator to generate scoped views:
|
37
|
+
|
38
|
+
rails generate devise_sms_activable:views users
|
39
|
+
|
40
|
+
Please refer to {Devise's README}[http://github.com/plataformatec/devise] for more information about views.
|
41
|
+
|
42
|
+
== Usage
|
43
|
+
|
44
|
+
The model is specular to the Devise's own Confirmable model. It only requires the user to supply a valid phone number.
|
45
|
+
|
46
|
+
On registration it will send an SMS with a token to be inserted to complete activation process.
|
47
|
+
By default users MUST activate by SMS before entering.
|
48
|
+
If you want something more "relaxed" just override <tt>sms_confirmation_required?</tt> in your model and make it your way.
|
49
|
+
You can use the convenience filter <tt>require_sms_activated!</tt> in your controller to block sms-unactive users from specific pages.
|
50
|
+
|
51
|
+
== Controller filter
|
52
|
+
|
53
|
+
DeviseSmsActivable extends your controllers with a <tt>require_sms_activated!</tt> method. Use it to restrict part of the site to "confirmed users" only
|
54
|
+
|
55
|
+
== I18n
|
56
|
+
|
57
|
+
DeviseSmsActivable installs a localizable file in your config/locales folder.
|
58
|
+
|
59
|
+
== Contributing to devise_sms_activable
|
60
|
+
|
61
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
62
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
63
|
+
* Fork the project
|
64
|
+
* Start a feature/bugfix branch
|
65
|
+
* Commit and push until you are happy with your contribution
|
66
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
67
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
68
|
+
|
69
|
+
== Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2011 Stefano Valicchia. See LICENSE.txt for
|
72
|
+
further details.
|
73
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Devise::SmsActivationsController < ApplicationController
|
2
|
+
include Devise::Controllers::InternalHelpers
|
3
|
+
|
4
|
+
# GET /resource/sms_activation/new
|
5
|
+
def new
|
6
|
+
build_resource({})
|
7
|
+
render_with_scope :new
|
8
|
+
end
|
9
|
+
|
10
|
+
# POST /resource/sms_activation
|
11
|
+
def create
|
12
|
+
self.resource = resource_class.send_sms_token(params[resource_name])
|
13
|
+
|
14
|
+
if resource.errors.empty?
|
15
|
+
set_flash_message :notice, :send_token, :phone => self.resource.phone
|
16
|
+
redirect_to new_session_path(resource_name)
|
17
|
+
else
|
18
|
+
render_with_scope :new
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# GET /resource/sms_activation/insert
|
23
|
+
def insert
|
24
|
+
build_resource({})
|
25
|
+
end
|
26
|
+
|
27
|
+
# GET or POST /resource/sms_activation/consume?sms_token=abcdef
|
28
|
+
def consume
|
29
|
+
self.resource = resource_class.confirm_by_sms_token(params[:sms_token])
|
30
|
+
|
31
|
+
if resource.errors.empty?
|
32
|
+
set_flash_message :notice, :confirmed
|
33
|
+
sign_in_and_redirect(resource_name, resource)
|
34
|
+
else
|
35
|
+
render_with_scope :new
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<h2>Insert SMS Token</h2>
|
2
|
+
|
3
|
+
<%= form_for(resource, :as => resource_name, :url => consume_sms_activation_path(resource_name), :html => { :method => :post }) do |f| %>
|
4
|
+
|
5
|
+
<p><%=label_tag :sms_token %><br />
|
6
|
+
<%=text_field_tag :sms_token, "", :maxlength => 5%></p>
|
7
|
+
|
8
|
+
<p><%= f.submit "Activate" %></p>
|
9
|
+
<% end %>
|
10
|
+
|
11
|
+
<%= render :partial => "devise/shared/links" %>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<h2>Resend SMS Token</h2>
|
2
|
+
|
3
|
+
<%= form_for(resource, :as => resource_name, :url => sms_activation_path(resource_name), :html => { :method => :post }) do |f| %>
|
4
|
+
<%= devise_error_messages! %>
|
5
|
+
|
6
|
+
<p><%= f.label :email %><br />
|
7
|
+
<%= f.email_field :email %></p>
|
8
|
+
|
9
|
+
<p><%= f.submit "Resend SMS" %></p>
|
10
|
+
<% end %>
|
11
|
+
|
12
|
+
<%= render :partial => "devise/shared/links" %>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
no_phone_associated: "No phone associated"
|
5
|
+
sms_already_confirmed: "This token has been already used"
|
6
|
+
sms_token_invalid: "was not locked"
|
7
|
+
devise:
|
8
|
+
sms_activations:
|
9
|
+
send_token: 'An activation token was sent by SMS to %{phone}.'
|
10
|
+
sms_token_invalid: 'The sms token provided is not valid!'
|
11
|
+
confirmed: 'Your account has been activated. You are now signed in.'
|
12
|
+
sms_activation_required: 'SMS Activation is required'
|
13
|
+
sms_body: 'Your Activation Token is %{sms_confirmation_token}.'
|
14
|
+
unconfirmed_sms: 'Your account need to be activated with an SMS token'
|
15
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "devise"
|
2
|
+
|
3
|
+
$: << File.expand_path("..", __FILE__)
|
4
|
+
|
5
|
+
require "devise_sms_activable/routes"
|
6
|
+
require "devise_sms_activable/schema"
|
7
|
+
require 'devise_sms_activable/controllers/url_helpers'
|
8
|
+
require 'devise_sms_activable/controllers/helpers'
|
9
|
+
require 'devise_sms_activable/rails'
|
10
|
+
|
11
|
+
module Devise
|
12
|
+
mattr_accessor :sms_confirm_within
|
13
|
+
@@sms_confirm_within = 2.days
|
14
|
+
mattr_accessor :sms_confirmation_keys
|
15
|
+
@@sms_confirmation_keys = [:email]
|
16
|
+
|
17
|
+
# Get the sms sender class from the mailer reference object.
|
18
|
+
def self.sms_sender
|
19
|
+
@@sms_sender_ref.get
|
20
|
+
end
|
21
|
+
|
22
|
+
# Set the smser reference object to access the smser.
|
23
|
+
def self.sms_sender=(class_name)
|
24
|
+
@@sms_sender_ref = ActiveSupport::Dependencies.ref(class_name)
|
25
|
+
end
|
26
|
+
|
27
|
+
self.sms_sender = "Devise::SmsSender"
|
28
|
+
end
|
29
|
+
|
30
|
+
Devise.add_module :sms_activable, :model => "models/sms_activable", :controller => :sms_activations, :route => :sms_activation
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module DeviseSmsActivable::Controllers::Helpers
|
2
|
+
protected
|
3
|
+
# Convenience helper to check if user has confirmed the token (and the phone) or not.
|
4
|
+
def require_sms_activated!
|
5
|
+
if(send(:"authenticate_#{resource_name}!"))
|
6
|
+
res=send(:"current_#{resource_name}")
|
7
|
+
fail!(:sms_activation_required) if (!res) || (!res.sms_confirmed?)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
ActionController::Base.send :include, DeviseSmsActivable::Controllers::Helpers
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module DeviseSmsActivable
|
2
|
+
module Controllers
|
3
|
+
module UrlHelpers
|
4
|
+
[:path, :url].each do |path_or_url|
|
5
|
+
[nil, :new_, :create_, :activate_].each do |action|
|
6
|
+
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
7
|
+
def #{action}sms_activation_#{path_or_url}(resource, *args)
|
8
|
+
resource = case resource
|
9
|
+
when Symbol, String
|
10
|
+
resource
|
11
|
+
when Class
|
12
|
+
resource.name.underscore
|
13
|
+
else
|
14
|
+
resource.class.name.underscore
|
15
|
+
end
|
16
|
+
|
17
|
+
send("#{action}\#{resource}_sms_activation_#{path_or_url}", *args)
|
18
|
+
end
|
19
|
+
URL_HELPERS
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Deny user access whenever his account is not active yet. All strategies that inherits from
|
2
|
+
# Devise::Strategies::Authenticatable and uses the validate already check if the user is active?
|
3
|
+
# before actively signing him in. However, we need this as hook to validate the user activity
|
4
|
+
# in each request and in case the user is using other strategies beside Devise ones.
|
5
|
+
Warden::Manager.after_set_user do |record, warden, options|
|
6
|
+
if record && record.respond_to?(:active?) && !record.active?
|
7
|
+
scope = options[:scope]
|
8
|
+
warden.logout(scope)
|
9
|
+
throw :warden, :scope => scope, :message => record.inactive_message
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module DeviseInvitable
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
|
4
|
+
ActiveSupport.on_load(:action_controller) { include DeviseSmsActivable::Controllers::UrlHelpers }
|
5
|
+
ActiveSupport.on_load(:action_view) { include DeviseSmsActivable::Controllers::UrlHelpers }
|
6
|
+
|
7
|
+
config.after_initialize do
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActionDispatch::Routing
|
2
|
+
class Mapper
|
3
|
+
|
4
|
+
protected
|
5
|
+
def devise_sms_activation(mapping, controllers)
|
6
|
+
resource :sms_activation, :only => [:new, :create], :path => mapping.path_names[:sms_activation], :controller => controllers[:sms_activations] do
|
7
|
+
match :consume, :path => mapping.path_names[:consume], :as => :consume
|
8
|
+
get :insert, :path => mapping.path_names[:insert], :as => :insert
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module DeviseSmsActivable
|
2
|
+
module Schema
|
3
|
+
|
4
|
+
def sms_activable
|
5
|
+
apply_devise_schema :phone, String
|
6
|
+
apply_devise_schema :sms_confirmation_token, String, :limit => 5
|
7
|
+
apply_devise_schema :confirmation_sms_sent_at, DateTime
|
8
|
+
apply_devise_schema :sms_confirmed_at, DateTime
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Devise::Schema.send :include, DeviseSmsActivable::Schema
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Generators
|
5
|
+
class DeviseSmsActivableGenerator < ActiveRecord::Generators::Base
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
def copy_devise_migration
|
9
|
+
migration_template "migration.rb", "db/migrate/devise_sms_activable_add_to_#{table_name}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class DeviseSmsActivableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
change_table :<%= table_name %> do |t|
|
4
|
+
t.string :phone
|
5
|
+
t.string :sms_confirmation_token, :limit => 5
|
6
|
+
t.datetime :confirmation_sms_sent_at
|
7
|
+
t.datetime :sms_confirmed_at
|
8
|
+
t.index :sms_confirmation_token # for sms_activable
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
remove_column :<%= table_name %>, :sms_confirmation_token
|
14
|
+
remove_column :<%= table_name %>, :sms_confirmed_at
|
15
|
+
remove_column :<%= table_name %>, :confirmation_sms_sent_at
|
16
|
+
remove_column :<%= table_name %>, :phone
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module DeviseSmsActivable
|
2
|
+
module Generators
|
3
|
+
class DeviseSmsActivableGenerator < Rails::Generators::NamedBase
|
4
|
+
namespace "devise_sms_activable"
|
5
|
+
|
6
|
+
desc "Add :sms_activable directive in the given model. Also generate migration for ActiveRecord"
|
7
|
+
|
8
|
+
# def devise_generate_model
|
9
|
+
# invoke "devise", [name]
|
10
|
+
# end
|
11
|
+
|
12
|
+
def inject_devise_sms_activable_content
|
13
|
+
path = File.join("app", "models", "#{file_path}.rb")
|
14
|
+
inject_into_file(path, "sms_activable, :", :after => "devise :") if File.exists?(path)
|
15
|
+
end
|
16
|
+
|
17
|
+
hook_for :orm
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module DeviseSmsActivable
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../templates", __FILE__)
|
5
|
+
desc "Add DeviseSmsActivable config variables to the Devise initializer and copy DeviseSms locale files to your application."
|
6
|
+
|
7
|
+
# def devise_install
|
8
|
+
# invoke "devise:install"
|
9
|
+
# end
|
10
|
+
|
11
|
+
def add_config_options_to_initializer
|
12
|
+
devise_initializer_path = "config/initializers/devise.rb"
|
13
|
+
if File.exist?(devise_initializer_path)
|
14
|
+
old_content = File.read(devise_initializer_path)
|
15
|
+
|
16
|
+
if old_content.match(Regexp.new(/^\s# ==> Configuration for :sms_activable\n/))
|
17
|
+
false
|
18
|
+
else
|
19
|
+
inject_into_file(devise_initializer_path, :before => " # ==> Configuration for :confirmable\n") do
|
20
|
+
<<-CONTENT
|
21
|
+
# ==> Configuration for :sms_activable
|
22
|
+
# The period the generated sms token is valid, after
|
23
|
+
# this period, the user won't be able to activate.
|
24
|
+
# config.sms_confirm_within = 0.days
|
25
|
+
|
26
|
+
# The keys searched for confirmation values.
|
27
|
+
# config.sms_confirmation_keys = [:email]
|
28
|
+
|
29
|
+
# Your SmsSender class. The provided one uses
|
30
|
+
# moonshado-sms gem so install it and configure
|
31
|
+
# if you want to use it.
|
32
|
+
# A simple instance of the class has been copied in your lib folder
|
33
|
+
# For further informations on using and configuring moonshado-sms gem check
|
34
|
+
# https://github.com/moonshado/moonshado-sms
|
35
|
+
# config.sms_sender = "Devise::SmsSender"
|
36
|
+
|
37
|
+
CONTENT
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def copy_locale
|
44
|
+
copy_file "../../../config/locales/en.yml", "config/locales/devise_sms_activable.en.yml"
|
45
|
+
end
|
46
|
+
|
47
|
+
def copy_default_smser
|
48
|
+
copy_file "lib/sms_sender.rb", "lib/devise_sms_sender.rb"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'generators/devise/views_generator'
|
2
|
+
|
3
|
+
module DeviseSmsActivable
|
4
|
+
module Generators
|
5
|
+
class ViewsGenerator < Devise::Generators::ViewsGenerator
|
6
|
+
source_root File.expand_path("../../../../app/views", __FILE__)
|
7
|
+
desc 'Copies all DeviseSmsActivable views to your application.'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require "devise_sms_activable/hooks"
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Models
|
5
|
+
# SmsActivable is responsible to verify if an account is already confirmed to
|
6
|
+
# sign in, and to send sms with confirmation instructions.
|
7
|
+
# Confirmation instructions are sent to the user phone after creating a
|
8
|
+
# record and when manually requested by a new confirmation instruction request.
|
9
|
+
#
|
10
|
+
# == Options
|
11
|
+
#
|
12
|
+
# Confirmable adds the following options to devise_for:
|
13
|
+
#
|
14
|
+
# * +sms_confirm_within+: the time you want to allow the user to access his account
|
15
|
+
# before confirming it. After this period, the user access is denied. You can
|
16
|
+
# use this to let your user access some features of your application without
|
17
|
+
# confirming the account, but blocking it after a certain period (ie 7 days).
|
18
|
+
# By default confirm_within is 0 days, so the user must confirm before entering.
|
19
|
+
# If you want to allow user to use parts of the site and block others override sms_confirmation_required?
|
20
|
+
# and check manually on selected pages using the require_sms_activated! helper or sms_confirmed? property on record
|
21
|
+
#
|
22
|
+
# == Examples
|
23
|
+
#
|
24
|
+
# User.find(1).sms_confirm! # returns true unless it's already confirmed
|
25
|
+
# User.find(1).sms_confirmed? # true/false
|
26
|
+
# User.find(1).send_sms_token # manually send token
|
27
|
+
#
|
28
|
+
module SmsActivable
|
29
|
+
extend ActiveSupport::Concern
|
30
|
+
|
31
|
+
included do
|
32
|
+
before_create :generate_sms_token, :if => :sms_confirmation_required?
|
33
|
+
after_create :resend_sms_token, :if => :sms_confirmation_required?
|
34
|
+
end
|
35
|
+
|
36
|
+
# Confirm a user by setting it's sms_confirmed_at to actual time. If the user
|
37
|
+
# is already confirmed, add en error to email field
|
38
|
+
def confirm_sms!
|
39
|
+
unless_sms_confirmed do
|
40
|
+
self.sms_confirmation_token = nil
|
41
|
+
self.sms_confirmed_at = Time.now
|
42
|
+
save(:validate => false)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Verifies whether a user is sms-confirmed or not
|
47
|
+
def confirmed_sms?
|
48
|
+
!!sms_confirmed_at
|
49
|
+
end
|
50
|
+
|
51
|
+
# Send confirmation token by sms
|
52
|
+
def send_sms_token
|
53
|
+
if(self.phone?)
|
54
|
+
generate_sms_token! if self.generate_sms_token.nil?
|
55
|
+
::Devise.sms_sender.send_sms(self.phone, I18n.t(:"devise.sms_activations.sms_body", :sms_confirmation_token => self.sms_confirmation_token, :default => self.sms_confirmation_token))
|
56
|
+
else
|
57
|
+
self.errors.add(:sms_confirmation_token, :no_phone_associated)
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Resend sms confirmation token. This method does not need to generate a new token.
|
63
|
+
def resend_sms_token
|
64
|
+
unless_sms_confirmed { send_sms_token }
|
65
|
+
end
|
66
|
+
|
67
|
+
# Overwrites active? from Devise::Models::Activatable for sms confirmation
|
68
|
+
# by verifying whether a user is active to sign in or not. If the user
|
69
|
+
# is already confirmed, it should never be blocked. Otherwise we need to
|
70
|
+
# calculate if the confirm time has not expired for this user.
|
71
|
+
|
72
|
+
def active?
|
73
|
+
super && !sms_confirmation_required? || confirmed_sms? || confirmation_sms_period_valid?
|
74
|
+
end
|
75
|
+
|
76
|
+
# The message to be shown if the account is inactive.
|
77
|
+
def inactive_message
|
78
|
+
!confirmed_sms? ? I18n.t(:"devise.sms_activations.unconfirmed_sms") : super
|
79
|
+
end
|
80
|
+
|
81
|
+
# If you don't want confirmation to be sent on create, neither a code
|
82
|
+
# to be generated, call skip_sms_confirmation!
|
83
|
+
def skip_sms_confirmation!
|
84
|
+
self.sms_confirmed_at = Time.now
|
85
|
+
end
|
86
|
+
|
87
|
+
protected
|
88
|
+
|
89
|
+
# Callback to overwrite if an sms confirmation is required or not.
|
90
|
+
def sms_confirmation_required?
|
91
|
+
!confirmed_sms?
|
92
|
+
end
|
93
|
+
|
94
|
+
# Checks if the confirmation for the user is within the limit time.
|
95
|
+
# We do this by calculating if the difference between today and the
|
96
|
+
# confirmation sent date does not exceed the confirm in time configured.
|
97
|
+
# Confirm_in is a model configuration, must always be an integer value.
|
98
|
+
#
|
99
|
+
# Example:
|
100
|
+
#
|
101
|
+
# # sms_confirm_within = 1.day and sms_confirmation_sent_at = today
|
102
|
+
# confirmation_period_valid? # returns true
|
103
|
+
#
|
104
|
+
# # sms_confirm_within = 5.days and sms_confirmation_sent_at = 4.days.ago
|
105
|
+
# confirmation_period_valid? # returns true
|
106
|
+
#
|
107
|
+
# # sms_confirm_within = 5.days and sms_confirmation_sent_at = 5.days.ago
|
108
|
+
# confirmation_period_valid? # returns false
|
109
|
+
#
|
110
|
+
# # sms_confirm_within = 0.days
|
111
|
+
# confirmation_period_valid? # will always return false
|
112
|
+
#
|
113
|
+
def confirmation_sms_period_valid?
|
114
|
+
confirmation_sms_sent_at && confirmation_sms_sent_at.utc >= self.class.sms_confirm_within.ago
|
115
|
+
end
|
116
|
+
|
117
|
+
# Checks whether the record is confirmed or not, yielding to the block
|
118
|
+
# if it's already confirmed, otherwise adds an error to email.
|
119
|
+
def unless_sms_confirmed
|
120
|
+
unless confirmed_sms?
|
121
|
+
yield
|
122
|
+
else
|
123
|
+
self.errors.add(:sms_confirmation_token, :sms_already_confirmed)
|
124
|
+
false
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# Generates a new random token for confirmation, and stores the time
|
129
|
+
# this token is being generated
|
130
|
+
def generate_sms_token
|
131
|
+
self.sms_confirmed_at = nil
|
132
|
+
self.sms_confirmation_token = self.class.sms_confirmation_token
|
133
|
+
self.confirmation_sms_sent_at = Time.now.utc
|
134
|
+
end
|
135
|
+
|
136
|
+
def generate_sms_token!
|
137
|
+
generate_sms_token && save(:validate => false)
|
138
|
+
end
|
139
|
+
|
140
|
+
module ClassMethods
|
141
|
+
# Attempt to find a user by it's email. If a record is found, send a new
|
142
|
+
# sms token instructions to it. If not user is found, returns a new user
|
143
|
+
# with an email not found error.
|
144
|
+
# Options must contain the user email
|
145
|
+
def send_sms_token(attributes={})
|
146
|
+
sms_confirmable = find_or_initialize_with_errors(sms_confirmation_keys, attributes, :not_found)
|
147
|
+
sms_confirmable.resend_sms_token if sms_confirmable.persisted?
|
148
|
+
sms_confirmable
|
149
|
+
end
|
150
|
+
|
151
|
+
# Find a user by it's sms confirmation token and try to confirm it.
|
152
|
+
# If no user is found, returns a new user with an error.
|
153
|
+
# If the user is already confirmed, create an error for the user
|
154
|
+
# Options must have the sms_confirmation_token
|
155
|
+
def confirm_by_sms_token(sms_confirmation_token)
|
156
|
+
sms_confirmable = find_or_initialize_with_error_by(:sms_confirmation_token, sms_confirmation_token)
|
157
|
+
sms_confirmable.confirm_sms! if sms_confirmable.persisted?
|
158
|
+
sms_confirmable
|
159
|
+
end
|
160
|
+
|
161
|
+
# Generates a small token that can be used conveniently on SMS's.
|
162
|
+
# The token is 5 chars long and uppercased.
|
163
|
+
|
164
|
+
def generate_small_token(column)
|
165
|
+
loop do
|
166
|
+
token = Devise.friendly_token[0,5].upcase
|
167
|
+
break token unless to_adapter.find_first({ column => token })
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# Generate an sms token checking if one does not already exist in the database.
|
172
|
+
def sms_confirmation_token
|
173
|
+
generate_small_token(:sms_confirmation_token)
|
174
|
+
end
|
175
|
+
|
176
|
+
Devise::Models.config(self, :sms_confirm_within, :sms_confirmation_keys)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise_sms_activable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.9
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stefano Valicchia
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-23 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: bundler
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.0.7
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-rails
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 2.5.0
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rails
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.0.0
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: devise
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.1.0
|
58
|
+
type: :runtime
|
59
|
+
version_requirements: *id004
|
60
|
+
description: It adds support for sending activation tokens via SMS and accepting them.
|
61
|
+
email:
|
62
|
+
- stefano.valicchia@gmail.com
|
63
|
+
executables: []
|
64
|
+
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files: []
|
68
|
+
|
69
|
+
files:
|
70
|
+
- app/controllers/devise/sms_activations_controller.rb
|
71
|
+
- app/views/devise/sms_activations/insert.html.erb
|
72
|
+
- app/views/devise/sms_activations/new.html.erb
|
73
|
+
- config/locales/en.yml
|
74
|
+
- lib/devise_sms_activable/controllers/helpers.rb
|
75
|
+
- lib/devise_sms_activable/controllers/url_helpers.rb
|
76
|
+
- lib/devise_sms_activable/hooks.rb
|
77
|
+
- lib/devise_sms_activable/rails.rb
|
78
|
+
- lib/devise_sms_activable/routes.rb
|
79
|
+
- lib/devise_sms_activable/schema.rb
|
80
|
+
- lib/devise_sms_activable/version.rb
|
81
|
+
- lib/devise_sms_activable.rb
|
82
|
+
- lib/generators/active_record/devise_sms_activable_generator.rb
|
83
|
+
- lib/generators/active_record/templates/migration.rb
|
84
|
+
- lib/generators/devise_sms_activable/devise_sms_activable_generator.rb
|
85
|
+
- lib/generators/devise_sms_activable/install_generator.rb
|
86
|
+
- lib/generators/devise_sms_activable/views_generator.rb
|
87
|
+
- lib/generators/mongoid/devise_sms_activable_generator.rb
|
88
|
+
- lib/generators/templates/lib/sms_sender.rb
|
89
|
+
- lib/models/sms_activable.rb
|
90
|
+
- LICENSE
|
91
|
+
- README.rdoc
|
92
|
+
has_rdoc: true
|
93
|
+
homepage: https://github.com/giano/devise_sms_activable
|
94
|
+
licenses: []
|
95
|
+
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options:
|
98
|
+
- --main
|
99
|
+
- README.rdoc
|
100
|
+
- --charset=UTF-8
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 1.8.6
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: 1.3.6
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.5.0
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: An SMS based activation strategy for Devise
|
122
|
+
test_files: []
|
123
|
+
|