Я много исследовал эту тему, но по какой-то причине я не могу выполнить сложность пароля в своем веб-приложении на Ruby on Rails, подписывая пользователя через Facebook. Я установил гем devise и следовал за внедрением защиты гибких паролей Best гибких рельсов и Как пропустить проверку надежности паролей с помощью Devise в Ruby on Rails?.
Мой файл user.rb:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :omniauthable
validates :fullname, presence: true, length: {maximum: 50}
validate :password_complexity
has_many :campaigns
has_many :contents
has_many :influencer_reviews, class_name: "InfluencerReview", foreign_key: "influencer_id"
has_many :brand_reviews, class_name: "BrandReview", foreign_key: "brand_id"
has_many :notifications
has_one :setting
after_create :add_setting
def add_setting
Setting.create(user: self, enable_sms: true, enable_email: true)
end
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.fullname = auth.info.name
user.image = auth.info.image
user.uid = auth.uid
user.provider = auth.provider
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
user.skip_confirmation!
end
end
end
def is_active_influencer
!self.merchant_id.blank?
end
private
def password_complexity
# Regexp extracted from https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a
return if password.blank? || password =~ /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,70}$/
errors.add :password, 'Complexity requirement not met. Length should be 8-70 characters and include: 1 uppercase, 1 lowercase, 1 digit and 1 special character'
end
end