Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class HonchoAuthenticator < ::Auth::Authenticator
def name
"honcho"
end
def description_for_user(user)
"Connected & updated on login"
end
def enabled?
true
end
def can_revoke?
false
end
def can_connect_existing_user?
true
end
def after_authenticate(auth_hash, existing_account: nil)
data = auth_hash[:extra][:raw_info]
result = ::Auth::Result.new
return result if data[:access] != "manager"
return result if data[:active] != true
return result if data[:purchased] != true
user = nil
existing_info = load_user_info(auth_hash[:uid])
if existing_account && (existing_info.present? || existing_account.id != existing_info[:user_id])
store_user_info(auth_hash[:uid], user_id: existing_account.id, honcho_id: auth_hash[:uid])
user = existing_account
end
user = User.find(existing_info[:user_id]) if user.nil? && existing_info[:user_id]
user = create_user(data) if user.nil?
if user.present?
result.email = data[:email]
result.name = data[:full_name]
result.user = user
store_user_info(auth_hash[:uid], user_id: result.user.id, honcho_id: auth_hash[:uid])
update_user_info(user, data)
retrieve_avatar(result.user, data[:image])
end
result
end
def after_create_account(user, auth)
store_user_info(auth[:uid], user_id: user.id, honcho_id: auth[:uid])
retrieve_avatar(user, extra_data)
true
end
def register_middleware(omniauth)
omniauth.provider :honcho,
"...",
"..."
end
private
def create_user(data)
return false unless data[:email] && data[:name]
user = User.new
user.name = data[:name]
user.email = data[:email]
user.active = true
user.trust_level = TrustLevel[0]
user.username = UserNameSuggester.find_available_username_based_on(
UserNameSuggester.parse_name_from_email(data[:email])
)
user.save
user
end
def update_user_info(user, data)
user.email = data[:email] if user.email != data[:email]
user.name = data[:name] if user.name != data[:name]
user.save if user.changed?
user
end
def store_user_info(id, data)
::PluginStore.set("honcho", "honcho_uid_#{id}", data)
end
def load_user_info(id)
::PluginStore.get("honcho", "honcho_uid_#{id}") || {}
end
def retrieve_avatar(user, image_url)
return unless user && image_url
return if user.user_avatar.try(:custom_upload_id).present?
Jobs.enqueue(:download_avatar_from_url, url: image_url, user_id: user.id, override_gravatar: false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.