From 0733d42439d295c7599cb89ff5f2062488cee204 Mon Sep 17 00:00:00 2001 From: Marcus Oladell Date: Fri, 8 Mar 2013 12:35:46 -0600 Subject: [PATCH 1/5] Adding Sequel support to sorcery --- .gitignore | 8 ++--- Gemfile | 1 + VERSION | 2 +- lib/sorcery.rb | 10 ++++-- lib/sorcery/model/adapters/sequel.rb | 49 ++++++++++++++++++++++++++++ sorcery.gemspec | 4 +++ 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 lib/sorcery/model/adapters/sequel.rb diff --git a/.gitignore b/.gitignore index 5993b73a..8c4f6fc0 100644 --- a/.gitignore +++ b/.gitignore @@ -33,16 +33,16 @@ pkg # # For MacOS: # -#.DS_Store +.DS_Store # # For TextMate #*.tmproj tmtags # # For emacs: -#*~ -#\#* -#.\#* +*~ +\#* +.\#* # # For vim: #*.swp diff --git a/Gemfile b/Gemfile index df58fd4d..c9c721f0 100644 --- a/Gemfile +++ b/Gemfile @@ -24,4 +24,5 @@ group :development do gem 'capybara' gem 'mongo_mapper' gem 'mongoid', "~> 2.4.4" + gem 'sequel', '~> 3.40.0' end diff --git a/VERSION b/VERSION index c18d72be..53a48a1e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.8.1 \ No newline at end of file +0.8.2 \ No newline at end of file diff --git a/lib/sorcery.rb b/lib/sorcery.rb index 47fd9312..0278098d 100644 --- a/lib/sorcery.rb +++ b/lib/sorcery.rb @@ -6,6 +6,7 @@ module Adapters autoload :ActiveRecord, 'sorcery/model/adapters/active_record' autoload :Mongoid, 'sorcery/model/adapters/mongoid' autoload :MongoMapper, 'sorcery/model/adapters/mongo_mapper' + autoload :Sequel, 'sorcery/model/adapters/sequel' end module Submodules autoload :UserActivation, 'sorcery/model/submodules/user_activation' @@ -65,7 +66,12 @@ module Internal ActiveRecord::Base.send(:include, Sorcery::Model) ActiveRecord::Base.send(:include, Sorcery::Model::Adapters::ActiveRecord) end - + + if defined?(Sequel) + Sequel::Model.send(:include, Sorcery::Model) + Sequel::Model.send(:include, Sorcery::Model::Adapters::Sequel) + end + if defined?(Mongoid) Mongoid::Document.module_eval do included do @@ -81,4 +87,4 @@ module Internal end require 'sorcery/engine' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 -end \ No newline at end of file +end diff --git a/lib/sorcery/model/adapters/sequel.rb b/lib/sorcery/model/adapters/sequel.rb new file mode 100644 index 00000000..7591e1f2 --- /dev/null +++ b/lib/sorcery/model/adapters/sequel.rb @@ -0,0 +1,49 @@ +module Sorcery + module Model + module Adapters + module Sequel + def self.included(klass) + klass.extend ClassMethods + klass.send(:include, InstanceMethods) + end + + module InstanceMethods + def update_many_attributes(attrs) + attrs.each do |name, value| + self.send(:"#{name}=", value) + end + primary_key = self.class.primary_key + self.dataset.where(:"#{primary_key}" => self.send(:"#{primary_key}")).all{ |a| a.update(attrs) } + end + + def update_single_attribute(name, value) + update_many_attributes(name => value) + end + end + + module ClassMethods + def column_name(attribute) + return "LOWER(#{attribute})" if (@sorcery_config.downcase_username_before_authenticating) + return "#{attribute}" + end + + def find_by_credentials(credentials) + sql = @sorcery_config.username_attribute_names.map{|attribute| column_name(attribute) + " = :login"} + dataset.where(sql.join(' OR '), :login => credentials[0]).first + end + + def find_by_sorcery_token(token_attr_name, token) + dataset.where("#{token_attr_name} = ?", token).first + end + + def get_current_users + config = sorcery_config + dataset.where("#{config.last_activity_at_attribute_name} IS NOT NULL") \ + .where("#{config.last_logout_at_attribute_name} IS NULL OR #{config.last_activity_at_attribute_name} > #{config.last_logout_at_attribute_name}") \ + .where("#{config.last_activity_at_attribute_name} > ? ", config.activity_timeout.seconds.ago.utc.to_s(:db)) + end + end + end + end + end +end diff --git a/sorcery.gemspec b/sorcery.gemspec index 72c7bcfb..4f24ffa7 100644 --- a/sorcery.gemspec +++ b/sorcery.gemspec @@ -66,6 +66,7 @@ Gem::Specification.new do |s| "lib/sorcery/model/adapters/active_record.rb", "lib/sorcery/model/adapters/mongo_mapper.rb", "lib/sorcery/model/adapters/mongoid.rb", + "lib/sorcery/model/adapters/sequel.rb", "lib/sorcery/model/submodules/activity_logging.rb", "lib/sorcery/model/submodules/brute_force_protection.rb", "lib/sorcery/model/submodules/external.rb", @@ -329,6 +330,7 @@ Gem::Specification.new do |s| s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, ["~> 2.4.4"]) + s.add_development_dependency(%q, ["~> 3.40.0"]) else s.add_dependency(%q, ["~> 0.4.4"]) s.add_dependency(%q, ["~> 0.8.0"]) @@ -348,6 +350,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, ["~> 2.4.4"]) + s.add_dependency(%q, ["~> 3.40.0"]) end else s.add_dependency(%q, ["~> 0.4.4"]) @@ -368,6 +371,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, ["~> 2.4.4"]) + s.add_dependency(%q, ["~> 3.40.0"]) end end From c56790e03006046b89a91f9096e1dc8297e54ee9 Mon Sep 17 00:00:00 2001 From: Marcus Oladell Date: Fri, 8 Mar 2013 12:40:08 -0600 Subject: [PATCH 2/5] Corrected call to User::DataSet --- lib/sorcery/model/adapters/sequel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sorcery/model/adapters/sequel.rb b/lib/sorcery/model/adapters/sequel.rb index 7591e1f2..7f508a7b 100644 --- a/lib/sorcery/model/adapters/sequel.rb +++ b/lib/sorcery/model/adapters/sequel.rb @@ -13,7 +13,7 @@ def update_many_attributes(attrs) self.send(:"#{name}=", value) end primary_key = self.class.primary_key - self.dataset.where(:"#{primary_key}" => self.send(:"#{primary_key}")).all{ |a| a.update(attrs) } + self.class.dataset.where(:"#{primary_key}" => self.send(:"#{primary_key}")).all{ |a| a.update(attrs) } end def update_single_attribute(name, value) From 85abe00890ae4ad7d4ce135e7b12240fe8a4b48b Mon Sep 17 00:00:00 2001 From: Marcus Oladell Date: Fri, 8 Mar 2013 12:53:11 -0600 Subject: [PATCH 3/5] Added dependency on sequel_simple_callbacks to support ActiveRecord before_create and after_create class hooks --- lib/sorcery/model/adapters/sequel.rb | 1 + sorcery.gemspec | 3 +++ 2 files changed, 4 insertions(+) diff --git a/lib/sorcery/model/adapters/sequel.rb b/lib/sorcery/model/adapters/sequel.rb index 7f508a7b..90e83646 100644 --- a/lib/sorcery/model/adapters/sequel.rb +++ b/lib/sorcery/model/adapters/sequel.rb @@ -5,6 +5,7 @@ module Sequel def self.included(klass) klass.extend ClassMethods klass.send(:include, InstanceMethods) + klass.send(:plugin, SequelSimpleCallbacks) end module InstanceMethods diff --git a/sorcery.gemspec b/sorcery.gemspec index 4f24ffa7..bbef57bf 100644 --- a/sorcery.gemspec +++ b/sorcery.gemspec @@ -331,6 +331,7 @@ Gem::Specification.new do |s| s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, ["~> 2.4.4"]) s.add_development_dependency(%q, ["~> 3.40.0"]) + s.add_development_dependency(%q, ["~> 0.1.2"]) else s.add_dependency(%q, ["~> 0.4.4"]) s.add_dependency(%q, ["~> 0.8.0"]) @@ -351,6 +352,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, ["~> 2.4.4"]) s.add_dependency(%q, ["~> 3.40.0"]) + s.add_dependency(%q, ["~> 0.1.2"]) end else s.add_dependency(%q, ["~> 0.4.4"]) @@ -372,6 +374,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, ["~> 2.4.4"]) s.add_dependency(%q, ["~> 3.40.0"]) + s.add_dependency(%q, ["~> 0.1.2"]) end end From 7aa06ccbe2bfc7f215b2e641e886f19892e1e1f5 Mon Sep 17 00:00:00 2001 From: Marcus Oladell Date: Fri, 8 Mar 2013 13:04:58 -0600 Subject: [PATCH 4/5] Removed called to plugin --- lib/sorcery/model/adapters/sequel.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/sorcery/model/adapters/sequel.rb b/lib/sorcery/model/adapters/sequel.rb index 90e83646..7f508a7b 100644 --- a/lib/sorcery/model/adapters/sequel.rb +++ b/lib/sorcery/model/adapters/sequel.rb @@ -5,7 +5,6 @@ module Sequel def self.included(klass) klass.extend ClassMethods klass.send(:include, InstanceMethods) - klass.send(:plugin, SequelSimpleCallbacks) end module InstanceMethods From ab8c4cee4bccafbb0933bf941d0be8db33b34d4d Mon Sep 17 00:00:00 2001 From: Marcus Oladell Date: Fri, 8 Mar 2013 13:26:28 -0600 Subject: [PATCH 5/5] Turned plugin on for SequelSimpleCallbacks to support ActiveRecord class level hook API --- Gemfile | 1 + Gemfile.lock | 4 ++++ lib/sorcery/model/adapters/sequel.rb | 3 +++ 3 files changed, 8 insertions(+) diff --git a/Gemfile b/Gemfile index c9c721f0..6e6798f0 100644 --- a/Gemfile +++ b/Gemfile @@ -25,4 +25,5 @@ group :development do gem 'mongo_mapper' gem 'mongoid', "~> 2.4.4" gem 'sequel', '~> 3.40.0' + gem 'sequel_simple_callbacks', "~> 0.1.2" end diff --git a/Gemfile.lock b/Gemfile.lock index 0723640c..cf8c240d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -132,6 +132,8 @@ GEM ffi (~> 1.0) multi_json (~> 1.0) rubyzip + sequel (3.40.0) + sequel_simple_callbacks (0.1.2) simplecov (0.6.1) multi_json (~> 1.0) simplecov-html (~> 0.5.3) @@ -171,6 +173,8 @@ DEPENDENCIES rails (>= 3.0.0) rspec (~> 2.5.0) rspec-rails (~> 2.5.0) + sequel (~> 3.40.0) + sequel_simple_callbacks (~> 0.1.2) simplecov (>= 0.3.8) sqlite3-ruby timecop diff --git a/lib/sorcery/model/adapters/sequel.rb b/lib/sorcery/model/adapters/sequel.rb index 7f508a7b..e66892c5 100644 --- a/lib/sorcery/model/adapters/sequel.rb +++ b/lib/sorcery/model/adapters/sequel.rb @@ -5,6 +5,9 @@ module Sequel def self.included(klass) klass.extend ClassMethods klass.send(:include, InstanceMethods) + # This is required for compatibility with ActiveRecord API for class hooks + # (i.e. before_create, after_create, etc.) + klass.send(:plugin, SequelSimpleCallbacks) end module InstanceMethods