Improved asset management.

This commit is contained in:
Martin Edenhofer 2014-08-03 00:06:51 +02:00
parent 5458de229f
commit da7af2fc63
14 changed files with 194 additions and 79 deletions

View file

@ -75,21 +75,23 @@ class App.Auth
if type isnt 'check' if type isnt 'check'
App.Event.trigger( 'clearStore' ) App.Event.trigger( 'clearStore' )
# set avatar
data.session.image = App.Config.get('api_path') + '/users/image/' + data.session.image
# update config # update config
for key, value of data.config for key, value of data.config
App.Config.set( key, value ) App.Config.set( key, value )
# store user data # load assets
for key, value of data.session if data.assets
App.Session.set( key, value ) App.Collection.loadAssets( data.assets )
# refresh default collections # refresh default collections
if data.collections if data.collections
App.Collection.resetCollections( 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 # trigger auth ok with new session data
App.Event.trigger( 'auth', data.session ) App.Event.trigger( 'auth', data.session )

View file

@ -68,7 +68,7 @@ class _collectionSingleton extends Spine.Module
reset: (params) -> reset: (params) ->
if !App[ params.type ] if !App[ params.type ]
@log 'error', 'reset', 'no such collection', params @log 'error', 'reset', "no such collection #{params.type}", params
return return
@log 'debug', 'reset', params @log 'debug', 'reset', params

View file

@ -6,7 +6,6 @@ class ApplicationController < ActionController::Base
helper_method :current_user, helper_method :current_user,
:authentication_check, :authentication_check,
:config_frontend, :config_frontend,
:user_data_full,
:is_role, :is_role,
:model_create_render, :model_create_render,
:model_update_render, :model_update_render,

View file

@ -59,7 +59,7 @@ class LongPollingController < ApplicationController
user_id = session[:user_id] user_id = session[:user_id]
user = {} user = {}
if user_id if user_id
user = User.user_data_full( user_id ) user = User.find( user_id )
end end
log 'notice', "send auth login (user_id #{user_id})", client_id log 'notice', "send auth login (user_id #{user_id})", client_id
Sessions.create( client_id, user, { :type => 'ajax' } ) Sessions.create( client_id, user, { :type => 'ajax' } )

View file

@ -49,7 +49,7 @@ class TicketOverviewsController < ApplicationController
end end
# get related users # get related users
assets = { User.to_app_model => {} } assets = {}
overview[:ticket_ids].each {|ticket_id| overview[:ticket_ids].each {|ticket_id|
ticket = Ticket.lookup( :id => ticket_id ) ticket = Ticket.lookup( :id => ticket_id )
assets = ticket.assets(assets) assets = ticket.assets(assets)
@ -70,9 +70,7 @@ class TicketOverviewsController < ApplicationController
Group.find(group_id).users.each {|user| Group.find(group_id).users.each {|user|
next if !agents[ user.id ] next if !agents[ user.id ]
groups_users[ group_id ].push user.id groups_users[ group_id ].push user.id
if !assets[ User.to_app_model ][user.id] assets = user.assets( assets )
assets[ User.to_app_model ][user.id] = User.user_data_full(user.id)
end
} }
} }

View file

@ -12,7 +12,7 @@ get all assets / related models for this user
returns returns
result = { result = {
:users => { :User => {
123 => user_model_123, 123 => user_model_123,
1234 => user_model_1234, 1234 => user_model_1234,
} }
@ -26,23 +26,18 @@ returns
data[ self.class.to_app_model ] = {} data[ self.class.to_app_model ] = {}
end end
if !data[ self.class.to_app_model ][ self.id ] 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 end
return data if !self['created_by_id'] && !self['updated_by_id'] return data if !self['created_by_id'] && !self['updated_by_id']
if !data[ User.to_app_model ] ['created_by_id', 'updated_by_id'].each {|item|
data[ User.to_app_model ] = {} if self[ item ]
end if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
if self['created_by_id'] user = User.find( self[ item ] )
if !data[ User.to_app_model ][ self['created_by_id'] ] data = user.assets( data )
data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] )
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
end end
}
data data
end end

View file

@ -22,11 +22,9 @@ returns
def assets (data) def assets (data)
if !data[ User.to_app_model ] if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ]
data[ User.to_app_model ] = {} user = User.find( self['created_by_id'] )
end data = user.assets( data )
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 end
data data

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Organization < ApplicationModel class Organization < ApplicationModel
require 'organization/assets'
include Organization::Assets include Organization::Assets
extend Organization::Search extend Organization::Search
include Organization::SearchIndex include Organization::SearchIndex

