user.rb 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. class User < ApplicationModel
  2. include Gmaps
  3. before_create :check_name, :check_email, :check_login, :check_image, :check_geo
  4. before_update :check_password, :check_image, :check_geo, :check_email, :check_login
  5. after_create :cache_delete
  6. after_update :cache_delete
  7. after_destroy :cache_delete
  8. has_and_belongs_to_many :groups, :after_add => :cache_update, :after_remove => :cache_update
  9. has_and_belongs_to_many :roles, :after_add => :cache_update, :after_remove => :cache_update
  10. has_and_belongs_to_many :organizations, :after_add => :cache_update, :after_remove => :cache_update
  11. has_many :tokens, :after_add => :cache_update, :after_remove => :cache_update
  12. has_many :authorizations, :after_add => :cache_update, :after_remove => :cache_update
  13. belongs_to :organization, :class_name => 'Organization'
  14. store :preferences
  15. def fullname
  16. fullname = ''
  17. if self.firstname
  18. fullname = fullname + self.firstname
  19. end
  20. if self.lastname
  21. if fullname != ''
  22. fullname = fullname + ' '
  23. end
  24. fullname = fullname + self.lastname
  25. end
  26. return fullname
  27. end
  28. def is_role( role_name )
  29. self.roles.each { |role|
  30. return role if role.name == role_name
  31. }
  32. return false
  33. end
  34. def self.authenticate( username, password )
  35. # do not authenticate with nothing
  36. return if !username || username == ''
  37. return if !password || password == ''
  38. # try to find user based on login
  39. user = User.where( :login => username, :active => true ).first
  40. # try second lookup with email
  41. if !user
  42. user = User.where( :email => username, :active => true ).first
  43. end
  44. # no user found
  45. return nil if !user
  46. # auth ok
  47. if user.password == password
  48. return user
  49. end
  50. # auth failed
  51. return false
  52. end
  53. def self.create_from_hash!(hash)
  54. url = ''
  55. if hash['info']['urls'] then
  56. url = hash['info']['urls']['Website'] || hash['info']['urls']['Twitter'] || ''
  57. end
  58. roles = Role.where( :name => 'Customer' )
  59. self.create(
  60. :login => hash['info']['nickname'] || hash['uid'],
  61. :firstname => hash['info']['name'],
  62. :email => hash['info']['email'],
  63. :image => hash['info']['image'],
  64. # :url => url.to_s,
  65. :note => hash['info']['description'],
  66. :source => hash['provider'],
  67. :roles => roles,
  68. :created_by_id => 1
  69. )
  70. end
  71. def self.password_reset_send(username)
  72. return if !username || username == ''
  73. # try to find user based on login
  74. user = User.where( :login => username, :active => true ).first
  75. # try second lookup with email
  76. if !user
  77. user = User.where( :email => username, :active => true ).first
  78. end
  79. # check if email address exists
  80. return if !user
  81. return if !user.email
  82. # generate token
  83. token = Token.create( :action => 'PasswordReset', :user_id => user.id )
  84. # send mail
  85. data = {}
  86. data[:subject] = 'Reset your #{config.product_name} password'
  87. data[:body] = 'Forgot your password?
  88. We received a request to reset the password for your #{config.product_name} account (#{user.login}).
  89. If you want to reset your password, click on the link below (or copy and paste the URL into your browser):
  90. #{config.http_type}://#{config.fqdn}/#password_reset_verify/#{token.name}
  91. This link takes you to a page where you can change your password.
  92. If you don\'t want to reset your password, please ignore this message. Your password will not be reset.
  93. Your #{config.product_name} Team
  94. '
  95. # prepare subject & body
  96. [:subject, :body].each { |key|
  97. data[key.to_sym] = NotificationFactory.build(
  98. :string => data[key.to_sym],
  99. :objects => {
  100. :token => token,
  101. :user => user,
  102. }
  103. )
  104. }
  105. # send notification
  106. NotificationFactory.send(
  107. :recipient => user,
  108. :subject => data[:subject],
  109. :body => data[:body]
  110. )
  111. return true
  112. end
  113. # check token
  114. def self.password_reset_check(token)
  115. token = Token.check( :action => 'PasswordReset', :name => token )
  116. return if !token
  117. return true
  118. end
  119. def self.password_reset_via_token(token,password)
  120. # check token
  121. token = Token.check( :action => 'PasswordReset', :name => token )
  122. return if !token
  123. # reset password
  124. token.user.update_attributes( :password => password )
  125. # delete token
  126. token.delete
  127. token.save
  128. return true
  129. end
  130. def self.find_fulldata(user_id)
  131. cache = self.cache_get(user_id)
  132. return cache if cache
  133. # get user
  134. user = User.find(user_id)
  135. data = user.attributes
  136. # do not show password
  137. user['password'] = ''
  138. # get linked accounts
  139. data['accounts'] = {}
  140. authorizations = user.authorizations() || []
  141. authorizations.each do | authorization |
  142. data['accounts'][authorization.provider] = {
  143. :uid => authorization[:uid],
  144. :username => authorization[:username]
  145. }
  146. end
  147. # set roles
  148. roles = []
  149. user.roles.select('id, name').where( :active => true ).each { |role|
  150. roles.push role.attributes
  151. }
  152. data['roles'] = roles
  153. data['role_ids'] = user.role_ids
  154. groups = []
  155. user.groups.select('id, name').where( :active => true ).each { |group|
  156. groups.push group.attributes
  157. }
  158. data['groups'] = groups
  159. data['group_ids'] = user.group_ids
  160. organization = user.organization
  161. if organization
  162. data['organization'] = organization.attributes
  163. end
  164. organizations = []
  165. user.organizations.select('id, name').where( :active => true ).each { |organization|
  166. organizations.push organization.attributes
  167. }
  168. data['organizations'] = organizations
  169. data['organization_ids'] = user.organization_ids
  170. self.cache_set(user.id, data)
  171. return data
  172. end
  173. def self.user_data_full (user_id)
  174. # get user
  175. user = User.find_fulldata(user_id)
  176. # do not show password
  177. user['password'] = ''
  178. # TEMP: compat. reasons
  179. user['preferences'] = {} if user['preferences'] == nil
  180. items = []
  181. if user['preferences'][:tickets_open].to_i > 0
  182. item = {
  183. :url => '',
  184. :name => 'open',
  185. :count => user['preferences'][:tickets_open] || 0,
  186. :title => 'Open Tickets',
  187. :class => 'user-tickets',
  188. :data => 'open'
  189. }
  190. items.push item
  191. end
  192. if user['preferences'][:tickets_closed].to_i > 0
  193. item = {
  194. :url => '',
  195. :name => 'closed',
  196. :count => user['preferences'][:tickets_closed] || 0,
  197. :title => 'Closed Tickets',
  198. :class => 'user-tickets',
  199. :data => 'closed'
  200. }
  201. items.push item
  202. end
  203. # show linked topics and items
  204. if items.count > 0
  205. topic = {
  206. :title => 'Tickets',
  207. :items => items,
  208. }
  209. user['links'] = []
  210. user['links'].push topic
  211. end
  212. return user
  213. end
  214. # update all users geo data
  215. def self.geo_update_all
  216. User.all.each { |user|
  217. user.geo_update
  218. user.save
  219. }
  220. end
  221. # update geo data of one user
  222. def geo_update
  223. address = ''
  224. location = ['street', 'zip', 'city', 'country']
  225. location.each { |item|
  226. if self[item] && self[item] != ''
  227. address = address + ',' + self[item]
  228. end
  229. }
  230. # return if no address is given
  231. return if address == ''
  232. # dp lookup
  233. latlng = Gmaps.geocode(address)
  234. if latlng
  235. self.preferences['lat'] = latlng[0]
  236. self.preferences['lng'] = latlng[1]
  237. end
  238. end
  239. def update_last_login
  240. self.last_login = Time.now
  241. self.save
  242. end
  243. private
  244. def check_geo
  245. # geo update if no user exists
  246. if !self.id
  247. self.geo_update
  248. return
  249. end
  250. location = ['street', 'zip', 'city', 'country']
  251. # get current user data
  252. current = User.where( :id => self.id ).first
  253. return if !current
  254. # check if geo update is needed
  255. current_location = {}
  256. location.each { |item|
  257. current_location[item] = current[item]
  258. }
  259. # get full address
  260. next_location = {}
  261. location.each { |item|
  262. next_location[item] = self[item]
  263. }
  264. # return if address hasn't changed and geo data is already available
  265. return if ( current_location == next_location ) && ( self.preferences['lat'] && self.preferences['lng'] )
  266. # geo update
  267. self.geo_update
  268. end
  269. def check_name
  270. if ( self.firstname && !self.firstname.empty? ) && ( !self.lastname || self.lastname.empty? )
  271. # Lastname, Firstname
  272. scan = self.firstname.scan(/, /)
  273. if scan[0]
  274. name = self.firstname.split(', ', 2)
  275. self.lastname = name[0]
  276. self.firstname = name[1]
  277. return
  278. end
  279. # Firstname Lastname
  280. name = self.firstname.split(' ', 2)
  281. self.firstname = name[0]
  282. self.lastname = name[1]
  283. return
  284. # -no name- firstname.lastname@example.com
  285. elsif ( !self.firstname || self.firstname.empty? ) && ( !self.lastname || self.lastname.empty? ) && ( self.email && !self.email.empty? )
  286. scan = self.email.scan(/^(.+?)\.(.+?)\@.+?$/)
  287. if scan[0]
  288. self.firstname = scan[0][0].capitalize
  289. self.lastname = scan[0][1].capitalize
  290. end
  291. end
  292. end
  293. def check_email
  294. if self.email
  295. self.email = self.email.downcase
  296. end
  297. end
  298. def check_login
  299. if self.login
  300. self.login = self.login.downcase
  301. end
  302. end
  303. def check_image
  304. require 'digest/md5'
  305. if !self.image || self.image == ''
  306. if self.email
  307. hash = Digest::MD5.hexdigest(self.email)
  308. self.image = "http://www.gravatar.com/avatar/#{hash}?s=48"
  309. end
  310. end
  311. end
  312. def check_password
  313. # set old password again
  314. if self.password == '' || !self.password
  315. # get current record
  316. current = User.find(self.id)
  317. self.password = current.password
  318. end
  319. end
  320. end