# frozen_string_literal: true class User < ApplicationRecord has_many :screenshots, inverse_of: :user # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, # :registerable, # :recoverable, # :rememberable, :trackable, # :validatable, :timeoutable, # :lockable, :omniauthable, omniauth_providers: [ :salsa # :launchpad, # :stackexchange, # :google_oauth2, # :amazon, # :github ] include Gravtastic gravtastic size: 120 # Return a human-friendly string describing the user's SSO provider def pretty_provider case provider # when 'launchpad' # 'Ubuntu One/Launchpad' # when 'stackexchange' # 'StackExchange' # when 'google_oauth2' # 'Google' # when 'amazon' # 'Amazon' # when 'github' # 'GitHub' when 'salsa' 'salsa.debian.org' else 'local authentication' end end def pretty_name if pseudo 'Anonymous' elsif name? name else 'Dr. Who' end end def pretty_role if pseudo 'Contributor' elsif moderator_role 'Moderator' elsif admin_role 'Admin' end end # Return from the Salsa SSO process def self.from_omniauth(auth) # Finds or creates a user based on the provider and email from the authentication data this_user = where(provider: auth.provider, email: auth.info.email).first_or_create do |user| user.provider = auth.provider user.uid = auth.uid user.email = auth.info.email user.name = auth.info.name # Set a random password user.password = Devise.friendly_token[0, 20] # Users coming through salsa.debian.net/OpenID-Connect will get moderator role # TODO: allow DMs who do not have an @debian.org address #user.moderator_role = true if user.email.end_with?('@debian.org') && user.provider == 'salsa' user.moderator_role = true if user.provider == 'salsa' end # Get the groups of the user from the OAuth server # this_user.groups = auth.extra.raw_info.groups if auth.extra.raw_info.respond_to?(:groups) # # # Log the extra information from the OAuth server # Rails.logger.info "OAuth extra info: #{auth.extra.raw_info.inspect}" # # this_user end # Seamlessly create a user account for the current visitor. # It helps track uploads because uploaded screenshots get assigned # to this user record. # TODO: The user can later decide to use a real account and get their screenshots transferred to that. def self.create_pseudo_user generated_password = Devise.friendly_token.first(8) User.create( name: 'Anonymous', password: generated_password, pseudo: true ) end def to_s if pseudo "Pseudo user #{id}" else "#{email} (#{provider})" end end end