Browse Source

Merge pull request #2 from carvil/master

Add generators to make it easier to install and fix deprecation warnings
for_activeadmin
Dmitrii Golub 13 years ago
parent
commit
e9351da36b
  1. 34
      README.md
  2. 8
      lib/generators/active_record/templates/migration.rb
  3. 14
      lib/generators/active_record/two_factor_authentication_generator.rb
  4. 17
      lib/generators/two_factor_authentication/two_factor_authentication_generator.rb
  5. 2
      lib/two_factor_authentication/controllers/helpers.rb
  6. 2
      lib/two_factor_authentication/models/two_factor_authenticatable.rb
  7. 2
      lib/two_factor_authentication/orm/active_record.rb
  8. 5
      lib/two_factor_authentication/schema.rb

34
README.md

@ -9,7 +9,33 @@
## Configuration ## Configuration
To enable two factor authentication for User model, you should add two_factor_authentication to your devise line, like: ### Initial Setup
In a Rails environment, require the gem in your Gemfile:
gem 'two_factor_authentication', git: "http://github.com/Houdini/two_factor_authentication.git"
Once that's done, run:
bundle install
### Automatic installation
In order to add two factor authorisation to a model, run the command:
bundle exec rails g two_factor_authentication MODEL
Where MODEL is your model name (e.g. User or Admin). This generator will add `:two_factor_authenticatable` to your model
and create a migration in `db/migrate/`, which will add `::second_factor_pass_code` and `:second_factor_attempts_count` to your table.
Finally, run the migration with:
bundle exec rake db:migrate
### Manual installation
To manually enable two factor authentication for the User model, you should add two_factor_authentication to your devise line, like:
```ruby ```ruby
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
@ -32,7 +58,9 @@ Possible random patterns
see more https://github.com/benburkert/randexp see more https://github.com/benburkert/randexp
By default second factor authentication enabled for each user, you can change it with this method in your User mdoel: ### Customisation
By default second factor authentication enabled for each user, you can change it with this method in your User model:
```ruby ```ruby
def need_two_factor_authentication?(request) def need_two_factor_authentication?(request)
@ -50,4 +78,4 @@ Your send sms logic should be in this method in your User model:
end end
``` ```
This example just puts code in logs This example just puts the code in the logs.

8
lib/generators/active_record/templates/migration.rb

@ -0,0 +1,8 @@
class TwoFactorAuthenticationAddTo<%= table_name.camelize %> < ActiveRecord::Migration
def change
change_table :<%= table_name %> do |t|
t.string :second_factor_pass_code , :limit => 32
t.integer :second_factor_attempts_count, :default => 0
end
end
end

14
lib/generators/active_record/two_factor_authentication_generator.rb

@ -0,0 +1,14 @@
require 'rails/generators/active_record'
module ActiveRecord
module Generators
class TwoFactorAuthenticationGenerator < ActiveRecord::Generators::Base
source_root File.expand_path("../templates", __FILE__)
def copy_two_factor_authentication_migration
migration_template "migration.rb", "db/migrate/two_factor_authentication_add_to_#{table_name}"
end
end
end
end

17
lib/generators/two_factor_authentication/two_factor_authentication_generator.rb

@ -0,0 +1,17 @@
module TwoFactorAuthenticatable
module Generators
class TwoFactorAuthenticationGenerator < Rails::Generators::NamedBase
namespace "two_factor_authentication"
desc "Adds :two_factor_authenticable directive in the given model. It also generates an active record migration."
def inject_two_factor_authentication_content
path = File.join("app", "models", "#{file_path}.rb")
inject_into_file(path, "two_factor_authenticatable, :", :after => "devise :") if File.exists?(path)
end
hook_for :orm
end
end
end

2
lib/two_factor_authentication/controllers/helpers.rb

@ -7,7 +7,6 @@ module TwoFactorAuthentication
before_filter :handle_two_factor_authentication before_filter :handle_two_factor_authentication
end end
module InstanceMethods
private private
def handle_two_factor_authentication def handle_two_factor_authentication
@ -31,4 +30,3 @@ module TwoFactorAuthentication
end end
end end
end end
end

2
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -8,7 +8,6 @@ module Devise
::Devise::Models.config(self, :login_code_random_pattern, :max_login_attempts) ::Devise::Models.config(self, :login_code_random_pattern, :max_login_attempts)
end end
module InstanceMethods
def need_two_factor_authentication? def need_two_factor_authentication?
true true
end end
@ -27,4 +26,3 @@ module Devise
end end
end end
end end
end

2
lib/two_factor_authentication/orm/active_record.rb

@ -1,10 +1,8 @@
module TwoFactorAuthentication module TwoFactorAuthentication
module Orm module Orm
module ActiveRecord module ActiveRecord
module Schema module Schema
include TwoFactorAuthentication::Schema include TwoFactorAuthentication::Schema
end end
end end
end end

5
lib/two_factor_authentication/schema.rb

@ -1,7 +1,10 @@
module TwoFactorAuthentication module TwoFactorAuthentication
module Schema module Schema
def two_factor_authenticatable def second_factor_pass_code
apply_devise_schema :second_factor_pass_code, String, :limit => 32 apply_devise_schema :second_factor_pass_code, String, :limit => 32
end
def second_factor_attempts_count
apply_devise_schema :second_factor_attempts_count, Integer, :default => 0 apply_devise_schema :second_factor_attempts_count, Integer, :default => 0
end end
end end

Loading…
Cancel
Save