2018-05-07 16:47:29 -07:00
# frozen_string_literal: true
2021-02-20 09:57:27 -08:00
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# affiliation :string
# avatar_content_type :string
# avatar_file_name :string
# avatar_file_size :integer
# avatar_updated_at :datetime
# biography :text
# confirmation_sent_at :datetime
# confirmation_token :string
# confirmed_at :datetime
# current_sign_in_at :datetime
# current_sign_in_ip :string
# email :string default(""), not null
# email_public :boolean default(FALSE)
# encrypted_password :string default(""), not null
# is_admin :boolean default(FALSE)
# is_disabled :boolean default(FALSE)
# languages :string
# last_sign_in_at :datetime
# last_sign_in_ip :string
# mobile :string
# name :string
# nickname :string
# picture :string
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0)
# tshirt :string
# unconfirmed_email :string
# username :string
# volunteer_experience :text
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_users_on_confirmation_token (confirmation_token) UNIQUE
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_username (username) UNIQUE
#
2014-11-04 17:42:47 +01:00
class IChainRecordNotFound < StandardError
end
2014-11-06 16:17:11 +01:00
class UserDisabled < StandardError
end
2017-09-05 22:23:34 +03:00
class User < ApplicationRecord
2014-08-12 11:51:59 +03:00
rolify
2018-03-28 17:31:18 -07:00
# prevent N+1 queries with has_cached_role? by preloading roles *always*
default_scope { preload ( :roles ) }
2019-05-04 21:03:08 +02:00
has_many :ticket_purchases , dependent : :destroy
2017-06-03 16:05:59 +05:30
has_many :physical_tickets , through : :ticket_purchases do
def by_conference ( conference )
where ( 'ticket_purchases.conference_id = ?' , conference )
end
end
2016-05-24 23:54:46 +05:30
has_many :users_roles
has_many :roles , through : :users_roles , dependent : :destroy
has_paper_trail on : [ :create , :update ] , ignore : [ :sign_in_count , :remember_created_at , :current_sign_in_at , :last_sign_in_at , :current_sign_in_ip , :last_sign_in_ip , :unconfirmed_email ,
:avatar_content_type , :avatar_file_size , :avatar_updated_at , :updated_at , :confirmation_sent_at , :confirmation_token , :reset_password_token ]
2020-07-14 21:06:15 -07:00
# A user may have an uploaded avatar or use gravatar.
# The uploaded picture takes precedence.
2014-06-23 19:07:50 +03:00
include Gravtastic
2014-06-24 12:14:53 +03:00
gravtastic size : 32
2014-06-23 19:07:50 +03:00
2020-07-14 21:06:15 -07:00
mount_uploader :picture , PictureUploader , mount_on : :picture
2014-08-12 11:51:59 +03:00
before_create :setup_role
2018-02-23 00:00:25 -08:00
after_save :touch_events
2021-03-19 11:13:39 -07:00
after_commit :mailbluster_create_lead , on : :create
2021-03-23 13:45:05 -07:00
after_commit :mailbluster_delete_lead , on : :destroy
2021-03-19 11:13:39 -07:00
2015-07-17 18:55:47 +02:00
# add scope
2015-09-07 18:56:26 +02:00
scope :comment_notifiable , - > ( conference ) { joins ( :roles ) . where ( 'roles.name IN (?)' , [ :organizer , :cfp ] ) . where ( 'roles.resource_type = ? AND roles.resource_id = ?' , 'Conference' , conference . id ) }
2015-07-17 18:55:47 +02:00
2018-12-19 12:59:52 -08:00
# scopes for user distributions
2019-10-24 18:34:12 +03:00
scope :recent , lambda {
2018-12-19 12:59:52 -08:00
where ( 'last_sign_in_at > ?' , Date . today - 3 . months ) . where ( is_disabled : false )
}
scope :unconfirmed , - > { where ( 'confirmed_at IS NULL' ) }
scope :dead , - > { where ( 'last_sign_in_at < ?' , Date . today - 1 . year ) }
2013-01-07 09:04:09 +01:00
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
2014-11-04 17:42:47 +01:00
devise_modules = [ ]
2017-07-14 14:12:54 +02:00
devise_modules += if ENV [ 'OSEM_ICHAIN_ENABLED' ] == 'true'
[ :ichain_authenticatable , :ichain_registerable , :omniauthable , omniauth_providers : [ ] ]
else
[ :database_authenticatable , :registerable ,
2014-11-04 17:42:47 +01:00
:recoverable , :rememberable , :trackable , :validatable , :confirmable ,
2020-02-04 23:50:28 -08:00
:omniauthable ,
2021-02-18 21:03:28 -08:00
omniauth_providers : [ :suse , :google , :facebook , :github , :discourse ] ]
2021-02-23 19:49:34 -08:00
# omniauth_providers: [:google, :discourse]
2017-07-14 14:12:54 +02:00
end
2014-11-04 17:42:47 +01:00
devise ( * devise_modules )
2013-01-07 09:04:09 +01:00
2014-06-18 16:58:30 +03:00
has_many :openids
2013-01-07 09:04:09 +01:00
2014-11-03 15:25:33 +01:00
attr_accessor :login
2014-06-23 19:07:50 +03:00
2014-06-24 12:14:53 +03:00
has_many :event_users , dependent : :destroy
2017-12-12 22:08:48 +01:00
has_many :events , - > { distinct } , through : :event_users
has_many :presented_events , - > { joins ( :event_users ) . where ( event_users : { event_role : 'speaker' } ) . distinct } , through : :event_users , source : :event
2017-08-22 14:57:42 +05:30
has_many :registrations , dependent : :destroy do
def for_conference conference
where ( conference : conference ) . first
end
end
2016-04-22 18:18:46 +03:00
has_many :events_registrations , through : :registrations
2016-07-27 19:27:39 +05:30
has_many :payments , dependent : :destroy
2017-08-12 03:01:36 +05:30
has_many :tickets , through : :ticket_purchases , source : :ticket do
def for_registration conference
where ( conference : conference , registration_ticket : true ) . first
end
end
2014-06-24 12:14:53 +03:00
has_many :votes , dependent : :destroy
has_many :voted_events , through : :votes , source : :events
2014-07-24 21:13:03 +05:30
has_many :subscriptions , dependent : :destroy
2017-06-08 12:25:33 +03:00
has_many :tracks , foreign_key : 'submitter_id'
2017-05-17 13:31:27 +03:00
has_many :booth_requests
has_many :booth_requests , dependent : :destroy
has_many :booths , through : :booth_requests
2016-06-28 18:16:06 +03:00
has_many :survey_replies
has_many :survey_submissions
2013-01-07 09:04:09 +01:00
accepts_nested_attributes_for :roles
2014-11-06 16:17:11 +01:00
scope :admin , - > { where ( is_admin : true ) }
2019-10-24 18:34:12 +03:00
scope :active , lambda {
where ( is_disabled : false )
}
2014-11-06 16:17:11 +01:00
2014-11-04 17:42:47 +01:00
validates :email , presence : true
2014-05-12 17:53:07 +04:00
2014-11-03 15:25:33 +01:00
validates :username ,
2014-11-04 17:42:47 +01:00
uniqueness : {
case_sensitive : false
} ,
2018-11-13 18:01:49 -08:00
presence : true
2014-11-03 15:25:33 +01:00
2016-04-27 00:21:39 +02:00
validate :biography_limit
2018-12-19 12:59:52 -08:00
DISTRIBUTION_COLORS = {
'Active' = > 'green' ,
'Unconfirmed' = > 'red' ,
'Dead' = > 'black'
} . freeze
2016-04-27 00:41:40 +02:00
##
# Checkes if the user attended the event
# This is used for events that require registration
# The user must have registered to attend the event
# Gets an event
# === Returns
# * +true+ if the user attended the event
# * +false+ if the user did not attend the event
2016-04-22 18:18:46 +03:00
def attended_event? event
2017-03-28 13:27:52 +05:30
event_registration = event . events_registrations . find_by ( registration : registrations )
2016-04-22 18:18:46 +03:00
return false unless event_registration . present?
2018-11-13 18:01:49 -08:00
2016-04-22 18:18:46 +03:00
event_registration . attended
end
2017-08-22 14:57:42 +05:30
def mark_attendance_for_conference conference
registration = registrations . for_conference ( conference )
registration . attended = true
registration . save
end
2016-03-10 22:49:54 +01:00
def name
2016-05-24 23:54:46 +05:30
self [ :name ] . blank? ? username : self [ :name ]
2016-03-10 22:49:54 +01:00
end
2016-04-22 18:18:46 +03:00
##
# Checks if a user has registered to an event
# ====Returns
# * +true+ or +false+
def registered_to_event? event
2017-03-15 23:35:29 +01:00
event . registrations . include? registrations . find_by ( conference : event . program . conference )
2016-04-22 18:18:46 +03:00
end
2014-10-31 22:45:15 +02:00
def subscribed? conference
2017-03-28 13:27:52 +05:30
subscriptions . find_by ( conference_id : conference . id ) . present?
2014-10-31 22:45:15 +02:00
end
2014-12-05 16:04:34 +01:00
def supports? conference
ticket_purchases . find_by ( conference_id : conference . id ) . present?
end
2020-07-14 21:06:15 -07:00
##
# Returns a user's profile picture URL.
# Partials should *not* directly call `gravatar_url`
2020-07-15 23:27:22 -07:00
def profile_picture ( opts = { } )
return gravatar_url ( opts ) unless picture . present?
2021-02-18 21:03:28 -08:00
2020-07-15 23:27:22 -07:00
size = ( opts [ :size ] || 0 ) . to_i
if size < 50
picture . tiny . url
elsif size < = 100
picture . thumb . url
else
picture . large . url
end
2020-07-14 21:06:15 -07:00
end
2014-11-04 17:42:47 +01:00
def self . for_ichain_username ( username , attributes )
user = find_by ( username : username )
2014-11-06 16:17:11 +01:00
2018-05-08 14:17:30 -07:00
raise UserDisabled if user & . is_disabled
2014-11-06 16:17:11 +01:00
2014-11-04 17:42:47 +01:00
if user
2018-11-13 18:01:49 -08:00
user . update_attributes ( email : attributes [ :email ] ,
last_sign_in_at : user . current_sign_in_at ,
2016-03-10 13:42:30 +01:00
current_sign_in_at : Time . current )
2014-11-04 17:42:47 +01:00
else
begin
2014-11-12 11:45:17 +01:00
user = create! ( username : username , email : attributes [ :email ] )
2014-11-04 17:42:47 +01:00
rescue ActiveRecord :: RecordNotUnique
raise IChainRecordNotFound
end
end
user
end
2018-12-19 12:59:52 -08:00
##
# Returns a hash with user distribution => {value: count of user state, color: color}
# active: signed in during the last 3 months
# unconfirmed: registered but not confirmed
# dead: not signed in during the last year
#
# ====Returns
# * +hash+ -> hash
def self . distribution
{
2019-10-24 18:34:12 +03:00
'Active' = > User . recent . count ,
2018-12-19 12:59:52 -08:00
'Unconfirmed' = > User . unconfirmed . count ,
'Dead' = > User . dead . count
}
end
2014-11-03 15:25:33 +01:00
def self . find_for_database_authentication ( warden_conditions )
conditions = warden_conditions . dup
2014-11-04 17:42:47 +01:00
login = conditions . delete ( :login )
if login
where ( conditions ) . where ( [ 'lower(username) = :value OR lower(email) = :value' , { value : login . downcase } ] ) . first
2014-11-03 15:25:33 +01:00
else
where ( conditions ) . first
end
end
2014-06-18 16:58:30 +03:00
# Searches for user based on email. Returns found user or new user.
# ====Returns
# * +User::ActiveRecord_Relation+ -> user
def self . find_for_auth ( auth , current_user = nil )
user = current_user
if user . nil? # No current user available, user is not already logged in
user = User . where ( email : auth . info . email ) . first_or_initialize
end
if user . new_record?
user . email = auth . info . email
2014-06-23 19:07:50 +03:00
user . name = auth . info . name
2014-11-30 00:08:56 +02:00
user . username = auth . info . username
2014-06-18 18:05:28 +03:00
user . password = Devise . friendly_token [ 0 , 20 ]
2014-06-18 16:58:30 +03:00
user . skip_confirmation!
end
user
end
2014-08-12 11:51:59 +03:00
# Gets the roles of the user, groups them by role.name and returns the resource(s) of each role
# ====Returns
2016-04-17 11:32:06 +05:30
# * +Hash+ * -> e.g. 'organizer' => [conf1, conf2]
2013-01-07 09:04:09 +01:00
def get_roles
2014-08-12 11:51:59 +03:00
result = { }
2016-04-17 11:32:06 +05:30
roles . each do | role |
2017-07-28 02:05:08 +03:00
resource = if role . resource_type == 'Conference'
Conference . find ( role . resource_id ) . short_title
elsif role . resource_type == 'Track'
Track . find ( role . resource_id ) . name
end
2016-04-17 11:32:06 +05:30
if result [ role . name ] . nil?
result [ role . name ] = [ resource ]
else
result [ role . name ] << resource
end
2014-08-12 11:51:59 +03:00
end
result
2013-01-07 09:04:09 +01:00
end
2020-07-25 17:38:44 -07:00
# TODO: Use a real authorization in the right place....
def manages_volunteers? ( conference )
organizer_roles = get_roles [ 'organizer' ]
2021-02-18 21:03:28 -08:00
organizer_roles & . include? ( conference . short_title ) # TODO: or Volunteer Coorinator.
2020-07-25 17:38:44 -07:00
end
2014-06-23 19:07:50 +03:00
def registered
registrations = self . registrations
if registrations . count == 0
'None'
else
registrations . map { | r | r . conference . title } . join ', '
end
end
def attended
2014-06-26 15:43:24 +03:00
registrations_attended = registrations . where ( attended : true )
2014-06-23 19:07:50 +03:00
if registrations_attended . count == 0
'None'
else
registrations_attended . map { | r | r . conference . title } . join ', '
end
2013-01-07 09:04:09 +01:00
end
2018-06-18 10:04:17 -07:00
def attended_count
attributes [ 'attended_count' ] || registrations . where ( attended : true ) . count
end
2014-06-04 16:11:33 +02:00
def confirmed?
! confirmed_at . nil?
end
2014-06-26 15:43:24 +03:00
def proposals ( conference )
2017-11-23 01:19:26 +05:30
events . where ( 'program_id = ? AND (event_users.event_role=? OR event_users.event_role=?)' , conference . program . id , 'submitter' , 'speaker' )
2014-06-23 19:07:50 +03:00
end
2014-06-26 15:43:24 +03:00
def proposal_count ( conference )
2014-06-23 19:07:50 +03:00
proposals ( conference ) . count
end
2020-07-25 17:38:44 -07:00
def volunteer_duties ( conference )
events . where ( program_id : conference . program . id , 'event_users.event_role' : 'volunteer' )
end
2021-03-04 11:55:45 -08:00
def count_registration_tickets ( conference )
count = 0
2021-03-04 16:34:47 -08:00
ticket_purchases . by_conference ( conference ) . each do | ticket_purchase |
2021-03-04 21:50:26 -08:00
if ticket_purchase . ticket . registration_ticket
count += 1
2021-03-04 11:33:27 -08:00
end
end
2021-03-04 16:34:47 -08:00
count
2021-03-04 11:33:27 -08:00
end
2019-01-29 21:20:44 +01:00
def self . empty?
User . count == 1 && User . first . email == 'deleted@localhost.osem'
end
2021-03-23 13:05:45 -07:00
# TODO: email_hash function for mailbluster
2021-03-19 11:13:39 -07:00
# def email_hash
# Digest::MD5.hexdigest user.email
# end
2021-03-17 14:54:57 -07:00
2014-06-23 19:07:50 +03:00
private
2014-06-24 12:14:53 +03:00
2016-04-17 10:48:56 +05:30
def setup_role
2019-01-29 21:20:44 +01:00
self . is_admin = true if User . empty?
2014-06-24 12:14:53 +03:00
end
2016-04-25 01:44:48 +02:00
2021-03-19 11:13:39 -07:00
def mailbluster_create_lead
ApplicationController . helpers . create_lead ( self )
end
2021-03-23 13:45:05 -07:00
def mailbluster_delete_lead
ApplicationController . helpers . delete_lead ( self )
end
2018-02-23 00:00:25 -08:00
def touch_events
event_users . each ( & :touch )
end
2016-04-25 01:44:48 +02:00
##
# Check if biography has an allowed number of words. Used as validation.
#
2016-04-27 00:21:39 +02:00
def biography_limit
2017-03-28 13:27:52 +05:30
if biography . present?
errors . add ( :biography , 'is limited to 150 words.' ) if biography . split . length > 150
2016-04-25 01:44:48 +02:00
end
end
2019-01-29 23:20:22 +01:00
def send_devise_notification ( notification , * args )
devise_mailer . send ( notification , self , * args ) . deliver_later
end
2013-01-07 09:04:09 +01:00
end