Commit 7f7e83c0 authored by Manoj M J's avatar Manoj M J

Refactor two factor query methods on `User`

parent c6a5f71e
...@@ -231,14 +231,7 @@ class Member < ApplicationRecord ...@@ -231,14 +231,7 @@ class Member < ApplicationRecord
end end
def left_join_users def left_join_users
users = User.arel_table left_outer_joins(:user)
members = Member.arel_table
member_users = members.join(users, Arel::Nodes::OuterJoin)
.on(members[:user_id].eq(users[:id]))
.join_sources
joins(member_users)
end end
def access_for_user_ids(user_ids) def access_for_user_ids(user_ids)
......
...@@ -536,27 +536,15 @@ class User < ApplicationRecord ...@@ -536,27 +536,15 @@ class User < ApplicationRecord
end end
def self.with_two_factor def self.with_two_factor
with_u2f_registrations = <<-SQL where(otp_required_for_login: true)
EXISTS ( .or(where_exists(U2fRegistration.where(U2fRegistration.arel_table[:user_id].eq(arel_table[:id]))))
SELECT * .or(where_exists(WebauthnRegistration.where(WebauthnRegistration.arel_table[:user_id].eq(arel_table[:id]))))
FROM u2f_registrations AS u2f
WHERE u2f.user_id = users.id
) OR users.otp_required_for_login = ?
OR
EXISTS (
SELECT *
FROM webauthn_registrations AS webauthn
WHERE webauthn.user_id = users.id
)
SQL
where(with_u2f_registrations, true)
end end
def self.without_two_factor def self.without_two_factor
joins("LEFT OUTER JOIN u2f_registrations AS u2f ON u2f.user_id = users.id where
LEFT OUTER JOIN webauthn_registrations AS webauthn ON webauthn.user_id = users.id") .missing(:u2f_registrations, :webauthn_registrations)
.where("u2f.id IS NULL AND webauthn.id IS NULL AND users.otp_required_for_login = ?", false) .where(otp_required_for_login: false)
end end
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment