Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

support mongoid aswell #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/devise_security_extension.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#require 'rails/all'
require 'active_record/connection_adapters/abstract/schema_definitions'
require 'active_record/connection_adapters/abstract/schema_definitions' if defined?(ActiveRecord)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

require 'active_support/core_ext/integer'
require 'active_support/ordered_hash'
require 'active_support/concern'
Expand Down Expand Up @@ -80,6 +80,8 @@ module Controllers
end
end

orm = defined?(Mongoid) ? 'mongoid' : 'active_record'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# modules
Devise.add_module :password_expirable, :controller => :password_expirable, :model => 'devise_security_extension/models/password_expirable', :route => :password_expired
Devise.add_module :secure_validatable, :model => 'devise_security_extension/models/secure_validatable'
Expand All @@ -91,6 +93,6 @@ module Controllers
# requires
require 'devise_security_extension/routes'
require 'devise_security_extension/rails'
require 'devise_security_extension/orm/active_record'
require 'devise_security_extension/models/old_password'
require 'devise_security_extension/models/security_question'
require 'devise_security_extension/orm/active_record' if defined?(ActiveRecord)
require "devise_security_extension/models/#{orm}/old_password"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach to addressing the issue with OldPassword and Mongoid.

require "devise_security_extension/models/#{orm}/security_question"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class OldPassword < ActiveRecord::Base
belongs_to :password_archivable, :polymorphic => true
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
class SecurityQuestion < ActiveRecord::Base

end
end
12 changes: 12 additions & 0 deletions lib/devise_security_extension/models/mongoid/old_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class OldPassword
include Mongoid::Document
include Mongoid::Timestamps

field :encrypted_password, :type => String
field :password_salt, :type => String
field :password_archivable_id, :type => Integer
field :password_archivable_type, :type => String

belongs_to :password_archivable, :polymorphic => true
attr_accessible :encrypted_password
end
Copy link

@csroberson csroberson Nov 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment on this model (and I'm not a Ruby on Rails expert, so take it with a grain of salt but) wouldn't defining belongs_to :password_archivable, :polymorphic automatically created the password_archivable_id and password_archivable_type fields?

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SecurityQuestion
include Mongoid::Document
include Mongoid::Timestamps

field :locale, type: String
field :name, type: String
end
3 changes: 0 additions & 3 deletions lib/devise_security_extension/models/old_password.rb

This file was deleted.

6 changes: 4 additions & 2 deletions lib/devise_security_extension/models/secure_validatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def self.included(base)
end

# extra validations
validates :email, :email => email_validation if email_validation # use rails_email_validator or similar
if email_validation # use rails_email_validator or similar
validates :email, :email => email_validation
end
validates :password, :format => { :with => password_regex, :message => :password_format }, :if => :password_required?

# don't allow use same password
Expand Down Expand Up @@ -76,7 +78,7 @@ module ClassMethods
private
def has_uniqueness_validation_of_login?
validators.any? do |validator|
validator.kind_of?(ActiveRecord::Validations::UniquenessValidator) &&
validator.class.name =~ /::Validations::UniquenessValidator$/) &&
validator.attributes.include?(login_attribute)
end
end
Expand Down