View file

@ -29,14 +29,24 @@ returns
data[ User.to_app_model ] = {} data[ User.to_app_model ] = {}
end end
if !data[ Organization.to_app_model ][ self.id ] if !data[ Organization.to_app_model ][ self.id ]
data[ Organization.to_app_model ][ self.id ] = self.attributes data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations
data[ Organization.to_app_model ][ self.id ][:user_ids] = [] if data[ Organization.to_app_model ][ self.id ]['member_ids']
users = User.where( :organization_id => self.id ).limit(10) data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id|
users.each {|user| if !data[ User.to_app_model ][ user_id ]
data[ User.to_app_model ][ user.id ] = User.user_data_full( user.id ) user = User.find( user_id )
data[ Organization.to_app_model ][ self.id ][:user_ids].push user.id data = user.assets( data )
end
} }
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 data
end end

View file

@ -41,15 +41,14 @@ returns
data[ Ticket::Article.to_app_model ][ self.id ]['attachments'] = self.attachments data[ Ticket::Article.to_app_model ][ self.id ]['attachments'] = self.attachments
end end
if !data[ User.to_app_model ] ['created_by_id', 'updated_by_id'].each {|item|
data[ User.to_app_model ] = {} 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
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 end
}
data data
end end

View file

@ -29,22 +29,14 @@ returns
if !data[ Ticket.to_app_model ][ self.id ] if !data[ Ticket.to_app_model ][ self.id ]
data[ Ticket.to_app_model ][ self.id ] = self.attributes data[ Ticket.to_app_model ][ self.id ] = self.attributes
end end
['created_by_id', 'updated_by_id', 'owner_id', 'customer_id'].each {|item|
if !data[ User.to_app_model ] if self[ item ]
data[ User.to_app_model ] = {} if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end 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 end
}
data data
end end

View file

@ -3,6 +3,7 @@
require 'digest/md5' require 'digest/md5'
class User < ApplicationModel class User < ApplicationModel
require 'user/assets'
include User::Assets include User::Assets
extend User::Search extend User::Search
include User::SearchIndex include User::SearchIndex

View file

@ -12,7 +12,7 @@ get all assets / related models for this user
returns returns
result = { result = {
:users => { :User => {
123 => user_model_123, 123 => user_model_123,
1234 => user_model_1234, 1234 => user_model_1234,
} }
@ -26,22 +26,61 @@ returns
data[ User.to_app_model ] = {} data[ User.to_app_model ] = {}
end end
if !data[ User.to_app_model ][ self.id ] 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 end
if self.organization_id if self.organization_id
if !data[ Organization.to_app_model ] if !data[ Organization.to_app_model ] || !data[ Organization.to_app_model ][ self.organization_id ]
data[ Organization.to_app_model ] = {} organization = Organization.find( self.organization_id )
end data = organization.assets( data )
if !data[ Organization.to_app_model ][ self.organization_id ]
data[ Organization.to_app_model ][ self.organization_id ] = Organization.find( self.organization_id )
end end
end end
if !data[ User.to_app_model ][ self.created_by_id ] ['created_by_id', 'updated_by_id'].each {|item|
data[ User.to_app_model ][ self.created_by_id ] = User.user_data_full( self.created_by_id ) if self[ item ]
if !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end 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 end
}
data data
end end

81
test/unit/assets_test.rb Normal file
View file

@ -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