FEATURE: Add users:disable_2factor rake task

https://meta.discourse.org/t/admin-locked-out-of-2fa/92156/2?u=pfaffman
This commit is contained in:
Jay Pfaffman 2018-07-12 10:20:45 +02:00 committed by Guo Xiang Tan
parent d24e53d155
commit 0ed2834c2d
2 changed files with 16 additions and 5 deletions

View File

@ -1,6 +1,13 @@
class UserSecondFactor < ActiveRecord::Base
belongs_to :user
scope :backup_codes, -> { where(method: 2, enabled: true) }
scope :totp, -> do
where(method: UserSecondFactor.methods[:totp])
end
scope :backup_codes, -> do
where(method: UserSecondFactor.methods[:backup_codes], enabled: true)
end
def self.methods
@methods ||= Enum.new(
@ -9,10 +16,6 @@ class UserSecondFactor < ActiveRecord::Base
)
end
def self.totp
where(method: 1).first
end
end
# == Schema Information

View File

@ -135,6 +135,14 @@ GROUP BY p.user_id
puts 'Done!', ''
end
desc "Disable 2FA for user with the given username"
task "users:disable_2fa", [:username] => [:environment] do |_, args|
username = args[:username]
user = find_user(username)
UserSecondFactor.totp.where(user_id: user.id).each(&:destroy!)
puts "2FA disabled for #{username}"
end
def find_user(username)
user = User.find_by_username(username)