Use alias_attribute to define User#two_factor_enabled
This commit is contained in:
parent
aedb546964
commit
dcfce8b198
2 changed files with 3 additions and 38 deletions
|
@ -80,6 +80,7 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
devise :two_factor_authenticatable,
|
devise :two_factor_authenticatable,
|
||||||
otp_secret_encryption_key: File.read(Rails.root.join('.secret')).chomp
|
otp_secret_encryption_key: File.read(Rails.root.join('.secret')).chomp
|
||||||
|
alias_attribute :two_factor_enabled, :otp_required_for_login
|
||||||
|
|
||||||
devise :two_factor_backupable, otp_number_of_backup_codes: 10
|
devise :two_factor_backupable, otp_number_of_backup_codes: 10
|
||||||
serialize :otp_backup_codes, JSON
|
serialize :otp_backup_codes, JSON
|
||||||
|
@ -198,8 +199,8 @@ class User < ActiveRecord::Base
|
||||||
scope :active, -> { with_state(:active) }
|
scope :active, -> { with_state(:active) }
|
||||||
scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all }
|
scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all }
|
||||||
scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM members)') }
|
scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM members)') }
|
||||||
scope :with_two_factor, -> { where(otp_required_for_login: true) }
|
scope :with_two_factor, -> { where(two_factor_enabled: true) }
|
||||||
scope :without_two_factor, -> { where(otp_required_for_login: false) }
|
scope :without_two_factor, -> { where(two_factor_enabled: false) }
|
||||||
|
|
||||||
#
|
#
|
||||||
# Class methods
|
# Class methods
|
||||||
|
@ -309,18 +310,6 @@ class User < ActiveRecord::Base
|
||||||
@reset_token
|
@reset_token
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check if the user has enabled Two-factor Authentication
|
|
||||||
def two_factor_enabled?
|
|
||||||
otp_required_for_login
|
|
||||||
end
|
|
||||||
|
|
||||||
# Set whether or not Two-factor Authentication is enabled for the current user
|
|
||||||
#
|
|
||||||
# setting - Boolean
|
|
||||||
def two_factor_enabled=(setting)
|
|
||||||
self.otp_required_for_login = setting
|
|
||||||
end
|
|
||||||
|
|
||||||
def namespace_uniq
|
def namespace_uniq
|
||||||
namespace_name = self.username
|
namespace_name = self.username
|
||||||
existing_namespace = Namespace.by_path(namespace_name)
|
existing_namespace = Namespace.by_path(namespace_name)
|
||||||
|
|
|
@ -210,30 +210,6 @@ describe User do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#two_factor_enabled' do
|
|
||||||
it 'returns two-factor authentication status' do
|
|
||||||
enabled = build_stubbed(:user, two_factor_enabled: true)
|
|
||||||
disabled = build_stubbed(:user)
|
|
||||||
|
|
||||||
expect(enabled).to be_two_factor_enabled
|
|
||||||
expect(disabled).not_to be_two_factor_enabled
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#two_factor_enabled=' do
|
|
||||||
it 'enables two-factor authentication' do
|
|
||||||
user = build_stubbed(:user, two_factor_enabled: false)
|
|
||||||
expect { user.two_factor_enabled = true }.
|
|
||||||
to change { user.two_factor_enabled? }.to(true)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'disables two-factor authentication' do
|
|
||||||
user = build_stubbed(:user, two_factor_enabled: true)
|
|
||||||
expect { user.two_factor_enabled = false }.
|
|
||||||
to change { user.two_factor_enabled? }.to(false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'authentication token' do
|
describe 'authentication token' do
|
||||||
it "should have authentication token" do
|
it "should have authentication token" do
|
||||||
user = create(:user)
|
user = create(:user)
|
||||||
|
|
Loading…
Reference in a new issue