Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: master
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 17 commits
  • 23 files changed
  • 1 commit comment
  • 7 contributors
Showing with 145 additions and 167 deletions.
  1. +1 −1  MIT-LICENSE → LICENSE
  2. +1 −1  README.md
  3. +3 −5 app/controllers/devise/autosignin_controller.rb
  4. +1 −1  config/locales/devise.en.yml
  5. +17 −0 devise-autosigninable.gemspec
  6. +0 −21 devise_autosigninable.gemspec
  7. +0 −7 generators/devise_autosigninable/devise_autosigninable_generator.rb
  8. +1 −0  lib/devise-autosigninable.rb
  9. +21 −0 lib/devise/autosigninable.rb
  10. +8 −10 lib/{devise_autosigninable/view_helpers.rb → devise/autosigninable/helpers.rb}
  11. +48 −56 lib/{devise_autosigninable → devise/autosigninable}/model.rb
  12. +11 −0 lib/devise/autosigninable/routes.rb
  13. +0 −2  lib/{devise_autosigninable → devise/autosigninable}/strategy.rb
  14. +5 −0 lib/devise/autosigninable/version.rb
  15. +0 −25 lib/devise_autosigninable.rb
  16. +0 −23 lib/devise_autosigninable/routes.rb
  17. +0 −7 lib/devise_autosigninable/schema.rb
  18. +0 −3  lib/devise_autosigninable/version.rb
  19. 0  { → lib}/generators/devise_autosigninable/USAGE
  20. +25 −0 lib/generators/devise_autosigninable/devise_autosigninable_generator.rb
  21. +1 −1  { → lib}/generators/devise_autosigninable/templates/migration.rb
  22. +2 −2 lib/tasks/devise_autosigninable.rake
  23. +0 −2  rails/init.rb
