2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-11-02 21:32:00 +00:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2014-12-18 14:58:47 +00:00
|
|
|
# @model User
|
|
|
|
#
|
|
|
|
# @property id(required) [Integer] The identifier for the User.
|
|
|
|
# @property login(required) [String] The login of the User used for authentication.
|
|
|
|
# @property firstname [String] The firstname of the User.
|
|
|
|
# @property lastname [String] The lastname of the User.
|
|
|
|
# @property email [String] The email of the User.
|
|
|
|
# @property image [String] The Image used as the User avatar (TODO: Image model?).
|
|
|
|
# @property web [String] The website/URL of the User.
|
|
|
|
# @property password [String] The password of the User.
|
|
|
|
# @property phone [String] The phone number of the User.
|
|
|
|
# @property fax [String] The fax number of the User.
|
|
|
|
# @property mobile [String] The mobile number of the User.
|
|
|
|
# @property department [String] The department the User is working at.
|
|
|
|
# @property street [String] The street the User lives in.
|
|
|
|
# @property zip [Integer] The zip postal code of the User city.
|
|
|
|
# @property city [String] The city the User lives in.
|
|
|
|
# @property country [String] The country the User lives in.
|
|
|
|
# @property verified [Boolean] The flag that shows the verified state of the User.
|
|
|
|
# @property active [Boolean] The flag that shows the active state of the User.
|
|
|
|
# @property note [String] The note or comment stored to the User.
|
2012-04-16 08:04:49 +00:00
|
|
|
class User < ApplicationModel
|
2014-12-31 13:56:37 +00:00
|
|
|
include User::Permission
|
2014-10-17 12:15:54 +00:00
|
|
|
load 'user/assets.rb'
|
2013-08-19 06:29:49 +00:00
|
|
|
include User::Assets
|
|
|
|
extend User::Search
|
2016-07-06 06:13:44 +00:00
|
|
|
load 'user/search_index.rb'
|
|
|
|
include User::SearchIndex
|
2013-08-19 06:29:49 +00:00
|
|
|
|
2016-02-07 13:00:29 +00:00
|
|
|
before_create :check_name, :check_email, :check_login, :check_password, :check_preferences_default
|
|
|
|
before_update :check_password, :check_email, :check_login, :check_preferences_default
|
2015-07-06 18:33:37 +00:00
|
|
|
after_create :avatar_for_email_check
|
|
|
|
after_update :avatar_for_email_check
|
2015-02-01 12:08:11 +00:00
|
|
|
after_destroy :avatar_destroy
|
|
|
|
notify_clients_support
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
has_and_belongs_to_many :groups, after_add: :cache_update, after_remove: :cache_update, class_name: 'Group'
|
|
|
|
has_and_belongs_to_many :roles, after_add: [:cache_update, :check_notifications], after_remove: :cache_update, class_name: 'Role'
|
|
|
|
has_and_belongs_to_many :organizations, after_add: :cache_update, after_remove: :cache_update, class_name: 'Organization'
|
2015-04-27 13:42:53 +00:00
|
|
|
has_many :tokens, after_add: :cache_update, after_remove: :cache_update
|
|
|
|
has_many :authorizations, after_add: :cache_update, after_remove: :cache_update
|
|
|
|
belongs_to :organization, class_name: 'Organization'
|
2012-04-16 08:04:49 +00:00
|
|
|
|
|
|
|
store :preferences
|
2012-04-14 15:53:00 +00:00
|
|
|
|
2013-10-05 14:44:50 +00:00
|
|
|
activity_stream_support(
|
2015-04-27 13:42:53 +00:00
|
|
|
role: Z_ROLENAME_ADMIN,
|
|
|
|
ignore_attributes: {
|
|
|
|
last_login: true,
|
2016-04-13 07:34:45 +00:00
|
|
|
login_failed: true,
|
2015-04-27 13:42:53 +00:00
|
|
|
image: true,
|
|
|
|
image_source: true,
|
2015-09-29 19:32:05 +00:00
|
|
|
preferences: true,
|
2013-11-02 22:18:39 +00:00
|
|
|
}
|
2013-10-05 14:44:50 +00:00
|
|
|
)
|
2013-10-22 06:43:49 +00:00
|
|
|
history_support(
|
2015-04-27 13:42:53 +00:00
|
|
|
ignore_attributes: {
|
|
|
|
password: true,
|
|
|
|
image: true,
|
|
|
|
image_source: true,
|
2015-09-29 19:32:05 +00:00
|
|
|
preferences: true,
|
2013-10-22 06:43:49 +00:00
|
|
|
}
|
|
|
|
)
|
2014-01-27 22:59:41 +00:00
|
|
|
search_index_support(
|
2015-04-27 13:42:53 +00:00
|
|
|
ignore_attributes: {
|
|
|
|
password: true,
|
|
|
|
image: true,
|
|
|
|
image_source: true,
|
|
|
|
source: true,
|
|
|
|
login_failed: true,
|
|
|
|
preferences: true,
|
2016-03-08 06:32:58 +00:00
|
|
|
},
|
|
|
|
ignore_ids: [1],
|
2014-01-27 22:59:41 +00:00
|
|
|
)
|
2013-09-28 00:07:11 +00:00
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
fullname of user
|
|
|
|
|
|
|
|
user = User.find(123)
|
2016-02-20 10:12:15 +00:00
|
|
|
result = user.fullname
|
2013-08-17 22:10:02 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = "Bob Smith"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-07-10 08:09:58 +00:00
|
|
|
def fullname
|
2016-02-20 10:12:15 +00:00
|
|
|
name = ''
|
2015-05-07 12:10:38 +00:00
|
|
|
if firstname && !firstname.empty?
|
2016-02-20 10:12:15 +00:00
|
|
|
name = name + firstname
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2015-05-07 12:10:38 +00:00
|
|
|
if lastname && !lastname.empty?
|
2016-02-20 10:12:15 +00:00
|
|
|
if name != ''
|
|
|
|
name += ' '
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2016-02-20 10:12:15 +00:00
|
|
|
name += lastname
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2016-02-20 10:12:15 +00:00
|
|
|
if name == '' && email
|
|
|
|
name = email
|
2015-01-07 20:42:12 +00:00
|
|
|
end
|
2016-02-20 10:12:15 +00:00
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
longname of user
|
|
|
|
|
|
|
|
user = User.find(123)
|
|
|
|
result = user.longname
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = "Bob Smith"
|
|
|
|
|
|
|
|
or with org
|
|
|
|
|
|
|
|
result = "Bob Smith (Org ABC)"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def longname
|
|
|
|
name = fullname
|
|
|
|
if organization_id
|
|
|
|
organization = Organization.lookup(id: organization_id)
|
|
|
|
if organization
|
|
|
|
name += " (#{organization.name})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
name
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if user is in role
|
|
|
|
|
|
|
|
user = User.find(123)
|
2015-05-08 08:15:45 +00:00
|
|
|
result = user.role?('Customer')
|
2013-08-17 22:10:02 +00:00
|
|
|
|
2015-09-11 08:22:15 +00:00
|
|
|
result = user.role?(['Agent', 'Admin'])
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
returns
|
|
|
|
|
|
|
|
result = true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
def role?(role_name)
|
2015-05-08 08:15:45 +00:00
|
|
|
|
|
|
|
result = false
|
2015-05-07 12:10:38 +00:00
|
|
|
roles.each { |role|
|
2015-09-11 08:22:15 +00:00
|
|
|
if role_name.class == Array
|
|
|
|
next if !role_name.include?(role.name)
|
2016-01-15 17:22:57 +00:00
|
|
|
elsif role.name != role_name
|
|
|
|
next
|
2015-09-11 08:22:15 +00:00
|
|
|
end
|
2015-05-08 08:15:45 +00:00
|
|
|
result = true
|
|
|
|
break
|
2012-09-04 21:28:49 +00:00
|
|
|
}
|
2015-05-08 08:15:45 +00:00
|
|
|
result
|
2012-09-04 21:28:49 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
get users activity stream
|
|
|
|
|
|
|
|
user = User.find(123)
|
2016-01-15 19:09:16 +00:00
|
|
|
result = user.activity_stream(20)
|
2013-09-28 00:07:11 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [
|
|
|
|
{
|
2016-06-08 04:56:05 +00:00
|
|
|
id: 2,
|
|
|
|
o_id: 2,
|
|
|
|
created_by_id: 3,
|
|
|
|
created_at: '2013-09-28 00:57:21',
|
|
|
|
object: "User",
|
|
|
|
type: "created",
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
{
|
2016-06-08 04:56:05 +00:00
|
|
|
id: 2,
|
|
|
|
o_id: 2,
|
|
|
|
created_by_id: 3,
|
|
|
|
created_at: '2013-09-28 00:59:21',
|
|
|
|
object: "User",
|
|
|
|
type: "updated",
|
2013-09-28 00:07:11 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
def activity_stream(limit, fulldata = false)
|
|
|
|
activity_stream = ActivityStream.list(self, limit)
|
2013-09-28 00:07:11 +00:00
|
|
|
return activity_stream if !fulldata
|
|
|
|
|
|
|
|
# get related objects
|
2014-08-26 07:40:25 +00:00
|
|
|
assets = ApplicationModel.assets_of_object_list(activity_stream)
|
|
|
|
|
2015-04-30 17:20:27 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
activity_stream: activity_stream,
|
|
|
|
assets: assets,
|
2013-09-28 00:07:11 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
authenticate user
|
|
|
|
|
|
|
|
result = User.authenticate(username, password)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # user model if authentication was successfully
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
def self.authenticate(username, password)
|
2012-07-29 15:27:01 +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 == ''
|
2013-06-12 15:59:58 +00:00
|
|
|
return if !password || password == ''
|
2012-07-29 15:27:01 +00:00
|
|
|
|
2012-04-20 08:58:31 +00:00
|
|
|
# try to find user based on login
|
2016-02-27 16:00:04 +00:00
|
|
|
user = User.find_by(login: username.downcase, active: true)
|
2012-07-29 15:27:01 +00:00
|
|
|
|
2012-04-20 08:58:31 +00:00
|
|
|
# try second lookup with email
|
|
|
|
if !user
|
2016-02-27 16:00:04 +00:00
|
|
|
user = User.find_by(email: username.downcase, active: true)
|
2012-04-20 08:58:31 +00:00
|
|
|
end
|
2012-07-29 15:27:01 +00:00
|
|
|
|
2013-02-07 21:24:03 +00:00
|
|
|
# check failed logins
|
2015-02-11 23:04:13 +00:00
|
|
|
max_login_failed = Setting.get('password_max_login_failed').to_i || 10
|
2013-02-12 22:49:52 +00:00
|
|
|
if user && user.login_failed > max_login_failed
|
2015-05-05 05:55:06 +00:00
|
|
|
logger.info "Max login faild reached for user #{user.login}."
|
2013-02-12 22:49:52 +00:00
|
|
|
return false
|
2013-02-07 21:24:03 +00:00
|
|
|
end
|
2012-07-29 15:27:01 +00:00
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
user_auth = Auth.check(username, password, user)
|
2013-01-23 22:13:02 +00:00
|
|
|
|
2013-02-07 21:24:03 +00:00
|
|
|
# set login failed +1
|
2013-02-12 23:21:56 +00:00
|
|
|
if !user_auth && user
|
2013-08-17 21:48:01 +00:00
|
|
|
sleep 1
|
2013-02-12 23:21:56 +00:00
|
|
|
user.login_failed = user.login_failed + 1
|
|
|
|
user.save
|
|
|
|
end
|
2013-02-07 21:24:03 +00:00
|
|
|
|
2013-08-17 21:48:01 +00:00
|
|
|
# auth ok
|
2014-12-30 23:51:19 +00:00
|
|
|
user_auth
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
authenticate user agains sso
|
|
|
|
|
|
|
|
result = User.sso(sso_params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # user model if authentication was successfully
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-02-17 18:28:32 +00:00
|
|
|
def self.sso(params)
|
|
|
|
|
|
|
|
# try to login against configure auth backends
|
2016-01-15 19:09:16 +00:00
|
|
|
user_auth = Sso.check(params)
|
2013-08-17 21:48:01 +00:00
|
|
|
return if !user_auth
|
2013-02-17 18:28:32 +00:00
|
|
|
|
2014-12-30 23:51:19 +00:00
|
|
|
user_auth
|
2013-02-17 18:28:32 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
create user from from omni auth hash
|
|
|
|
|
|
|
|
result = User.create_from_hash!(hash)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # user model if create was successfully
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.create_from_hash!(hash)
|
2015-07-06 18:33:37 +00:00
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
roles = Role.where(name: 'Customer')
|
2012-04-10 14:06:46 +00:00
|
|
|
url = ''
|
2015-04-30 18:16:25 +00:00
|
|
|
if hash['info']['urls']
|
2016-06-30 20:04:48 +00:00
|
|
|
hash['info']['urls'].each { |_name, local_url|
|
2015-07-06 18:33:37 +00:00
|
|
|
next if !local_url
|
|
|
|
next if local_url.empty?
|
|
|
|
url = local_url
|
|
|
|
}
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2015-05-07 12:10:38 +00:00
|
|
|
create(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: hash['info']['nickname'] || hash['uid'],
|
|
|
|
firstname: hash['info']['name'],
|
|
|
|
email: hash['info']['email'],
|
2015-07-06 18:33:37 +00:00
|
|
|
image_source: hash['info']['image'],
|
|
|
|
web: url,
|
|
|
|
address: hash['info']['location'],
|
2015-04-27 13:42:53 +00:00
|
|
|
note: hash['info']['description'],
|
|
|
|
source: hash['provider'],
|
|
|
|
roles: roles,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-04-10 14:06:46 +00:00
|
|
|
)
|
|
|
|
end
|
2012-04-23 06:55:16 +00:00
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
2016-02-19 21:05:36 +00:00
|
|
|
generate new token for reset password
|
2013-08-17 22:10:02 +00:00
|
|
|
|
2016-02-19 21:05:36 +00:00
|
|
|
result = User.password_reset_new_token(username)
|
2013-08-17 22:10:02 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2016-02-19 21:05:36 +00:00
|
|
|
result = {
|
|
|
|
token: token,
|
|
|
|
user: user,
|
|
|
|
}
|
2013-08-17 22:10:02 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-02-19 21:05:36 +00:00
|
|
|
def self.password_reset_new_token(username)
|
2012-04-23 06:55:16 +00:00
|
|
|
return if !username || username == ''
|
|
|
|
|
|
|
|
# try to find user based on login
|
2016-01-15 19:09:16 +00:00
|
|
|
user = User.find_by(login: username.downcase, active: true)
|
2012-10-18 08:10:12 +00:00
|
|
|
|
2012-04-23 06:55:16 +00:00
|
|
|
# try second lookup with email
|
|
|
|
if !user
|
2016-01-15 19:09:16 +00:00
|
|
|
user = User.find_by(email: username.downcase, active: true)
|
2012-04-23 06:55:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check if email address exists
|
2012-09-20 12:08:02 +00:00
|
|
|
return if !user
|
2012-04-23 06:55:16 +00:00
|
|
|
return if !user.email
|
|
|
|
|
|
|
|
# generate token
|
2016-01-15 19:09:16 +00:00
|
|
|
token = Token.create(action: 'PasswordReset', user_id: user.id)
|
2012-04-23 06:55:16 +00:00
|
|
|
|
2016-02-19 21:05:36 +00:00
|
|
|
{
|
|
|
|
token: token,
|
|
|
|
user: user,
|
2012-04-23 06:55:16 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check reset password token
|
|
|
|
|
|
|
|
result = User.password_reset_check(token)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # user_model if token was verified
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-23 06:55:16 +00:00
|
|
|
def self.password_reset_check(token)
|
2016-01-15 19:09:16 +00:00
|
|
|
user = Token.check(action: 'PasswordReset', name: token)
|
2013-07-16 07:05:59 +00:00
|
|
|
|
|
|
|
# reset login failed if token is valid
|
|
|
|
if user
|
|
|
|
user.login_failed = 0
|
|
|
|
user.save
|
|
|
|
end
|
2014-12-30 23:51:19 +00:00
|
|
|
user
|
2012-04-23 06:55:16 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
2016-06-01 14:58:11 +00:00
|
|
|
reset password with token and set new password
|
2013-08-17 22:10:02 +00:00
|
|
|
|
|
|
|
result = User.password_reset_via_token(token,password)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # user_model if token was verified
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 14:53:29 +00:00
|
|
|
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
|
2016-01-15 19:09:16 +00:00
|
|
|
user = Token.check(action: 'PasswordReset', name: token)
|
2013-01-03 12:00:55 +00:00
|
|
|
return if !user
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2012-04-23 06:55:16 +00:00
|
|
|
# reset password
|
2016-01-15 19:09:16 +00:00
|
|
|
user.update_attributes(password: password)
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2012-04-23 06:55:16 +00:00
|
|
|
# delete token
|
2016-01-15 19:09:16 +00:00
|
|
|
Token.find_by(action: 'PasswordReset', name: token).destroy
|
2014-12-30 23:51:19 +00:00
|
|
|
user
|
2012-04-23 06:55:16 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 22:10:02 +00:00
|
|
|
=begin
|
|
|
|
|
2013-10-22 06:43:49 +00:00
|
|
|
update last login date and reset login_failed (is automatically done by auth and sso backend)
|
2013-08-17 22:10:02 +00:00
|
|
|
|
|
|
|
user = User.find(123)
|
|
|
|
result = user.update_last_login
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = new_user_model
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-10-18 08:10:12 +00:00
|
|
|
def update_last_login
|
2015-05-08 10:20:33 +00:00
|
|
|
self.last_login = Time.zone.now
|
2013-10-22 06:43:49 +00:00
|
|
|
|
|
|
|
# reset login failed
|
|
|
|
self.login_failed = 0
|
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
save
|
2012-10-18 08:10:12 +00:00
|
|
|
end
|
|
|
|
|
2015-07-25 14:36:16 +00:00
|
|
|
=begin
|
|
|
|
|
2016-06-01 14:58:11 +00:00
|
|
|
generate new token for signup
|
|
|
|
|
|
|
|
result = User.signup_new_token(user) # or email
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = {
|
|
|
|
token: token,
|
|
|
|
user: user,
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.signup_new_token(user)
|
|
|
|
return if !user
|
|
|
|
return if !user.email
|
|
|
|
|
|
|
|
# generate token
|
|
|
|
token = Token.create(action: 'Signup', user_id: user.id)
|
|
|
|
|
|
|
|
{
|
|
|
|
token: token,
|
|
|
|
user: user,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
verify signup with token
|
|
|
|
|
|
|
|
result = User.signup_verify_via_token(token, user)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # user_model if token was verified
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.signup_verify_via_token(token, user = nil)
|
|
|
|
|
|
|
|
# check token
|
|
|
|
local_user = Token.check(action: 'Signup', name: token)
|
|
|
|
return if !local_user
|
|
|
|
|
|
|
|
# if requested user is different to current user
|
|
|
|
return if user && local_user.id != user.id
|
|
|
|
|
|
|
|
# set verified
|
|
|
|
local_user.update_attributes(verified: true)
|
|
|
|
|
|
|
|
# delete token
|
|
|
|
Token.find_by(action: 'Signup', name: token).destroy
|
|
|
|
local_user
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
merge two users to one
|
|
|
|
|
|
|
|
user = User.find(123)
|
|
|
|
result = user.merge(user_id_of_duplicate_user)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = new_user_model
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def merge(user_id_of_duplicate_user)
|
|
|
|
|
|
|
|
# find email addresses and move them to primary user
|
|
|
|
duplicate_user = User.find(user_id_of_duplicate_user)
|
|
|
|
|
|
|
|
# merge missing attibutes
|
|
|
|
Models.merge('User', id, user_id_of_duplicate_user)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-07-25 14:36:16 +00:00
|
|
|
list of active users in role
|
|
|
|
|
2016-02-07 13:00:29 +00:00
|
|
|
result = User.of_role('Agent', group_ids)
|
2015-07-25 14:36:16 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [user1, user2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-02-07 13:00:29 +00:00
|
|
|
def self.of_role(role, group_ids = nil)
|
2016-01-15 19:09:16 +00:00
|
|
|
roles_ids = Role.where(active: true, name: role).map(&:id)
|
2016-02-07 13:00:29 +00:00
|
|
|
if !group_ids
|
2016-04-25 23:41:38 +00:00
|
|
|
return User.where(active: true).joins(:users_roles).where('roles_users.role_id IN (?)', roles_ids).order('users.updated_at DESC')
|
2016-02-07 13:00:29 +00:00
|
|
|
end
|
|
|
|
User.where(active: true)
|
|
|
|
.joins(:users_roles)
|
|
|
|
.joins(:users_groups)
|
2016-04-25 23:41:38 +00:00
|
|
|
.where('roles_users.role_id IN (?) AND users_groups.group_ids IN (?)', roles_ids, group_ids).order('users.updated_at DESC')
|
2015-07-25 14:36:16 +00:00
|
|
|
end
|
|
|
|
|
2016-02-08 07:12:04 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
update/sync default preferences of users in a dedecated role
|
|
|
|
|
|
|
|
result = User.update_default_preferences('Agent')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true # false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.update_default_preferences(role_name)
|
|
|
|
role = Role.lookup(name: role_name)
|
2016-06-30 20:04:48 +00:00
|
|
|
User.of_role(role_name).each { |user|
|
2016-02-08 07:12:04 +00:00
|
|
|
user.check_notifications(role)
|
|
|
|
user.check_preferences_default
|
|
|
|
user.save
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_notifications(o)
|
|
|
|
default = Rails.configuration.preferences_default_by_role
|
|
|
|
return if !default
|
|
|
|
default.deep_stringify_keys!
|
|
|
|
return if !default[o.name]
|
|
|
|
if !@preferences_default
|
|
|
|
@preferences_default = {}
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
default[o.name].each { |key, value|
|
2016-02-08 07:12:04 +00:00
|
|
|
next if @preferences_default[key]
|
|
|
|
@preferences_default[key] = value
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_preferences_default
|
|
|
|
return if !@preferences_default
|
|
|
|
return if @preferences_default.empty?
|
|
|
|
preferences_tmp = @preferences_default.merge(preferences)
|
|
|
|
self.preferences = preferences_tmp
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
2012-10-25 22:12:16 +00:00
|
|
|
|
2015-06-18 22:39:34 +00:00
|
|
|
def cache_delete
|
|
|
|
super
|
|
|
|
|
|
|
|
# delete asset caches
|
|
|
|
key = "User::authorizations::#{id}"
|
|
|
|
Cache.delete(key)
|
|
|
|
key = "User::role_ids::#{id}"
|
|
|
|
Cache.delete(key)
|
|
|
|
key = "User::group_ids::#{id}"
|
|
|
|
Cache.delete(key)
|
|
|
|
key = "User::organization_ids::#{id}"
|
|
|
|
Cache.delete(key)
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def check_name
|
2012-10-25 22:12:16 +00:00
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
if (firstname && !firstname.empty?) && (!lastname || lastname.empty?)
|
2012-10-25 22:12:16 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# Lastname, Firstname
|
2015-05-07 12:10:38 +00:00
|
|
|
scan = firstname.scan(/, /)
|
2013-06-12 15:59:58 +00:00
|
|
|
if scan[0]
|
2015-05-07 12:10:38 +00:00
|
|
|
name = firstname.split(', ', 2)
|
2015-04-30 17:47:49 +00:00
|
|
|
if !name[0].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
self.lastname = name[0]
|
|
|
|
end
|
2015-04-30 17:47:49 +00:00
|
|
|
if !name[1].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
self.firstname = name[1]
|
|
|
|
end
|
2012-10-25 22:12:16 +00:00
|
|
|
return
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-10-25 22:12:16 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# Firstname Lastname
|
2015-05-07 12:10:38 +00:00
|
|
|
name = firstname.split(' ', 2)
|
2015-04-30 17:47:49 +00:00
|
|
|
if !name[0].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
self.firstname = name[0]
|
|
|
|
end
|
2015-04-30 17:47:49 +00:00
|
|
|
if !name[1].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
self.lastname = name[1]
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
return
|
2012-10-25 22:12:16 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# -no name- firstname.lastname@example.com
|
2016-01-15 19:09:16 +00:00
|
|
|
elsif (!firstname || firstname.empty?) && (!lastname || lastname.empty?) && (email && !email.empty?)
|
2015-05-07 12:10:38 +00:00
|
|
|
scan = email.scan(/^(.+?)\.(.+?)\@.+?$/)
|
2013-06-12 15:59:58 +00:00
|
|
|
if scan[0]
|
2015-04-30 17:47:49 +00:00
|
|
|
if !scan[0][0].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
self.firstname = scan[0][0].capitalize
|
|
|
|
end
|
2015-04-30 17:47:49 +00:00
|
|
|
if !scan[0][1].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
self.lastname = scan[0][1].capitalize
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-04-29 20:47:35 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def check_email
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !email
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
self.email = email.downcase
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-04-29 20:47:35 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def check_login
|
2015-01-07 20:42:12 +00:00
|
|
|
|
|
|
|
# use email as login if not given
|
2015-05-07 12:10:38 +00:00
|
|
|
if !login && email
|
|
|
|
self.login = email
|
2014-09-25 06:20:20 +00:00
|
|
|
end
|
2015-01-07 20:42:12 +00:00
|
|
|
|
|
|
|
# if email has changed, login is old email, change also login
|
2015-05-07 12:10:38 +00:00
|
|
|
if changes && changes['email']
|
|
|
|
if changes['email'][0] == login
|
|
|
|
self.login = email
|
2015-01-07 20:42:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if login already exists
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !login
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
self.login = login.downcase
|
2015-04-30 15:25:04 +00:00
|
|
|
check = true
|
|
|
|
while check
|
2016-01-15 19:09:16 +00:00
|
|
|
exists = User.find_by(login: login)
|
2015-05-07 12:10:38 +00:00
|
|
|
if exists && exists.id != id
|
|
|
|
self.login = login + rand(999).to_s
|
2015-04-30 15:25:04 +00:00
|
|
|
else
|
|
|
|
check = false
|
2013-02-19 19:04:35 +00:00
|
|
|
end
|
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-02-19 19:04:35 +00:00
|
|
|
|
2015-07-06 18:33:37 +00:00
|
|
|
def avatar_for_email_check
|
2012-04-29 20:47:35 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !email
|
|
|
|
return if email.empty?
|
2015-10-23 11:02:11 +00:00
|
|
|
return if email !~ /@/
|
2013-11-02 21:32:00 +00:00
|
|
|
|
2014-12-01 07:32:35 +00:00
|
|
|
# save/update avatar
|
|
|
|
avatar = Avatar.auto_detection(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'User',
|
2015-05-07 12:10:38 +00:00
|
|
|
o_id: id,
|
|
|
|
url: email,
|
2015-04-27 13:42:53 +00:00
|
|
|
source: 'app',
|
2015-05-07 12:10:38 +00:00
|
|
|
updated_by_id: updated_by_id,
|
|
|
|
created_by_id: updated_by_id,
|
2014-12-01 07:32:35 +00:00
|
|
|
)
|
2013-11-02 21:32:00 +00:00
|
|
|
|
2014-12-01 07:32:35 +00:00
|
|
|
# update user link
|
2015-04-30 15:25:04 +00:00
|
|
|
return if !avatar
|
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
update_column(:image, avatar.store_hash)
|
2015-05-07 12:10:38 +00:00
|
|
|
cache_delete
|
2014-12-01 07:32:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_destroy
|
2016-01-15 19:09:16 +00:00
|
|
|
Avatar.remove('User', id)
|
2013-11-02 21:32:00 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def check_password
|
2012-10-18 11:42:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# set old password again if not given
|
2015-05-07 12:10:38 +00:00
|
|
|
if password == '' || !password
|
2012-04-20 15:39:50 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# get current record
|
2015-05-07 12:10:38 +00:00
|
|
|
if id
|
2014-12-01 07:32:35 +00:00
|
|
|
#current = User.find(self.id)
|
|
|
|
#self.password = current.password
|
2015-05-07 12:10:38 +00:00
|
|
|
self.password = password_was
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-01-23 22:13:02 +00:00
|
|
|
|
2014-12-01 07:32:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# crypt password if not already crypted
|
2015-05-07 12:10:38 +00:00
|
|
|
return if !password
|
|
|
|
return if password =~ /^\{sha2\}/
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2016-01-15 19:09:16 +00:00
|
|
|
crypted = Digest::SHA2.hexdigest(password)
|
2015-04-30 15:25:04 +00:00
|
|
|
self.password = "{sha2}#{crypted}"
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2016-02-07 13:00:29 +00:00
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|