2014-11-04 17:42:47 +01:00
class IChainRecordNotFound < StandardError
end
2014-11-06 16:17:11 +01:00
class UserDisabled < StandardError
end
2013-01-07 09:04:09 +01:00
class User < ActiveRecord :: Base
2014-08-12 11:51:59 +03:00
rolify
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
2014-08-12 11:51:59 +03:00
before_create :setup_role
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
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 = [ ]
if CONFIG [ 'authentication' ] [ 'ichain' ] [ 'enabled' ]
2014-11-06 11:16:54 +01:00
devise_modules += [ :ichain_authenticatable , :ichain_registerable , :omniauthable , omniauth_providers : [ ] ]
2014-11-04 17:42:47 +01:00
else
devise_modules += [ :database_authenticatable , :registerable ,
:recoverable , :rememberable , :trackable , :validatable , :confirmable ,
:omniauthable , omniauth_providers : [ :suse , :google , :facebook , :github ] ]
end
devise ( * devise_modules )
2013-01-07 09:04:09 +01:00
2015-09-03 12:17:42 +02:00
has_and_belongs_to_many :roles
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
has_many :events , - > { uniq } , through : :event_users
has_many :registrations , dependent : :destroy
2014-12-18 13:38:40 +01:00
has_many :ticket_purchases , dependent : :destroy
2014-08-28 15:21:05 +02:00
has_many :tickets , through : :ticket_purchases , source : :ticket
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
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 ) }
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
} ,
presence : true
2014-11-03 15:25:33 +01:00
2014-10-31 22:45:15 +02:00
def subscribed? conference
self . subscriptions . find_by ( conference_id : conference . id ) . present?
end
2014-12-05 16:04:34 +01:00
# Returns the purchased ticket
2014-08-28 15:21:05 +02:00
# ====Returns
# * +TicketUser::ActiveRecord_Relation+ -> user
def ticket ( id )
ticket_purchases . where ( ticket_id : id ) . first
end
2014-12-05 16:04:34 +01:00
def supports? conference
ticket_purchases . find_by ( conference_id : conference . id ) . present?
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
raise UserDisabled if user && user . is_disabled
2014-11-04 17:42:47 +01:00
if user
user . update_attributes ( email : attributes [ :email ] )
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
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
2013-01-07 09:04:09 +01:00
def setup_role
2014-09-12 08:47:50 +03:00
if User . count == 1 && User . first . email == 'deleted@localhost.osem'
self . is_admin = true
end
2014-07-23 16:24:05 +02:00
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
# * +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 = { }
Role :: ACTIONABLES . each do | role |
resources = self . roles . where ( name : role . parameterize . underscore ) . map { | myrole | Conference . find ( myrole . resource_id ) . short_title } . join ', '
result [ role . parameterize . underscore ] = " ( #{ resources } ) " unless resources . blank?
end
result
2013-01-07 09:04:09 +01:00
end
2014-06-23 19:07:50 +03:00
def self . prepare ( params )
email = params [ 'email' ]
user = User . where ( email : email ) . first_or_initialize
# If there is a new user, add the necessary attributes
if user . new_record?
2014-06-24 12:14:53 +03:00
user . password = Devise . friendly_token [ 0 , 20 ]
2014-06-23 19:07:50 +03:00
user . skip_confirmation!
user . attributes = params
end
user
end
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
2014-06-04 16:11:33 +02:00
def confirmed?
! confirmed_at . nil?
end
2014-06-26 15:43:24 +03:00
def attending_conference? ( conference )
2014-06-24 12:14:53 +03:00
Registration . where ( conference_id : conference . id ,
2014-06-26 15:43:24 +03:00
user_id : id ) . count
2014-06-23 19:07:50 +03:00
end
2014-06-26 15:43:24 +03:00
def proposals ( conference )
2015-10-25 13:29:02 +02:00
events . where ( 'program_id = ? AND event_users.event_role=?' , conference . program . id , 'submitter' )
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
def biography_word_count
2014-06-26 15:43:24 +03:00
if biography . nil?
2014-06-23 19:07:50 +03:00
0
else
2014-06-26 15:43:24 +03:00
biography . split . size
2014-06-23 19:07:50 +03:00
end
2014-05-13 10:35:23 +02:00
end
2014-06-24 12:14:53 +03:00
2014-06-23 19:07:50 +03:00
private
2014-06-24 12:14:53 +03:00
def biography_limit
2014-06-26 17:41:46 +03:00
errors . add ( :abstract , 'cannot have more than 150 words' ) if biography &&
2014-06-26 15:43:24 +03:00
biography . split . size > 150
2014-06-24 12:14:53 +03:00
end
2013-01-07 09:04:09 +01:00
end