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:
parent
d24e53d155
commit
0ed2834c2d
|
@ -1,6 +1,13 @@
|
||||||
class UserSecondFactor < ActiveRecord::Base
|
class UserSecondFactor < ActiveRecord::Base
|
||||||
belongs_to :user
|
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
|
def self.methods
|
||||||
@methods ||= Enum.new(
|
@methods ||= Enum.new(
|
||||||
|
@ -9,10 +16,6 @@ class UserSecondFactor < ActiveRecord::Base
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.totp
|
|
||||||
where(method: 1).first
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -135,6 +135,14 @@ GROUP BY p.user_id
|
||||||
puts 'Done!', ''
|
puts 'Done!', ''
|
||||||
end
|
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)
|
def find_user(username)
|
||||||
user = User.find_by_username(username)
|
user = User.find_by_username(username)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue