debshots/app/models/user.rb

98 lines
2.8 KiB
Ruby

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 => [
:launchpad,
:stackexchange,
:google_oauth2,
:amazon,
:github
]
# Return a human-friendly string describing the user's SSO provider
def pretty_provider
case self.provider
when 'launchpad'
'Ubuntu One/Launchpad'
when 'stackexchange'
'StackExchange'
when 'google_oauth2'
'Google'
when 'amazon'
'Amazon'
when 'github'
'GitHub'
when 'debian-sso'
'Debian single-sign-on'
else
'local authentication'
end
end
def is_admin?
self.admin == 1
end
# Check if a user has been created on-the-fly and is just an
# anonymous user who uploaded a screenshot. They can turn this
# user record into a registered account though.
def is_anonymous?
self.provider == nil
end
# Do uploads from this user get approved automatically?
def auto_approve?
# Anonymous users need to go through moderation
return false unless user_signed_in?
# Admins do not need moderation
return true if current_user.is_admin?
# Debian developers do not need moderation
return true if current_user.provider == 'debian-sso'
# After one successfully approved screenshot users do not need moderation
return true if current_user.approved_screenshots.count > 0
# Any other user's upload must be moderated
return false
end
def self.from_omniauth(auth)
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]
end
end
# Seamlessly create a user account for the current client.
# It helps track uploads because uploaded screenshots get assigned
# to this user record. 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)
# new_user = User.create(
# name: 'Anonymous',
# password: generated_password)
# Log.log "New pseudo user for anonymous upload created: #{new_user}"
# sign_in new_user
# end
#
# Currently the approach is different: do not create an account
# for a user. Instead store the IDs of the uploaded screenshots
# and transfer them if the user decides to do a real login using SSO.
end