osem-fcy/app/models/user.rb

83 lines
2.4 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
2014-06-18 16:58:30 +03:00
:recoverable, :rememberable, :trackable, :validatable, :confirmable,
:omniauthable, omniauth_providers: [:novell, :google, :facebook]
2013-01-07 09:04:09 +01:00
has_and_belongs_to_many :roles
has_one :person, :inverse_of => :user
2014-06-18 16:58:30 +03:00
has_many :openids
2013-01-07 09:04:09 +01:00
2014-06-18 16:58:30 +03:00
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids,
:person_attributes
2013-01-07 09:04:09 +01:00
accepts_nested_attributes_for :person
accepts_nested_attributes_for :roles
before_create :setup_role
2013-01-07 09:04:09 +01:00
before_create :create_person
2014-05-12 18:22:41 +04:00
delegate :last_name, :first_name, :public_name, to: :person
2014-05-12 17:53:07 +04:00
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-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 role?(role)
Rails.logger.debug("Checking role in user")
2014-05-10 16:09:24 +02:00
!!roles.find_by_name(role.to_s.downcase.camelize)
2013-01-07 09:04:09 +01:00
end
def get_roles
return self.roles
end
def setup_role
2014-05-13 10:39:03 +02:00
roles << Role.find_by(name: 'Admin') if User.count == 0
2014-05-13 11:12:53 +02:00
roles << Role.find_by(name: 'Participant') if roles.empty?
2013-01-07 09:04:09 +01:00
end
def popup_details
details = "<b>Sign-in Count</b><br>"
details += "#{self.sign_in_count}<br>"
details += "<b>Current Sign-in</b><br>"
details += "#{self.current_sign_in_at}<br>"
details += "<b>Last Sign-in</b><br>"
details += "#{self.last_sign_in_at}<br>"
details += "<b>Current Sign-in IP</b><br>"
details += "#{self.current_sign_in_ip}<br>"
details += "<b>Last Sign-in IP</b><br>"
details += "#{self.last_sign_in_ip}<br>"
details += "<b>Created at</b><br>"
details += "#{self.created_at}<br>"
end
def confirmed?
!confirmed_at.nil?
end
2013-01-07 09:04:09 +01:00
private
def create_person
# TODO Search people for existing email address, add to their account
2014-05-13 10:39:03 +02:00
build_person(email: email) if person.nil?
true
end
2013-01-07 09:04:09 +01:00
end