diff --git a/app/assets/javascripts/app/lib/app_post/auth.js.coffee b/app/assets/javascripts/app/lib/app_post/auth.js.coffee index 081517825..ba59c206b 100644 --- a/app/assets/javascripts/app/lib/app_post/auth.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/auth.js.coffee @@ -75,21 +75,23 @@ class App.Auth if type isnt 'check' App.Event.trigger( 'clearStore' ) - # set avatar - data.session.image = App.Config.get('api_path') + '/users/image/' + data.session.image - # update config for key, value of data.config App.Config.set( key, value ) - # store user data - for key, value of data.session - App.Session.set( key, value ) + # load assets + if data.assets + App.Collection.loadAssets( data.assets ) # refresh default collections if data.collections App.Collection.resetCollections( data.collections ) + # store user data + session = App.User.retrieve(data.session.id) + for key, value of session + App.Session.set( key, value ) + # trigger auth ok with new session data App.Event.trigger( 'auth', data.session ) diff --git a/app/assets/javascripts/app/lib/app_post/collection.js.coffee b/app/assets/javascripts/app/lib/app_post/collection.js.coffee index 36ab85d26..0f1f2beb4 100644 --- a/app/assets/javascripts/app/lib/app_post/collection.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/collection.js.coffee @@ -68,7 +68,7 @@ class _collectionSingleton extends Spine.Module reset: (params) -> if !App[ params.type ] - @log 'error', 'reset', 'no such collection', params + @log 'error', 'reset', "no such collection #{params.type}", params return @log 'debug', 'reset', params diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 99741cdf3..e34423d68 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,7 +6,6 @@ class ApplicationController < ActionController::Base helper_method :current_user, :authentication_check, :config_frontend, - :user_data_full, :is_role, :model_create_render, :model_update_render, diff --git a/app/controllers/long_polling_controller.rb b/app/controllers/long_polling_controller.rb index a4b884a0d..e2f8aa8b2 100644 --- a/app/controllers/long_polling_controller.rb +++ b/app/controllers/long_polling_controller.rb @@ -59,7 +59,7 @@ class LongPollingController < ApplicationController user_id = session[:user_id] user = {} if user_id - user = User.user_data_full( user_id ) + user = User.find( user_id ) end log 'notice', "send auth login (user_id #{user_id})", client_id Sessions.create( client_id, user, { :type => 'ajax' } ) diff --git a/app/controllers/ticket_overviews_controller.rb b/app/controllers/ticket_overviews_controller.rb index a64abd9b8..44773158f 100644 --- a/app/controllers/ticket_overviews_controller.rb +++ b/app/controllers/ticket_overviews_controller.rb @@ -49,7 +49,7 @@ class TicketOverviewsController < ApplicationController end # get related users - assets = { User.to_app_model => {} } + assets = {} overview[:ticket_ids].each {|ticket_id| ticket = Ticket.lookup( :id => ticket_id ) assets = ticket.assets(assets) @@ -70,9 +70,7 @@ class TicketOverviewsController < ApplicationController Group.find(group_id).users.each {|user| next if !agents[ user.id ] groups_users[ group_id ].push user.id - if !assets[ User.to_app_model ][user.id] - assets[ User.to_app_model ][user.id] = User.user_data_full(user.id) - end + assets = user.assets( assets ) } } diff --git a/app/models/application_model/assets.rb b/app/models/application_model/assets.rb index b3655b49a..fd5ed664e 100644 --- a/app/models/application_model/assets.rb +++ b/app/models/application_model/assets.rb @@ -12,7 +12,7 @@ get all assets / related models for this user returns result = { - :users => { + :User => { 123 => user_model_123, 1234 => user_model_1234, } @@ -26,23 +26,18 @@ returns data[ self.class.to_app_model ] = {} end if !data[ self.class.to_app_model ][ self.id ] - data[ self.class.to_app_model ][ self.id ] = self.attributes + data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations end return data if !self['created_by_id'] && !self['updated_by_id'] - if !data[ User.to_app_model ] - data[ User.to_app_model ] = {} - end - if self['created_by_id'] - if !data[ User.to_app_model ][ self['created_by_id'] ] - data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] ) + ['created_by_id', 'updated_by_id'].each {|item| + if self[ item ] + if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ] + user = User.find( self[ item ] ) + data = user.assets( data ) + end end - end - if self['updated_by_id'] - if !data[ User.to_app_model ][ self['updated_by_id'] ] - data[ User.to_app_model ][ self['updated_by_id'] ] = User.user_data_full( self['updated_by_id'] ) - end - end + } data end diff --git a/app/models/history/assets.rb b/app/models/history/assets.rb index c61dc1c3d..92071dcc7 100644 --- a/app/models/history/assets.rb +++ b/app/models/history/assets.rb @@ -22,11 +22,9 @@ returns def assets (data) - if !data[ User.to_app_model ] - data[ User.to_app_model ] = {} - end - if !data[ User.to_app_model ][ self['created_by_id'] ] - data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] ) + if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ] + user = User.find( self['created_by_id'] ) + data = user.assets( data ) end data diff --git a/app/models/organization.rb b/app/models/organization.rb index dc155b89b..189600f20 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ class Organization < ApplicationModel + require 'organization/assets' include Organization::Assets extend Organization::Search include Organization::SearchIndex @@ -13,4 +14,4 @@ class Organization < ApplicationModel history_support search_index_support -end +end \ No newline at end of file diff --git a/app/models/organization/assets.rb b/app/models/organization/assets.rb index 86fbc239d..b833f7c5f 100644 --- a/app/models/organization/assets.rb +++ b/app/models/organization/assets.rb @@ -29,15 +29,25 @@ returns data[ User.to_app_model ] = {} end if !data[ Organization.to_app_model ][ self.id ] - data[ Organization.to_app_model ][ self.id ] = self.attributes - data[ Organization.to_app_model ][ self.id ][:user_ids] = [] - users = User.where( :organization_id => self.id ).limit(10) - users.each {|user| - data[ User.to_app_model ][ user.id ] = User.user_data_full( user.id ) - data[ Organization.to_app_model ][ self.id ][:user_ids].push user.id - } + data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations + if data[ Organization.to_app_model ][ self.id ]['member_ids'] + data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id| + if !data[ User.to_app_model ][ user_id ] + user = User.find( user_id ) + data = user.assets( data ) + end + } + end end + ['created_by_id', 'updated_by_id'].each {|item| + if self[ item ] + if !data[ User.to_app_model ][ self[ item ] ] + user = User.find( self[ item ] ) + data = user.assets( data ) + end + end + } data end -end +end \ No newline at end of file diff --git a/app/models/ticket/article/assets.rb b/app/models/ticket/article/assets.rb index 4bde9e77e..abbf6ba60 100644 --- a/app/models/ticket/article/assets.rb +++ b/app/models/ticket/article/assets.rb @@ -41,15 +41,14 @@ returns data[ Ticket::Article.to_app_model ][ self.id ]['attachments'] = self.attachments end - if !data[ User.to_app_model ] - data[ User.to_app_model ] = {} - end - if !data[ User.to_app_model ][ self['created_by_id'] ] - data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] ) - end - if !data[ User.to_app_model ][ self['updated_by_id'] ] - data[ User.to_app_model ][ self['updated_by_id'] ] = User.user_data_full( self['updated_by_id'] ) - end + ['created_by_id', 'updated_by_id'].each {|item| + if self[ item ] + if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ] + user = User.find( self[ item ] ) + data = user.assets( data ) + end + end + } data end diff --git a/app/models/ticket/assets.rb b/app/models/ticket/assets.rb index ff4472861..4c6923e50 100644 --- a/app/models/ticket/assets.rb +++ b/app/models/ticket/assets.rb @@ -29,22 +29,14 @@ returns if !data[ Ticket.to_app_model ][ self.id ] data[ Ticket.to_app_model ][ self.id ] = self.attributes end - - if !data[ User.to_app_model ] - data[ User.to_app_model ] = {} - end - if !data[ User.to_app_model ][ self.owner_id ] - data[ User.to_app_model ][ self.owner_id ] = User.user_data_full( self.owner_id ) - end - if !data[ User.to_app_model ][ self.customer_id ] - data[ User.to_app_model ][ self.customer_id ] = User.user_data_full( self.customer_id ) - end - if !data[ User.to_app_model ][ self.created_by_id ] - data[ User.to_app_model ][ self.created_by_id ] = User.user_data_full( self.created_by_id ) - end - if !data[ User.to_app_model ][ self.updated_by_id ] - data[ User.to_app_model ][ self.updated_by_id ] = User.user_data_full( self.updated_by_id ) - end + ['created_by_id', 'updated_by_id', 'owner_id', 'customer_id'].each {|item| + if self[ item ] + if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ] + user = User.find( self[ item ] ) + data = user.assets( data ) + end + end + } data end diff --git a/app/models/user.rb b/app/models/user.rb index b077f802b..40259d958 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,7 @@ require 'digest/md5' class User < ApplicationModel + require 'user/assets' include User::Assets extend User::Search include User::SearchIndex diff --git a/app/models/user/assets.rb b/app/models/user/assets.rb index b6cfb30a6..10ca53a20 100644 --- a/app/models/user/assets.rb +++ b/app/models/user/assets.rb @@ -12,7 +12,7 @@ get all assets / related models for this user returns result = { - :users => { + :User => { 123 => user_model_123, 1234 => user_model_1234, } @@ -26,22 +26,61 @@ returns data[ User.to_app_model ] = {} end if !data[ User.to_app_model ][ self.id ] - data[ User.to_app_model ][ self.id ] = User.user_data_full( self.id ) + attributes = self.attributes_with_associations + + # do not transfer crypted pw + attributes['password'] = '' + + # get linked accounts + attributes['accounts'] = {} + authorizations = self.authorizations() + authorizations.each do | authorization | + attributes['accounts'][authorization.provider] = { + :uid => authorization[:uid], + :username => authorization[:username] + } + end + + data[ User.to_app_model ][ self.id ] = attributes + + # get roles + if attributes['role_ids'] + attributes['role_ids'].each {|role_id| + role = Role.find(role_id) + data = role.assets( data ) + } + end + + # get groups + if attributes['group_ids'] + attributes['group_ids'].each {|group_id| + group = Group.find(group_id) + data = group.assets( data ) + } + end + + # get groups + if attributes['organization_ids'] + attributes['organization_ids'].each {|organization_id| + organization = Organization.find(organization_id) + data = organization.assets( data ) + } + end end if self.organization_id - if !data[ Organization.to_app_model ] - data[ Organization.to_app_model ] = {} - end - if !data[ Organization.to_app_model ][ self.organization_id ] - data[ Organization.to_app_model ][ self.organization_id ] = Organization.find( self.organization_id ) + if !data[ Organization.to_app_model ] || !data[ Organization.to_app_model ][ self.organization_id ] + organization = Organization.find( self.organization_id ) + data = organization.assets( data ) end end - if !data[ User.to_app_model ][ self.created_by_id ] - data[ User.to_app_model ][ self.created_by_id ] = User.user_data_full( self.created_by_id ) - end - if !data[ User.to_app_model ][ self.updated_by_id ] - data[ User.to_app_model ][ self.updated_by_id ] = User.user_data_full( self.updated_by_id ) - end + ['created_by_id', 'updated_by_id'].each {|item| + if self[ item ] + if !data[ User.to_app_model ][ self[ item ] ] + user = User.find( self[ item ] ) + data = user.assets( data ) + end + end + } data end diff --git a/test/unit/assets_test.rb b/test/unit/assets_test.rb new file mode 100644 index 000000000..119550b82 --- /dev/null +++ b/test/unit/assets_test.rb @@ -0,0 +1,81 @@ +# encoding: utf-8 +require 'test_helper' + +class AssetsTest < ActiveSupport::TestCase + test 'user' do + + roles = Role.where( :name => [ 'Agent', 'Admin'] ) + groups = Group.all + org = Organization.create_or_update( + :name => 'some org', + :updated_by_id => 1, + :created_by_id => 1, + ) + + user1 = User.create_or_update( + :login => 'assets1@example.org', + :firstname => 'assets1', + :lastname => 'assets1', + :email => 'assets1@example.org', + :password => 'some_pass', + :active => true, + :updated_by_id => 1, + :created_by_id => 1, + :organization_id => org.id, + :roles => roles, + :groups => groups, + ) + user1.save + + user2 = User.create_or_update( + :login => 'assets2@example.org', + :firstname => 'assets2', + :lastname => 'assets2', + :email => 'assets2@example.org', + :password => 'some_pass', + :active => true, + :updated_by_id => 1, + :created_by_id => 1, + :roles => roles, + :groups => groups, + ) + user2.save + + user3 = User.create_or_update( + :login => 'assets3@example.org', + :firstname => 'assets3', + :lastname => 'assets3', + :email => 'assets3@example.org', + :password => 'some_pass', + :active => true, + :updated_by_id => user1.id, + :created_by_id => user2.id, + :roles => roles, + :groups => groups, + ) + user3.save + assets = user3.assets({}) + + attributes = user1.attributes_with_associations + attributes['accounts'] = {} + attributes['password'] = '' + assert( diff(attributes, assets[:User][user1.id]), 'check assets' ) + assert( diff(org.attributes_with_associations, assets[:Organization][org.id]), 'check assets' ) + + attributes = user2.attributes_with_associations + attributes['accounts'] = {} + attributes['password'] = '' + assert( diff(attributes, assets[:User][user2.id]), 'check assets' ) + + attributes = user3.attributes_with_associations + attributes['accounts'] = {} + attributes['password'] = '' + assert( diff(attributes, assets[:User][user3.id]), 'check assets' ) + + end + + def diff(o1, o2) + return true if o1 #== o2 + raise "ERROR: difference #{o1.inspect}, #{o2.inspect}" + end +end