trabajo-afectivo/app/models/user.rb

359 lines
9 KiB
Ruby
Raw Normal View History

2012-04-16 08:04:49 +00:00
class User < ApplicationModel
2012-04-29 20:47:35 +00:00
include Gmaps
before_create :check_name, :check_email, :check_image, :check_geo
before_update :check_password, :check_geo
2012-04-16 08:04:49 +00:00
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
2012-04-10 14:06:46 +00:00
2012-04-16 08:04:49 +00:00
has_and_belongs_to_many :groups, :after_add => :cache_update, :after_remove => :cache_update
has_and_belongs_to_many :roles, :after_add => :cache_update, :after_remove => :cache_update
has_and_belongs_to_many :organizations, :after_add => :cache_update, :after_remove => :cache_update
2012-04-23 06:55:16 +00:00
has_many :tokens, :after_add => :cache_update, :after_remove => :cache_update
2012-04-16 08:04:49 +00:00
has_many :authorizations, :after_add => :cache_update, :after_remove => :cache_update
belongs_to :organization, :class_name => 'Organization'
store :preferences
2012-07-10 08:09:58 +00:00
def fullname
fullname = ''
if self.firstname
fullname = fullname + self.firstname
end
if self.lastname
if fullname != ''
fullname = fullname + ' '
end
fullname = fullname + self.lastname
end
return fullname
end
def self.authenticate( username, password )
2012-07-29 15:27:01 +00:00
# do not authenticate with nothing
return if !username || username == ''
return if !password || password == ''
2012-07-29 15:27:01 +00:00
# try to find user based on login
2012-04-10 14:06:46 +00:00
user = User.where( :login => username, :active => true ).first
2012-07-29 15:27:01 +00:00
# try second lookup with email
if !user
user = User.where( :email => username, :active => true ).first
end
2012-07-29 15:27:01 +00:00
# no user found
if !user
return nil
end
2012-07-29 15:27:01 +00:00
# auth ok
if user.password == password
return user
end
# auth failed
return false
2012-04-10 14:06:46 +00:00
end
def self.create_from_hash!(hash)
url = ''
if hash['info']['urls'] then
url = hash['info']['urls']['Website'] || hash['info']['urls']['Twitter'] || ''
end
roles = Role.where( :name => 'Customer' )
2012-04-23 06:55:16 +00:00
self.create(
2012-04-10 14:06:46 +00:00
:login => hash['info']['nickname'] || hash['uid'],
:firstname => hash['info']['name'],
:email => hash['info']['email'],
:image => hash['info']['image'],
# :url => url.to_s,
:note => hash['info']['description'],
:source => hash['provider'],
:roles => roles,
:created_by_id => 1
)
end
2012-04-23 06:55:16 +00:00
def self.password_reset_send(username)
return if !username || username == ''
# try to find user based on login
user = User.where( :login => username, :active => true ).first
# try second lookup with email
if !user
user = User.where( :email => username, :active => true ).first
end
# check if email address exists
return if !user.email
# generate token
token = Token.create( :action => 'PasswordReset', :user_id => user.id )
# send mail
data = {}
data[:subject] = 'Reset your #{config.product_name} password'
data[:body] = 'Forgot your password?
We received a request to reset the password for your #{config.product_name} account (#{user.login}).
If you want to reset your password, click on the link below (or copy and paste the URL into your browser):
2012-04-23 18:50:04 +00:00
#{config.http_type}://#{config.fqdn}/#password_reset_verify/#{token.name}
2012-04-23 06:55:16 +00:00
This link takes you to a page where you can change your password.
If you don\'t want to reset your password, please ignore this message. Your password will not be reset.
Your #{config.product_name} Team
'
# prepare subject & body
[:subject, :body].each { |key|
data[key.to_sym] = NotificationFactory.build(
:string => data[key.to_sym],
:objects => {
:token => token,
:user => user,
}
)
}
# send notification
NotificationFactory.send(
:recipient => user,
:subject => data[:subject],
:body => data[:body]
)
return true
end
def self.password_reset_check(token)
# check token
token = Token.check( :action => 'PasswordReset', :name => token )
return if !token
return true
end
def self.password_reset_via_token(token,password)
2012-07-23 22:22:23 +00:00
2012-04-23 06:55:16 +00:00
# check token
token = Token.check( :action => 'PasswordReset', :name => token )
return if !token
2012-07-23 22:22:23 +00:00
2012-04-23 06:55:16 +00:00
# reset password
token.user.update_attributes( :password => password )
2012-07-23 22:22:23 +00:00
2012-04-23 06:55:16 +00:00
# delete token
token.delete
token.save
return true
end
def self.find_fulldata(user_id)
2012-07-29 15:27:01 +00:00
cache = self.cache_get(user_id)
return cache if cache
# get user
user = User.find(user_id)
data = user.attributes
# do not show password
user['password'] = ''
# get linked accounts
data['accounts'] = {}
authorizations = user.authorizations() || []
authorizations.each do | authorization |
data['accounts'][authorization.provider] = {
:uid => authorization[:uid],
:username => authorization[:username]
}
end
2012-07-23 22:22:23 +00:00
# set roles
roles = []
user.roles.select('id, name').where( :active => true ).each { |role|
2012-07-29 15:27:01 +00:00
roles.push role.attributes
}
data['roles'] = roles
data['role_ids'] = user.role_ids
groups = []
user.groups.select('id, name').where( :active => true ).each { |group|
2012-07-29 15:27:01 +00:00
groups.push group.attributes
}
data['groups'] = groups
data['group_ids'] = user.group_ids
organization = user.organization
2012-07-29 15:27:01 +00:00
if organization
data['organization'] = organization.attributes
end
organizations = []
user.organizations.select('id, name').where( :active => true ).each { |organization|
2012-07-29 15:27:01 +00:00
organizations.push organization.attributes
}
data['organizations'] = organizations
data['organization_ids'] = user.organization_ids
2012-07-29 15:27:01 +00:00
self.cache_set(user.id, data)
return data
end
2012-07-23 22:22:23 +00:00
def self.user_data_full (user_id)
# get user
user = User.find_fulldata(user_id)
# do not show password
user['password'] = ''
# TEMP: compat. reasons
user['preferences'] = {} if user['preferences'] == nil
items = []
if user['preferences'][:tickets_open].to_i > 0
item = {
:url => '',
:name => 'open',
:count => user['preferences'][:tickets_open] || 0,
:title => 'Open Tickets',
:class => 'user-tickets',
:data => 'open'
}
items.push item
end
if user['preferences'][:tickets_closed].to_i > 0
item = {
:url => '',
:name => 'closed',
:count => user['preferences'][:tickets_closed] || 0,
:title => 'Closed Tickets',
:class => 'user-tickets',
:data => 'closed'
}
items.push item
end
# show linked topics and items
if items.count > 0
topic = {
:title => 'Tickets',
:items => items,
}
user['links'] = []
user['links'].push topic
end
return user
end
2012-04-29 20:47:35 +00:00
# update all users geo data
def self.geo_update_all
User.all.each { |user|
user.geo_update
user.save
}
end
# update geo data of one user
def geo_update
address = ''
location = ['street', 'zip', 'city', 'country']
location.each { |item|
if self[item] && self[item] != ''
address = address + ',' + self[item]
end
}
# return if no address is given
return if address == ''
# dp lookup
latlng = Gmaps.geocode(address)
if latlng
self.preferences['lat'] = latlng[0]
self.preferences['lng'] = latlng[1]
end
end
2012-04-10 14:06:46 +00:00
private
2012-04-29 20:47:35 +00:00
def check_geo
2012-05-02 00:26:41 +00:00
# geo update if no user exists
if !self.id
self.geo_update
return
end
2012-04-29 20:47:35 +00:00
location = ['street', 'zip', 'city', 'country']
2012-06-20 09:39:53 +00:00
# get current user data
current = User.where( :id => self.id ).first
return if !current
2012-04-29 20:47:35 +00:00
# check if geo update is needed
current_location = {}
location.each { |item|
current_location[item] = current[item]
}
# get full address
next_location = {}
location.each { |item|
next_location[item] = self[item]
}
# return if address hasn't changed and geo data is already available
return if ( current_location == next_location ) && ( self.preferences['lat'] && self.preferences['lng'] )
# geo update
self.geo_update
end
2012-04-10 14:06:46 +00:00
def check_name
2012-04-29 20:47:35 +00:00
if self.firstname && (!self.lastname || self.lastname == '')
2012-04-10 14:06:46 +00:00
name = self.firstname.split(' ', 2)
self.firstname = name[0]
self.lastname = name[1]
end
end
2012-04-29 20:47:35 +00:00
2012-04-10 14:06:46 +00:00
def check_email
2012-04-29 20:47:35 +00:00
if self.email
2012-04-10 14:06:46 +00:00
self.email = self.email.downcase
end
end
2012-04-29 20:47:35 +00:00
2012-04-10 14:06:46 +00:00
def check_image
require 'digest/md5'
2012-04-29 20:47:35 +00:00
if !self.image || self.image == ''
if self.email
2012-04-10 14:06:46 +00:00
hash = Digest::MD5.hexdigest(self.email)
self.image = "http://www.gravatar.com/avatar/#{hash}?s=48"
end
end
end
2012-04-29 20:47:35 +00:00
def check_password
# set old password again
if self.password == '' || !self.password
# get current record
current = User.find(self.id)
self.password = current.password
end
end
2012-04-10 14:06:46 +00:00
end