2012-04-10 14:06:46 +00:00
|
|
|
class User < ActiveRecord::Base
|
2012-04-14 15:53:00 +00:00
|
|
|
before_create :check_name, :check_email, :check_image
|
2012-04-10 14:06:46 +00:00
|
|
|
has_and_belongs_to_many :groups
|
|
|
|
has_and_belongs_to_many :roles
|
|
|
|
has_and_belongs_to_many :organizations
|
2012-04-14 15:53:00 +00:00
|
|
|
belongs_to :organization, :class_name => 'Organization'
|
|
|
|
has_many :authorizations
|
|
|
|
after_create :delete_cache
|
|
|
|
after_update :delete_cache
|
|
|
|
after_destroy :delete_cache
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-14 15:53:00 +00:00
|
|
|
@@cache = {}
|
|
|
|
|
|
|
|
def self.authenticate( username, password )
|
2012-04-10 14:06:46 +00:00
|
|
|
user = User.where( :login => username, :active => true ).first
|
|
|
|
return nil if user.nil?
|
|
|
|
logger.debug 'auth'
|
|
|
|
logger.debug username
|
|
|
|
logger.debug user.login
|
|
|
|
logger.debug password
|
|
|
|
logger.debug user.password
|
|
|
|
logger.debug user.inspect
|
|
|
|
# return user
|
|
|
|
return user if user.password == password
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_from_hash!(hash)
|
|
|
|
# logger.debug(hash.inspect)
|
|
|
|
# raise hash.to_yaml
|
|
|
|
# exit
|
|
|
|
url = ''
|
|
|
|
if hash['info']['urls'] then
|
|
|
|
url = hash['info']['urls']['Website'] || hash['info']['urls']['Twitter'] || ''
|
|
|
|
end
|
|
|
|
# logger.debug(hash['info'].inspect)
|
|
|
|
# raise url.to_yaml
|
|
|
|
# exit
|
|
|
|
# logger.debug('aaaaaaaa')
|
|
|
|
roles = Role.where( :name => 'Customer' )
|
|
|
|
create(
|
|
|
|
: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-14 15:53:00 +00:00
|
|
|
def self.find_fulldata(user_id)
|
|
|
|
|
|
|
|
return @@cache[user_id] if @@cache[user_id]
|
|
|
|
|
|
|
|
# get user
|
|
|
|
user = User.find(user_id)
|
2012-04-14 16:48:40 +00:00
|
|
|
data = user.attributes
|
|
|
|
|
2012-04-14 15:53:00 +00:00
|
|
|
|
|
|
|
# get linked accounts
|
2012-04-14 16:48:40 +00:00
|
|
|
data['accounts'] = {}
|
2012-04-14 15:53:00 +00:00
|
|
|
authorizations = user.authorizations() || []
|
|
|
|
authorizations.each do | authorization |
|
2012-04-14 16:48:40 +00:00
|
|
|
data['accounts'][authorization.provider] = {
|
2012-04-14 15:53:00 +00:00
|
|
|
:uid => authorization[:uid],
|
|
|
|
:username => authorization[:username]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# set roles
|
2012-04-14 16:48:40 +00:00
|
|
|
roles = []
|
|
|
|
user.roles.select('id, name').where( :active => true ).each { |role|
|
|
|
|
roles.push role
|
|
|
|
}
|
|
|
|
data['roles'] = roles
|
|
|
|
|
|
|
|
groups = []
|
|
|
|
user.groups.select('id, name').where( :active => true ).each { |group|
|
|
|
|
groups.push group
|
|
|
|
}
|
|
|
|
data['groups'] = groups
|
|
|
|
|
|
|
|
organization = user.organization
|
|
|
|
data['organization'] = organization
|
|
|
|
|
|
|
|
organizations = []
|
|
|
|
user.organizations.select('id, name').where( :active => true ).each { |organization|
|
|
|
|
organizations.push organization
|
|
|
|
}
|
|
|
|
data['organizations'] = organizations
|
2012-04-14 15:53:00 +00:00
|
|
|
|
2012-04-14 16:48:40 +00:00
|
|
|
@@cache[user_id] = data
|
2012-04-14 15:53:00 +00:00
|
|
|
|
2012-04-14 16:48:40 +00:00
|
|
|
return data
|
2012-04-14 15:53:00 +00:00
|
|
|
end
|
2012-04-14 16:48:40 +00:00
|
|
|
|
|
|
|
def cache_reset
|
|
|
|
@@cache[self.id] = nil
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
2012-04-14 15:53:00 +00:00
|
|
|
def delete_cache
|
2012-04-14 16:48:40 +00:00
|
|
|
puts 'delete_cache', self.insepct
|
2012-04-14 15:53:00 +00:00
|
|
|
@@cache[self.id] = nil
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def check_name
|
|
|
|
if self.firstname && (!self.lastname || self.lastname == '') then
|
|
|
|
name = self.firstname.split(' ', 2)
|
|
|
|
self.firstname = name[0]
|
|
|
|
self.lastname = name[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def check_email
|
|
|
|
if self.email then
|
|
|
|
self.email = self.email.downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def check_image
|
|
|
|
require 'digest/md5'
|
|
|
|
if !self.image || self.image == '' then
|
|
|
|
if self.email then
|
|
|
|
hash = Digest::MD5.hexdigest(self.email)
|
|
|
|
self.image = "http://www.gravatar.com/avatar/#{hash}?s=48"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|