2012-04-16 08:04:49 +00:00
|
|
|
class User < ApplicationModel
|
2012-04-14 15:53:00 +00:00
|
|
|
before_create :check_name, :check_email, :check_image
|
2012-04-20 15:39:50 +00:00
|
|
|
before_update :check_password
|
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-04-14 15:53:00 +00:00
|
|
|
|
|
|
|
def self.authenticate( username, password )
|
2012-04-20 08:58:31 +00:00
|
|
|
|
2012-04-20 12:24:37 +00:00
|
|
|
# do not authenticate with nothing
|
2012-04-20 15:39:50 +00:00
|
|
|
return if !username || username == ''
|
|
|
|
return if !password || password == ''
|
2012-04-20 12:24:37 +00:00
|
|
|
|
2012-04-20 08:58:31 +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-04-20 08:58:31 +00:00
|
|
|
|
|
|
|
# try second lookup with email
|
|
|
|
if !user
|
|
|
|
user = User.where( :email => username, :active => true ).first
|
|
|
|
end
|
|
|
|
|
|
|
|
# no user found
|
|
|
|
if !user
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
|
|
|
|
#{config.http_type}://#{config.fqdn}/password_reset_verify/#{token.name}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# check token
|
|
|
|
token = Token.check( :action => 'PasswordReset', :name => token )
|
|
|
|
return if !token
|
|
|
|
|
|
|
|
# reset password
|
|
|
|
token.user.update_attributes( :password => password )
|
|
|
|
|
|
|
|
# delete token
|
|
|
|
token.delete
|
|
|
|
token.save
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-04-14 15:53:00 +00:00
|
|
|
def self.find_fulldata(user_id)
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
return cache_get(user_id) if cache_get(user_id)
|
2012-04-14 15:53:00 +00:00
|
|
|
|
|
|
|
# get user
|
|
|
|
user = User.find(user_id)
|
2012-04-14 16:48:40 +00:00
|
|
|
data = user.attributes
|
|
|
|
|
2012-04-20 15:39:50 +00:00
|
|
|
# do not show password
|
|
|
|
user['password'] = ''
|
|
|
|
|
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
|
2012-04-20 15:39:50 +00:00
|
|
|
data['role_ids'] = user.role_ids
|
2012-04-14 16:48:40 +00:00
|
|
|
|
|
|
|
groups = []
|
|
|
|
user.groups.select('id, name').where( :active => true ).each { |group|
|
|
|
|
groups.push group
|
|
|
|
}
|
|
|
|
data['groups'] = groups
|
2012-04-20 15:39:50 +00:00
|
|
|
data['group_ids'] = user.group_ids
|
|
|
|
|
2012-04-14 16:48:40 +00:00
|
|
|
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-20 15:39:50 +00:00
|
|
|
data['organization_ids'] = user.organization_ids
|
2012-04-14 15:53:00 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
cache_set(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
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
|
|
|
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
|
2012-04-20 15:39:50 +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
|