View
2  MIT-LICENSE → LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2011 [name of plugin creator]
+Copyright (c) 2014 Andrey Deryabin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
2  README.md
@@ -18,7 +18,7 @@ Installation
* Add devise_autosigninable to Gemfile:
- `gem 'devise_autosigininable', '1.0.0'`
+ `gem 'devise_autosigninable', '1.0.0'`
* Add :autosigninable to your Devise modules in model, for example:
View
8 app/controllers/devise/autosignin_controller.rb
@@ -1,8 +1,6 @@
-class Devise::AutosigninController < ApplicationController
-
- include Devise::Controllers::InternalHelpers
+class Devise::AutosigninController < DeviseController
include Devise::Autosigninable::Helpers
-
+
def create
sign_out(resource_name)
if resource = warden.authenticate!(:scope => resource_name)
@@ -10,6 +8,6 @@ def create
else
set_now_flash_message :alert, (warden.message || :invalid)
end
- sign_in_and_redirect_to_url(resource, params[:return_to])
+ sign_in_and_redirect_to_url(resource, params[:return_to])
end
end
View
2  config/locales/devise.en.yml
@@ -1,4 +1,4 @@
-ru:
+en:
devise:
autosignin:
signed_in: "You have signed in successfully"
View
17 devise-autosigninable.gemspec
@@ -0,0 +1,17 @@
+require File.expand_path('../lib/devise/autosigninable/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ['Andrey Deryabin']
+ gem.description = 'It adds support to be logged in by uniq link'
+ gem.summary = 'It adds support to be logged in by uniq link. It useful for mailing and access from admin panel'
+ gem.homepage = 'http://github.com/plataformatec/devise-autosigninable'
+ gem.license = 'MIT'
+
+ gem.files = Dir['LICENSE', 'README.md', 'lib/**/*']
+ gem.test_files = Dir['test/**/*.rb']
+ gem.name = 'devise-autosigninable'
+ gem.require_paths = ['lib']
+ gem.version = Devise::Autosigninable::VERSION
+
+ gem.add_dependency 'devise', '>= 2.1.0'
+end
View
21 devise_autosigninable.gemspec
@@ -1,21 +0,0 @@
-# -*- encoding: utf-8 -*-
-$:.push File.expand_path("../lib", __FILE__)
-require "devise_autosigninable/version"
-
-Gem::Specification.new do |s|
- s.name = %q{devise_autosigninable}
- s.version = DeviseAutosigninable::VERSION
- s.platform = Gem::Platform::RUBY
- s.authors = ["Andrey Deryabin"]
- s.date = Date.today
- s.description = %q{It adds support to be logged in by uniq link.}
- s.summary = %q{It adds support to be logged in by uniq link. It useful for mailing and access from admin panel.}
- s.email = %q{deriabin@gmail.com}
- s.homepage = %q{http://github.com/evilmartians/devise_autosigninable}
-
- s.files = `git ls-files`.split("\n")
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
- s.require_paths = ["lib"]
-end
-
View
7 generators/devise_autosigninable/devise_autosigninable_generator.rb
@@ -1,7 +0,0 @@
-class DeviseAutosigninableGenerator < Rails::Generator::NamedBase
- def manifest
- record do |m|
- m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "add_autosigninable_to_#{table_name}"
- end
- end
-end
View
1  lib/devise-autosigninable.rb
@@ -0,0 +1 @@
+require 'devise/autosigninable'
View
21 lib/devise/autosigninable.rb
@@ -0,0 +1,21 @@
+require 'devise'
+
+begin
+ Rails::Engine
+rescue
+else
+ module Devise::Autosigninable
+ class Engine < Rails::Engine
+ end
+ end
+end
+
+Devise.add_module :autosigninable,
+ :controller => :autosignin,
+ :model => 'devise/autosigninable/model',
+ :route => :autosignin,
+ :strategy => true
+
+require 'devise/autosigninable/routes'
+require 'devise/autosigninable/helpers'
+require 'devise/autosigninable/strategy'
View
18 lib/devise_autosigninable/view_helpers.rb → lib/devise/autosigninable/helpers.rb
@@ -1,24 +1,22 @@
-# encoding: utf-8
require 'devise/mapping'
module Devise #:nodoc:
module Autosigninable #:nodoc:
-
module Helpers
- # Create the link to autosignin url based on resource with given link_text
- # example: link_to_autosignin(user, user.email)
+ # Create the link to autosignin url based on resource with given
+ # link_text.
+ # Example: link_to_autosignin(user, user.email).
def link_to_autosignin(resource, link_text, options={})
link_to link_text, auto_signin_url_for(resource) , options
end
-
- # return autosignin url for given resource
+ # Return autosignin url for given resource
def auto_signin_url_for(resource)
resource_name = resource.class.to_s.downcase
send("#{resource_name}_autosignin_url",
- {:"#{resource_name}_id" => resource.id,
- :autosignin_token => resource.autosignin_token}
+ { :"#{resource_name}_id" => resource.id,
+ :autosignin_token => resource.autosignin_token }
)
end
@@ -26,7 +24,7 @@ def auto_signin_url_for(resource)
# then to the url specified by after_sign_in_path_for.
#
# If resource is blank than tries redirect to given url
- # or root url
+ # or root url.
def sign_in_and_redirect_to_url(resource = nil, url=nil)
if resource
sign_out(resource)
@@ -40,4 +38,4 @@ def sign_in_and_redirect_to_url(resource = nil, url=nil)
end
end
-::ActionView::Base.send :include, Devise::Autosigninable::Helpers
+::ActionView::Base.send :include, Devise::Autosigninable::Helpers
View
104 lib/devise_autosigninable/model.rb → lib/devise/autosigninable/model.rb
@@ -1,18 +1,56 @@
module Devise
module Models
module Autosigninable
-# include Devise::Models::Lockable
+ extend ActiveSupport::Concern
- def self.included(base)
- base.extend ClassMethods
+ included do
+ before_create :reset_autosignin_token
- base.class_eval do
- before_create :reset_autosignin_token
-
-# indicator to expire autosignin token
+ # Indicator: expire autosignin token.
mattr_accessor :autosignin_expire
@@autosignin_expire = false
-
+ end
+
+ module ClassMethods
+
+ RETRY_COUNT = 20
+
+ # Generate autosignin tokens unless already exists and save the records.
+ def ensure_all_autosignin_tokens(batch_size=500)
+ user_count = count(:conditions => { :autosignin_token => nil })
+ find_in_batches(:batch_size => batch_size,
+ :conditions => {
+ :autosignin_token => nil }) do |group|
+ group.each { |user| user.ensure_autosignin_token! }
+ end
+ end
+
+ # Generate autosignin tokens and save the records.
+ def reset_all_autosignin_tokens(batch_size=500)
+ user_count = count
+ find_in_batches(:batch_size => batch_size) do |group|
+ group.each { |user| user.reset_autosignin_token! }
+ end
+ end
+
+ # generation random autosignin token
+ def autosignin_token(uniq = false, field = 'autosignin_token')
+ if uniq
+ RETRY_COUNT.times do
+ token = Digest::SHA1.hexdigest("--#{Time.now.utc}--#{rand}--")
+ return token unless exists? field => token
+ end
+ raise Exception.new("Couldn't generate #{self.class}:#{field} for #{RETRY_COUNT} times")
+ else
+ Digest::SHA1.hexdigest("--#{Time.now.utc}--#{rand}--")
+ end
+ end
+
+ # Authenticate a user based on authentication token.
+ def authenticate_with_autosignin_token(attributes = {})
+ key = attributes[primary_key]
+ resource = where(primary_key => key).first
+ resource if resource.try(:valid_for_autosignin_token_authentication?, attributes)
end
end
@@ -64,55 +102,9 @@ def valid_for_autosignin_token_authentication?(attributes)
end
end
reset_autosignin_token! if self.class.autosignin_expire
- save(false) if changed?
+ save(:validate => false) if changed?
result
end
-
-
- module ClassMethods
-
- RETRY_COUNT = 20
-
- # Generate autosignin tokens unless already exists and save the records.
- def ensure_all_autosignin_tokens(batch_size=500)
- user_count = count( :conditions=>{:autosignin_token => nil})
- find_in_batches(:batch_size =>batch_size, :conditions=>{:autosignin_token => nil}) do |group|
- group.each { |user| user.ensure_autosignin_token! }
- end
- end
-
- # Generate autosignin tokens and save the records.
- def reset_all_autosignin_tokens(batch_size=500)
- user_count = count
- find_in_batches(:batch_size =>batch_size) do |group|
- group.each { |user| user.reset_autosignin_token! }
- end
- end
-
- # generation random autosignin token
- def autosignin_token(uniq = false, field = 'autosignin_token')
- if uniq
- RETRY_COUNT.times do
- token = Digest::SHA1.hexdigest("--#{Time.now.utc}--#{rand}--")
- return token unless exists? field => token
- end
- raise Exception.new("Couldn't generate #{self.class}:#{field} for #{RETRY_COUNT} times")
- else
- Digest::SHA1.hexdigest("--#{Time.now.utc}--#{rand}--")
- end
- end
-
- # Authenticate a user based on authentication token.
- def authenticate_with_autosignin_token(attributes={})
- resource = find_by_id(attributes[self.to_s.foreign_key.to_sym])
- if resource.try(:valid_for_autosignin_token_authentication?, attributes)
- resource
- else
- nil
- end
- end
- end
end
end
-
-end
+end
View
11 lib/devise/autosigninable/routes.rb
@@ -0,0 +1,11 @@
+ActionDispatch::Routing::Mapper.class_eval do
+ protected
+
+ # Setup routes for +AutosigninController+.
+ def devise_autosignin(mapping, controllers)
+ get "/:#{mapping.name}_id/autosignin/:autosignin_token",
+ :controller => controllers[:autosignin],
+ :action => 'create',
+ :as => :autosignin
+ end
+end
View
2  lib/devise_autosigninable/strategy.rb → lib/devise/autosigninable/strategy.rb
@@ -3,7 +3,6 @@
module Devise
module Strategies
class Autosigninable < Base
-
def valid?
valid_controller? && valid_params? && mapping.to.respond_to?('authenticate_with_autosignin_token')
end
@@ -25,7 +24,6 @@ def valid_controller?
def valid_params?
params[:autosignin_token].present?
end
-
end
end
end
View
5 lib/devise/autosigninable/version.rb
@@ -0,0 +1,5 @@
+module Devise
+ module Autosigninable
+ VERSION = '1.0.3'
+ end
+end
View
25 lib/devise_autosigninable.rb
@@ -1,25 +0,0 @@
-# DeviseAutosigninable
-require 'devise'
-
-begin
- Rails::Engine
-rescue
-else
- module DeviseAutosigninable
- class Engine < Rails::Engine
- end
- end
-
-end
-
-
-Devise.add_module :autosigninable,
- :strategy => true,
- :controller => :autosignin,
- :model => 'devise_autosigninable/model',
- :route => :autosigninable
-
-require 'devise_autosigninable/routes'
-require 'devise_autosigninable/schema'
-require 'devise_autosigninable/view_helpers'
-require 'devise_autosigninable/strategy'
View
23 lib/devise_autosigninable/routes.rb
@@ -1,23 +0,0 @@
-if ActionController::Routing.name =~ /ActionDispatch/
- #RAILS 3
- ActionDispatch::Routing::Mapper.class_eval do
- protected
-
- # Setup routes for +AutosigninController+.
- def devise_autosigninable(mapping, controllers)
- match "/:#{mapping.name}_id/autosignin/:autosignin_token" => 'devise/autosignin#create',
- :as => "autosignin", :via => :get
- end
- end
-
-else
- #RAILS 2
- ActionController::Routing::RouteSet::Mapper.class_eval do
- protected
-
- # Setup routes for +AutosigninController+.
- def autosigninable(routes, mapping)
- routes.autosignin "/:#{mapping.name.to_s}_id/autosignin/:autosignin_token", :controller => 'devise/autosignin', :action => 'create', :conditions => { :method => :get }
- end
- end
-end
View
7 lib/devise_autosigninable/schema.rb
@@ -1,7 +0,0 @@
-Devise::Schema.class_eval do
-
- # add to migration neccessary fields for autosignin
- def autosigninable
- apply_schema :autosignin_token, String, :limit=>40
- end
-end
View
3  lib/devise_autosigninable/version.rb
@@ -1,3 +0,0 @@
-module DeviseAutosigninable
- VERSION = "1.0.0"
-end
View
0  generators/devise_autosigninable/USAGE → lib/generators/devise_autosigninable/USAGE
File renamed without changes
View
25 lib/generators/devise_autosigninable/devise_autosigninable_generator.rb
@@ -0,0 +1,25 @@
+require 'rails/generators'
+require 'rails/generators/migration'
+
+class DeviseAutosigninableGenerator < Rails::Generators::NamedBase
+
+ source_root File.expand_path('../templates', __FILE__)
+
+ include Rails::Generators::Migration
+
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
+ migration_number += 1
+ migration_number.to_s
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+
+ def copy_migration
+ migration_template 'migration.rb',
+ "db/migrate/add_autosigninable_to_#{table_name}"
+ end
+
+end
View
2  ...tors/devise_autosigninable/templates/migration.rb → ...tors/devise_autosigninable/templates/migration.rb
@@ -1,7 +1,7 @@
class AddAutosigninableTo<%= table_name.camelize %> < ActiveRecord::Migration
def self.up
change_table :<%= table_name %> do |t|
- t.autosigninable
+ t.string :autosignin_token, :limit => 40
end
add_index :<%= table_name %>, :autosignin_token, :unique => true
end
View
4 lib/tasks/devise_autosigninable.rake
@@ -1,13 +1,13 @@
namespace :devise do
namespace :autosigninable do
desc 'Generate missed autosignin tokens, by default for User model'
- task :ensure, :model, :needs => [:environment] do |t, args|
+ task :ensure, [:model] => :environment do |t, args|
args.with_defaults(:model => "User")
args.model.camelize.constantize.ensure_all_autosignin_tokens
end
desc 'Reset all autosignin tokens, by default for User model'
- task :reset, :model, :needs => [:environment] do |t, args|
+ task :reset, [:model] => :environment do |t, args|
args.with_defaults(:model => "User")
args.model.camelize.constantize.reset_all_autosignin_tokens
end
View
2  rails/init.rb
@@ -1,2 +0,0 @@
-# Include hook code here
-require 'devise_autosigninable'

Showing you all comments on commits in this comparison.

@brainopia
Collaborator

Thanks :)

Something went wrong with that request. Please try again.