Last active
December 4, 2018 01:30
-
-
Save danielvlopes/25f6fc621697d71f3247a11eb47effe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
result.name = data[:name] | |
result.email = data[:email] | |
result.email_valid = result.email.present? | |
result.user = existing_account | |
existing_info = load_user_info(auth_hash[:uid]) | |
if result.user && (existing_info["user_id"].present? || result.user.id != existing_info["user_id"]) | |
store_user_info(auth_hash[:uid], user_id: result.user.id, honcho_id: auth_hash[:uid]) | |
end | |
result.user = User.find_by_email(result.email) if !result.user && result.email_valid | |
result.user = create_user(data) if !result.user && result.email_valid | |
if result.user.present? | |
store_user_info(auth_hash[:uid], user_id: result.user.id, honcho_id: auth_hash[:uid]) | |
update_user_info(result.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, | |
"CLIENT ID", | |
"CLIENT SECRET" | |
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